Mercurial > hg
diff mercurial/extensions.py @ 33052:45b0e9d05ee9
extensions: register functions always at loading extension (issue5601)
Before this patch, functions defined in extensions are registered via
extra loaders only in _dispatch(). Therefore, loading extensions in
other code paths like below omits registration of functions.
- WSGI service
- operation across repositories (e.g. subrepo)
- test-duplicateoptions.py, using extensions.loadall() directly
To register functions always at loading new extension, this patch
moves implementation for extra loading from dispatch._dispatch() to
extensions.loadall().
AFAIK, only commands module causes cyclic dependency between
extensions module, but this patch imports all related modules just
before extra loading in loadall(), in order to centralize them.
This patch makes extensions.py depend on many other modules, even
though extensions.py itself doesn't. It should be avoided if possible,
but I don't have any better idea. Some other places like below aren't
reasonable for extra loading, IMHO.
- specific function in newly added module:
existing callers of extensions.loadall() should invoke it, too
- hg.repository() or so:
no-repo commands aren't covered by this.
BTW, this patch removes _loaded.add(name) on relocation, because
dispatch._loaded is used only for extraloaders (for similar reason,
"exts" variable is removed, too).
author | FUJIWARA Katsunori <foozy@lares.dti.ne.jp> |
---|---|
date | Sat, 24 Jun 2017 02:39:20 +0900 |
parents | 80a5d237a4ae |
children | c467d13334ee |
line wrap: on
line diff
--- a/mercurial/extensions.py Sat Jun 24 23:09:21 2017 -0400 +++ b/mercurial/extensions.py Sat Jun 24 02:39:20 2017 +0900 @@ -243,6 +243,43 @@ # entries could result in double execution. See issue4646. _aftercallbacks.clear() + # delay importing avoids cyclic dependency (especially commands) + from . import ( + color, + commands, + fileset, + revset, + templatefilters, + templatekw, + templater, + ) + + # list of (objname, loadermod, loadername) tuple: + # - objname is the name of an object in extension module, + # from which extra information is loaded + # - loadermod is the module where loader is placed + # - loadername is the name of the function, + # which takes (ui, extensionname, extraobj) arguments + extraloaders = [ + ('cmdtable', commands, 'loadcmdtable'), + ('colortable', color, 'loadcolortable'), + ('filesetpredicate', fileset, 'loadpredicate'), + ('revsetpredicate', revset, 'loadpredicate'), + ('templatefilter', templatefilters, 'loadfilter'), + ('templatefunc', templater, 'loadfunction'), + ('templatekeyword', templatekw, 'loadkeyword'), + ] + + for name in _order[newindex:]: + module = _extensions[name] + if not module: + continue # loading this module failed + + for objname, loadermod, loadername in extraloaders: + extraobj = getattr(module, objname, None) + if extraobj is not None: + getattr(loadermod, loadername)(ui, name, extraobj) + def afterloaded(extension, callback): '''Run the specified function after a named extension is loaded.