Skip to content

Commit

Permalink
PyPy compatible louie.robustapply.function implementation
Browse files Browse the repository at this point in the history
Based on PR #3 - thanks @svpcom!
  • Loading branch information
matthewryanscott committed Jan 8, 2016
1 parent b0c1304 commit 4abe2a9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Expand Up @@ -8,6 +8,7 @@ python:
- "3.5"
- "3.5-dev" # 3.5 development branch
- "nightly" # currently points to 3.6-dev
- "pypy"
# command to install dependencies
install: "pip install -e ."
# command to run tests
Expand Down
2 changes: 2 additions & 0 deletions doc/contributors.rst
Expand Up @@ -11,3 +11,5 @@ The following people have contributed code to PyDispatcher and Louie:
- `Matthew R. Scott <https://github.com/gldnspud>`__

- `Brad Arndt <https://github.com/digdugg>`__

- `Vasily Evseenko <https://github.com/svpcom>`__
40 changes: 20 additions & 20 deletions louie/robustapply.py
Expand Up @@ -5,6 +5,15 @@
those which are acceptable.
"""

import sys
if sys.hexversion >= 0x3000000:
IM_FUNC = '__func__'
FUNC_CODE = '__code__'
else:
IM_FUNC = 'im_func'
FUNC_CODE = 'func_code'


def function(receiver):
"""Get function-like callable object for given receiver.
Expand All @@ -13,28 +22,19 @@ def function(receiver):
If fromMethod is true, then the callable already has its first
argument bound.
"""
if hasattr(receiver, '__call__'):
# receiver is a class instance; assume it is callable.
# Reassign receiver to the actual method that will be called.
c = receiver.__call__
if hasattr(c, 'im_func') or hasattr(c, 'im_code'):
receiver = c
if hasattr(c, '__func__') or hasattr(c, '__code__'):
receiver = c
if hasattr(receiver, 'im_func'):
# receiver is an instance-method.
return receiver, receiver.im_func.func_code, 1
if hasattr(receiver, '__func__'):
#receiver is an instance method in python3
return receiver, receiver.__func__.__code__, 1
if hasattr(receiver, '__code__'):
#receiver is a function and has code, name changed in python3?
return receiver, receiver.__code__, 0

elif not hasattr(receiver, 'func_code'):
if hasattr(receiver, IM_FUNC):
# Instance method.
im_func = getattr(receiver, IM_FUNC)
func_code = getattr(im_func, FUNC_CODE)
return receiver, func_code, True
elif hasattr(receiver, FUNC_CODE):
func_code = getattr(receiver, FUNC_CODE)
return receiver, func_code, False
elif hasattr(receiver, '__call__'):
return function(receiver.__call__)
else:
raise ValueError(
'unknown reciever type {} {}'.format(receiver, type(receiver)))
return receiver, receiver.func_code, 0


def robust_apply(receiver, signature, *arguments, **named):
Expand Down

0 comments on commit 4abe2a9

Please sign in to comment.