2006-12-13 22:27:09 +03:00
|
|
|
# demandimport.py - global demand-loading of modules for Mercurial
|
|
|
|
#
|
2007-06-19 10:51:34 +04:00
|
|
|
# Copyright 2006, 2007 Matt Mackall <mpm@selenic.com>
|
2006-12-13 22:27:09 +03:00
|
|
|
#
|
2009-04-26 03:08:54 +04:00
|
|
|
# This software may be used and distributed according to the terms of the
|
2010-01-20 07:20:08 +03:00
|
|
|
# GNU General Public License version 2 or any later version.
|
2006-12-13 22:27:09 +03:00
|
|
|
|
|
|
|
'''
|
|
|
|
demandimport - automatic demandloading of modules
|
|
|
|
|
|
|
|
To enable this module, do:
|
|
|
|
|
|
|
|
import demandimport; demandimport.enable()
|
|
|
|
|
|
|
|
Imports of the following forms will be demand-loaded:
|
|
|
|
|
|
|
|
import a, b.c
|
|
|
|
import a.b as c
|
|
|
|
from a import b,c # a will be loaded immediately
|
|
|
|
|
|
|
|
These imports will not be delayed:
|
|
|
|
|
|
|
|
from a import *
|
|
|
|
b = __import__(a)
|
|
|
|
'''
|
|
|
|
|
2009-02-05 19:40:25 +03:00
|
|
|
import __builtin__
|
2006-12-13 22:27:09 +03:00
|
|
|
_origimport = __import__
|
|
|
|
|
2011-07-26 06:15:48 +04:00
|
|
|
nothing = object()
|
|
|
|
|
2011-08-23 00:50:52 +04:00
|
|
|
try:
|
|
|
|
_origimport(__builtin__.__name__, {}, {}, None, -1)
|
|
|
|
except TypeError: # no level argument
|
|
|
|
def _import(name, globals, locals, fromlist, level):
|
|
|
|
"call _origimport with no level argument"
|
|
|
|
return _origimport(name, globals, locals, fromlist)
|
|
|
|
else:
|
|
|
|
_import = _origimport
|
|
|
|
|
demandimport: allow extensions to import own modules by absolute name
Before this patch, python modules of each extensions can't import
another one in own extension by absolute name, because root modules of
each extensions are loaded with "hgext_" prefix.
For example, "import extroot.bar" in "extroot/foo.py" of "extroot"
extension fails, even though "import bar" in it succeeds.
Installing extensions into site-packages of python library path can
avoid this problem, but this solution is not reasonable in some cases:
using binary package of Mercurial on Windows, for example.
This patch retries to import with "hgext_" prefix after ImportError,
if the module in the extension may try to import another one in own
extension.
This patch doesn't change some "_import()"/"_origimport()" invocations
below, because ordinary extensions shouldn't cause such invocations.
- invocation of "_import()" when root module imports sub-module by
absolute path without "fromlist"
for example, "import a.b" in "a.__init__.py".
extensions are loaded with "hgext_" prefix, and this causes
execution of another (= fixed by this patch) code path.
- invocation of "_origimport()" when "level != -1" with "fromlist"
for example, importing after "from __future__ import
absolute_import" (level == 0), or "from . import b" or "from .a
import b" (0 < level),
for portability between python versions and environments,
extensions shouldn't cause "level != -1".
2013-10-04 20:02:22 +04:00
|
|
|
def _hgextimport(importfunc, name, globals, *args):
|
|
|
|
try:
|
|
|
|
return importfunc(name, globals, *args)
|
|
|
|
except ImportError:
|
|
|
|
if not globals:
|
|
|
|
raise
|
|
|
|
# extensions are loaded with "hgext_" prefix
|
|
|
|
hgextname = 'hgext_%s' % name
|
|
|
|
nameroot = hgextname.split('.', 1)[0]
|
|
|
|
contextroot = globals.get('__name__', '').split('.', 1)[0]
|
|
|
|
if nameroot != contextroot:
|
|
|
|
raise
|
|
|
|
# retry to import with "hgext_" prefix
|
|
|
|
return importfunc(hgextname, globals, *args)
|
|
|
|
|
2006-12-13 22:27:09 +03:00
|
|
|
class _demandmod(object):
|
|
|
|
"""module demand-loader and proxy"""
|
2013-10-04 20:02:22 +04:00
|
|
|
def __init__(self, name, globals, locals, level=-1):
|
2006-12-13 22:27:09 +03:00
|
|
|
if '.' in name:
|
|
|
|
head, rest = name.split('.', 1)
|
|
|
|
after = [rest]
|
|
|
|
else:
|
|
|
|
head = name
|
|
|
|
after = []
|
2013-10-04 20:02:22 +04:00
|
|
|
object.__setattr__(self, "_data",
|
|
|
|
(head, globals, locals, after, level))
|
2006-12-15 01:51:41 +03:00
|
|
|
object.__setattr__(self, "_module", None)
|
2006-12-13 22:27:09 +03:00
|
|
|
def _extend(self, name):
|
|
|
|
"""add to the list of submodules to load"""
|
|
|
|
self._data[3].append(name)
|
|
|
|
def _load(self):
|
|
|
|
if not self._module:
|
2013-10-04 20:02:22 +04:00
|
|
|
head, globals, locals, after, level = self._data
|
demandimport: allow extensions to import own modules by absolute name
Before this patch, python modules of each extensions can't import
another one in own extension by absolute name, because root modules of
each extensions are loaded with "hgext_" prefix.
For example, "import extroot.bar" in "extroot/foo.py" of "extroot"
extension fails, even though "import bar" in it succeeds.
Installing extensions into site-packages of python library path can
avoid this problem, but this solution is not reasonable in some cases:
using binary package of Mercurial on Windows, for example.
This patch retries to import with "hgext_" prefix after ImportError,
if the module in the extension may try to import another one in own
extension.
This patch doesn't change some "_import()"/"_origimport()" invocations
below, because ordinary extensions shouldn't cause such invocations.
- invocation of "_import()" when root module imports sub-module by
absolute path without "fromlist"
for example, "import a.b" in "a.__init__.py".
extensions are loaded with "hgext_" prefix, and this causes
execution of another (= fixed by this patch) code path.
- invocation of "_origimport()" when "level != -1" with "fromlist"
for example, importing after "from __future__ import
absolute_import" (level == 0), or "from . import b" or "from .a
import b" (0 < level),
for portability between python versions and environments,
extensions shouldn't cause "level != -1".
2013-10-04 20:02:22 +04:00
|
|
|
mod = _hgextimport(_import, head, globals, locals, None, level)
|
2006-12-13 22:27:09 +03:00
|
|
|
# load submodules
|
2006-12-17 23:56:12 +03:00
|
|
|
def subload(mod, p):
|
|
|
|
h, t = p, None
|
|
|
|
if '.' in p:
|
|
|
|
h, t = p.split('.', 1)
|
2011-07-26 06:15:48 +04:00
|
|
|
if getattr(mod, h, nothing) is nothing:
|
2010-10-30 20:32:04 +04:00
|
|
|
setattr(mod, h, _demandmod(p, mod.__dict__, mod.__dict__))
|
2006-12-18 05:34:42 +03:00
|
|
|
elif t:
|
2006-12-17 23:56:12 +03:00
|
|
|
subload(getattr(mod, h), t)
|
|
|
|
|
2006-12-13 22:27:09 +03:00
|
|
|
for x in after:
|
2006-12-17 23:56:12 +03:00
|
|
|
subload(mod, x)
|
|
|
|
|
2006-12-13 22:27:09 +03:00
|
|
|
# are we in the locals dictionary still?
|
|
|
|
if locals and locals.get(head) == self:
|
|
|
|
locals[head] = mod
|
2006-12-15 01:51:41 +03:00
|
|
|
object.__setattr__(self, "_module", mod)
|
2007-06-19 04:43:26 +04:00
|
|
|
|
2006-12-13 22:27:09 +03:00
|
|
|
def __repr__(self):
|
2007-06-19 04:43:26 +04:00
|
|
|
if self._module:
|
|
|
|
return "<proxied module '%s'>" % self._data[0]
|
2006-12-13 22:27:09 +03:00
|
|
|
return "<unloaded module '%s'>" % self._data[0]
|
|
|
|
def __call__(self, *args, **kwargs):
|
2007-12-10 19:24:47 +03:00
|
|
|
raise TypeError("%s object is not callable" % repr(self))
|
2006-12-16 07:16:20 +03:00
|
|
|
def __getattribute__(self, attr):
|
|
|
|
if attr in ('_data', '_extend', '_load', '_module'):
|
|
|
|
return object.__getattribute__(self, attr)
|
2006-12-13 22:27:09 +03:00
|
|
|
self._load()
|
|
|
|
return getattr(self._module, attr)
|
|
|
|
def __setattr__(self, attr, val):
|
|
|
|
self._load()
|
|
|
|
setattr(self._module, attr, val)
|
|
|
|
|
2010-12-01 23:46:08 +03:00
|
|
|
def _demandimport(name, globals=None, locals=None, fromlist=None, level=-1):
|
2006-12-13 22:27:09 +03:00
|
|
|
if not locals or name in ignore or fromlist == ('*',):
|
|
|
|
# these cases we can't really delay
|
demandimport: allow extensions to import own modules by absolute name
Before this patch, python modules of each extensions can't import
another one in own extension by absolute name, because root modules of
each extensions are loaded with "hgext_" prefix.
For example, "import extroot.bar" in "extroot/foo.py" of "extroot"
extension fails, even though "import bar" in it succeeds.
Installing extensions into site-packages of python library path can
avoid this problem, but this solution is not reasonable in some cases:
using binary package of Mercurial on Windows, for example.
This patch retries to import with "hgext_" prefix after ImportError,
if the module in the extension may try to import another one in own
extension.
This patch doesn't change some "_import()"/"_origimport()" invocations
below, because ordinary extensions shouldn't cause such invocations.
- invocation of "_import()" when root module imports sub-module by
absolute path without "fromlist"
for example, "import a.b" in "a.__init__.py".
extensions are loaded with "hgext_" prefix, and this causes
execution of another (= fixed by this patch) code path.
- invocation of "_origimport()" when "level != -1" with "fromlist"
for example, importing after "from __future__ import
absolute_import" (level == 0), or "from . import b" or "from .a
import b" (0 < level),
for portability between python versions and environments,
extensions shouldn't cause "level != -1".
2013-10-04 20:02:22 +04:00
|
|
|
return _hgextimport(_import, name, globals, locals, fromlist, level)
|
2006-12-13 22:27:09 +03:00
|
|
|
elif not fromlist:
|
|
|
|
# import a [as b]
|
|
|
|
if '.' in name: # a.b
|
|
|
|
base, rest = name.split('.', 1)
|
2006-12-16 07:16:20 +03:00
|
|
|
# email.__init__ loading email.mime
|
|
|
|
if globals and globals.get('__name__', None) == base:
|
2011-08-23 00:50:52 +04:00
|
|
|
return _import(name, globals, locals, fromlist, level)
|
2006-12-13 22:27:09 +03:00
|
|
|
# if a is already demand-loaded, add b to its submodule list
|
|
|
|
if base in locals:
|
|
|
|
if isinstance(locals[base], _demandmod):
|
|
|
|
locals[base]._extend(rest)
|
|
|
|
return locals[base]
|
2013-10-04 20:02:22 +04:00
|
|
|
return _demandmod(name, globals, locals, level)
|
2006-12-13 22:27:09 +03:00
|
|
|
else:
|
2010-12-01 23:46:08 +03:00
|
|
|
if level != -1:
|
2010-10-30 20:32:04 +04:00
|
|
|
# from . import b,c,d or from .a import b,c,d
|
|
|
|
return _origimport(name, globals, locals, fromlist, level)
|
|
|
|
# from a import b,c,d
|
demandimport: allow extensions to import own modules by absolute name
Before this patch, python modules of each extensions can't import
another one in own extension by absolute name, because root modules of
each extensions are loaded with "hgext_" prefix.
For example, "import extroot.bar" in "extroot/foo.py" of "extroot"
extension fails, even though "import bar" in it succeeds.
Installing extensions into site-packages of python library path can
avoid this problem, but this solution is not reasonable in some cases:
using binary package of Mercurial on Windows, for example.
This patch retries to import with "hgext_" prefix after ImportError,
if the module in the extension may try to import another one in own
extension.
This patch doesn't change some "_import()"/"_origimport()" invocations
below, because ordinary extensions shouldn't cause such invocations.
- invocation of "_import()" when root module imports sub-module by
absolute path without "fromlist"
for example, "import a.b" in "a.__init__.py".
extensions are loaded with "hgext_" prefix, and this causes
execution of another (= fixed by this patch) code path.
- invocation of "_origimport()" when "level != -1" with "fromlist"
for example, importing after "from __future__ import
absolute_import" (level == 0), or "from . import b" or "from .a
import b" (0 < level),
for portability between python versions and environments,
extensions shouldn't cause "level != -1".
2013-10-04 20:02:22 +04:00
|
|
|
mod = _hgextimport(_origimport, name, globals, locals)
|
2006-12-13 22:27:09 +03:00
|
|
|
# recurse down the module chain
|
|
|
|
for comp in name.split('.')[1:]:
|
2011-07-26 06:15:48 +04:00
|
|
|
if getattr(mod, comp, nothing) is nothing:
|
2010-10-30 20:32:04 +04:00
|
|
|
setattr(mod, comp, _demandmod(comp, mod.__dict__, mod.__dict__))
|
2006-12-13 22:27:09 +03:00
|
|
|
mod = getattr(mod, comp)
|
|
|
|
for x in fromlist:
|
|
|
|
# set requested submodules for demand load
|
2011-07-26 06:15:48 +04:00
|
|
|
if getattr(mod, x, nothing) is nothing:
|
2010-10-30 20:32:04 +04:00
|
|
|
setattr(mod, x, _demandmod(x, mod.__dict__, locals))
|
2006-12-13 22:27:09 +03:00
|
|
|
return mod
|
|
|
|
|
2007-08-14 18:03:15 +04:00
|
|
|
ignore = [
|
|
|
|
'_hashlib',
|
|
|
|
'_xmlplus',
|
|
|
|
'fcntl',
|
|
|
|
'win32com.gen_py',
|
2010-01-13 13:57:32 +03:00
|
|
|
'_winreg', # 2.7 mimetypes needs immediate ImportError
|
2009-03-10 05:00:37 +03:00
|
|
|
'pythoncom',
|
2007-08-14 18:03:15 +04:00
|
|
|
# imported by tarfile, not available under Windows
|
|
|
|
'pwd',
|
|
|
|
'grp',
|
|
|
|
# imported by profile, itself imported by hotshot.stats,
|
|
|
|
# not available under Windows
|
|
|
|
'resource',
|
2009-09-18 02:39:43 +04:00
|
|
|
# this trips up many extension authors
|
|
|
|
'gtk',
|
2010-01-21 04:23:36 +03:00
|
|
|
# setuptools' pkg_resources.py expects "from __main__ import x" to
|
|
|
|
# raise ImportError if x not defined
|
|
|
|
'__main__',
|
2010-03-09 18:03:57 +03:00
|
|
|
'_ssl', # conditional imports in the stdlib, issue1964
|
2011-03-02 08:35:22 +03:00
|
|
|
'rfc822',
|
|
|
|
'mimetools',
|
2007-08-14 18:03:15 +04:00
|
|
|
]
|
2006-12-13 22:27:09 +03:00
|
|
|
|
2014-02-11 02:51:06 +04:00
|
|
|
def isenabled():
|
|
|
|
return __builtin__.__import__ == _demandimport
|
|
|
|
|
2006-12-13 22:27:09 +03:00
|
|
|
def enable():
|
|
|
|
"enable global demand-loading of modules"
|
2009-02-05 19:40:25 +03:00
|
|
|
__builtin__.__import__ = _demandimport
|
2006-12-13 22:27:09 +03:00
|
|
|
|
|
|
|
def disable():
|
|
|
|
"disable global demand-loading of modules"
|
2009-02-05 19:40:25 +03:00
|
|
|
__builtin__.__import__ = _origimport
|