mercurial/demandimport.py
changeset 19933 621a26eb3a99
parent 19932 e3a5922e18c3
child 20422 aac87f70f38e
equal deleted inserted replaced
19932:e3a5922e18c3 19933:621a26eb3a99
    36         "call _origimport with no level argument"
    36         "call _origimport with no level argument"
    37         return _origimport(name, globals, locals, fromlist)
    37         return _origimport(name, globals, locals, fromlist)
    38 else:
    38 else:
    39     _import = _origimport
    39     _import = _origimport
    40 
    40 
       
    41 def _hgextimport(importfunc, name, globals, *args):
       
    42     try:
       
    43         return importfunc(name, globals, *args)
       
    44     except ImportError:
       
    45         if not globals:
       
    46             raise
       
    47         # extensions are loaded with "hgext_" prefix
       
    48         hgextname = 'hgext_%s' % name
       
    49         nameroot = hgextname.split('.', 1)[0]
       
    50         contextroot = globals.get('__name__', '').split('.', 1)[0]
       
    51         if nameroot != contextroot:
       
    52             raise
       
    53         # retry to import with "hgext_" prefix
       
    54         return importfunc(hgextname, globals, *args)
       
    55 
    41 class _demandmod(object):
    56 class _demandmod(object):
    42     """module demand-loader and proxy"""
    57     """module demand-loader and proxy"""
    43     def __init__(self, name, globals, locals, level=-1):
    58     def __init__(self, name, globals, locals, level=-1):
    44         if '.' in name:
    59         if '.' in name:
    45             head, rest = name.split('.', 1)
    60             head, rest = name.split('.', 1)
    54         """add to the list of submodules to load"""
    69         """add to the list of submodules to load"""
    55         self._data[3].append(name)
    70         self._data[3].append(name)
    56     def _load(self):
    71     def _load(self):
    57         if not self._module:
    72         if not self._module:
    58             head, globals, locals, after, level = self._data
    73             head, globals, locals, after, level = self._data
    59             mod = _import(head, globals, locals, None, level)
    74             mod = _hgextimport(_import, head, globals, locals, None, level)
    60             # load submodules
    75             # load submodules
    61             def subload(mod, p):
    76             def subload(mod, p):
    62                 h, t = p, None
    77                 h, t = p, None
    63                 if '.' in p:
    78                 if '.' in p:
    64                     h, t = p.split('.', 1)
    79                     h, t = p.split('.', 1)
    91         setattr(self._module, attr, val)
   106         setattr(self._module, attr, val)
    92 
   107 
    93 def _demandimport(name, globals=None, locals=None, fromlist=None, level=-1):
   108 def _demandimport(name, globals=None, locals=None, fromlist=None, level=-1):
    94     if not locals or name in ignore or fromlist == ('*',):
   109     if not locals or name in ignore or fromlist == ('*',):
    95         # these cases we can't really delay
   110         # these cases we can't really delay
    96         return _import(name, globals, locals, fromlist, level)
   111         return _hgextimport(_import, name, globals, locals, fromlist, level)
    97     elif not fromlist:
   112     elif not fromlist:
    98         # import a [as b]
   113         # import a [as b]
    99         if '.' in name: # a.b
   114         if '.' in name: # a.b
   100             base, rest = name.split('.', 1)
   115             base, rest = name.split('.', 1)
   101             # email.__init__ loading email.mime
   116             # email.__init__ loading email.mime
   110     else:
   125     else:
   111         if level != -1:
   126         if level != -1:
   112             # from . import b,c,d or from .a import b,c,d
   127             # from . import b,c,d or from .a import b,c,d
   113             return _origimport(name, globals, locals, fromlist, level)
   128             return _origimport(name, globals, locals, fromlist, level)
   114         # from a import b,c,d
   129         # from a import b,c,d
   115         mod = _origimport(name, globals, locals)
   130         mod = _hgextimport(_origimport, name, globals, locals)
   116         # recurse down the module chain
   131         # recurse down the module chain
   117         for comp in name.split('.')[1:]:
   132         for comp in name.split('.')[1:]:
   118             if getattr(mod, comp, nothing) is nothing:
   133             if getattr(mod, comp, nothing) is nothing:
   119                 setattr(mod, comp, _demandmod(comp, mod.__dict__, mod.__dict__))
   134                 setattr(mod, comp, _demandmod(comp, mod.__dict__, mod.__dict__))
   120             mod = getattr(mod, comp)
   135             mod = getattr(mod, comp)