Your IP : 216.73.216.213


Current Path : /proc/242857/root/opt/alt/python34/lib64/python3.4/test/__pycache__/
Upload File :
Current File : //proc/242857/root/opt/alt/python34/lib64/python3.4/test/__pycache__/test_runpy.cpython-34.pyo

�
f f�r�	@s%ddlZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZmZm
Z
mZmZmZmZddlmZmZmZmZmZddlZddlmZmZmZmZdZidd6dd6dd6dd	6dd
6dd6Zied6ed
6ddgd6ejdd6dd6dd6e edddddd�d6Z!e!j"e�Gdd�d�Z#Gdd�dej$e#�Z%Gdd�dej$e#�Z&Gd d!�d!ej$e#�Z'e(d"kr!ej)�ndS)#�N)�forget�make_legacy_pyc�run_unittest�unload�verbose�
no_tracing�create_empty_file)�make_pkg�make_script�make_zip_pkg�make_zip_script�temp_dir)�	_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__�sys�runpyzTop level assignmentzLower level reference�result�	run_argv0F�run_name_in_sys_modules�module_in_sys_modules�x�z<run>�
__loader__�nestedc@s@eZdZddddddgZdd�Zd	d
�ZdS)�CodeExecutionMixin�name�parent�origin�cached�has_location�submodule_search_locationsc	Cs�|j�}|j�}xut|�D]g}|jd�r%|jd�r%||kre|j|�n||dkr�|dj|�q�q%q%W|jd�}|jd�}|dkr�|j|�n�|jdk	r�|jt|j�t|j��nxU|j	D]J}d|}|t
||�f}|t
||�f}|j||�qW|jt|�t|��x>|D]6}|||f}|||f}|j||�qyWdS)zbCheck two namespaces match.

           Ignores any unspecified interpreter created names
        �__r!rNz	__spec__.)�copy�list�
startswith�endswith�popZassertIsNone�loader�assertEqual�type�CHECKED_SPEC_ATTRIBUTES�getattr�set)	�self�	result_ns�expected_ns�kZresult_specZ
expected_spec�attrZactualZexpected�r:�4/opt/alt/python34/lib64/python3.4/test/test_runpy.py�assertNamespaceMatchesLs2

z)CodeExecutionMixin.assertNamespaceMatchesc
Cst�}|j�}|d}tjd}tjj||�}|d�}|j||�|jtjd|�|jtjj||�|�i|d6}	||d<||	�}|j||	�|j||�|jtjd|�|jtjj||�|�dS)z�Check 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
        rrN�sentinel)	�objectr*r�argv�modules�getr<ZassertIsZassertIsNot)
r5Zcreate_namespaceZexpected_namespacer=r7�run_nameZsaved_argv0Z	saved_modr6Z
initial_nsr:r:r;�check_code_executionts 	



z'CodeExecutionMixin.check_code_executionN)r�
__module__�__qualname__r2r<rCr:r:r:r;r"@s	(r"c@s.eZdZdZdd�Zdd�ZdS)�ExecutionLayerTestCasez9Unit tests for runpy._run_code and runpy._run_module_codecCs@tj�}|jidd6�dd�}|j||�dS)Nr cSstti|�S)N)r�example_source)�init_globalsr:r:r;�	create_ns�sz7ExecutionLayerTestCase.test_run_code.<locals>.create_ns)�example_namespacer*�updaterC)r5r7rIr:r:r;�
test_run_code�s
	z$ExecutionLayerTestCase.test_run_codecs�d�d}d}d}tjj�d|d|��tj�}|ji�d6|d6|d	6|d
6�d6|d6d
d6d
d6���fdd�}|j||�dS)Nz
<Nonsense>zSome other nonsensezNow you're just being silly�r%r/rrr rrrTrrcstt|���S)N)rrG)rH)�mod_name�mod_specr:r;rI�sz>ExecutionLayerTestCase.test_run_module_code.<locals>.create_ns)�	importlib�	machinery�
ModuleSpecrJr*rKrC)r5�	mod_fnameZ
mod_loaderZmod_packager7rIr:)rNrOr;�test_run_module_code�s&		z+ExecutionLayerTestCase.test_run_module_codeN)rrDrErrLrTr:r:r:r;rF�s	rFc@s]eZdZdZdd�Zdd�Zdd�Zdd	d
�Zdddd
ddd�Zdd�Z	dd�Z
dddd
ddd�Zdddd
ddd�Zdd�Z
ddd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�Zd1d2�Zd3d4�ZdS)5�RunModuleTestCasezUnit tests for runpy.run_modulecCs8yt|�Wntk
r"YnX|jd|�dS)NzExpected import error for )r�ImportErrorZfail)r5rNr:r:r;�expect_import_error�s

