a  ze$@sVdZddlmZddlZddlZGdddZejeejd\ZZ e dkrRe dS) z Test relative imports (PEP 328).)utilNc@seZdZdZddZddZddZdd Zd d Zd d Z ddZ ddZ ddZ ddZ ddZddZddZddZddZd S)!RelativeImportsaPEP 328 introduced relative imports. This allows for imports to occur from within a package without having to specify the actual package name. A simple example is to import another module within the same package [module from module]:: # From pkg.mod1 with pkg.mod2 being a module. from . import mod2 This also works for getting an attribute from a module that is specified in a relative fashion [attr from module]:: # From pkg.mod1. from .mod2 import attr But this is in no way restricted to working between modules; it works from [package to module],:: # From pkg, importing pkg.module which is a module. from . import module [module to package],:: # Pull attr from pkg, called from pkg.module which is a module. from . import attr and [package to package]:: # From pkg.subpkg1 (both pkg.subpkg[1,2] are packages). from .. import subpkg2 The number of dots used is in no way restricted [deep import]:: # Import pkg.attr from pkg.pkg1.pkg2.pkg3.pkg4.pkg5. from ...... import attr To prevent someone from accessing code that is outside of a package, one cannot reach the location containing the root package itself:: # From pkg.__init__ [too high from package] from .. import top_level # From pkg.module [too high from module] from .. import top_level Relative imports are the only type of import that allow for an empty module name for an import [empty name]. c Csg}|D]2}|ds"||q||dtd qtj|}tj|gd~tVtd|D]6}tj |||Wdqp1s0YqpWdn1s0YWdn1s0YWdn1s0YdS)z;Abstract out boilerplace for setting up for an import test. .__init__N) meta_pathignore) endswithappendlenrZ mock_specZ import_statewarningscatch_warnings simplefilterZuncache)selfcreateglobals_callbackZ uncache_namesnameZimporterglobal_re/opt/bitninja-python-dojo/embedded/lib/python3.9/test/test_importlib/import_/test_relative_imports.pyrelative_import_test;s      z$RelativeImports.relative_import_testcs2d}ddiddif}fdd}|||dS)N pkg.__init__pkg.mod2 __package__pkg__name__pkg.mod1csPdjd|dgdd}|jdt|d|jjddS)Nrmod2fromlistlevelr) __import__ assertEqualr assertTruehasattrrattrrmodulerrrrPs  z9RelativeImports.test_module_from_module..callbackrrrrrrr+rtest_module_from_moduleLs z'RelativeImports.test_module_from_modulecs2d}ddiddif}fdd}|||dS)Nrrrrrcs>djd|dgdd}|jd|jddS)Nrrr(r r!r)r$r%rr(r)r+rrr\s   z7RelativeImports.test_attr_from_module..callbackr,r-rr+rtest_attr_from_moduleXs z%RelativeImports.test_attr_from_modulecs6d}ddiddgdf}fdd}|||dS)Nr pkg.modulerrblahr__path__csPdjd|dgdd}|jdt|d|jjddS)Nrrr*r r!r1)r$r%rr&r'r*r(r)r+rrris  z8RelativeImports.test_package_to_module..callbackr,r-rr+rtest_package_to_moduleds   z&RelativeImports.test_package_to_modulecs2d}ddiddif}fdd}|||dS)Nr0rrrr1cs0djd|dgdd}|jddS)Nrrr(r r!r$r%rr)r+rrrvs z8RelativeImports.test_module_to_package..callbackr,r-rr+rtest_module_to_packagers z&RelativeImports.test_module_to_packagecs6d}ddiddgdf}fdd}|||dS)N)rzpkg.subpkg1.__init__pkg.subpkg2.__init__rz pkg.subpkg1r2r3csFjd|dgdd}|jdt|d|jjddS)Nrsubpkg2rr!rr8)r$r%rr&r'r9r(r)r+rrrs  z9RelativeImports.test_package_to_package..callbackr,r-rr+rtest_package_to_package|s   z'RelativeImports.test_package_to_packagec sndg}tddD](}|d|ddtd |qddidd gd ffd d }||dS) Nrr z{0}.pkg{1}.__init__rrzpkg.pkg1.pkg2.pkg3.pkg4.pkg5r2r3cs8ddjd|dgdd}|jddS)Nrrrr(r;r!rr6r)rrrrrsz2RelativeImports.test_deep_import..callback)ranger formatr r)rrcountrrr=rtest_deep_importsz RelativeImports.test_deep_importcs:ddg}ddiddgdf}fdd}|||dS) N top_levelrrrr2r3csLdt$jd|dgddWdn1s>0YdSNrrrBrr!r$ assertRaises ImportErrorrr+rrrs    z.callbackr,r-rr+rtest_too_high_from_packages   z*RelativeImports.test_too_high_from_packagecs6gd}ddiddif}fdd}|||dS)N)rBrr1rrrr1csLdt$jd|dgddWdn1s>0YdSrCrDrGr+rrrs    z;RelativeImports.test_too_high_from_module..callbackr,r-rr+rtest_too_high_from_modules z)RelativeImports.test_too_high_from_modulecCs8|t|dWdn1s*0YdS)Nr)rE ValueErrorr$r+rrrtest_empty_name_w_level_0s z)RelativeImports.test_empty_name_w_level_0cs0gd}ddifdd}||dS)N)z__runpy_pkg__.__init__z$__runpy_pkg__.__runpy_pkg__.__init__z__runpy_pkg__.uncle.__init__z#__runpy_pkg__.uncle.cousin.__init__z!__runpy_pkg__.uncle.cousin.nephewr__runpy_pkg__.__runpy_pkg__cs2djdidgdd}|jddS)NrLz uncle.cousinZnephewrr!z__runpy_pkg__.uncle.cousinr6r)r=rrrs   zDRelativeImports.test_import_from_different_package..callbackr,)rrrrr=r"test_import_from_different_packagesz2RelativeImports.test_import_from_different_packagecs2ddg}dddg}fdd}|||dS)Nzcrash.__init__ crash.modcrash)rrcs.dd|igd}|jddS)NrOmodr rNr6)rrPr+rrrs zIRelativeImports.test_import_relative_import_no_fromlist..callbackr,r-rr+r'test_import_relative_import_no_fromlists  z7RelativeImports.test_import_relative_import_no_fromlistc CsntRtd|t|jdddWdn1sB0YWdn1s`0YdS)Nrsysr r#)r r r rEKeyErrorr$r+rrrtest_relative_import_no_globalss   z/RelativeImports.test_relative_import_no_globalscCsD|t&|jddddddWdn1s60YdS)Narr__spec__r rSrErFr$r+rrrtest_relative_import_no_packages z/RelativeImports.test_relative_import_no_packagecCsD|t&|jddddddWdn1s60YdS)NrRrrWr rSrYr+rrr/test_relative_import_no_package_exists_absolutes z?RelativeImports.test_relative_import_no_package_exists_absoluteN)r __module__ __qualname____doc__rr.r/r5r7r:rArHrIrKrMrQrUrZr[rrrrrs 2      r)r$__main__) r^rrZunittestr rZ test_bothr$ZFrozen_RelativeImportsZSource_RelativeImportsrmainrrrrs _