changeset 34128:82bd4c5a81e5

extensions: fix wrapcommand/function of class instance 5361771f9714 changed _updatewrapper() to copy the __name__ attribute, but not all callable objects has __name__. Spotted by loading mq with extdiff.
author Yuya Nishihara <yuya@tcha.org>
date Sun, 10 Sep 2017 23:37:14 +0900
parents 709b44f38ab9
children 902219a99901
files mercurial/extensions.py tests/test-extensions-wrapfunction.py tests/test-extensions-wrapfunction.py.out
diffstat 3 files changed, 13 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/extensions.py	Tue Sep 12 09:13:02 2017 -0700
+++ b/mercurial/extensions.py	Sun Sep 10 23:37:14 2017 +0900
@@ -333,7 +333,10 @@
 
 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__', {}))
--- a/tests/test-extensions-wrapfunction.py	Tue Sep 12 09:13:02 2017 -0700
+++ b/tests/test-extensions-wrapfunction.py	Sun Sep 10 23:37:14 2017 +0900
@@ -54,3 +54,11 @@
         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())
--- a/tests/test-extensions-wrapfunction.py.out	Tue Sep 12 09:13:02 2017 -0700
+++ b/tests/test-extensions-wrapfunction.py.out	Sun Sep 10 23:37:14 2017 +0900
@@ -18,3 +18,4 @@
 context manager [2, 0, 1, 'orig']
 context manager [2, 1, 'orig']
 context manager [2, 'orig']
+wrap callable object [0, 'orig']