z%RunModuleTestCase.expect_import_errorcCs_|jd�|jd�|jd�|jd�|jd�|jd�|jd�dS)Nrzsys.imp.ericzos.path.halfza.beez.howardz..eatenZmultiprocessing)rW)r5r:r:r;�test_invalid_names�s





z$RunModuleTestCase.test_invalid_namescCs|jtd�dd�dS)Nrr)r0r)r5r:r:r;�test_library_module�sz%RunModuleTestCase.test_library_moduleFcCs:tj|�|rdStjj|d�}t|�|S)Nz__init__.py)�os�mkdir�path�joinr)r5�pkg_dir�	namespace�	pkg_fnamer:r:r;�_add_pkg_dir�s

zRunModuleTestCase._add_pkg_dirZ
runpy_testr_�parent_namespacescCs�|s|r"|r"td��nd}|tjd}tjjtj��}}	tdkrqtd|	�nt	jj
d|�tdkr�tdt	jd�n|r7|g|}
||
d<xp|
D]e}tjj|	|�}	|j|	|�}tdkrtd|	�ntdkr�td	|�q�q�Wntjj|	|�}
t
|
d
�}|j|�|j�tdkr�td	|
�n|d||}tjj||
�}||
||fS)
Nz2Can't mark top level module as a namespace package�
__runpy_pkg__�pyrz  Package tree in:rz  Updated sys.path:z  Next level in:z
  Created:�w�.���)�RuntimeErrorrZ�extsepr\�realpath�tempfileZmkdtempr�printr�insertr]ra�open�write�closerP�util�spec_from_file_location)r5�source�depthZmod_baser_rb�pkg_nameZ
