B
    18\0|                 @   s^  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Zd dlZd dlZd dl	Z	d dl
Z
d dlmZmZmZmZmZmZmZ d dlmZmZmZmZ d dlZd dlmZmZmZmZ dZdddddddZeeddgejd  d	d	e ed
ddddZ!e!"e G dd dZ#G dd de j$e#Z%G dd de j$e#Z&G dd de j$e#Z'e(dkrZe )  dS )    N)forgetmake_legacy_pycunloadverbose
no_tracingcreate_empty_filetemp_dir)make_pkgmake_scriptmake_zip_pkgmake_zip_script)	_run_code_run_module_code
run_modulerun_patha  # Check basic code execution
result = ['Top level assignment']
def f():
    result.append('Lower level reference')
f()
del f
# Check the sys module
import sys
run_argv0 = sys.argv[0]
run_name_in_sys_modules = __name__ in sys.modules
module_in_sys_modules = (run_name_in_sys_modules and
                         globals() is sys.modules[__name__].__dict__)
# Check nested operation
import runpy
nested = runpy._run_module_code('x=1\n', mod_name='<run>')
)__name____file__
__cached____package____doc____spec__zTop level assignmentzLower level referenceF   z<run>)xr   
__loader__)sysrunpyresult	run_argv0run_name_in_sys_modulesmodule_in_sys_modulesnestedc               @   s,   e Zd ZddddddgZdd Zd	d
 ZdS )CodeExecutionMixinnameparentorigincachedhas_locationsubmodule_search_locationsc       	      C   s8  |  }|  }xPt|D ]D}|dr|dr||krD|| ||d kr|d | qW |d}|d}|dkr| | nb|jdk	r| t|jt|j x>| j	D ]4}d| }|t
||f}|t
||f}| || qW | t|t| x2|D ]*}||| f}||| f}| || qW dS )zbCheck two namespaces match.

           Ignores any unspecified interpreter created names
        __r    r   Nz	__spec__.)copylist
startswithendswithpopassertIsNoneloaderassertEqualtypeCHECKED_SPEC_ATTRIBUTESgetattrset)	self	result_nsexpected_nskZresult_specZexpected_specattractualexpected r<   S/oak/stanford/groups/akundaje/marinovg/programs/Python-3.7.3/Lib/test/test_runpy.pyassertNamespaceMatchesM   s2    




z)CodeExecutionMixin.assertNamespaceMatchesc       
      C   s   t  }| }|d }tjd }tj||}|d}| || | tjd | | tj||| d|i}	||d< ||	}| ||	 | || | tjd | | tj||| dS )zCheck that an interface runs the example code correctly

           First argument is a callable accepting the initial globals and
           using them to create the actual namespace
           Second argument is the expected result
        r   r   Nsentinel)	objectr)   r   argvmodulesgetr>   assertIsassertIsNot)
r5   Zcreate_namespaceZexpected_namespacer?   r7   run_nameZsaved_argv0Z	saved_modr6   Z
initial_nsr<   r<   r=   check_code_executionu   s     
z'CodeExecutionMixin.check_code_executionN)r   
__module____qualname__r2   r>   rG   r<   r<   r<   r=   r!   A   s   	(r!   c               @   s    e Zd ZdZdd Zdd ZdS )ExecutionLayerTestCasez9Unit tests for runpy._run_code and runpy._run_module_codec             C   s.   t  }|dd i dd }| || d S )Nr   c             S   s   t ti | S )N)r   example_source)init_globalsr<   r<   r=   	create_ns   s    z7ExecutionLayerTestCase.test_run_code.<locals>.create_ns)example_namespacer)   updaterG   )r5   r7   rM   r<   r<   r=   test_run_code   s
    
