[PATCH 2 of 3 V2] extensions: add getwrapperchain to get a list of wrappers

Jun Wu quark at fb.com
Wed Aug 10 11:33:01 EDT 2016


# HG changeset patch
# User Jun Wu <quark at fb.com>
# Date 1470838902 -3600
#      Wed Aug 10 15:21:42 2016 +0100
# Node ID 0045afd02d847964b2b75d6a62ad13ebff82ef6d
# Parent  d5740aef5d7b45f977d7a78cd1072386c5196c5a
# Available At https://bitbucket.org/quark-zju/hg-draft
#              hg pull https://bitbucket.org/quark-zju/hg-draft -r 0045afd02d84
extensions: add getwrapperchain to get a list of wrappers

The getwrapperchain returns a list of wrappers + the original function, making
it easier to understand what has been wrapped by whom. For example:

  In : mercurial.extensions.getwrapperchain(mercurial.dispatch, '_runcommand')
  Out:
  [<function hgext.pager.pagecmd>,
   <function hgext.color.colorcmd>,
   <function hgext.zeroconf.cleanupafterdispatch>,
   <function mercurial.dispatch._runcommand>]

It will also be useful to safely unwrap a function. See the next patch.

diff --git a/mercurial/extensions.py b/mercurial/extensions.py
--- a/mercurial/extensions.py
+++ b/mercurial/extensions.py
@@ -309,6 +309,22 @@ def wrapfunction(container, funcname, wr
     setattr(container, funcname, wrap)
     return origfn
 
+def getwrapperchain(container, funcname):
+    '''get a chain of wrappers of a function
+
+    Return a list of functions: [newest wrapper, ..., oldest wrapper, origfunc]
+
+    The wrapper functions are the ones passed to wrapfunction, whose first
+    argument is origfunc.
+    '''
+    result = []
+    fn = getattr(container, funcname)
+    while fn:
+        assert callable(fn)
+        result.append(getattr(fn, '_unboundwrapper', fn))
+        fn = getattr(fn, '_origfunc', None)
+    return result
+
 def _disabledpaths(strip_init=False):
     '''find paths of disabled extensions. returns a dict of {name: path}
     removes /__init__.py from packages if strip_init is True'''


More information about the Mercurial-devel mailing list