Patchwork D632: wrapfunction: use functools.partial if possible

login
register
mail settings
Submitter phabricator
Date Sept. 8, 2017, 4:21 a.m.
Message ID <01ea7c28c58b0dd13bb0c5f22ea454ae@localhost.localdomain>
Download mbox | patch
Permalink /patch/23739/
State Not Applicable
Headers show

Comments

phabricator - Sept. 8, 2017, 4:21 a.m.
This revision was automatically updated to reflect the committed changes.
Closed by commit rHG5361771f9714: wrapfunction: use functools.partial if possible (authored by quark).

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D632?vs=1678&id=1681

REVISION DETAIL
  https://phab.mercurial-scm.org/D632

AFFECTED FILES
  mercurial/dispatch.py
  mercurial/extensions.py

CHANGE DETAILS




To: quark, #hg-reviewers, phillco
Cc: martinvonz, phillco, mercurial-devel

Patch

diff --git a/mercurial/extensions.py b/mercurial/extensions.py
--- a/mercurial/extensions.py
+++ b/mercurial/extensions.py
@@ -7,6 +7,7 @@ 
 
 from __future__ import absolute_import
 
+import functools
 import imp
 import inspect
 import os
@@ -332,6 +333,7 @@ 
 
 def _updatewrapper(wrap, origfn, unboundwrapper):
     '''Copy and add some useful attributes to wrapper'''
+    wrap.__name__ = origfn.__name__
     wrap.__module__ = getattr(origfn, '__module__')
     wrap.__doc__ = getattr(origfn, '__doc__')
     wrap.__dict__.update(getattr(origfn, '__dict__', {}))
@@ -459,7 +461,14 @@ 
 
     origfn = getattr(container, funcname)
     assert callable(origfn)
-    wrap = bind(wrapper, origfn)
+    if inspect.ismodule(container):
+        # origfn is not an instance or class method. "partial" can be used.
+        # "partial" won't insert a frame in traceback.
+        wrap = functools.partial(wrapper, origfn)
+    else:
+        # "partial" cannot be safely used. Emulate its effect by using "bind".
+        # The downside is one more frame in traceback.
+        wrap = bind(wrapper, origfn)
     _updatewrapper(wrap, origfn, wrapper)
     setattr(container, funcname, wrap)
     return origfn
diff --git a/mercurial/dispatch.py b/mercurial/dispatch.py
--- a/mercurial/dispatch.py
+++ b/mercurial/dispatch.py
@@ -357,7 +357,10 @@ 
     return -1
 
 def aliasargs(fn, givenargs):
-    args = getattr(fn, 'args', [])
+    args = []
+    # only care about alias 'args', ignore 'args' set by extensions.wrapfunction
+    if not util.safehasattr(fn, '_origfunc'):
+        args = getattr(fn, 'args', args)
     if args:
         cmd = ' '.join(map(util.shellquote, args))