z$ExecutionLayerTestCase.test_run_codec                sd   d d}d}d}t jj ||dt }| ||||ddd  fdd	}| || d S )
Nz
<Nonsense>zSome other nonsensezNow you're just being silly )r$   r/   T)r   r   r   r   r   r   r   r   c                s   t t|  S )N)r   rK   )rL   )mod_namemod_specr<   r=   rM      s    z>ExecutionLayerTestCase.test_run_module_code.<locals>.create_ns)	importlib	machinery
ModuleSpecrN   r)   rO   rG   )r5   	mod_fnameZ
mod_loaderZmod_packager7   rM   r<   )rR   rS   r=   test_run_module_code   s&    
z+ExecutionLayerTestCase.test_run_module_codeN)r   rH   rI   r   rP   rX   r<   r<   r<   r=   rJ      s   	rJ   c               @   s  e Zd ZdZdd Zdd Zdd Zd:d	d
Zd;dddddZdd Z	dd Z
d<dddddZd=dddddZdd Zd>ddZdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 ZdS )?RunModuleTestCasezUnit tests for runpy.run_modulec             C   s4   yt | W n tk
r    Y nX | d|  d S )NzExpected import error for )r   ImportErrorfail)r5   rR   r<   r<   r=   expect_import_error   s
    z%RunModuleTestCase.expect_import_errorc             C   s^   |  d |  d |  d |  d |  d |  d |  d |  d |  d	 d S )
Nr   zsys.imp.ericzos.path.halfza.beez.howardz..eatenz.test_runpyz	.unittestmultiprocessing)r\   )r5   r<   r<   r=   test_invalid_names   s    







z$RunModuleTestCase.test_invalid_namesc             C   s   |  tdd d d S )Nr   r   )r0   r   )r5   r<   r<   r=   test_library_module   s    z%RunModuleTestCase.test_library_moduleFc             C   s,   t | |rd S t j|d}t| |S )Nz__init__.py)osmkdirpathjoinr   )r5   pkg_dir	namespace	pkg_fnamer<   r<   r=   _add_pkg_dir   s    
zRunModuleTestCase._add_pkg_dir
runpy_test)re   parent_namespacesc            C   s<  |s|r|st dd}|tj d }tjt  }}	tdkrLtd|	 t	j
d| tdkrrtdt	jd  |r|g| }
||
d< xJ|
D ]B}tj|	|}	| |	|}tdkrtd	|	 tdkrtd
| qW tj|	|}t|d}|| |  tdkrtd
| |d | | }tj||}||||fS )Nz2Can't mark top level module as a namespace package__runpy_pkg__pyr   z  Package tree in:r   z  Updated sys.path:z  Next level in:z
  Created:w.)RuntimeErrorr`   extseprb   realpathtempfilemkdtempr   printr   insertrc   rg   openwritecloserT   utilspec_from_file_location)r5   sourcedepthZmod_basere   ri   pkg_nameZ
test_fnamerd   Zsub_dirZnamespace_flagsZnamespace_flagrf   rW   mod_filerR   rS   r<   r<   r=   	_make_pkg   s<     
 

 
 


 
zRunModuleTestCase._make_pkgc       	      C   s  x$t tjD ]}|drtj|= qW tdkr6td tjd= tdkrNtd xtj|ddD ]\}}}xX|D ]P}yt	tj
|| W qn tk
r } ztdkrt| W d d }~X Y qnX qnW x`|D ]X}tj
||}yt| W q tk
r } ztdkrt| W d d }~X Y qX qW q^W y t| tdkrFtd W n6 tk
r~ } ztdkrnt| W d d }~X Y nX d S )	Nrj   r   z  Removed sys.modules entriesr   z  Removed sys.path entryF)topdownz  Removed package tree)r*   r   rB   r+   r   rt   rb   r`   walkremoverc   OSErrorrmdir)	r5   topentryrootdirsfilesr"   exfullnamer<   r<   r=   _del_pkg   s:    
  
 

 "

 
 zRunModuleTestCase._del_pkgc             C   s\   d}|d  |7  < |d |d< |d }t j|j|d }||d< |rX|d  |7  < d S )Ncr   r   r   r   )rT   ry   rz   r"   )r5   ns	alter_sysZchar_to_addspecZnew_specr<   r<   r=   _fix_ns_for_legacy_pyc  s    

z(RunModuleTestCase._fix_ns_for_legacy_pycc      
   	      s  | j t|||d\}}}t t }|||jdd |d  rb||ddd  fdd}	ztd	krt	d
 | 
|	| t  t t| tjst| t t  td	krt	d | |  | 
|	| W d | | X td	krt	d d S )N)re   ri   rn   r   )r   r   r   r   r   T)r   r   r   c                s   t |  dS )N)r   )r   )rL   )r   rR   r<   r=   rM   4  s    z2RunModuleTestCase._check_module.<locals>.create_nsr   zRunning from source:zRunning from compiled:zModule executed successfully)r   rK   r   rN   r)   rO   r%   
rpartitionr   rt   rG   rT   invalidate_caches
__import__r`   r   r   dont_write_bytecoder   r   r   r   )
r5   r|   r   re   ri   rd   rW   rS   r7   rM   r<   )r   rR   r=   _check_module  sF    

 

 

 zRunModuleTestCase._check_modulec         	      s$  | j t|d||d\}}}}|dd t| t }	|	||tj	||d  rn|	|ddd  fdd	}
