e f@*@smdZddlZddlZddlZddlZddlmZmZddgZ Gddde Z Gdd d e Z dddddd d Z dddddd d ZddZdddZdddddZddZddZddddZedkrieejdkrKeddejqiejd=eejdndS)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(||_tj||_g|_dS)N)mod_nametypes ModuleTypemodule _saved_module)selfrr */opt/alt/python34/lib64/python3.4/runpy.py__init__s z_TempModule.__init__c CsM|j}y|jjtj|Wntk r8YnX|jtj|<|S)N)rr appendsysmodulesKeyErrorr )r rr r r __enter__s  z_TempModule.__enter__cGs=|jr#|jdtj|js(         r3c Csu|dkr|n|j}t|B}t|-|jj}t|||||||WdQXWdQX|jS)z5Helper to run code in new namespace with sys modifiedN)r)rrr __dict__r3copy) r-r.rr/r0r1r2 temp_module mod_globalsr r r_run_module_codeXs   r8cCsytjj|}Wn[ttttfk rs}z/d}t|j|t|||WYdd}~XnX|dkrtd|n|j dk r)|dks|j drtdny|d}t |SWq)tk r%}ztdd||fWYdd}~Xq)Xn|j }|dkrQtd|n|j |}|dkrtd |n|||fS) Nz*Error while finding spec for {!r} ({}: {})zNo module named %s__main__z .__main__z%Cannot use package as __main__ modulez%s; %r is a package and cannot zbe directly executedz0%r is a namespace package and cannot be executedzNo code object available for %s) importlibutil find_spec ImportErrorAttributeError TypeError ValueErrorformattypesubmodule_search_locationsendswith_get_module_detailsr(get_code)rspecZexmsgZ pkg_main_nameer(r-r r rrEfs07  *    rETcCsy@|s|dkr-t|\}}}nt\}}}Wnktk r}zK|rjt|}ndtjd}dtj|f}tj|WYdd}~XnXtjdj }|r|j tjd.rFZimpZ NullImporterTr0r1) rpartitionrrBrr isinstancerXr8rrRinsertrJrrr r4r3r5remover@) Z path_namer.rQr0ZimporterZis_NullImporterr-r2rr/r6r7r r rrs0        # r9z!No module specified for executionfile)rrimportlib.machineryr:importlib.utilrZpkgutilrr__all__rrrr3r8rErPrrJrXrrlenr!printstderrr r r rs4      !%  1