\dg3dZddlZddlZddlZddlZddlZddgZeeZ Gdde Z Gdde Z dd Z dd Zefd ZGd d eZddZ ddZefdZdZddZedkrHeejdkredejdSejd=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$eZdZdZdZdZdZdS) _TempModulezCTemporarily replace a module in sys.modules with an empty namespacecJ||_t||_g|_dSN)mod_name ModuleTypemodule _saved_moduleselfr s ..\python\lib\runpy.py__init__z_TempModule.__init__s&   ** c|j} |jtj|n#t $rYnwxYw|jtj|<|Sr)r r appendsysmodulesKeyErrorr r s r __enter__z_TempModule.__enter__!sb=    % %ck(&; < < < <    D  $  H s*4 AAc|jr |jdtj|j<ntj|j=g|_dSNr)r rrr rargss r__exit__z_TempModule.__exit__*s@   +)-);A)>CK & & DM*rN)__name__ __module__ __qualname____doc__rrrrrrrsGMM        rrc eZdZdZdZdZdS)_ModifiedArgv0cH||_tx|_|_dSr)valueobject _saved_value _sentinel)rr%s rrz_ModifiedArgv0.__init__2s  -3XX5DNNNrc|j|jurtdtjd|_|jtjd<dS)NzAlready preserving saved valuer)r'r( RuntimeErrorrargvr%)rs rrz_ModifiedArgv0.__enter__6s@  DN 2 2?@@ @HQKj rcF|j|_|jtjd<dSr)r(r%r'rr+rs rrz_ModifiedArgv0.__exit__<s^ ' rN)rrrrrrr!rrr#r#1sA666!!! (((((rr#c ||||d}|}d} n|j}|j}|j} ||j}|||| d|||t |||S)z)Helper to run code in nominated namespaceN)r__file__ __cached__r __loader__ __package____spec__)updateloaderorigincachedparentexec) code run_globals init_globalsr mod_specpkg_name script_namer4fnamer6s r _run_coder@As<(((  H("'$*!%$*%-"* ,,, { rc ||n|j}t|5}t|5|jj}t |||||||dddn #1swxYwYdddn #1swxYwY|S)z5Helper to run code in new namespace with sys modifiedN)r5rr#r __dict__r@copy) r9r;r r<r=r>r? temp_module mod_globalss r_run_module_coderF[s$+KKE X  =+~e/D/D==!(1 $ \Hh  = = ===============================     s4A0"A A0A A0 A !A00A47A4c |dr |d|d\}}}|r t|nG#t$r:}|j(|j|kr||jdzsYd}~nd}~wwxYwt j|}|Et|ds5ddl m }d ||}|t| tj|}n#tt t"t$f$rc} d}|d r|d |dd d |d z }|| |t)| j| | d} ~ wwxYw||d|z|ji|dks|dr |d |dz} t/| |S#|$r%}|t jvr||d|dd}~wwxYw|j} | |d|z | |} n+#t$r}|t||d}~wwxYw| |d|z||| 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)r r=z: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__ modulez; z- 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__ ImportErrornamerrgethasattrwarningsrJformatRuntimeWarning importlibutil find_specAttributeError TypeError ValueErrorendswithtypersubmodule_search_locations_get_module_detailsr4get_code) r errorr=_eexistingrJmsgspecex pkg_main_namer4r9s rr`r`isl3;e9:::((--NHa&  x    v~!&H"4"4 ++AFSL99#5  ;??8,,  *(E(E  % % % % % %#FHxFHH  D$$ % % % I~''11 J ?IIIK   U # # 9 8HSbSM88 888 9CeCJJxb):B??@@bHI |e(83444 &2 z ! !X%6%6{%C%C !%?@@ @ G${2M&}e<< < G G Gs{**%9:HHHFGG G G [F ~eFCKLMM M&x(( &&&eF1IIA%& |e5@AAA T4 s`A B0B  B;DF8AFFG11H6 HH6I I4I//I4ceZdZdZdS)_ErrorzBError that _run_module_as_main() should report without a tracebackN)rrrr r!rrrkrksLLLLrrkTcr |s|dkrt|t\}}}ntt\}}}n<#t$r/}tjd|}tj|Yd}~nd}~wwxYwtjdj}|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__ rLz: Nr) r`rk_get_main_module_detailsr executableexitrrBr5r+r@)r alter_argvr<r9excrf main_globalss r_run_module_as_mainrss  HZ//':8V'L'L $Hh'?'G'G $Hh ...##.  ;z*3L&o T< + ++s:= A6%A11A6Fc~t|\}}}||}|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. )r`rFr@)r r;run_name alter_sysr<r9s rrrsX* 38<<HhElHhGGGr<8DDDrc6d}tj|}tj|= t||tj|<S#t$r9}|t |vr"|d|dtjd|d}~wwxYw#|tj|<wxYw)NrLz can't find z module in r)rrr`rPstrpath)rb main_name saved_mainrqs rrmrms IY'J I,"9--", I  C %%(yy#(1++788=@ A  ", I++++s#A B 4A??BBBcddlm}tjtj|}t j|5}||}dddn #1swxYwY|Ot j|5}t| |d}dddn #1swxYwY||fS)Nr) read_coder8) pkgutilr}osryabspathfsdecodeio open_codecompileread)rur?r} decoded_pathfr9s r_get_code_from_filers*!!!!!!7??2;u#5#566L l # #qy|| | \, ' ' 4116688UF33D 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 ;s$ A$$A(+A($B55B9<B9c |d}|dd}ddlm}||}d}t|jdkrt|jdkrd }t |tds|r't||\}}t||||| Stj d| t\} } }t|5} t|5| jj} t#|| ||| |cdddcddd tj |S#t($rYSwxYw#1swxYwYdddn #1swxYwY tj |dS#t($rYdSwxYw# tj |w#t($rYwwxYwxYw) 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 rHr) get_importerFimp NullImporterT)r=r>)rNr~rr^rr isinstancerrFrryinsertrmrr#r rBr@rCremover\) path_namer;rur=rimporteris_NullImporterr9r?r r<rDrEs rrrs""3''*H$$$$$$|I&&HO H~~ E)) >> "n 4 4"O(DJJ''?*(I>> elH)1uFFF F 9%%% (@'A'A $HhX&& I+ ** I I)09  {L$,hBBBF$&&  I I I I I I I I I I I I I I   ****     I I I I I I I I I I I I I I I I I I I I I I I   *****      ****    s !G -F=2E8/ F; G E(( E54E58E< <F?E< F G FG FG F<< G  G  G?G/.G?/ G<9G?;G<<G?rLz!No module specified for execution)file)NNNNN)T)NNF)NN)r rimportlib.machineryrWimportlib.utilrr__all__r^r r&rr#r@rFrPr` Exceptionrkrsrrmrrrlenr+printstderrr!rrrs  *  T#YY      &   . ( ( ( ( (V ( ( ( /3&*)-4)-,0/3    )4; ; ; ; zMMMMMYMMM++++8'+(-EEEE<$/,,,,&   4444n z s38}}q 1 CCCCCC HQKCHQK((((( r