zt
d
krtd | |
|	 t  t| t| tjst| t| t
d
krtd t  | |	  | |
|	 W d | | X t
d
kr td d S )N__main__)re   ri   rn   r   )r   r   r   r   r   T)r   r   r   c                s   t |  dS )N)r   )r   )rL   )r   r}   r<   r=   rM   ]  s    z3RunModuleTestCase._check_package.<locals>.create_nsr   zRunning from source:zRunning from compiled:zPackage executed successfully)r   rK   r   r   rN   r)   rO   rT   ry   cache_from_sourcer   rt   rG   r   r   r`   r   r   r   r   r   r   r   )r5   r|   r   re   ri   rd   rW   rR   rS   r7   rM   r<   )r   r}   r=   _check_packageG  sH    



 

 

 z RunModuleTestCase._check_packagec             C   s   |dkrt dd}|}x"t|D ]}|}tj||}q"W tj|d}t| tdkrdtd| tj|d}	| |	 tdkrtd|	 tj|	d}
| |
 tdkrtd	|
 tj|
d
}t| tdkrtd| d S )Nr   z$Relative module test needs depth > 1rj   z
sibling.pyz  Added sibling module:unclez  Added uncle package:Zcousinz  Added cousin package:z	nephew.pyz  Added nephew module:)	
ValueErrorranger`   rb   rc   r   r   rt   rg   )r5   base_dirr{   r|   r}   
module_diriZ
parent_dirZsibling_fnameZ	uncle_dirZ
cousin_dirZnephew_fnamer<   r<   r=   _add_relative_modulesp  s.     

 

 
 z'RunModuleTestCase._add_relative_modulesNc          	   C   sZ  d}|  ||\}}}}|d kr&|}n|}z
| ||| |dd }	tdkr\td| t||d}
| |
d | | |
d |	 | d	|
 | d
|
 ~
