# HG changeset patch # User Jun Wu # Date 1470838902 -3600 # Node ID 8bf97c4c6c2af58c5a69419be51f51723697611a # Parent ce6317dcb94404929f49184f4b5f532bf0a6e8e2 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: [, , , ] It will also be useful to safely unwrap a function. See the next patch. diff -r ce6317dcb944 -r 8bf97c4c6c2a mercurial/extensions.py --- a/mercurial/extensions.py Wed Aug 10 15:21:42 2016 +0100 +++ b/mercurial/extensions.py Wed Aug 10 15:21:42 2016 +0100 @@ -309,6 +309,22 @@ 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'''