Skip to content

Commit

Permalink
Replaced im_func and im_self by __func__ and __self__.
Browse files Browse the repository at this point in the history
The new names are Python 3 compatible.
  • Loading branch information
claudep committed May 12, 2012
1 parent 33ffd28 commit bbb1258
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
4 changes: 2 additions & 2 deletions django/dispatch/dispatcher.py
Expand Up @@ -6,8 +6,8 @@
WEAKREF_TYPES = (weakref.ReferenceType, saferef.BoundMethodWeakref) WEAKREF_TYPES = (weakref.ReferenceType, saferef.BoundMethodWeakref)


def _make_id(target): def _make_id(target):
if hasattr(target, 'im_func'): if hasattr(target, '__func__'):
return (id(target.im_self), id(target.im_func)) return (id(target.__self__), id(target.__func__))
return id(target) return id(target)


class Signal(object): class Signal(object):
Expand Down
28 changes: 14 additions & 14 deletions django/dispatch/saferef.py
Expand Up @@ -19,11 +19,11 @@ def safeRef(target, onDelete = None):
goes out of scope with the reference object, (either a goes out of scope with the reference object, (either a
weakref or a BoundMethodWeakref) as argument. weakref or a BoundMethodWeakref) as argument.
""" """
if hasattr(target, 'im_self'): if hasattr(target, '__self__'):
if target.im_self is not None: if target.__self__ is not None:
# Turn a bound method into a BoundMethodWeakref instance. # Turn a bound method into a BoundMethodWeakref instance.
# Keep track of these instances for lookup by disconnect(). # Keep track of these instances for lookup by disconnect().
assert hasattr(target, 'im_func'), """safeRef target %r has im_self, but no im_func, don't know how to create reference"""%( target,) assert hasattr(target, '__func__'), """safeRef target %r has __self__, but no __func__, don't know how to create reference"""%( target,)
reference = get_bound_method_weakref( reference = get_bound_method_weakref(
target=target, target=target,
onDelete=onDelete onDelete=onDelete
Expand Down Expand Up @@ -97,9 +97,9 @@ def __init__(self, target, onDelete=None):
"""Return a weak-reference-like instance for a bound method """Return a weak-reference-like instance for a bound method
target -- the instance-method target for the weak target -- the instance-method target for the weak
reference, must have im_self and im_func attributes reference, must have __self__ and __func__ attributes
and be reconstructable via: and be reconstructable via:
target.im_func.__get__( target.im_self ) target.__func__.__get__( target.__self__ )
which is true of built-in instance methods. which is true of built-in instance methods.
onDelete -- optional callback which will be called onDelete -- optional callback which will be called
when this weak reference ceases to be valid when this weak reference ceases to be valid
Expand Down Expand Up @@ -128,18 +128,18 @@ def remove(weak, self=self):
) )
self.deletionMethods = [onDelete] self.deletionMethods = [onDelete]
self.key = self.calculateKey( target ) self.key = self.calculateKey( target )
self.weakSelf = weakref.ref(target.im_self, remove) self.weakSelf = weakref.ref(target.__self__, remove)
self.weakFunc = weakref.ref(target.im_func, remove) self.weakFunc = weakref.ref(target.__func__, remove)
self.selfName = str(target.im_self) self.selfName = str(target.__self__)
self.funcName = str(target.im_func.__name__) self.funcName = str(target.__func__.__name__)


def calculateKey( cls, target ): def calculateKey( cls, target ):
"""Calculate the reference key for this reference """Calculate the reference key for this reference
Currently this is a two-tuple of the id()'s of the Currently this is a two-tuple of the id()'s of the
target object and the target function respectively. target object and the target function respectively.
""" """
return (id(target.im_self),id(target.im_func)) return (id(target.__self__),id(target.__func__))
calculateKey = classmethod( calculateKey ) calculateKey = classmethod( calculateKey )


def __str__(self): def __str__(self):
Expand Down Expand Up @@ -201,19 +201,19 @@ def __init__(self, target, onDelete=None):
"""Return a weak-reference-like instance for a bound method """Return a weak-reference-like instance for a bound method
target -- the instance-method target for the weak target -- the instance-method target for the weak
reference, must have im_self and im_func attributes reference, must have __self__ and __func__ attributes
and be reconstructable via: and be reconstructable via:
target.im_func.__get__( target.im_self ) target.__func__.__get__( target.__self__ )
which is true of built-in instance methods. which is true of built-in instance methods.
onDelete -- optional callback which will be called onDelete -- optional callback which will be called
when this weak reference ceases to be valid when this weak reference ceases to be valid
(i.e. either the object or the function is garbage (i.e. either the object or the function is garbage
collected). Should take a single argument, collected). Should take a single argument,
which will be passed a pointer to this object. which will be passed a pointer to this object.
""" """
assert getattr(target.im_self, target.__name__) == target, \ assert getattr(target.__self__, target.__name__) == target, \
("method %s isn't available as the attribute %s of %s" % ("method %s isn't available as the attribute %s of %s" %
(target, target.__name__, target.im_self)) (target, target.__name__, target.__self__))
super(BoundNonDescriptorMethodWeakref, self).__init__(target, onDelete) super(BoundNonDescriptorMethodWeakref, self).__init__(target, onDelete)


def __call__(self): def __call__(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/regressiontests/decorators/tests.py
Expand Up @@ -220,7 +220,7 @@ def method(self):
self.assertEqual(getattr(Test.method, 'myattr2', False), True) self.assertEqual(getattr(Test.method, 'myattr2', False), True)


self.assertEqual(Test.method.__doc__, 'A method') self.assertEqual(Test.method.__doc__, 'A method')
self.assertEqual(Test.method.im_func.__name__, 'method') self.assertEqual(Test.method.__func__.__name__, 'method')




class XFrameOptionsDecoratorsTests(TestCase): class XFrameOptionsDecoratorsTests(TestCase):
Expand Down

0 comments on commit bbb1258

Please sign in to comment.