Mercurial > hg
comparison hgdemandimport/demandimportpy2.py @ 33529:ded3ebae8779
demandimport: drop hack for old Pythons which had no level argument
_import() was added by 868282fa29d8, but it is identical to _origimport()
now.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sun, 16 Jul 2017 17:16:32 +0900 |
parents | 91a2ec8e7fa0 |
children | 05e3fa254b6b |
comparison
equal
deleted
inserted
replaced
33528:c384ac3ea147 | 33529:ded3ebae8779 |
---|---|
44 | 44 |
45 # Python 3 doesn't have relative imports nor level -1. | 45 # Python 3 doesn't have relative imports nor level -1. |
46 level = -1 | 46 level = -1 |
47 if sys.version_info[0] >= 3: | 47 if sys.version_info[0] >= 3: |
48 level = 0 | 48 level = 0 |
49 _import = _origimport | |
50 | 49 |
51 def _hgextimport(importfunc, name, globals, *args, **kwargs): | 50 def _hgextimport(importfunc, name, globals, *args, **kwargs): |
52 try: | 51 try: |
53 return importfunc(name, globals, *args, **kwargs) | 52 return importfunc(name, globals, *args, **kwargs) |
54 except ImportError: | 53 except ImportError: |
97 self._data[5].add(name) | 96 self._data[5].add(name) |
98 | 97 |
99 def _load(self): | 98 def _load(self): |
100 if not self._module: | 99 if not self._module: |
101 head, globals, locals, after, level, modrefs = self._data | 100 head, globals, locals, after, level, modrefs = self._data |
102 mod = _hgextimport(_import, head, globals, locals, None, level) | 101 mod = _hgextimport(_origimport, head, globals, locals, None, level) |
103 if mod is self: | 102 if mod is self: |
104 # In this case, _hgextimport() above should imply | 103 # In this case, _hgextimport() above should imply |
105 # _demandimport(). Otherwise, _hgextimport() never | 104 # _demandimport(). Otherwise, _hgextimport() never |
106 # returns _demandmod. This isn't intentional behavior, | 105 # returns _demandmod. This isn't intentional behavior, |
107 # in fact. (see also issue5304 for detail) | 106 # in fact. (see also issue5304 for detail) |
174 _pypy = '__pypy__' in sys.builtin_module_names | 173 _pypy = '__pypy__' in sys.builtin_module_names |
175 | 174 |
176 def _demandimport(name, globals=None, locals=None, fromlist=None, level=level): | 175 def _demandimport(name, globals=None, locals=None, fromlist=None, level=level): |
177 if locals is None or name in ignore or fromlist == ('*',): | 176 if locals is None or name in ignore or fromlist == ('*',): |
178 # these cases we can't really delay | 177 # these cases we can't really delay |
179 return _hgextimport(_import, name, globals, locals, fromlist, level) | 178 return _hgextimport(_origimport, name, globals, locals, fromlist, level) |
180 elif not fromlist: | 179 elif not fromlist: |
181 # import a [as b] | 180 # import a [as b] |
182 if '.' in name: # a.b | 181 if '.' in name: # a.b |
183 base, rest = name.split('.', 1) | 182 base, rest = name.split('.', 1) |
184 # email.__init__ loading email.mime | 183 # email.__init__ loading email.mime |
185 if globals and globals.get('__name__', None) == base: | 184 if globals and globals.get('__name__', None) == base: |
186 return _import(name, globals, locals, fromlist, level) | 185 return _origimport(name, globals, locals, fromlist, level) |
187 # if a is already demand-loaded, add b to its submodule list | 186 # if a is already demand-loaded, add b to its submodule list |
188 if base in locals: | 187 if base in locals: |
189 if isinstance(locals[base], _demandmod): | 188 if isinstance(locals[base], _demandmod): |
190 locals[base]._extend(rest) | 189 locals[base]._extend(rest) |
191 return locals[base] | 190 return locals[base] |