
[[\@*                 @   sm  d  Z  d d l Z d d l Z d d l Z d d l Z d d l m Z m Z d d g Z	 Gd d   d e
  Z Gd d	   d	 e
  Z d d d d d d
 d  Z d d d d d d d  Z d d   Z d d d  Z d d d d d  Z d d   Z d d   Z d d d d  Z e d k rie e j  d k  rKe d d e j qie j d =e e j d  n  d S)aZ  runpy.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_modulerun_pathc               @   s:   e  Z d  Z d Z d d   Z d d   Z d d   Z d S)	_TempModulezCTemporarily replace a module in sys.modules with an empty namespacec             C   s(   | |  _  t j |  |  _ g  |  _ d  S)N)mod_nametypes
ModuleTypemodule_saved_module)selfr    r   /usr/lib/python3.4/runpy.py__init__   s    	z_TempModule.__init__c             C   sM   |  j  } y |  j j t j |  Wn t k
 r8 Yn X|  j t j | <|  S)N)r   r   appendsysmodulesKeyErrorr
   )r   r   r   r   r   	__enter__   s    	z_TempModule.__enter__c             G   s=   |  j  r# |  j  d t j |  j <n t j |  j =g  |  _  d  S)Nr   )r   r   r   r   )r   argsr   r   r   __exit__'   s    	z_TempModule.__exit__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r      s   	r   c               @   s4   e  Z d  Z d d   Z d d   Z d d   Z d S)_ModifiedArgv0c             C   s    | |  _  t   |  _ |  _ d  S)N)valueobject_saved_value	_sentinel)r   r   r   r   r   r   /   s    	z_ModifiedArgv0.__init__c             C   sE   |  j  |  j k	 r! t d   n  t j d |  _  |  j t j d <d  S)NzAlready preserving saved valuer   )r   r   RuntimeErrorr   argvr   )r   r   r   r   r   3   s    z_ModifiedArgv0.__enter__c             G   s    |  j  |  _ |  j t j d <d  S)Nr   )r   r   r   r   r!   )r   r   r   r   r   r   9   s    z_ModifiedArgv0.__exit__N)r   r   r   r   r   r   r   r   r   r   r   .   s   r   c       
      C   s   | d k	 r | j  |  n  | d k r= d } | } d }	 n3 | j } | j } | j }	 | d k rp | j } n  | j  d | d | d |	 d d d | d | d |  t |  |  | S)	z)Helper to run code in nominated namespaceNr   __file__
__cached__r   
__loader____package____spec__)updateloaderorigincachedparentexec)
codeZrun_globalsinit_globalsr   mod_specpkg_namescript_namer(   fnamer*   r   r   r   	_run_code>   s(    				r3   c       	      C   su   | d k r | n | j  } t |  B } t |  - | j j } t |  | | | | | |  Wd QXWd QX| j   S)z5Helper to run code in new namespace with sys modifiedN)r)   r   r   r
   __dict__r3   copy)	r-   r.   r   r/   r0   r1   r2   temp_modulemod_globalsr   r   r   _run_module_codeX   s    r8   c             C   s  y t  j j |   } Wn[ t t t t f k
 rs } z/ d } t | j |  t |  |   |  WYd  d  } ~ Xn X| d  k r t d |    n  | j	 d  k	 r)|  d k s |  j
 d  r t d   n  y |  d } t |  SWq)t k
 r%} z t d d | |  f   WYd  d  } ~ Xq)Xn  | j } | d  k rQt d |    n  | j |   } | d  k rt d	 |    n  |  | | f S)
Nz*Error while finding spec for {!r} ({}: {})zNo module named %s__main__z	.__main__z%Cannot use package as __main__ modulez%s; %r is a package and cannot zbe directly executedz0%r is a namespace package and cannot be executedzNo code object available for %s)	importlibutil	find_specImportErrorAttributeError	TypeError
ValueErrorformattypesubmodule_search_locationsendswith_get_module_detailsr(   get_code)r   specZexmsgZpkg_main_nameer(   r-   r   r   r   rE   f   s0    7
*	rE   Tc             C   s   y@ | s |  d k r- t  |   \ }  } } n t   \ }  } } Wnk t k
 r } zK | rj t |  } n d t j d } d t j | f } t j |  WYd d } ~ Xn Xt j d j	 } | r | j
 t j d <n  t | | d d |  S)a  Runs 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__
    r9   z"can't find '__main__' module in %rr   z%s: %sN)rE   _get_main_module_detailsr=   strr   r!   
executableexitr   r4   r)   r3   )r   Z
alter_argvr/   r-   excinforH   Zmain_globalsr   r   r   _run_module_as_main   s     rP   Fc             C   s]   t  |   \ }  } } | d k r* |  } n  | rC t | | | |  St | i  | | |  Sd S)znExecute a module's code without importing it

       Returns the resulting top level namespace dictionary
    N)rE   r8   r3   )r   r.   run_nameZ	alter_sysr/   r-   r   r   r   r      s    	c              C   s   d }  t  j |  } t  j |  =zr y t |   SWn] t k
 r } z= |  t |  k rx t d |  t  j d f  |  n    WYd  d  } ~ Xn XWd  | t  j |  <Xd  S)Nr9   zcan't find %r module in %rr   )r   r   rE   r=   rK   path)Z	main_nameZ
saved_mainrN   r   r   r   rJ      s    
rJ   c             C   sm   t  | d   } t |  } Wd  QX| d  k rc t  | d   } t | j   | d  } Wd  QXn  | | f S)Nrbr,   )openr   compileread)rQ   r2   fr-   r   r   r   _get_code_from_file   s    !rX   c             C   si  | d k r d } n  | j  d  d } t |   } d } t |  j d k rp t |  j d k rp d } qp n  t | t d   s | r t | |   \ } } t | | | d	 | d
 | St j	 j
 d |   zi t   \ } }	 } t |  E }
 t |   0 |
 j j } t | | | | |	 |  j   SWd QXWd QXWd y t j	 j |   Wn t k
 rcYn XXd S)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
<run_path>.r   FZimpZNullImporterTr0   r1   )
rpartitionr   rB   r   r   
isinstancerX   r8   r   rR   insertrJ   r   r   r
   r4   r3   r5   remover@   )Z	path_namer.   rQ   r0   ZimporterZis_NullImporterr-   r2   r   r/   r6   r7   r   r   r   r      s.    
	/r9      z!No module specified for executionfile)r   r   importlib.machineryr:   importlib.utilr   Zpkgutilr   r   __all__r   r   r   r3   r8   rE   rP   r   rJ   rX   r   r   lenr!   printstderrr   r   r   r   <module>   s4   !%
1
