B \].@sdZddlZddlZddlZddlZddlmZmZddgZ Gddde Z Gdd d e Z d d d Z d!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|_dS)N)mod_nametypesZ ModuleTypemodule _saved_module)selfrr !/usr/local/lib/python3.7/runpy.py__init__s z_TempModule.__init__cCsB|j}y|jtj|Wntk r0YnX|jtj|<|S)N)rr appendsysmodulesKeyErrorr )r rr r r __enter__s z_TempModule.__enter__cGs.|jr|jdtj|j<n tj|j=g|_dS)Nr)r rrr)r argsr r r __exit__'s z_TempModule.__exit__N)__name__ __module__ __qualname____doc__rrrr r r r rs rc@s$eZdZddZddZddZdS)_ModifiedArgv0cCs||_t|_|_dS)N)valueobject _saved_value _sentinel)r rr r r r/sz_ModifiedArgv0.__init__cCs0|j|jk rtdtjd|_|jtjd<dS)NzAlready preserving saved valuer)rr RuntimeErrorrargvr)r r r r r3s  z_ModifiedArgv0.__enter__cGs|j|_|jtjd<dS)Nr)rrrrr )r rr r r r9sz_ModifiedArgv0.__exit__N)rrrrrrr r r r r.src Csn|dk r|||dkr(d}|}d} n |j}|j}|j} |dkrH|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(  r2c Cs^|dkr |n|j}t|6}t|"|jj}t|||||||WdQRXWdQRX|S)z5Helper to run code in new namespace with sys modifiedN)r(rrr __dict__r2copy) r,r-rr.r/r0r1 temp_module mod_globalsr r r _run_module_codeXs  r7c Cs0|dr|d|d\}}}|ry t|WnHtk rz}z*|jdksh|j|krj||jdsjWdd}~XYnXtj|}|dk rt|dsddl m }dj ||d}|t |yt j|}WnJttttfk r} z"d}|| |t| j| | Wdd} ~ XYnX|dkr2|d ||jdk r|d ksT|d r\|d y|d } t| |S|k r}z"|tjkr|d ||fWdd}~XYnX|j} | dkr|d|y| |} Wn2tk r}z|t ||Wdd}~XYnX| dkr&|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)rr/z:Error while finding module specification for {!r} ({}: {})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 ValueErrortypersubmodule_search_locationsendswith_get_module_detailsr'get_code) rerrorr/_eZexistingr:msgspecZexZ pkg_main_namer'r,r r r rNfsZ      ,         rNc@seZdZdZdS)_ErrorzBError that _run_module_as_main() should report without a tracebackN)rrrrr r r r rUsrUTc Csy0|s|dkr t|t\}}}ntt\}}}Wn:tk rj}zdtj|f}t|Wdd}~XYnXtjdj}|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) rNrU_get_main_module_detailsr executableexitrr3r(r r2)rZ alter_argvr.r,excrSZ main_globalsr r r _run_module_as_mains   rZFcCs@t|\}}}|dkr|}|r,t||||St|i|||SdS)znExecute a module's code without importing it Returns the resulting top level namespace dictionary N)rNr7r2)rr-run_nameZ alter_sysr.r,r r r rs c Cs~d}tj|}tj|=zVyt|Stk rh}z*|t|krV|d|tjdf|Wdd}~XYnXWd|tj|<XdS)Nr;zcan't find %r module in %rr)rrrNr?strpath)rPZ main_nameZ saved_mainrYr r r rVs  rVc CsTt|d}t|}WdQRX|dkrLt|d}t||d}WdQRX||fS)NZrbr+)openrcompileread)r[r1fr,r r r _get_code_from_files   rbc Cs|dkr d}|dd}t|}d}t|jdkrFt|jdkrFd}t|tdsX|rxt||\}}t|||||d Stj d|zRt \}} }t |4} t | | jj} t|| ||| |SQRXWdQRXWdytj |Wntk rYnXXdS) 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 r8rFZimpZ NullImporterT)r/r0)r=rrKrr isinstancerbr7rr]insertrVrrr r3r2r4removerJ) Z path_namer-r[r/ZimporterZis_NullImporterr,r1rr.r5r6r r r rs0     "r;z!No module specified for execution)file)NNNNN)NNNNN)T)NNF)NN)rrZimportlib.machineryrFZimportlib.utilrZpkgutilrr__all__rrrr2r7r?rN ExceptionrUrZrrVrbrrlenr printstderrr r r r s6  :    1