comparison mercurial/demandimport.py @ 25937:4f1144c3c72b

demandimport: support lazy loading for absolute_import Before, we didn't support lazy loading if absolute_import was in effect and a fromlist was used. This meant that "from . import X" wasn't lazy and performance could suffer as a result. With this patch, we now support lazy loading for this scenario. As part of developing this, I discovered issues when module names are defined. Since the enforced import style only allows "from X import Y" or "from .X import Y" in very few scenarios when absolute_import is enabled - scenarios where Y is not a module and thus there is nothing to lazy load - I decided to drop support for this case instead of chasing down the errors. I don't think much harm will come from this. But I'd like to take another look once all modules are using absolute_import and I can see the full extent of what is using names in absolute_import mode.
author Gregory Szorc <gregory.szorc@gmail.com>
date Sat, 08 Aug 2015 16:13:27 -0700
parents f90bb2002bcf
children 3beed01daff9
comparison
equal deleted inserted replaced
25936:f90bb2002bcf 25937:4f1144c3c72b
137 # level >= 0: absolute only (Python 2 w/ absolute_import and Python 3). 137 # level >= 0: absolute only (Python 2 w/ absolute_import and Python 3).
138 # The modern Mercurial convention is to use absolute_import everywhere, 138 # The modern Mercurial convention is to use absolute_import everywhere,
139 # so modern Mercurial code will have level >= 0. 139 # so modern Mercurial code will have level >= 0.
140 140
141 if level >= 0: 141 if level >= 0:
142 return _origimport(name, globals, locals, fromlist, level) 142 # Mercurial's enforced import style does not use
143 # "from a import b,c,d" or "from .a import b,c,d" syntax. In
144 # addition, this appears to be giving errors with some modules
145 # for unknown reasons. Since we shouldn't be using this syntax
146 # much, work around the problems.
147 if name:
148 return _hgextimport(_origimport, name, globals, locals,
149 fromlist, level)
150
151 mod = _hgextimport(_origimport, name, globals, locals, level=level)
152 for x in fromlist:
153 # Missing symbols mean they weren't defined in the module
154 # itself which means they are sub-modules.
155 if getattr(mod, x, nothing) is nothing:
156 setattr(mod, x,
157 _demandmod(x, mod.__dict__, locals, level=level))
158
159 return mod
143 160
144 # But, we still need to support lazy loading of standard library and 3rd 161 # But, we still need to support lazy loading of standard library and 3rd
145 # party modules. So handle level == -1. 162 # party modules. So handle level == -1.
146 mod = _hgextimport(_origimport, name, globals, locals) 163 mod = _hgextimport(_origimport, name, globals, locals)
147 # recurse down the module chain 164 # recurse down the module chain