a  ze@svdZddlmZddlZddlmZddlZGdddZejeej d\Z Z Gd d d eej Z ed krredS) z1Test that sys.modules is used properly by import.)utilN) MethodTypec@s eZdZdZddZddZdS)UseCacheaWhen it comes to sys.modules, import prefers it over anything else. Once a name has been resolved, sys.modules is checked to see if it contains the module desired. If so, then it is returned [use cache]. If it is not found, then the proper steps are taken to perform the import, but sys.modules is still used to return the imported module (e.g., not what a loader returns) [from cache on return]. This also applies to imports of things contained within a package and thus get assigned as an attribute [from cache to attribute] or pulled in thanks to a fromlist import [from cache for fromlist]. But if sys.modules contains None then ImportError is raised [None in cache]. cCsZd}td8|tjd<|d}|t|t|Wdn1sL0YdS)Nzsome module found!Z some_module)runcachesysmodules __import__ assertEqualid)selfZ module_to_usemoduler\/opt/bitninja-python-dojo/embedded/lib/python3.9/test/test_importlib/import_/test_caching.pytest_using_caches    zUseCache.test_using_cachec Csd}t|^dtj|<|t}||Wdn1sD0Y||jj |Wdn1sr0YdS)NZ using_None) rrrrZ assertRaises ImportErrorr r exceptionname)r rcmrrrtest_None_in_cache s    (zUseCache.test_None_in_cacheN)__name__ __module__ __qualname____doc__rrrrrrrsr)r c@s<eZdZejdZddddZddZdd Zd d ZdS) ImportlibUseCacheZSourceN)return_cs.tj|}|jfdd}t|||_|S)Ncs |S)Nr)r fullnameZ original_loadrrr load_module8sz2ImportlibUseCache.create_mock..load_module)rZ mock_modulesrr)r rnamesmockrrrr create_mock5s   zImportlibUseCache.create_mockc Cs|db}tj|gd4|d}|t|ttjdWdn1sT0YWdn1sr0YdS)Nr  meta_path)r!r import_stater r r rr)r r r rrrtest_using_cache_after_loader@s  z/ImportlibUseCache.test_using_cache_after_loaderc Cs|ddt}tj|gdF|d}|t|d|t|jtt j dWdn1sh0YWdn1s0YdS)N pkg.__init__ pkg.moduler"r r!rr$r Z assertTruehasattrr r r rrr Zimporterr rrr+test_using_cache_for_assigning_to_attributeHs   z=ImportlibUseCache.test_using_cache_for_assigning_to_attributec Cs|ddz}tj|gdL|jddgd}|t|d|t|jtt j dWdn1sn0YWdn1s0YdS)Nr&r'r"Zpkgr )fromlistr(r*rrrtest_using_cache_for_fromlistRs  z/ImportlibUseCache.test_using_cache_for_fromlist) rrrrr r!r%r+r-rrrrr/s    r__main__)rrrtypesrZunittestrZ test_bothr ZFrozen_UseCacheZSource_UseCacheZTestCaserrmainrrrrs  $-