test_fnamer^Zsub_dirZnamespace_flagsZnamespace_flagr`rSZmod_filerNrOr:r:r;�	_make_pkg�s<




	zRunModuleTestCase._make_pkgcCs�x3ttj�D]"}|jd�rtj|=qqWtdkrOtd�ntjd=tdkrrtd�nxtj|dd�D]�\}}}xo|D]g}y tj	tjj
||��Wq�tk
r}	ztdkr�t|	�nWYdd}	~	Xq�Xq�Wxu|D]m}tjj
||�}
ytj|
�Wqtk
r|}	ztdkrjt|	�nWYdd}	~	XqXqWq�Wy*tj|�tdkr�td�nWn>tk
r�}	ztdkr�t|	�nWYdd}	~	XnXdS)	Nrcrz  Removed sys.modules entriesrz  Removed sys.path entry�topdownFz  Removed package tree)
r+rr@r,rrlr\rZ�walk�remover]�OSError�rmdir)r5�toprtrN�entry�root�dirs�filesr#Zex�fullnamer:r:r;�_del_pkg�s:


%
 $
(
zRunModuleTestCase._del_pkgcCsytrdnd}|d|7<|d|d<|d}tjj|j|d�}||d<|ru|d|7<ndS)N�c�orrrr)�	__debug__rPrqrrr#)r5�ns�	alter_sysZchar_to_add�specZnew_specr:r:r;�_fix_ns_for_legacy_pycs


z(RunModuleTestCase._fix_ns_for_legacy_pycc

s�|jt|d|d|�\}}�}t��tj�}|ji�d6|d6|jd6�jd�dd6|d	6��r�|ji|d
6dd6dd
6�n��fdd�}	z�tdkr�t	d��n|j
|	|�tj�t
��tj|�tjsvt|�t��tj�tdkrSt	d��n|j|��|j
|	|�nWd|j||��Xtdkr�t	d�ndS)Nr_rbrrrrfrrrrTrrcst�|d��S)Nr�)r)rH)r�rNr:r;rI0sz2RunModuleTestCase._check_module.<locals>.create_nsrzRunning from source:zRunning from compiled:zModule executed successfully)rvrGrrJr*rKr&�
rpartitionrrlrCrP�invalidate_caches�
__import__rZryr�dont_write_bytecoderrr�r�)
r5rtr�r_rbr^rSrOr7rIr:)r�rNr;�
_check_modulesF
	
	


	


zRunModuleTestCase._check_modulec
s�|jt|dd|d|�\}}}}|jd�d�t|�tj�}	|	ji|d6|d6tjj	|�d6�d	6|d
6��r�|	ji|d6dd
6dd6�n��fdd�}
z�t
dkr�td��n|j|
|	�tj
�t|�tj|�tjs�t|�t|�t
dkr[td��ntj
�|j|	��|j|
|	�nWd|j||��Xt
dkr�td�ndS)N�__main__r_rbrfrrrrrrrTrrcst�|d��S)Nr�)r)rH)r�rur:r;rIYsz3RunModuleTestCase._check_package.<locals>.create_nsrzRunning from source:zRunning from compiled:zPackage executed successfully)rvrGr�rrJr*rKrPrq�cache_from_sourcerrlrCr�r�rZryrr�rrr�r�)r5rtr�r_rbr^rSrNrOr7rIr:)r�rur;�_check_packageCsH
		


	


z RunModuleTestCase._check_packagecCsO|dkrtd��nd}|}x/t|�D]!}|}tjj||�}q4Wtjj|d�}t|�tdkr�td|�ntjj|d�}	|j|	�tdkr�td|	�ntjj|	d�}
|j|
�tdkrtd	|
�ntjj|
d
�}t|�tdkrKtd|�ndS)Nrz$Relative module test needs depth > 1rcz
sibling.pyz  Added sibling module:�unclez  Added uncle package:Zcousinz  Added cousin package:z	nephew.pyz  Added nephew module:)	�
ValueError�rangerZr\r]rrrlra)r5Zbase_dirrsrtruZ
module_dir�iZ
parent_dirZ
sibling_fnameZ	uncle_dirZ
cousin_dirZnephew_fnamer:r:r;�_add_relative_modulesls.



z'RunModuleTestCase._add_relative_modulesNc
Cs�d}|j||�\}}}}|dkr9|}n|}zg|j|||�|jd�d}	tdkr�td|�nt|d|�}
|j|
d|�|j|
d|	�|jd	|
�|jd
|
�~
tj	�t
|�tj|�t
js�t|�t|�tdkr;td|�ntj	�t|d|�}|j|d|�|j|d|	�|jd	|�|jd
|�~nWd|j|||�Xtdkr�td�ndS)
Nza\
from __future__ import absolute_import
from . import sibling
from ..uncle.cousin import nephew
rfrrzRunning from source:rBrrZsiblingZnephewzRunning from compiled:zModule executed successfully)rvr�r�rrlrr0ZassertInrPr�r�rZryrr�rrr�)r5rtrB�contentsr^rSrNrO�
expected_nameruZd1Zd2r:r:r;�_check_relative_imports�sD	


	



z)RunModuleTestCase._check_relative_importscCsDx=td�D]/}tdkr/td|�n|j|�q
WdS)N�rzTesting package depth:)r�rrlr�)r5rtr:r:r;�test_run_module�sz!RunModuleTestCase.test_run_modulecCsSxLtdd�D];}tdkr2td|�n|j|dddd�qWdS)Nrr�zTesting package depth:r_Trb)r�rrlr�)r5rtr:r:r;�$test_run_module_in_namespace_package�sz6RunModuleTestCase.test_run_module_in_namespace_packagecCsGx@tdd�D]/}tdkr2td|�n|j|�qWdS)Nrr�zTesting package depth:)r�rrlr�)r5rtr:r:r;�test_run_package�sz"RunModuleTestCase.test_run_packagecCsMxFtdd�D]5}tdkr2td|�n|j|dd�qWdS)Nrr�zTesting package depth:rbT)r�rrlr�)r5rtr:r:r;�%test_run_package_in_namespace_package�sz7RunModuleTestCase.test_run_package_in_namespace_packagecCsMxFtdd�D]5}tdkr2td|�n|j|dd�qWdS)Nrr�zTesting package depth:r_T)r�rrlr�)r5rtr:r:r;�test_run_namespace_package�sz,RunModuleTestCase.test_run_namespace_packagecCsSxLtdd�D];}tdkr2td|�n|j|dddd�qWdS)Nrr�zTesting package depth:r_Trb)r�rrlr�)r5rtr:r:r;�/test_run_namespace_package_in_namespace_package�szARunModuleTestCase.test_run_namespace_package_in_namespace_packagecCsJxCtd�D]5}tdkr/td|�n|j|dd�q
WdS)Nr�rzTesting package depth:r�T)r�rrlr�)r5rtr:r:r;�test_run_module_alter_sys�sz+RunModuleTestCase.test_run_module_alter_syscCsMxFtdd�D]5}tdkr2td|�n|j|dd�qWdS)Nrr�zTesting package depth:r�T)r�rrlr�)r5rtr:r:r;�test_run_package_alter_sys�sz,RunModuleTestCase.test_run_package_alter_syscCsGx@tdd�D]/}tdkr2td|�n|j|�qWdS)N��rz"Testing relative imports at depth:)r�rrlr�)r5rtr:r:r;�test_explicit_relative_import�sz/RunModuleTestCase.test_explicit_relative_importcCsJxCtdd�D]2}tdkr2td|�n|j|d�qWdS)Nr�r�rz'Testing main relative imports at depth:r�)r�rrlr�)r5rtr:r:r;�test_main_relative_import�sz+RunModuleTestCase.test_main_relative_importc
s�d}d�|jt|�\}}�}t��tj�}|ji�d6|d6tjj|�d6�j	d�dd6|d	6���fd
d�}z|j
||�Wd|j||��XdS)Nrz*And now for something completely differentrrrrfrrrcst�|��S)N)r)rH)rNrBr:r;rI�sz2RunModuleTestCase.test_run_name.<locals>.create_ns)rvrGrrJr*rKrPrqr�r�rCr�)r5rtr^rSrOr7rIr:)rNrBr;�
test_run_name�s
	zRunModuleTestCase.test_run_namecCsddl}d}d}ddg}d|dg}t�}t�}x�td|�D]u}d	j|g|�}	|j|	�x#|D]}
|j|	d	|
�q�Wx#|D]}
|j|	d	|
�q�WqVWd	j|g|�}	|j|	�|j|	d
�|jd|�\}}}
}|j|j|||
�x.td|d�D]}|j|d|�qNWx\|j	|g�D]H\}}
}|j
|tjj
�|r�|j|
�q~|j|
�q~W|jt|�d|�|jt|�d|�dS)
Nrr�rcr�zuncle.cousinzuncle.cousin.nephewz.siblingrrfz.runpy_testrMr�)�pkgutilr4r�r]�addrvZ
addCleanupr�r�Z
walk_packagesZassertIsInstancerPrQ�
FileFinderryr0�len)r5r�Z	max_depthZ	base_nameZpackage_suffixesZmodule_suffixesZexpected_packagesZexpected_modulesrtrur#r^rSrNrO�finderZispkgr:r:r;�test_pkgutil_walk_packages�s:		



"	
z,RunModuleTestCase.test_pkgutil_walk_packages)rrDrErrWrXrYrarvr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r:r:r:r;rU�s6'((rUc@s�eZdZdZdddd�Zddddd�Zd	d
�Zdd�Zd
d�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zedd��Zdd �ZdS)!�RunPathTestCasezUnit tests for runpy.run_pathNFcCs(|dkrt}nt||||�S)N)rGr
)r5�
script_dirZscript_basenamers�omit_suffixr:r:r;�_make_test_scripts		z!RunPathTestCase._make_test_scriptTc
sg�fdd�}tj�}	|dkr3|}
n|}
|rrtjj|
|�}|j}|s|d|_q|n
d}}|	ji|d6|d6|d6dd6|d6|d	6d
d6d
d6�|j||	�d
���fdd�}|r2|dkr2tjj�|�}|s%d|_n||	d<n�|	d<�j	d�d|	d<|j||	�dS)Ncs
t�|�S)N)r)rH)�script_namer:r;rI#sz0RunPathTestCase._check_script.<locals>.create_nsrrrrMrrrTrrzprove.issue15230.is.fixedcst�|��S)N)r)rH)rBr�r:r;rI@srfr)
rJr*rPrqrrr&r/rKrCr�)
r5r�r�Z
expected_fileZexpected_argv0rN�expect_spec�check_loaderrIr7Z	spec_namerOZ
mod_cachedr:)rBr�r;�
_check_scriptsB			
		

zRunPathTestCase._check_scriptcCs)tj|�}|jt|t|�dS)N)�re�escape�assertRaisesRegexrVr)r5r��msgr:r:r;�_check_import_errorLsz#RunPathTestCase._check_import_errorcCsJt��;}d}|j||�}|j|d||dd�WdQXdS)N�scriptz
<run_path>r�F)r
r�r�)r5r�rNr�r:r:r;�test_basic_scriptPs
z!RunPathTestCase.test_basic_scriptcCsPt��A}d}|j||dd�}|j|d||dd�WdQXdS)Nr�r�Tz
<run_path>r�F)r
r�r�)r5r�rNr�r:r:r;�test_basic_script_no_suffixWs	z+RunPathTestCase.test_basic_script_no_suffixcCslt��]}d}|j||�}tj|dd�}tj|�|j|d||dd�WdQXdS)Nr��doraiseTz
<run_path>r�F)r
r��
py_compile�compilerZryr�)r5r�rNr��
compiled_namer:r:r;�test_script_compiled_s
z$RunPathTestCase.test_script_compiledcCsJt��;}d}|j||�}|j|d||d|�WdQXdS)Nr�z
<run_path>rN)r
r�r�)r5r�rNr�r:r:r;�test_directoryhs
zRunPathTestCase.test_directorycCs�t��u}d}|j||�}tj|dd�}tj|�tjszt|�}|j	|d||d|�nWdQXdS)Nr�r�Tz
<run_path>rN)
r
r�r�r�rZryrr�rr�)r5r�rNr�r�Z
legacy_pycr:r:r;�test_directory_compiledos
	z'RunPathTestCase.test_directory_compiledc
CsHt��9}d}|j||�}d|}|j||�WdQXdS)N�not_mainz"can't find '__main__' module in %r)r
r�r�)r5r�rNr�r�r:r:r;�test_directory_errorzs

z$RunPathTestCase.test_directory_errorcCsht��Y}d}|j||�}t|d|�\}}|j|d||d|dd�WdQXdS)Nr��test_zipz
<run_path>rNr�F)r
r�rr�)r5r�rNr��zip_name�fnamer:r:r;�test_zipfile�szRunPathTestCase.test_zipfilecCs}t��n}d}|j||�}tj|dd�}t|d|�\}}|j|d||d|dd�WdQXdS)	Nr�r�Tr�z
<run_path>rNr�F)r
r�r�r�rr�)r5r�rNr�r�r�r�r:r:r;�test_zipfile_compiled�s	z%RunPathTestCase.test_zipfile_compiledcCs`t��Q}d}|j||�}t|d|�\}}d|}|j||�WdQXdS)Nr�r�z"can't find '__main__' module in %r)r
r�rr�)r5r�rNr�r�r�r�r:r:r;�test_zipfile_error�s
z"RunPathTestCase.test_zipfile_errorc	Cs�t��r}t��`}d}d|}|j|||�}t|d|�\}}d}|jt|t|�WdQXWdQXdS)Nr�z import runpy
runpy.run_path(%r)
r�zrecursion depth exceeded)r
r�rr�rhr)	r5r�Z	dummy_dirrNrsr�r�r�r�r:r:r;�test_main_recursion_error�sz)RunPathTestCase.test_main_recursion_errorcCsvt��g}tjj|d�}t|ddd��}|jd�WdQXt|�}|j|dd�WdQXdS)Nz	script.pyre�encoding�latin1u%
#coding:latin1
s = "non-ASCII: hé"
�sunon-ASCII: hé)r
rZr\r]rnrorr0)r5r��filename�frr:r:r;�
test_encoding�s
zRunPathTestCase.test_encoding)rrDrErr�r�r�r�r�r�r�r�r�r�r�r�rr�r�r:r:r:r;r�s +	
r�r�)*ZunittestrZZos.pathrr�rkrP�importlib.machinery�importlib.utilr�Ztest.supportrrrrrrrZtest.script_helperr	r
rrr
rrrrrrGZimplicit_namespacer?�dictrJrKr"ZTestCaserFrUr�r�mainr:r:r:r;�<module>sH$4("

	
O'�`