a  ze-H@sddlZddlZddlZddlZddlZddlZddlmZmZmZddl m Z ddl Z ddl Z ddl Z ddlZ ddlmZmZddlmZddlZddlZddlZddlZddlmZddlmZeZde_de_d ejvrd e_d ejvrd e_ea dt _!dt _"dt _#dt _$d t _%d d Z&e&ddZ'dDddZ(dEddZ)dFddZ*dZ+ej,dvre-.Z/e/e-kr|e-0Z/e j!1e/sdZ+e'd dZ2e3ej4e3e2j4dZ4ddZ5dGddZ6d d!Z7ej8d"d#Z9ej8dHdd$d%d&Z:ej8d'd(Z;Gd)d*d*Zd/d0Z?d1d2Z@ej8d3d4ZAej8d5d6ZBd7d8ZCGd9d:d:ZDdIdd?d?ejFZGGd@dAdAZHGdBdCdCeHZIdS)JN) machineryutilinvalidate_caches)ResourceReader)PathPurePath)support)data01) zipdata01errno importlibZ _testcapicCs`tjD]T}tjD]H}tj|}tj||}tj|r|t_|t_ |t_ |t_ dSqqdSN) syspathrEXTENSION_SUFFIXES EXTENSIONSnameosjoinexistsextfilename file_path)rrrrrL/opt/bitninja-python-dojo/embedded/lib/python3.9/test/test_importlib/util.py_extension_details's    rcCs4d|vr dnd}t|}tj||dd}||dS)zAImport a module from importlib both w/ and w/o _frozen_importlib..)r r)_frozen_importlib_frozen_importlib_external)freshZblockedFrozenSource)rZimport_fresh_module) module_namer frozensourcerrrimport_importlib7s  r'c Ks|durtj}nt|ts"||}d||j}||f}t||}|j|_|j|_ ||_ | D]\}}||} t ||| qb|S)Nz{}_{}) unittestZTestCase isinstancetypeformat__name__types new_class __module__Z_NAMEZ_KINDitemssetattr) clskindbasekwargsrbasesZ specializedattrvaluesvaluerrrspecialize_class@s  r:cKs0t|d|fi|}t|d|fi|}||fS)Nr"r#)r:)r2r4r5r%r&rrr split_frozenSsr;cKst||fi|Sr)r;)Z test_classr4r5rrr test_bothYsr<T)win32cygwinFr#r!cCstt d|S)zRClass decorator that nullifies tests requiring a case-insensitive file system.z&requires a case-insensitive filesystem)r(skipIfCASE_INSENSITIVE_FS)testrrrcase_insensitive_testsls rBcCsVtj||d}t|d}||Wdn1s<0Yd|||fS)N.pywz{}.{})rrropenwriter+)parentrZpkg_dircontentrZsubfilerrr submoduless (rJcCsBt|d$}|dt|WdS1s40YdS)znReads a pyc file and returns the unmarshalled code object within. No header validation is performed. rbN)rFseekmarshalload)Zpyc_pathZpyc_frrrget_code_from_pyczs  rPcgs|D]:}|dvrtd|z tj|=Wqty<Yq0qz4dVW|D]$}z tj|=WqNtypYqN0qNn,|D]$}z tj|=WqztyYqz0qz0dS)zUncache a module from sys.modules. A basic sanity check is performed to prevent uncaching modules that either cannot/shouldn't be uncached. )rrNZimpzcannot uncache {0}N) ValueErrorr+rmodulesKeyError)namesrrrruncaches*       rU)pkgc #s&fddtjD}td}tg|Rt|ttj |}|rptj |d}t n|d}|durd}|durt |d}| |Wdn1s0Y|VWdn1s0YWdn1s0YWdn1s0YdS)Ncs"g|]}|ddkr|qS)rr) partition).0nrrr ztemp_module..z __init__.pyrDrCrE) rrRrZtemp_cwdrUZ DirsOnSysPathrrrrmkdirrFrG)rrIrV conflictscwdlocationmodpathZmodfilerrZr temp_modules     (rbc ksi}zdgfdgfdgfdiffD]>\}}tt|||<||vrP||}||=n|}tt||q"t|r|td|dVW|D]\}}tt||qn |D]\}}tt||q0dS)a,Context manager to manage the various importers and stored state in the sys module. The 'modules' attribute is not supported as the interpreter state stores a pointer to the dict that the interpreter uses internally; reassigning to sys.modules does not have the desired effect. meta_pathr path_hookspath_importer_cachezunrecognized arguments: {0}N)getattrrr1lenrQr+keysr0)r5Z originalsr7defaultZ new_valuer9rrr import_states*    rjc@s6eZdZdZidddZddZddZd d Zd S) _ImporterMockz0Base class to help with creating importer mocks.) module_codecGsi|_i|_|D]}|ds$|}n|dtd }d|vrDd}n||kr^|ddd}n|}t|}||_d|_||_ ||_ ||krdg|_ ||j|<||vr|||j|<qdS)N .__init__rr rzz) rRrlendswithrgrsplitr- ModuleType __loader____file__ __package__r7__path__)selfrlrTr import_namepackagemodulerrr__init__s*   z_ImporterMock.__init__cCs |j|SrrR)rurrrr __getitem__sz_ImporterMock.__getitem__cCst|j|_|j|Sr)rUrRrh_uncache __enter__rurrrr}s z_ImporterMock.__enter__cGs|jddddSr)r|__exit__)ruexc_inforrrrsz_ImporterMock.__exit__N)r,r/ __qualname____doc__ryr{r}rrrrrrks rkc@s"eZdZdZdddZddZdS) mock_modulesz!Importer mock using PEP 302 APIs.NcCs||jvrdS|SdSrrz)rufullnamerrrr find_modules zmock_modules.find_modulecCsf||jvrtnR|j|tj|<||jvrXz|j|WntyVtj|=Yn0|j|SdSr)rR ImportErrorrrl Exception)rurrrr load_modules   zmock_modules.load_module)N)r,r/rrrrrrrrrs rc@s*eZdZdZd ddZddZddZdS) mock_specz!Importer mock using PEP 451 APIs.NcCsDz|j|}Wnty"YdS0tj||j|t|ddd}|S)Nrt)loadersubmodule_search_locations)rRrSrspec_from_file_locationrrrf)rurrrHrxspecrrr find_specs  zmock_spec.find_speccCs|j|jvrt|j|jSr)rrRr)rurrrr create_module$s zmock_spec.create_modulecCs,z|j|jjWnty&Yn0dSr)rl__spec__rrSrurxrrr exec_module)s zmock_spec.exec_module)NN)r,r/rrrrrrrrrrs rcs(tjrddStfdd}|S)ztDecorator to protect sys.dont_write_bytecode from mutation and to skip tests that require it to be set to False.c_sdSrr)argsr5rrr4r\z'writes_bytecode_files..cs2tj}dt_z|i|}W|t_n|t_0|S)NF)rdont_write_bytecode)rr5ZoriginalZ to_returnfxnrrwrapper5s z&writes_bytecode_files..wrapper)rr functoolswraps)rrrrrwrites_bytecode_files0s rc CsNzttj|Wn2tyH}z|jtjkr4WYd}~n d}~00dS)zEnsure that the __pycache__ directory for PEP 3147 pyc file exists. :param bytecode_path: File system path to PEP 3147 pyc file. N)rr]rdirnameOSErrorr EEXIST) bytecode_patherrorrrrensure_bytecode_pathAs  rccs*tj}|t_zdVW|t_n|t_0dS)z&Adjust and restore sys.pycache_prefix.N)rpycache_prefix)prefixZ _orig_prefixrrrtemporary_pycache_prefixMs rc gsd}g}i}d}d}zxt}||d<t}|D]}|dsF|} n|dtd } || | tjvrttj| =|d} |} | ddD]2} t j | | } t j | st | || qt j | | dd} t| d  } | ||Wdn1s 0Y|| | ||<q2t|}|t|gd }||VW|durl|ddd|dur|dddt|n<|dur|ddd|dur|dddt|0dS) aRTemporarily create each named module with an attribute (named 'attr') that contains the name passed into the context manager that caused the creation of the module. All files are created in a temporary directory returned by tempfile.mkdtemp(). This directory is inserted at the beginning of sys.path. When the context manager exits all created files (source and bytecode) are explicitly deleted. No magic is performed when creating packages! This means that if you create a module within a package you must also create the package's __init__ as well. z attr = {0!r}Nz.rootryrmrrDrEr)tempfilemkdtempsetrnrgaddrrRsplitrrrrr]appendrFrGr+rUr}rjrrrmtree)rTr&Z created_pathsmappingZ state_managerZuncache_managerZtemp_dirZ import_namesrrvZ name_partsrZ directoryfilerrrcreate_modulesXsV        0       rcsfdd}|S)zA mock sys.path_hooks entry.cs|vr tSr)r)entryentriesimporterrrhookszmock_path_hook..hookr)rrrrrrmock_path_hooksrc@seZdZddZdS)CASEOKTestBasecs,d}tfdd|D|kr(ddS)N)s PYTHONCASEOK PYTHONCASEOKc3s|]}|jjjjvVqdSr)r _bootstrap_external_osenviron)rXxr~rr sz4CASEOKTestBase.caseok_env_changed..z/os.environ changes not reflected in _os.environ)anyZskipTest)ruZ should_existZ possibilitiesrr~rcaseok_env_changeds z!CASEOKTestBase.caseok_env_changedN)r,r/rrrrrrrsrrc sNGfdddt}d}t|}|}tj||d|d}||_||_|S)NcsHeZdZddZfddZfddZfddZfd d Zd S) zcreate_package..ReadercSs|Srrrurwrrrget_resource_readersz2create_package..Reader.get_resource_readercs||_ttrnSdSr_pathr)rrur)rrr open_resources z,create_package..Reader.open_resourcecs||_ttrnSdSrr)rupath_rrr resource_paths z,create_package..Reader.resource_pathcsJ||_ttrD],}|d}t|dkr|d|krdSqdS)N/r rTF)rr)rrrg)rurrpartscontentsrrr is_resources  z*create_package..Reader.is_resourcec3s"ttrD] }|VqdSr)r)r)rurrrrrs z'create_package..Reader.contentsN)r,r/rrrrrrrrrrrrReaders    rZtestingpackagezdoes-not-exist)origin is_package)rr-rpr ModuleSpecrrq) rrrrrrrxrrrrrcreate_packages# rc@seZdZejddZddZddZddZe e j d kd d d Z d dZddZddZddZddZe e j dkdddZe e j dkdddZddZdS) CommonResourceTestscCstdSr)NotImplementedError)rurwrrrrexecuteszCommonResourceTests.executecCs|tjddSN utf-8.file)rr r,r~rrrtest_package_namesz%CommonResourceTests.test_package_namecCs|tddSrrr r~rrrtest_package_objectsz'CommonResourceTests.test_package_objectcCsd}|t|dSrrrrrrtest_string_pathsz$CommonResourceTests.test_string_path)zrequires os.PathLike supportcCstd}|t|dSr)rrr rrrrtest_pathlib_pathsz%CommonResourceTests.test_pathlib_pathcCsLtt}|jd}|t|t|Wdn1s>0YdSr)rrrrH assertRaisesrQrr )rur full_pathrrrtest_absolute_paths  z&CommonResourceTests.test_absolute_pathcCs:|t|tdWdn1s,0YdS)Nz../data01/utf-8.file)rrQrr r~rrrtest_relative_paths z&CommonResourceTests.test_relative_pathcCstjtj=|tjddSr)rrRr r,rr~rrr$test_importing_module_as_side_effects z8CommonResourceTests.test_importing_module_as_side_effectcCs:|t|tdWdn1s,0YdSr)r TypeErrorrr,r~rrrtest_non_package_by_names z,CommonResourceTests.test_non_package_by_namecCsD|t&tjd}||dWdn1s60YdS)Nztest.test_importlib.utilr)rrrrRrrrrrtest_non_package_by_packages  z/CommonResourceTests.test_non_package_by_package)rzNo ResourceReader in Python 2cCs8td}t|td}||d||jjddSNs Hello, world!rrr)ioBytesIOrFileNotFoundErrorr assertEqualrqr)ru bytes_datarwrrrtest_resource_opener s  z(CommonResourceTests.test_resource_openercCs:td}t}t||d}||d||jjddSr)rrrrrrrrqr)rurrrwrrrtest_resource_paths    z&CommonResourceTests.test_resource_pathcCsJtttd}|t||dWdn1s<0YdS)Nrr)rrrrrrrrtest_useless_loaders  z'CommonResourceTests.test_useless_loaderN)r,r/rabcabstractmethodrrrrr(r?r version_inforrrrrrrrrrrrrrs"    rc@s0eZdZdZeddZeddZddZdS) ZipSetupBaseNcCs>t|jj}|j}t|d|_tj|jt d|_ dS)Nzziptestdata.zipZ ziptestdata) r ZIP_MODULErrrHstr _zip_pathrrrr import_moduledata)r2Z data_pathZdata_dirrrr setUpClass#s  zZipSetupBase.setUpClasscCsxztj|jWnty$Yn0ztj|j=tj|jj=Wnt yRYn0z |`|`Wnt yrYn0dSr) rrremoverrQrerRrr,rSAttributeError)r2rrr tearDownClass+s    zZipSetupBase.tearDownClasscCs t}|jtjg|RdSr)rZ modules_setupZ addCleanupZmodules_cleanup)rurRrrrsetUp>szZipSetupBase.setUp)r,r/rr classmethodrrrrrrrr s   rc@seZdZeZdS)ZipSetupN)r,r/rr rrrrrrCsr)N)N)N)rC)rC)Tr)Jrbuiltins contextlibr rr rrr importlib.abcrrrNrZos.pathpathlibrrrArr(rrr-rCr r SimpleNamespaceZBUILTINSZ good_nameZbad_namebuiltin_module_namesrrrrrrrr'r:r;r<r@platformrrupperZ changed_namelowerrZsource_importlib staticmethod __import__rBrJrPcontextmanagerrUrbrjrkrrrrrrrrrABCrrrrrrrs                 )   9  2J#