t	  t
| t| tjs4t| t| tdkrtd| t	  t||d}| |d | | |d |	 | d	| | d
| ~W d | | X tdkrVtd d S )Nza\
from __future__ import absolute_import
from . import sibling
from ..uncle.cousin import nephew
rn   r   r   zRunning from source:)rF   r   r   ZsiblingZnephewzRunning from compiled:zModule executed successfully)r   r   r   r   rt   r   r0   assertInrT   r   r   r`   r   r   r   r   r   r   )r5   r|   rF   contentsrd   rW   rR   rS   expected_namer}   d1d2r<   r<   r=   _check_relative_imports  sD     

 

 z)RunModuleTestCase._check_relative_importsc             C   s2   x,t dD ] }tdkr td| | | q
W d S )N   r   zTesting package depth:)r   r   rt   r   )r5   r|   r<   r<   r=   test_run_module  s     
z!RunModuleTestCase.test_run_modulec             C   s:   x4t ddD ]&}tdkr"td| | j|ddd qW d S )Nr   r   zTesting package depth:T)re   ri   )r   r   rt   r   )r5   r|   r<   r<   r=   $test_run_module_in_namespace_package  s     
z6RunModuleTestCase.test_run_module_in_namespace_packagec             C   s4   x.t ddD ] }tdkr"td| | | qW d S )Nr   r   zTesting package depth:)r   r   rt   r   )r5   r|   r<   r<   r=   test_run_package  s     
z"RunModuleTestCase.test_run_packagec             C   sR  |  ddd}|\}}}}|dd}| | j| tj|dd}ttt	t
f}x|D ]}|j}| | d|}	t|dd	d
}
|
|	 W d Q R X yt| W n2 |k
r } z| dt| W d d }~X Y nX | d| yt|d  W n4 |k
r. } z| dt| W d d }~X Y nX | d| W d Q R X qVW d S )NrQ   r   r   z	.__main__rj   z__init__.pyz raise {0}('{0} in __init__.py.')Zwtascii)encodingzfinding speczNothing raised; expected {}z
.submodule)r   replace
addCleanupr   r`   rb   rc   rZ   AttributeError	TypeErrorr   r   subTestformatrv   rw   r   assertNotInr[   )r5   r   rd   _rR   init
exceptions	exceptionr"   r{   r~   errr<   r<   r=    test_run_package_init_exceptions  s,    

""z2RunModuleTestCase.test_run_package_init_exceptionsc          	   C   sR   |  dd\}}}}z,t| | td t| W d Q R X W d | | X d S )NrQ   r   zfound in sys\.modules)r   r   assertWarnsRegexRuntimeWarningr   r   )r5   rd   r   rR   r<   r<   r=   test_submodule_imported_warning  s    z1RunModuleTestCase.test_submodule_imported_warningc          	   C   s   |  ddd\}}}}| | j| |dd}t| | |tj t	  t
dt t| W d Q R X t| | td t| W d Q R X d S )NrQ   r   r   z	.__main__errorzfound in sys\.modules)r   r   r   r   r   r   r   rB   warningscatch_warningssimplefilterr   r   r   )r5   rd   r   rR   packager<   r<   r=    test_package_imported_no_warning  s    
z2RunModuleTestCase.test_package_imported_no_warningc             C   s8   x2t ddD ]$}tdkr"td| | j|dd qW d S )Nr   r   zTesting package depth:T)ri   )r   r   rt   r   )r5   r|   r<   r<   r=   %test_run_package_in_namespace_package  s     
z7RunModuleTestCase.test_run_package_in_namespace_packagec             C   s8   x2t ddD ]$}tdkr"td| | j|dd qW d S )Nr   r   zTesting package depth:T)re   )r   r   rt   r   )r5   r|   r<   r<   r=   test_run_namespace_package  s     
z,RunModuleTestCase.test_run_namespace_packagec             C   s:   x4t ddD ]&}tdkr"td| | j|ddd qW d S )Nr   r   zTesting package depth:T)re   ri   )r   r   rt   r   )r5   r|   r<   r<   r=   /test_run_namespace_package_in_namespace_package  s     
zARunModuleTestCase.test_run_namespace_package_in_namespace_packagec             C   s6   x0t dD ]$}tdkr td| | j|dd q
W d S )Nr   r   zTesting package depth:T)r   )r   r   rt   r   )r5   r|   r<   r<   r=   test_run_module_alter_sys  s     
z+RunModuleTestCase.test_run_module_alter_sysc             C   s8   x2t ddD ]$}tdkr"td| | j|dd qW d S )Nr   r   zTesting package depth:T)r   )r   r   rt   r   )r5   r|   r<   r<   r=   test_run_package_alter_sys  s     
z,RunModuleTestCase.test_run_package_alter_sysc             C   s4   x.t ddD ] }tdkr"td| | | qW d S )N      r   z"Testing relative imports at depth:)r   r   rt   r   )r5   r|   r<   r<   r=   test_explicit_relative_import  s     
z/RunModuleTestCase.test_explicit_relative_importc             C   s6   x0t ddD ]"}tdkr"td| | |d qW d S )Nr   r   r   z'Testing main relative imports at depth:r   )r   r   rt   r   )r5   r|   r<   r<   r=   test_main_relative_import  s     
z+RunModuleTestCase.test_main_relative_importc          	      s   d}d|  t|\}} }t  t }||tj| 	dd |d  fdd}z| 
|| W d | | X d S )Nr   z*And now for something completely differentrn   r   )r   r   r   r   r   c                s   t  | S )N)r   )rL   )rR   rF   r<   r=   rM   $  s    z2RunModuleTestCase.test_run_name.<locals>.create_ns)r   rK   r   rN   r)   rO   rT   ry   r   r   rG   r   )r5   r|   rd   rW   rS   r7   rM   r<   )rR   rF   r=   test_run_name  s    

