Re: [PATCH] extensions: fix wrapcommand/function of class instance

2017-09-12 Thread Augie Fackler
On Sun, Sep 10, 2017 at 11:47:02PM +0900, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara 
> # 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
___
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel


Re: [PATCH] extensions: fix wrapcommand/function of class instance

2017-09-10 Thread Jun Wu
LGTM. Thanks!

Excerpts from Yuya Nishihara's message of 2017-09-10 23:47:02 +0900:
> # HG changeset patch
> # User Yuya Nishihara 
> # 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']
___
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel


[PATCH] extensions: fix wrapcommand/function of class instance

2017-09-10 Thread Yuya Nishihara
# HG changeset patch
# User Yuya Nishihara 
# 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']
___
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel