##// END OF EJS Templates
demandimport: import sub-module relatively as expected (issue5208)...
FUJIWARA Katsunori -
r29736:14f077f7 default
parent child Browse files
Show More
@@ -117,7 +117,8 b' class _demandmod(object):'
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
@@ -210,8 +211,8 b' def _demandimport(name, globals=None, lo'
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
@@ -432,6 +432,36 b' Examine module importing.'
432 REL: this is absextroot.xsub1.xsub2.called.func()
432 REL: this is absextroot.xsub1.xsub2.called.func()
433 REL: this relimporter imports 'this is absextroot.relimportee'
433 REL: this relimporter imports 'this is absextroot.relimportee'
434
434
435 Examine whether sub-module is imported relatively as expected.
436
437 See also issue5208 for detail about example case on Python 3.x.
438
439 $ f -q $TESTTMP/extlibroot/lsub1/lsub2/notexist.py
440 $TESTTMP/extlibroot/lsub1/lsub2/notexist.py: file not found
441
442 $ cat > $TESTTMP/notexist.py <<EOF
443 > text = 'notexist.py at root is loaded unintentionally\n'
444 > EOF
445
446 $ cat > $TESTTMP/checkrelativity.py <<EOF
447 > from mercurial import cmdutil
448 > cmdtable = {}
449 > command = cmdutil.command(cmdtable)
450 >
451 > # demand import avoids failure of importing notexist here
452 > import extlibroot.lsub1.lsub2.notexist
453 >
454 > @command('checkrelativity', [], norepo=True)
455 > def checkrelativity(ui, *args, **opts):
456 > try:
457 > ui.write(extlibroot.lsub1.lsub2.notexist.text)
458 > return 1 # unintentional success
459 > except ImportError:
460 > pass # intentional failure
461 > EOF
462
463 $ (PYTHONPATH=${PYTHONPATH}${PATHSEP}${TESTTMP}; hg --config extensions.checkrelativity=$TESTTMP/checkrelativity.py checkrelativity)
464
435 #endif
465 #endif
436
466
437 $ cd ..
467 $ cd ..
General Comments 0
You need to be logged in to leave comments. Login now