zRunModuleTestCase.test_run_namec             C   s  dd l }d}d}ddg}d|d g}t }t }xltd|D ]^}d	|g| }	||	 x|D ]}
||	d	 |
  q`W x|D ]}
||	d	 |
  qW q<W d	|g| }	||	 ||	d
  | d|\}}}}| | j| x$td|d D ]}| |d| qW xX|	|gD ]H}| 
||j | 
|jtjj |jrV||j n||j qW | t|d| | t|d| d S )Nr   r   rj   r   zuncle.cousinzuncle.cousin.nephewz.siblingr   rn   z.runpy_testrQ   r   )pkgutilr4   r   rc   addr   r   r   r   walk_packagesassertIsInstance
ModuleInfomodule_finderrT   rU   
FileFinderispkgr   r"   r0   len)r5   r   Z	max_depth	base_nameZpackage_suffixesZmodule_suffixesZexpected_packagesZexpected_modulesr|   r}   r"   rd   rW   rR   rS   Z
moduleinfor<   r<   r=   test_pkgutil_walk_packages+  s<    




z,RunModuleTestCase.test_pkgutil_walk_packages)F)rh   )F)F)N)r   rH   rI   r   r\   r^   r_   rg   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r<   r<   r<   r=   rY      s<   
'(
(
rY   c               @   s   e Zd ZdZd!ddZd"ddZd	d
 Zdd Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zedd Zdd  ZdS )#RunPathTestCasezUnit tests for runpy.run_pathNFc             C   s   |d krt }t||||S )N)rK   r
   )r5   
script_dirZscript_basenamer{   omit_suffixr<   r<   r=   _make_test_scriptR  s    z!RunPathTestCase._make_test_scriptTc                s   fdd}t  }	|d kr"|}
n|}
|rJtj|
|}|j}|sRd |_nd  }}|	|||d||ddd | ||	 d  fdd}|r|d krtj |}|sd |_||	d<  |	d	<  	d
d |	d< | ||	 d S )Nc                s
   t  | S )N)r   )rL   )script_namer<   r=   rM   ]  s    z0RunPathTestCase._check_script.<locals>.create_nsrQ   T)r   r   r   r   r   r   r   r   zprove.issue15230.is.fixedc                s   t |  S )N)r   )rL   )rF   r   r<   r=   rM   z  s    r   r   rn   r   r   )
rN   r)   rT   ry   rz   r%   r/   rO   rG   r   )r5   r   r   Zexpected_fileZexpected_argv0rR   expect_speccheck_loaderrM   r7   Z	spec_namerS   Z
mod_cachedr<   )rF   r   r=   _check_scriptY  sB    
zRunPathTestCase._check_scriptc             C   s   t |}| t|t| d S )N)reescapeassertRaisesRegexrZ   r   )r5   r   msgr<   r<   r=   _check_import_error  s    
z#RunPathTestCase._check_import_errorc          	   C   s:   t  *}d}| ||}| j|d||dd W d Q R X d S )Nscriptz
<run_path>F)r   )r   r   r   )r5   r   rR   r   r<   r<   r=   test_basic_script  s
    
z!RunPathTestCase.test_basic_scriptc          	   C   s>   t  .}d}| j||dd}| j|d||dd W d Q R X d S )Nr   T)r   z
<run_path>F)r   )r   r   r   )r5   r   rR   r   r<   r<   r=   test_basic_script_no_suffix  s    
z+RunPathTestCase.test_basic_script_no_suffixc          	   C   sR   t  B}d}| ||}tj|dd}t| | j|d||dd W d Q R X d S )Nr   T)doraisez
<run_path>F)r   )r   r   
py_compilecompiler`   r   r   )r5   r   rR   r   compiled_namer<   r<   r=   test_script_compiled  s    

