U
    pd/                     @   s  d Z ddlZddl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G dd deZG dd	 d	eZd d
dZd!ddZefddZG dd deZd"ddZd#ddZefddZdd Zd$ddZedkreejdk  redejd nejd= eejd  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|| _g | _d S N)mod_nametypes
ModuleTypemodule_saved_moduleselfr    r   /usr/lib/python3.8/runpy.py__init__   s    z_TempModule.__init__c                 C   sB   | j }z| jtj|  W n tk
r0   Y nX | jtj|< | S r   )r   r   appendsysmodulesKeyErrorr   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                   @   s$   e Zd Zdd Zdd Zdd ZdS )_ModifiedArgv0c                 C   s   || _ t  | _| _d S r   )valueobject_saved_value	_sentinel)r   r    r   r   r   r   1   s    z_ModifiedArgv0.__init__c                 C   s0   | j | jk	rtdtjd | _ | jtjd< d S )NzAlready preserving saved valuer   )r"   r#   RuntimeErrorr   argvr    )r   r   r   r   r   5   s    z_ModifiedArgv0.__enter__c                 G   s   | j | _| jtjd< d S r   )r#   r    r"   r   r%   r   r   r   r   r   ;   s    z_ModifiedArgv0.__exit__N)r   r   r   r   r   r   r   r   r   r   r   0   s   r   c           
   	   C   sn   |dk	r| | |dkr(d}|}d}	n |j}|j}|j}	|dkrH|j}|j |||	d|||d t| | |S )z)Helper to run code in nominated namespaceN)r   __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*    

r7   c           	   
   C   s^   |dkr|n|j }t|6}t|" |jj}t| |||||| W 5 Q R X W 5 Q R X | S )z5Helper to run code in new namespace with sys modifiedN)r-   r   r   r   __dict__r7   copy)	r1   r2   r   r3   r4   r5   r6   temp_modulemod_globalsr   r   r   _run_module_codeZ   s       r<   c                 C   s2  |  dr|d| d\}}}|rzt| W nH tk
rz } z*|jd ksh|j|krj| |jd sj W 5 d }~X Y nX tj| }|d k	rt|dsddl	m
} dj| |d}|t| ztj| }W nJ ttttfk
r }	 z"d}||| t|	j|	|	W 5 d }	~	X Y nX |d kr2|d	|  |jd k	r| d
ksT| dr\|dz| d }
t|
|W S  |k
r } z"| tjkr |d|| f W 5 d }~X Y nX |j}|d kr|d|  z|| }W n2 tk
r } z|t||W 5 d }~X Y nX |d kr(|d|  | ||fS )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   r4   z:Error while finding module specification for {!r} ({}: {})zNo 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 executedzNo code object available for %s)
startswith
rpartition
__import__ImportErrornamer   r   gethasattrwarningsr?   formatRuntimeWarning	importlibutil	find_specAttributeError	TypeError
ValueErrortyper   submodule_search_locationsendswith_get_module_detailsr,   get_code)r   errorr4   _eZexistingr?   msgspecZexZpkg_main_namer,   r1   r   r   r   rT   h   sd    
 ,

 
rT   c                   @   s   e Zd ZdZdS )_ErrorzBError that _run_module_as_main() should report without a tracebackN)r   r   r   r   r   r   r   r   r[      s   r[   Tc              
   C   s   z0|s| dkr t | t\} }}ntt\} }}W n: tk
rj } zdtj|f }t| W 5 d}~X Y nX tjd j}|r|jtj	d< 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__
    r@   z%s: %sNr   )rT   r[   _get_main_module_detailsr   
executableexitr   r8   r-   r%   r7   )r   Z
alter_argvr3   r1   excrY   Zmain_globalsr   r   r   _run_module_as_main   s     r`   Fc                 C   s@   t | \} }}|dkr| }|r,t||||S t|i |||S dS )znExecute a module's code without importing it

       Returns the resulting top level namespace dictionary
    N)rT   r<   r7   )r   r2   run_nameZ	alter_sysr3   r1   r   r   r   r      s    c              
   C   s   d}t j| }t j|= z\zt|W W NS  tk
rn } z*|t|kr\| d|t jd f | W 5 d }~X Y nX W 5 |t j|< X d S )Nr@   zcan't find %r module in %rr   )r   r   rT   rD   strpath)rV   Z	main_nameZ
saved_mainr_   r   r   r   r\      s    
r\   c              	   C   sf   t jt |}t|}t|}W 5 Q R X |d kr^t|}t| |d}W 5 Q R X ||fS )Nr0   )	osrc   abspathfsdecodeio	open_coder   compileread)ra   r6   Zdecoded_pathfr1   r   r   r   _get_code_from_file   s    rl   c                 C   s$  |dkrd}| dd }t| }d}t|jdkrFt|jdkrFd}t|tdsX|rxt|| \}}t|||||d	S tj	
d|  znt \}}	}t|P}
t| < |
jj}t|||||	| W  5 Q R  W  5 Q R  W S Q R X W 5 Q R X W 5 ztj	|  W n tk
r   Y nX X d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=   r   FZimpZNullImporterT)r4   r5   )rB   r   rQ   r   r   
isinstancerl   r<   r   rc   insertremoverP   r\   r   r   r   r8   r7   r9   )Z	path_namer2   ra   r4   ZimporterZis_NullImporterr1   r6   r   r3   r:   r;   r   r   r   r      s<    
 
  8r@      z!No module specified for execution)file)NNNNN)NNNNN)T)NNF)NN)r   r   importlib.machineryrK   importlib.utilrg   r	   rd   Zpkgutilr   r   __all__r!   r   r   r7   r<   rD   rT   	Exceptionr[   r`   r   r\   rl   r   r   lenr%   printstderrr   r   r   r   <module>   sN           
       
:
    

1
