comparison mercurial/demandimport.py @ 29736:14f077f7519a

demandimport: import sub-module relatively as expected (issue5208) Before this patch, importing sub-module might (1) fail or (2) success but import incorrect module, because demandimport tries to import sub-module with level=-1 (on Python 2.x) or level=0 (on Python 3.x), which is default value of "level" argument at construction of "_demandmod" proxy object. (1) on Python 3.x, importing sub-module always fails to import existing sub-module (2) both on Python 2.x and 3.x, importing sub-module might import same name module on root level unintentionally On Python 2.x, existing sub-module is prior to this unexpected module. Therefore, this problem hasn't appeared. To import sub-module relatively as expected, this patch specifies "1" as import level explicitly at construction of "_demandmod" proxy object for sub-module.
author FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
date Sat, 06 Aug 2016 22:24:33 +0900
parents 8960fcb76ca4
children ae9a4d6a8d51
comparison
equal deleted inserted replaced
29735:919a4b7f531d 29736:14f077f7519a
115 def subload(mod, p): 115 def subload(mod, p):
116 h, t = p, None 116 h, t = p, None
117 if '.' in p: 117 if '.' in p:
118 h, t = p.split('.', 1) 118 h, t = p.split('.', 1)
119 if getattr(mod, h, nothing) is nothing: 119 if getattr(mod, h, nothing) is nothing:
120 setattr(mod, h, _demandmod(p, mod.__dict__, mod.__dict__)) 120 setattr(mod, h, _demandmod(p, mod.__dict__, mod.__dict__,
121 level=1))
121 elif t: 122 elif t:
122 subload(getattr(mod, h), t) 123 subload(getattr(mod, h), t)
123 124
124 for x in after: 125 for x in after:
125 subload(mod, x) 126 subload(mod, x)
208 def chainmodules(rootmod, modname): 209 def chainmodules(rootmod, modname):
209 # recurse down the module chain, and return the leaf module 210 # recurse down the module chain, and return the leaf module
210 mod = rootmod 211 mod = rootmod
211 for comp in modname.split('.')[1:]: 212 for comp in modname.split('.')[1:]:
212 if getattr(mod, comp, nothing) is nothing: 213 if getattr(mod, comp, nothing) is nothing:
213 setattr(mod, comp, 214 setattr(mod, comp, _demandmod(comp, mod.__dict__,
214 _demandmod(comp, mod.__dict__, mod.__dict__)) 215 mod.__dict__, level=1))
215 mod = getattr(mod, comp) 216 mod = getattr(mod, comp)
216 return mod 217 return mod
217 218
218 if level >= 0: 219 if level >= 0:
219 if name: 220 if name: