Fix "target" attribute calls via six

This commit is contained in:
Aniket21mathur 2019-07-13 18:15:02 +05:30 committed by James Cameron
parent ed46c219a9
commit 490a602367
2 changed files with 11 additions and 5 deletions

View File

@ -12,7 +12,7 @@ WEAKREF_TYPES = (weakref.ReferenceType, saferef.BoundMethodWeakref)
def _make_id(target): def _make_id(target):
if hasattr(target, 'im_func'): if hasattr(target, 'im_func') or hasattr(target, '__func__'):
return (id(im_self(target)), id(im_func(target))) return (id(im_self(target)), id(im_func(target)))
return id(target) return id(target)

View File

@ -21,13 +21,19 @@ 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, 'im_self') or hasattr(target, '__self__'):
if im_self(target) is not None: if im_self(target) 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'), \ if six.PY2:
"safeRef target %r has im_self, but no im_func, " \ assert hasattr(target, 'im_func'), \
"don't know how to create reference" % (target,) "safeRef target %r has im_self, but no im_func, " \
"don't know how to create reference" % (target,)
else:
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