a |_/@sdZddlZddlZddlZddlZddlZddlZddlm Z m Z ddgZ Gddde Z Gdd d e Zd d d Zd!d d ZefddZGdddeZd"ddZd#ddZefddZddZd$ddZedkreejdkredejdnejd=eejddS)%aZrunpy.py - locating and running Python code using the module namespace Provides support for locating and running Python scripts using the Python module namespace instead of the native filesystem. This allows Python code to play nicely with non-filesystem based PEP 302 importers when locating support scripts as well as when importing modules. N) read_code get_importer run_modulerun_pathc@s(eZdZdZddZddZddZdS) _TempModulezCTemporarily replace a module in sys.modules with an empty namespacecCs||_t||_g|_dSN)mod_nametypesZ ModuleTypemodule _saved_moduleselfrr!/usr/local/lib/python3.9/runpy.py__init__s z_TempModule.__init__cCs@|j}z|jtj|Wnty.Yn0|jtj|<|Sr)rr appendsysmodulesKeyErrorr r rrr __enter__ s  z_TempModule.__enter__cGs.|jr|jdtj|j<n tj|j=g|_dSNr)r rrrr argsrrr__exit__)s z_TempModule.__exit__N)__name__ __module__ __qualname____doc__rrrrrrrrs rc@s$eZdZddZddZddZdS)_ModifiedArgv0cCs||_t|_|_dSr)valueobject _saved_value _sentinel)r rrrrr1sz_ModifiedArgv0.__init__cCs0|j|jurtdtjd|_|jtjd<dS)NzAlready preserving saved valuer)r!r" RuntimeErrorrargvr)r rrrr5s  z_ModifiedArgv0.__enter__cGs|j|_|jtjd<dSr)r"rr!rr$rrrrr;sz_ModifiedArgv0.__exit__N)rrrrrrrrrrr0src Csn|dur|||dur(d}|}d} n |j}|j}|j} |durH|j}|j||| d|||dt|||S)z)Helper to run code in nominated namespaceN)r__file__ __cached__r __loader__ __package____spec__)updateloaderorigincachedparentexec) codeZ run_globals init_globalsrmod_specpkg_name script_namer+fnamer-rrr _run_code@s*  r6c Cs|dur |n|j}t|T}t|,|jj}t|||||||Wdn1sV0YWdn1st0Y|S)z5Helper to run code in new namespace with sys modifiedN)r,rrr __dict__r6copy) r0r1rr2r3r4r5 temp_module mod_globalsrrr_run_module_codeZs@r;c Csd|dr|d|d\}}}|rz t|WnJty|}z2|jdusf|j|krh||jdshWYd}~n d}~00tj|}|durt|dsddl m }dj ||d}|t |zt j|}WnvttttfyH} zTd}|d r|d |dd d |d 7}|| |t| j| | WYd} ~ n d} ~ 00|dur`|d||jdur|dks|dr|dz|d} t| |WS|y}z*|tjvr|d||fWYd}~n d}~00|j} | dur|d|z| |} Wn4tyB}z|t ||WYd}~n d}~00| durZ|d|||| fS)N.z#Relative module names not supported__path__r)warnz{mod_name!r} found in sys.modules after import of package {pkg_name!r}, but prior to execution of {mod_name!r}; this may result in unpredictable behaviour)rr3z:Error while finding module specification for {!r} ({}: {})z.pyz . Try using 'z' instead of 'z' as the module name.zNo module named %s__main__z .__main__z%Cannot use package as __main__ modulez3%s; %r is a package and cannot be directly executedz0%r is a namespace package and cannot be executedzNo code object available for %s) startswith rpartition __import__ ImportErrornamerrgethasattrwarningsr>formatRuntimeWarning importlibZutil find_specAttributeError TypeError ValueErrorendswithtypersubmodule_search_locations_get_module_detailsr+get_code) rerrorr3_eZexistingr>msgspecZexZ pkg_main_namer+r0rrrrShsj       0      $  rSc@seZdZdZdS)_ErrorzBError that _run_module_as_main() should report without a tracebackN)rrrrrrrrrZsrZTc Csz0|s|dkr t|t\}}}ntt\}}}Wn<tyl}z$dtj|f}t|WYd}~n d}~00tjdj}|r|jtj d<t ||dd|S)aRuns the designated module in the __main__ namespace Note that the executed module will have full access to the __main__ namespace. If this is not desirable, the run_module() function should be used to run the module code in a fresh namespace. At the very least, these variables in __main__ will be overwritten: __name__ __file__ __cached__ __loader__ __package__ r@z%s: %sNr) rSrZ_get_main_module_detailsr executableexitrr7r,r$r6)rZ alter_argvr2r0excrXZ main_globalsrrr_run_module_as_mains    r_FcCs@t|\}}}|dur|}|r,t||||St|i|||SdS)znExecute a module's code without importing it Returns the resulting top level namespace dictionary N)rSr;r6)rr1run_nameZ alter_sysr2r0rrrrs c Csd}tj|}tj|=zpzt|WW|tj|<Styx}z2|t|vrb|d|tjdf|WYd}~n d}~00W|tj|<n |tj|<0dS)Nr@zcan't find %r module in %rr)rrrSrDstrpath)rUZ main_nameZ saved_mainr^rrrr[s      r[cCstjt|}t|}t|}Wdn1s:0Y|durt| }t||d}Wdn1s|0Y||fS)Nr/) osrbabspathfsdecodeio open_codercompileread)r`r5Z decoded_pathfr0rrr_get_code_from_files & .rkc Cs|dur d}|dd}t|}d}t|jdkrFt|jdkrFd}t|tdsX|rxt||\}}t|||||d Stj d|zt \}} }t |} t |d| jj} t|| ||| |WdWdWztj |WStyYS0S1s0YWdn1s20YWztj |WntybYn0n(ztj |WntyYn00dS) a_Execute code located at the specified filesystem location Returns the resulting top level namespace dictionary The file path may refer directly to a Python script (i.e. one that could be directly executed with execfile) or else it may refer to a zipfile or directory containing a top level __main__.py script. Nz r<rFZimpZ NullImporterT)r3r4)rBrrQrr isinstancerkr;rrbinsertr[rrr r7r6r8removerO) Z path_namer1r`r3ZimporterZis_NullImporterr0r5rr2r9r:rrrrsH   $6r@z!No module specified for execution)file)NNNNN)NNNNN)T)NNF)NN)rrZimportlib.machineryrKZimportlib.utilrfr rcZpkgutilrr__all__r rrr6r;rDrS ExceptionrZr_rr[rkrrlenr$printstderrrrrrsB    =    1