Patchwork extensions: fix wrapcommand/function of class instance

login
register
mail settings
Submitter Yuya Nishihara
Date Sept. 10, 2017, 2:47 p.m.
Message ID <283ecec363f4901ec894.1505054822@mimosa>
Download mbox | patch
Permalink /patch/23745/
State Accepted
Headers show

Comments

Yuya Nishihara - Sept. 10, 2017, 2:47 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1505054234 -32400
#      Sun Sep 10 23:37:14 2017 +0900
# Node ID 283ecec363f4901ec8948dbdc7dba564296d8582
# Parent  a763c891f36e55f4869f443c220227d1da747d18
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.
Jun Wu - Sept. 10, 2017, 11:17 p.m.
LGTM. Thanks!

Excerpts from Yuya Nishihara's message of 2017-09-10 23:47:02 +0900:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1505054234 -32400
> #      Sun Sep 10 23:37:14 2017 +0900
> # Node ID 283ecec363f4901ec8948dbdc7dba564296d8582
> # Parent  a763c891f36e55f4869f443c220227d1da747d18
> 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.
> 
> diff --git a/mercurial/extensions.py b/mercurial/extensions.py
> --- a/mercurial/extensions.py
> +++ b/mercurial/extensions.py
> @@ -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__', {}))
> diff --git a/tests/test-extensions-wrapfunction.py b/tests/test-extensions-wrapfunction.py
> --- a/tests/test-extensions-wrapfunction.py
> +++ b/tests/test-extensions-wrapfunction.py
> @@ -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())
> diff --git a/tests/test-extensions-wrapfunction.py.out b/tests/test-extensions-wrapfunction.py.out
> --- a/tests/test-extensions-wrapfunction.py.out
> +++ b/tests/test-extensions-wrapfunction.py.out
> @@ -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']
Augie Fackler - Sept. 13, 2017, 2:39 a.m.
On Sun, Sep 10, 2017 at 11:47:02PM +0900, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1505054234 -32400
> #      Sun Sep 10 23:37:14 2017 +0900
> # Node ID 283ecec363f4901ec8948dbdc7dba564296d8582
> # Parent  a763c891f36e55f4869f443c220227d1da747d18
> extensions: fix wrapcommand/function of class instance

queued, thanks

Patch

diff --git a/mercurial/extensions.py b/mercurial/extensions.py
--- a/mercurial/extensions.py
+++ b/mercurial/extensions.py
@@ -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__', {}))
diff --git a/tests/test-extensions-wrapfunction.py b/tests/test-extensions-wrapfunction.py
--- a/tests/test-extensions-wrapfunction.py
+++ b/tests/test-extensions-wrapfunction.py
@@ -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())
diff --git a/tests/test-extensions-wrapfunction.py.out b/tests/test-extensions-wrapfunction.py.out
--- a/tests/test-extensions-wrapfunction.py.out
+++ b/tests/test-extensions-wrapfunction.py.out
@@ -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']