extensions: fix wrapcommand/function of class instance

5f4c097a17d2 changed _updatewrapper() to copy the __name__ attribute, but
not all callable objects has __name__.

Spotted by loading mq with extdiff.
This commit is contained in:
Yuya Nishihara 2017-09-10 23:37:14 +09:00
parent 792f16d38c
commit 04508d7f1c
3 changed files with 13 additions and 1 deletions

View File

@ -333,7 +333,10 @@ def bind(func, *args):
def _updatewrapper(wrap, origfn, unboundwrapper):
'''Copy and add some useful attributes to wrapper'''
wrap.__name__ = origfn.__name__
try:
wrap.__name__ = origfn.__name__
except AttributeError:
pass
wrap.__module__ = getattr(origfn, '__module__')
wrap.__doc__ = getattr(origfn, '__doc__')
wrap.__dict__.update(getattr(origfn, '__dict__', {}))

View File

@ -54,3 +54,11 @@ with wrap1:
print('context manager', dummy.getstack())
print('context manager', dummy.getstack())
print('context manager', dummy.getstack())
# Wrap callable object which has no __name__
class callableobj(object):
def __call__(self):
return ['orig']
dummy.cobj = callableobj()
extensions.wrapfunction(dummy, 'cobj', wrappers[0])
print('wrap callable object', dummy.cobj())

View File

@ -18,3 +18,4 @@ context manager [0, 1, 'orig']
context manager [2, 0, 1, 'orig']
context manager [2, 1, 'orig']
context manager [2, 'orig']
wrap callable object [0, 'orig']