o ?Og73@sdZddlZddlZddlZddlZddlZddlZddgZGddde Z Gddde Z   dd d Z   dd d Z efd dZGdddeZd ddZ  d!ddZefddZddZd"ddZedkreejdkr{edejddSejd=eejddSdS)#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 run_modulerun_pathc@s(eZdZdZddZddZddZdS) _TempModulezCTemporarily replace a module in sys.modules with an empty namespacecCs||_t||_g|_dSN)mod_nametypes ModuleTypemodule _saved_moduleselfrr ,/opt/alt/python310/lib64/python3.10/runpy.py__init__s  z_TempModule.__init__cCs@|j}z |jtj|Wn tyYnw|jtj|<|Sr)rr appendsysmodulesKeyErrorr r r r r __enter__s  z_TempModule.__enter__cGs.|jr |jdtj|j<ntj|j=g|_dSNr)r rrrr argsr r r__exit__(s  z_TempModule.__exit__N)__name__ __module__ __qualname____doc__rrrr r r rrs  rc@s$eZdZddZddZddZdS)_ModifiedArgv0cCs||_t|_|_dSr)valueobject _saved_value _sentinel)r rr r rr0sz_ModifiedArgv0.__init__cCs0|j|jur tdtjd|_|jtjd<dS)NzAlready preserving saved valuer)r r! RuntimeErrorrargvr)r r r rr4s  z_ModifiedArgv0.__enter__cGs|j|_|jtjd<dSr)r!rr rr#rr r rr:sz_ModifiedArgv0.__exit__N)rrrrrrr r r rr/s rc Csn|dur |||durd}|}d} n|j}|j}|j} |dur$|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,r r r _run_code?s*  r5c Cs|dur|n|j}t|7}t||jj}t|||||||Wdn1s+wYWd|SWd|S1sGwY|S)z5Helper to run code in new namespace with sys modifiedN)r+rrr __dict__r5copy) r/r0rr1r2r3r4 temp_module mod_globalsr r r_run_module_codeYs( r:c Cs.|dr |d|d\}}}|razt|Wn%ty>}z|jdus3|j|kr4||jds4WYd}~nd}~wwtj|}|durat|dsaddl m }dj ||d}|t |zt j|}Wn3ttttfy} z#d}|d r|d |dd d |d 7}|| |t| j| | d} ~ ww|dur|d||jdur|dks|dr|dz |d} t| |WS|y}z|tjvrт|d||fd}~ww|j} | dur|d|z| |} Wnty}z|t ||d}~ww| dur|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)rr2z: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 importlibutil find_specAttributeError TypeError ValueErrorendswithtypersubmodule_search_locations_get_module_detailsr*get_code) rerrorr2_eZexistingr=msgspecZexZ pkg_main_namer*r/r r rrSgsz              rSc@seZdZdZdS)_ErrorzBError that _run_module_as_main() should report without a tracebackN)rrrrr r r rrZsrZTc Csz|s|dkrt|t\}}}ntt\}}}Wnty6}zdtj|f}t|WYd}~nd}~wwtjdj}|rE|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 executableexitrr6r+r#r5)rZ alter_argvr1r/excrXZ main_globalsr r r_run_module_as_mains    r_FcCs<t|\}}}|dur |}|rt||||St|i|||S)u"Execute a module's code without importing it. mod_name -- an absolute module name or package name. Optional arguments: init_globals -- dictionary used to pre-populate the module’s globals dictionary before the code is executed. run_name -- if not None, this will be used for setting __name__; otherwise, __name__ will be set to mod_name + '__main__' if the named module is a package and to just mod_name otherwise. alter_sys -- if True, sys.argv[0] is updated with the value of __file__ and sys.modules[__name__] is updated with a temporary module object for the module being executed. Both are restored to their original values before the function returns. Returns the resulting module globals dictionary. N)rSr:r5)rr0run_nameZ alter_sysr1r/r r rrs c Cszd}tj|}tj|=z+z t|WW|tj|<Sty6}z|t|vr1|d|tjdf|d}~ww|tj|<w)Nr?zcan't find %r module in %rr)rrrSrCstrpath)rUZ main_nameZ saved_mainr^r r rr[s"      r[cCsddlm}tjt|}t| }||}Wdn1s#wY|durLt|}t| |d}Wd||fS1sGwY||fS)Nr) read_coder.) pkgutilrcosrbabspathfsdecodeio open_codecompileread)r`r4rcZ decoded_pathfr/r r r_get_code_from_files     rmc Cs|durd}|dd}ddlm}||}d}t|jdkr)t|jdkr)d }t|tds2|rBt||\}}t|||||d St j d|zkt \} } }t |D} t|0| jj} t|| ||| |WdWdWzt j |WStyYSw1swYWdn1swYWz t j |WdStyYdSwzt j |WwtyYww) uExecute code located at the specified filesystem location. path_name -- filesystem location of a Python script, zipfile, or directory containing a top level __main__.py script. Optional arguments: init_globals -- dictionary used to pre-populate the module’s globals dictionary before the code is executed. run_name -- if not None, this will be used to set __name__; otherwise, '' will be used for __name__. Returns the resulting module globals dictionary. Nz r;r) get_importerFZimpZ NullImporterT)r2r3)rArdrnrQrr isinstancermr:rrbinsertr[rrr r6r5r7removerO) Z path_namer0r`r2rnZimporterZis_NullImporterr/r4rr1r8r9r r rrsV    .  r?z!No module specified for execution)file)NNNNN)T)NNF)NN)rrimportlib.machineryrJimportlib.utilrhrre__all__rrrr5r:rCrS ExceptionrZr_rr[rmrrlenr#printstderrr r r rsD   =   7