comparison tests/test-extension.t @ 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 fcaf20175b1b
children 8540133f91a1
comparison
equal deleted inserted replaced
29735:919a4b7f531d 29736:14f077f7519a
430 LIB: this is extlibroot.recursedown.legacy.used 430 LIB: this is extlibroot.recursedown.legacy.used
431 REL: this is absextroot.xsub1.xsub2.used 431 REL: this is absextroot.xsub1.xsub2.used
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 ..
438 468
439 hide outer repo 469 hide outer repo