z$RunPathTestCase.test_script_compiledc          	   C   s:   t  *}d}| ||}| j|d|||d W d Q R X d S )Nr   z
<run_path>)rR   )r   r   r   )r5   r   rR   r   r<   r<   r=   test_directory  s
    
zRunPathTestCase.test_directoryc          	   C   s`   t  P}d}| ||}tj|dd}t| tjsRt|}| j	|d|||d W d Q R X d S )Nr   T)r   z
<run_path>)rR   )
r   r   r   r   r`   r   r   r   r   r   )r5   r   rR   r   r   
legacy_pycr<   r<   r=   test_directory_compiled  s    

z'RunPathTestCase.test_directory_compiledc          	   C   s:   t  *}d}| ||}d| }| || W d Q R X d S )Nnot_mainz"can't find '__main__' module in %r)r   r   r   )r5   r   rR   r   r   r<   r<   r=   test_directory_error  s
    z$RunPathTestCase.test_directory_errorc          	   C   sL   t  <}d}| ||}t|d|\}}| j|d|||dd W d Q R X d S )Nr   test_zipz
<run_path>F)rR   r   )r   r   r   r   )r5   r   rR   r   zip_namefnamer<   r<   r=   test_zipfile  s    zRunPathTestCase.test_zipfilec          	   C   sZ   t  J}d}| ||}tj|dd}t|d|\}}| j|d|||dd W d Q R X d S )Nr   T)r   r   z
<run_path>F)rR   r   )r   r   r   r   r   r   )r5   r   rR   r   r   r   r   r<   r<   r=   test_zipfile_compiled  s    
z%RunPathTestCase.test_zipfile_compiledc          	   C   sJ   t  :}d}| ||}t|d|\}}d| }| || W d Q R X d S )Nr   r   z"can't find '__main__' module in %r)r   r   r   r   )r5   r   rR   r   r   r   r   r<   r<   r=   test_zipfile_error  s    z"RunPathTestCase.test_zipfile_errorc       	   
   C   sf   t  V}t  D}d}d| }| |||}t|d|\}}d}| t|t| W d Q R X W d Q R X d S )Nr   z import runpy
runpy.run_path(%r)
r   zrecursion depth exceeded)r   r   r   r   RecursionErrorr   )	r5   r   Z	dummy_dirrR   r{   r   r   r   r   r<   r<   r=   test_main_recursion_error  s    z)RunPathTestCase.test_main_recursion_errorc          
   C   s`   t  P}tj|d}t|ddd}|d W d Q R X t|}| |d d W d Q R X d S )Nz	script.pyrm   latin1)r   u%   
#coding:latin1
s = "non-ASCII: hé"
su   non-ASCII: hé)r   r`   rb   rc   rv   rw   r   r0   )r5   r   filenamefr   r<   r<   r=   test_encoding  s    zRunPathTestCase.test_encoding)NF)NTT)r   rH   rI   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r<   r<   r<   r=   r   O  s    
 
+	
r   r   )*unittestr`   os.pathr   r   rr   rT   importlib.machineryimportlib.utilr   r   Ztest.supportr   r   r   r   r   r   r   Ztest.support.script_helperr	   r
   r   r   r   r   r   r   r   rK   Zimplicit_namespacerA   dictrN   rO   r!   TestCaserJ   rY   r   r   mainr<   r<   r<   r=   <module>   sL   $
O'    
