Your IP : 216.73.216.213


Current Path : /opt/alt/python35/lib64/python3.5/test/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib64/python3.5/test/__pycache__/test_tempfile.cpython-35.opt-2.pyc



���]���@sddlZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZddl
Z
ddlm
Z
ddlmZeed�r�ddlZdZndZejejkZeed�Zejjd�rd	Znd
ZGdd�de
j�ZGd
d�de
j�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�Z Gdd�de�Z!ej"dd��Z#dd�Z$Gdd�d�Z%Gdd �d e%e�Z&Gd!d"�d"e�Z'Gd#d$�d$e�Z(Gd%d&�d&e�Z)Gd'd(�d(e%e�Z*Gd)d*�d*e�Z+Gd+d,�d,e�Z,Gd-d.�d.e�Z-ej.ej/k	r�Gd/d0�d0e�Z0Gd1d2�d2�Z1Gd3d4�d4e�Z2e3d5kre
j4�dS)6�N)�mock)�support)�
script_helper�stat��spawnlZopenbsd�0�dc@s4eZdZdd�Zdd�Zdd�ZdS)�TestLowLevelInternalscCsO|jttjd��|jttjd��|jttjd��dS)N��)�assertIs�str�tempfile�_infer_return_type�bytes)�self�r�2/opt/alt/python35/lib64/python3.5/test_tempfile.py�test_infer_return_type_singles(sz4TestLowLevelInternals.test_infer_return_type_singlescCs�|jttjdd��|jttjdd��|jt��tjdd�WdQRX|jt��tjdd�WdQRXdS)Nrr)r
rrrr�assertRaises�	TypeError)rrrr� test_infer_return_type_multiples-sz6TestLowLevelInternals.test_infer_return_type_multiplescCs�|jttjdd��|jttjdd��|jttjdd��|jttjdd��|jttjdd��|jt��tjddd�WdQRX|jt��tjddd�WdQRXdS)Nrr)r
rrrrrr)rrrr�)test_infer_return_type_multiples_and_none5sz?TestLowLevelInternals.test_infer_return_type_multiples_and_noneN)�__name__�
__module__�__qualname__rrrrrrrr
'sr
c@sReZdZejd�Zejd�Zdd�Zdd�Zdd�Z	d	S)
�BaseTestCasez^[a-z0-9_-]{8}$s^[a-z0-9_-]{8}$cCs?tj�|_|jj�tjddtdddt�dS)N�ignore�category�message�mktemp�module)r�check_warnings�_warnings_manager�	__enter__�warnings�filterwarnings�RuntimeWarningr)rrrr�setUpHs
zBaseTestCase.setUpcCs|jjddd�dS)N)r$�__exit__)rrrr�tearDownNszBaseTestCase.tearDownc
Cstjj|�\}}|dt|��}|t|�t|�d�}|dk	r�|jt|�t|�tkr�tntd�|dk	r�|jt|�t|�tkr�tntd�|dk	r|jt|�t|�tkr�tntd�|||fdkr3|jt|�td�|jtjj	|�tjj	|�d||f�|j||d||f�|j||d||f�|t|�t|�t|��}t
|t�r�|jn|j}	|j
|	j|�d||	jf�dS)Nzunexpected return typezdefault return type must be strzfile %r not in directory %rzfile %r does not begin with %rzfile %r does not end with %rz$random characters %r do not match %r)NNN)�os�path�split�lenr
�typerr�assertEqual�abspath�
isinstance�	str_check�b_check�
assertTrue�match�pattern)
r�name�dir�pre�suf�ndir�nbaseZnpreZnsuf�checkrrr�	nameCheckRs2 ***$&!zBaseTestCase.nameCheckN)
rrr�re�compiler4r5r)r+r@rrrrrCs
rc@seZdZdd�ZdS)�TestExportscCs�tj}ddddddddddddddd	dd
dddddd
dddddi}g}x7|D]/}|ddkrp||krp|j|�qpW|jt|�dkd|�dS)N�NamedTemporaryFiler�
TemporaryFile�mkstemp�mkdtempr!�TMP_MAX�
gettempprefix�gettempprefixb�
gettempdir�gettempdirb�tempdir�template�SpooledTemporaryFile�TemporaryDirectoryr�_zunexpected keys: %s)r�__dict__�appendr6r/)r�dict�expectedZunexp�keyrrr�test_exportsss*	
zTestExports.test_exportsN)rrrrWrrrrrCrsrCcsmeZdZ�fdd�Zdd�Zdd�Zdd�Zeje	e
d	�d
�dd��Z�S)
�TestRandomNameSequencecs tj�|_t�j�dS)N)r�_RandomNameSequence�r�superr))r)�	__class__rrr)�szTestRandomNameSequence.setUpcCs)t|j�}|j|ddd�dS)Nr)�nextrZr@)r�srrr�test_get_six_char_str�sz,TestRandomNameSequence.test_get_six_char_strcCsfi}|j}xPtt�D]B}t|�}|j|ddd�|j||�d||<qWdS)Nrr)rZ�range�
TEST_FILESr]r@�assertNotIn)rrTrZ�ir^rrr�	test_many�s	z TestRandomNameSequence.test_manycCs;d}|j}x%|D]}|d7}|dkrPqWdS)Nrr�)rZ)rrcrZr^rrr�
supports_iter�s	

z$TestRandomNameSequence.supports_iter�forkz!os.fork is required for this testcCstj�\}}d}z�tj�}|svtj|�tj|t|j�jd��tj|�tjd�t|j�}tj	|t
|��jd�}Wd|r�ytj|t
j�Wntk
r�YnXtj|�tj|�X|j||�dS)N�asciir)r,�piperg�close�writer]rZ�encode�_exit�readr/�decode�kill�signal�SIGKILL�OSError�assertNotEqual)rZread_fdZwrite_fd�pidZparent_valueZchild_valuerrr�test_process_awareness�s&
"

%

z-TestRandomNameSequence.test_process_awareness)rrrr)r_rdrf�unittest�
skipUnless�hasattrr,rvrr)r\rrX�s
rXc@s(eZdZdd�Zdd�ZdS)�TestCandidateTempdirListcCsJtj�}|jt|�dk�x|D]}|j|t�q,WdS)Nr)r�_candidate_tempdir_list�assertFalser/�assertIsInstancer)r�cand�crrr�test_nonempty_list�s
z+TestCandidateTempdirList.test_nonempty_listcCs�tj���}x9dD]1}tj|�}|stjj|�||<qWtj�}x9dD]1}tj|�}|st�|j	||�q^Wytj
�}Wn!ttfk
r�tj
}YnX|j	||�WdQRXdS)N�TMPDIR�TEMP�TMP)r�r�r�)r�r�r�)r�EnvironmentVarGuardr,�getenvr-r2rr{�
ValueError�assertIn�getcwd�AttributeErrorrs�curdir)r�env�envname�dirnamer~rrr�test_wanted_dirs�s 

z)TestCandidateTempdirList.test_wanted_dirsN)rrrr�r�rrrrrz�s	rzc@seZdZdd�ZdS)�TestGetDefaultTempdirc-s=tj��*��fdd�}tjtd|���tj�|jtj��g�dd��tjtd���?|j	t
��tj�WdQRX|jtj��g�WdQRX��fdd�}tjtd|��A�|j	t
��tj�WdQRX|jtj��g�WdQRXWdQRXWdQRXdS)	Ncs�gS)Nrr)�our_temp_directoryrr�our_candidate_listszKTestGetDefaultTempdir.test_no_files_left_behind.<locals>.our_candidate_listr{c_s
t��dS)N)rs)�args�kwargsrrr�
raise_OSErrorszFTestGetDefaultTempdir.test_no_files_left_behind.<locals>.raise_OSError�opencs�||�}�|_|S)N)rk)r�r��fp)�	orig_openr�rr�
bad_writers	zCTestGetDefaultTempdir.test_no_files_left_behind.<locals>.bad_writer)rrPr�	swap_attr�_get_default_tempdirr1r,�listdir�ior�FileNotFoundError)rr�r�r)r�r�r�r�test_no_files_left_behind�s 

 z/TestGetDefaultTempdir.test_no_files_left_behindN)rrrr�rrrrr��sr�c@s(eZdZdd�Zdd�ZdS)�TestGetCandidateNamescCs#tj�}|j|tj�dS)N)r�_get_candidate_namesr}rY)r�objrrr�test_retval#sz!TestGetCandidateNames.test_retvalcCs/tj�}tj�}|j||k�dS)N)rr�r6)r�a�brrr�test_same_thing(sz%TestGetCandidateNames.test_same_thingN)rrrr�r�rrrrr� sr�ccsGtj�}z&tjtd|��
dVWdQRXWdtj|�XdS)NrM)rrGrr��rmtree)r:rrr�_inside_empty_temp_dir0s
r�cstjtd�fdd��S)Nr�cs
t��S)N)�iterr)�namesrr�<lambda>=sz'_mock_candidate_names.<locals>.<lambda>)rr�r)r�r)r�r�_mock_candidate_names:s	r�c@s4eZdZdd�Zdd�Zdd�ZdS)�TestBadTempdircCs�t���tjtj�j}}|tjtjBtjBM}tj	tj|�zftj
tjtj�rz|jd�|j
t��|j�WdQRX|jtjtj�g�Wdtj	tj|�XWdQRXdS)Nz!can't set the directory read-only)r�r,rrrM�st_mode�S_IWUSR�S_IWGRP�S_IWOTH�chmod�access�W_OKZskipTestr�PermissionError�	make_tempr1r�)rZoldmode�moderrr�test_read_only_directoryBs

 z'TestBadTempdir.test_read_only_directorycCskt��[tjjtjd�}tjtd|��&|jt	��|j
�WdQRXWdQRXWdQRXdS)N�nonexistentrM)r�r,r-�joinrrMrr�rr�r�)rrMrrr�test_nonexisting_directoryPs

z)TestBadTempdir.test_nonexisting_directorycCs�t��ttjjtjd�}t|d�j�tj	td|��,|j
ttf��|j
�WdQRXWdQRXWdQRXdS)N�file�wbrM)r�r,r-r�rrMr�rjrr�r�NotADirectoryErrorr�r�)rrMrrr�test_non_directoryWs
z!TestBadTempdir.test_non_directoryN)rrrr�r�r�rrrrr�@sr�c@s�eZdZGdd�d�Zdddddd�Zdd�Zd	d
�Zdd�Zd
d�Ze	j
ed�dd��Ze	j
e
d�dd��Ze	j
ed�dd��Zdd�Zdd�Zdd�ZdS)�TestMkstempInnerc@sXeZdZejZejZej	Z
ejZdd�Z
dd�Zdd�ZdS)zTestMkstempInner.mkstempedcCs[|r|j}n	|j}tj|||�}tj|||||�\|_|_dS)N)�_bflags�_tflagsrr�_mkstemp_inner�fdr9)rr:r;r<�bin�flags�output_typerrr�__init__is
	z#TestMkstempInner.mkstemped.__init__cCstj|j|�dS)N)r,rkr�)rrrrrrkpsz TestMkstempInner.mkstemped.writecCs$|j|j�|j|j�dS)N)�_closer��_unlinkr9)rrrr�__del__ssz"TestMkstempInner.mkstemped.__del__N)rrrr�_bin_openflagsr��_text_openflagsr�r,rjr��unlinkr�r�rkr�rrrr�	mkstempedcs				r�NrcCs�tj|||�}|dkrH|tkr<tj�}ntj�}|dkr]|�}|dkrr|�}|j||||�}|j|j|||�|S)N)rrrrKrLr�r@r9)rr:r;r<r�r�r�rrr�	do_createws		zTestMkstempInner.do_createcCs�|j�jd�|jdd�jd�|jdd�jd�|jdddd�jd�|jdddd�jd�dS)Nsblatr;r�r<r��aaz.txt)r�rk)rrrr�
test_basic�s
zTestMkstempInner.test_basiccCs_tj�}|jd|dd�jd�|jd|dd�jd�|jd|dd�jd�|jd|dddd�jd�|jd|dddd	�jd�|jt��$|jdd
dd�jd�WdQRX|jt��$|jd|dd
�jd�WdQRX|jt��*|jd|dddd
�jd�WdQRXdS)Nr:r<rsblatr;�a�bsaas.txtr)rrLr�rkrr)rZdir_brrr�test_basic_with_bytes_names�s%%&&z,TestMkstempInner.test_basic_with_bytes_namescCs=ttt��}x$|D]}|jdd�||<qWdS)Nr;r�)�listr`rar�)r�extantrcrrr�test_basic_many�s
z TestMkstempInner.test_basic_manycCs>tj�}z|jd|�jd�Wdtj|�XdS)Nr:sblat)rrGr�rkr,�rmdir)rr:rrr�test_choose_directory�sz&TestMkstempInner.test_choose_directoryzos.stat not availablecCsg|j�}tjtj|j�j�}d}tjdkrS|d?}|d}|j||�dS)	Ni��win32�r��@�	�I)	r�r�S_IMODEr,r9r��sys�platformr1)rr�r�rU�userrrr�test_file_mode�s

zTestMkstempInner.test_file_modezos.spawnl not availablecCs5tjrd}nd}|j�}|jtj|j�d�d|j}y
t}Wntk
rxt	j
d}YnXtjjtjj
tjj|��d�}t	jdkr�dt	j}d|}n	t	j}tjtjt	j||||�}|j|dkd	|�|j|dkd
|�dS)N�v�qFz%drztf_inherit_check.pyr�z"%s"z$child process caught fatal signal %dz child process reports failure %d)r�verboser�r1r,�get_inheritabler��__file__�	NameErrorr��argvr-r�r�r2r��
executabler�P_WAITr|)rr�r�r��meZtesterZ	decoratedZretvalrrr�test_noinherit�s(		


$	

	$zTestMkstempInner.test_noinheritztext mode not availablecCsh|jdd�}|jd�|jd�tj|jdtj�|jtj|jd�d�dS)Nr�rsblatsextra
resblat)r�rkr,�lseekr��SEEK_SETr1rn)r�frrr�
test_textmode�s


zTestMkstempInner.test_textmodecCs(tjtj�tj�dtjt�S)Nr)rr�rKrIr�r)rrrrr��s
	zTestMkstempInner.make_tempcCs�t���tddd��o|j�\}}tj|�|j|jd��|j�\}}tj|�|j|jd��WdQRXWdQRXdS)N�aaa�bbb)r�r�r�r,rjr6�endswith)r�fd1Zname1�fd2Zname2rrr�!test_collision_with_existing_file�s


z2TestMkstempInner.test_collision_with_existing_filecCs�t��vtddd��\tj�}|j|jd��|j�\}}tj|�|j|jd��WdQRXWdQRXdS)Nr�r�)	r�r�rrGr6r�r�r,rj)rr:r�r9rrr�&test_collision_with_existing_directorys

z7TestMkstempInner.test_collision_with_existing_directory)rrrr�r�r�r�r�r�rwrx�has_statr��
has_spawnlr��has_textmoder�r�r�r�rrrrr�`s&
r�c@s(eZdZdd�Zdd�ZdS)�TestGetTempPrefixcCshtj�}|j|t�|jt|�d�tj�}|j|t�|jt|�d�dS)Nr)rrIr}rZ
assertGreaterr/rJr)r�pZpbrrr�test_sane_templatesz$TestGetTempPrefix.test_sane_templatecCs�tj�d}tjdd�}zOtjj||�}tj|tjtjB�}tj	|�tj
|�Wdtj|�XdS)Nz
xxxxxx.xxx�prefixr)rrIrGr,r-r�r��O_RDWR�O_CREATrjr�r�)rr��dr�rrr�test_usable_templates
z&TestGetTempPrefix.test_usable_templateN)rrrrrrrrrr�sr�c@s@eZdZdd�Zdd�Zdd�Zdd�Zd	S)
�TestGetTempDircCsvxotj�tj�fD]U}|jtjj|�pC|tjkd|�|jtjj|�d|�qWdS)Nz%r is not an absolute pathz%r is not a directory)	rrKrLr6r,r-�isabsr��isdir)rrrrr�test_directory_exists1s
$z$TestGetTempDir.test_directory_existscCs'tj�}|jd�|j�dS)Nsblat)rrDrkrj)rr�rrr�test_directory_writable:s
z&TestGetTempDir.test_directory_writablecCsptj�}tj�}tj�}|j||k�|jt|�t|��|j|tj|��dS)N)	rrKrLr6rtr0r1r,�fsdecode)rr�r�rrrrr�DszTestGetTempDir.test_same_thingcCsztjd�}tjd}t_z:tj��'}||d<|jtj�|�WdQRXWd|t_tj|�XdS)Nz-Tempr�)rrGrMrr�r1rKr�)rZcase_sensitive_tempdirZ_tempdirr�rrr�test_case_sensitiveNs
!	z"TestGetTempDir.test_case_sensitiveN)rrrr	r
r�rrrrrr.s	

rc@sIeZdZddddd�Zdd�Zdd�Zdd	�ZdS)
�TestMkstempNc
Cstj|||�}|dkrH|tkr<tj�}ntj�}|dkr]|�}|dkrr|�}tjd|d|d|�\}}tjj|�\}}tjj	|�}	|j
|	|d|	|f�z|j||||�Wdtj|�tj
|�XdS)Nr:r�suffixz+Directory '%s' incorrectly returned as '%s')rrrrKrLrFr,r-r.r2r1r@rjr�)
rr:r;r<r�r�r9r=r>Zadirrrrr�`s$		$
zTestMkstemp.do_createcCsj|j�|jdd�|jdd�|jdddd�|jdddd�|jdd�dS)	Nr;r�r<r�r�z.txtr:�.)r�)rrrrr�ws
zTestMkstemp.test_basiccCs3tj�}|jd|dd�|jd|dd�|jd|dd�|jd|dddd�|jd|dddd�|jdd	�|jt��!|jdd
dddd�WdQRX|jt��!|jdd	dddd�WdQRX|jt��!|jdd	dddd�WdQRXdS)
Nr:r<rr;r�r�saas.txt�.rr�z.txt)rrLr�rr)rrrrrr��s##z'TestMkstemp.test_basic_with_bytes_namescCs5tj�}z|jd|�Wdtj|�XdS)Nr:)rrGr�r,r�)rr:rrrr��sz!TestMkstemp.test_choose_directory)rrrr�r�r�r�rrrrr
]s	r
c@s�eZdZdd�Zddddd�Zdd�Zdd	�Zd
d�Zdd
�Ze	j
ed�dd��Zdd�Z
dd�ZdS)�TestMkdtempcCs
tj�S)N)rrG)rrrrr��szTestMkdtemp.make_tempNcCs�tj|||�}|dkrH|tkr<tj�}ntj�}|dkr]|�}|dkrr|�}tjd|d|d|�}y|j||||�|SWntj|��YnXdS)Nr:rr)	rrrrKrLrGr@r,r�)rr:r;r<r�r9rrrr��s 		
zTestMkdtemp.do_createcCs�tj|j��tj|jdd��tj|jdd��tj|jdddd��tj|jdddd��dS)Nr;r�r<r�r�z.txt)r,r�r�)rrrrr��s
zTestMkdtemp.test_basiccCsetj�}tj|jd|��tj|jd|dd��tj|jd|dd��tj|jd|dddd��tj|jd|dddd��|jt��*tj|jd|dddd��WdQRX|jt��*tj|jd|dddd	��WdQRX|jt��*tj|jdd
dddd��WdQRXdS)Nr:r;r�r<r�saas.txtr�z.txtr)rrLr,r�r�rr)rrrrrr��s%%,,z'TestMkdtemp.test_basic_with_bytes_namesc
Csrttt��}z+x$|D]}|jdd�||<qWWdx*|D]"}t|t�rGtj|�qGWXdS)Nr;r�)r�r`rar�r3rr,r�)rr�rcrrrr��s

zTestMkdtemp.test_basic_manycCs>tj�}ztj|jd|��Wdtj|�XdS)Nr:)rrGr,r�r�)rr:rrrr��sz!TestMkdtemp.test_choose_directoryzos.stat not availablecCs�|j�}zbtjtj|�j�}|dM}d}tjdkr]|d?}|d	}|j||�Wdtj|�XdS)
Ni�i�r�r�rr�r�r�r�)	r�rr�r,r�r�r�r1r�)rr:r�rUr�rrr�	test_mode�s


zTestMkdtemp.test_modecCs�t��vtddd��\tjdd�}|j�|j|jjd��tj�}|j|jd��WdQRXWdQRXdS)Nr�r��deleteF)	r�r�rrDrjr6r9r�rG)rr�r:rrrr��s

z-TestMkdtemp.test_collision_with_existing_filecCsst��ctddd��Itj�}|j|jd��tj�}|j|jd��WdQRXWdQRXdS)Nr�r�)r�r�rrGr6r�)rZdir1Zdir2rrrr��s
z2TestMkdtemp.test_collision_with_existing_directory)rrrr�r�r�r�r�r�rwrxr�rr�r�rrrrr�srcsqeZdZ�fdd�Z�fdd�ZGdd�d�Zdddd	�Zd
d�Zdd
�Z�S)�
TestMktempcs tj�|_t�j�dS)N)rrGr:r[r))r)r\rrr)szTestMktemp.setUpcs3|jr"tj|j�d|_t�j�dS)N)r:r,r�r[r+)r)r\rrr+s		zTestMktemp.tearDownc@s:eZdZejZejZdd�Z	dd�Z
dS)zTestMktemp.mktempedcCsGtjd|d|d|�|_tjtj|j|jd��dS)Nr:rri�)rr!r9r,rjr�r�)rr:r;r<rrrr�s!zTestMktemp.mktemped.__init__cCs|j|j�dS)N)r�r9)rrrrr� szTestMktemp.mktemped.__del__N)rrrr,r�r�rr�r�r�r�rrrr�mktempeds		rrcCs8|j|j||�}|j|j|j||�|S)N)rr:r@r9)rr;r<r�rrrr�#szTestMktemp.do_createcCsZ|j�|jdd�|jdd�|jdddd�|jdddd�dS)Nr;r�r<r�r�z.txt)r�)rrrrr�)s

zTestMktemp.test_basiccCs=ttt��}x$|D]}|jdd�||<qWdS)Nr;r�)r�r`rar�)rr�rcrrrrd1s
zTestMktemp.test_many)	rrrr)r+rr�r�rdrr)r\rrs
rc@s�eZdZdddddd�Zdd�Zdd	�Zd
d�Zdd
�Zdd�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dS)�TestNamedTemporaryFileNrTc	CsY|dkrtj�}tjd|d|d|d|�}|j|j|||�|S)Nr:rrr)rrKrDr@r9)rr:r;r<rr�rrrr�Fs	z TestNamedTemporaryFile.do_createcCsZ|j�|jdd�|jdd�|jdddd�|jdddd�dS)Nr;r�r<r�r�z.txt)r�)rrrrr�Ps

z!TestNamedTemporaryFile.test_basiccCsp|j�}tj|�}|j}|j}~|d�~|d�~tjdd�rl|j|��dS)NsfoosbarZcpythonT)r��weakref�refrkrZcheck_impl_detail�assertIsNone)rr��wrrkZwrite2rrr�test_method_lookupXs		

z)TestNamedTemporaryFile.test_method_lookupcssdddg��fdd�}x1t|��D] \}}|j|�|�q1W|j|t��d�dS)Nsspam
seggs
sbeans
cs9tjdd�}|jdj���|jd�|S)Nr�zw+brr)rrDrkr��seek)r�)�linesrr�	make_filels
z3TestNamedTemporaryFile.test_iter.<locals>.make_filer)�	enumerater1r/)rrrc�lr)rr�	test_iterhs
z TestNamedTemporaryFile.test_itercCs6tj�}|jtjj|j�d|j�dS)Nz$NamedTemporaryFile %s does not exist)rrDr6r,r-�existsr9)rr�rrr�test_creates_namedusz)TestNamedTemporaryFile.test_creates_namedcCsttj�}zStjd|�}|jd�|j�|jtjj|j	�d|j	�Wdtj
|�XdS)Nr:sblatz(NamedTemporaryFile %s exists after close)rrGrDrkrjr|r,r-r"r9r�)rr:r�rrr�test_del_on_close{s

z(TestNamedTemporaryFile.test_del_on_closecCs�tj�}d}zbtjd|dd�}|j}|jd�|j�|jtjj	|j�d|j�Wd|dk	r�tj
|�tj|�XdS)Nr:rFsblatz)NamedTemporaryFile %s missing after close)rrGrDr9rkrjr6r,r-r"r�r�)rr:�tmpr�rrr�test_dis_del_on_close�s	


z,TestNamedTemporaryFile.test_dis_del_on_closecCs;tj�}|jd�|j�|j�|j�dS)Nsabc
)rrDrkrj)rr�rrr�test_multiple_close�s



z*TestNamedTemporaryFile.test_multiple_closec
sttj��#�|jtjj�j��WdQRX|jtjj�j���fdd�}|jt	|�dS)Nc	s��WdQRXdS)Nrr)r�rr�
use_closed�sz?TestNamedTemporaryFile.test_context_manager.<locals>.use_closed)
rrDr6r,r-r"r9r|rr�)rr(r)r�r�test_context_manager�s
#z+TestNamedTemporaryFile.test_context_managercs�g�tj���fdd�}tjdd|��Ktjddt��.|jttj�|jt	��d�WdQRXWdQRXdS)Ncs�j|��|�dS)N)rS)r�)�closed�os_closerrrj�s
z5TestNamedTemporaryFile.test_no_leak_fd.<locals>.closezos.close�side_effectzio.openr)
r,rjr�patchr�rrrDr1r/)rrjr)r*r+r�test_no_leak_fd�s	z&TestNamedTemporaryFile.test_no_leak_fdcCs�tj�}|jtj|�|jt��tjddd|�WdQRX|jt��tjddd|�WdQRX|j	t
j|�g�dS)Nr�rr:�)rrGZ
addCleanuprr�rr�rDrr1r,r�)rr:rrr�
test_bad_mode�sz$TestNamedTemporaryFile.test_bad_mode)rrrr�r�rr!r#r$r&r'r)r.r0rrrrrCs



rc@seZdZdddddd�Zdd�Zdd	�Zd
d�Zdd
�Zdd�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�ZdS)*�TestSpooledTemporaryFilerNrc	Cs@|dkrtj�}tjd|d|d|d|�}|S)N�max_sizer:rr)rrKrO)rr2r:r;r<r�rrrr��s$z"TestSpooledTemporaryFile.do_createcCsN|j�}|j|j�|jdddddd�}|j|j�dS)Nr2r	r;r�r<z.txt)r�r|�_rolled)rr�rrrr��sz#TestSpooledTemporaryFile.test_basiccCs�tj�}z�tjddd|�}|j|j�|jdd�|j|j�|j}|j�|jt	|t
�o�tjj
|�d|�Wdtj|�XdS)Nr2�
r:sblat �z*SpooledTemporaryFile %s exists after close)rrGrOr|r3rkr6r9rjr3rr,r-r"r�)rr:r��filenamerrrr$�s	
$z*TestSpooledTemporaryFile.test_del_on_closecCsj|jdd�}|j|j�x1td�D]#}|jdd�|jd�q/W|j|j�dS)Nr2�r5r�xresxxxxxxxxxxxxxxxxxxxx)r�r|r3r`rrk)rr�rcrrr�test_rewrite_small�sz+TestSpooledTemporaryFile.test_rewrite_smallcCs}|jdd�}|j|j�|jd�|j|j�|jd�|j|j�|jd�|j|j�dS)Nr2r7r8rer4sxxxxxxxxxxxxxxxxxxxxs
xxxxxxxxxx)r�r|r3rkr6)rr�rrr�test_write_sequential�s


z.TestSpooledTemporaryFile.test_write_sequentialcCsF|j�}|jd�|jd�|j�}|j|d�dS)Nr8�y�zrsxyz)r8r;r<)r��
writelinesrrnr1)rr��bufrrr�test_writelines�s


z(TestSpooledTemporaryFile.test_writelinescCsP|jdd�}|jd
�|j|j�|jd�|j|j�dS)Nr2�#r8rer4r5�xxxxxxxxxxxxxxxxxxxx�
xxxxxxxxxx�xxxxx)rArBrC)r�r=r|r3rkr6)rr�rrr�test_writelines_sequential�s


z3TestSpooledTemporaryFile.test_writelines_sequentialcCsc|jdd�}|j|j�|jdd�|j|j�|jd�|j|j�dS)Nr2r7r	rr8)r�r|r3rrkr6)rr�rrr�test_sparses
z$TestSpooledTemporaryFile.test_sparsecCsO|jdd�}|j|j�|j|j�dk�|j|j�dS)Nr2r7r)r�r|r3r6�fileno)rr�rrr�test_filenosz$TestSpooledTemporaryFile.test_filenocCsKtj�}|jd�|j|j�|j�|j�|j�dS)Nsabc
)rrOrkr|r3rj)rr�rrr�#test_multiple_close_before_rollovers


z<TestSpooledTemporaryFile.test_multiple_close_before_rollovercCsQtjdd�}|jd�|j|j�|j�|j�|j�dS)Nr2rsabc
)rrOrkr6r3rj)rr�rrr�"test_multiple_close_after_rollover"s


z;TestSpooledTemporaryFile.test_multiple_close_after_rollovercCs||jdd�}|j}|j}|j}|dd�|dd�|dd�|j|d�dddd�dS)Nr2r7r�r@r�r�F)r�rnrkrr1)rr�rnrkrrrr�test_bound_methods+s			
z+TestSpooledTemporaryFile.test_bound_methodscCstjdd�}|jd�|j|j�|j|jd�|j|j�|j	t
��|jWdQRX|j	t
��|jWdQRX|jd�|j
|j�|j|jd�|j|j�|j	t
��|jWdQRX|j	t
��|jWdQRXdS)Nr2r4r8zw+bzrb+s
xxxxxxxxxx)rrOrkr|r3r1r�rr9rr��newlines�encodingr6�assertIsNotNone)rr�rrr�test_properties9s"

z(TestSpooledTemporaryFile.test_propertiescCs�tjdddd�}|jd�|jd�|j|j�d�|jd�|jd�|j|j�d�|j|j�|j|jd�|j	|j
�|j	|j�|j	|j�|jd	�|jd�|j|j�d
�|jd�|jd�|j|j�d�|j
|j�|j|jd�|j|j
�|j|jtj�|j|j�dS)
Nr�zw+r2r4zabc
rzdef
zabc
def
zxyzzy
zabc
def
xyzzy
zfoobar
zabc
def
xyzzy
foobar
)rrOrkrr1rnr|r3r�rr9rLrMr6rNr,�linesep)rr�rrr�test_text_modeMs.







z'TestSpooledTemporaryFile.test_text_modec	Cs=tjdddddddd�}|jd	�|jd
�|j|j�d	�|j|j�|j|jd�|j	|j
�|j	|j�|j	|j�|jdd
�|jd
�|j|j�d	dd
�|j
|j�|j|jd�|j|j
�|j|j�|j|jd�dS)Nr�zw+r2r4�newlinerrMzutf-8uΛ
ruΛrez
u(ΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛu(ΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛ)rrOrkrr1rnr|r3r�rr9rLrMr6rN)rr�rrr�test_text_newline_and_encodingjs$


z7TestSpooledTemporaryFile.test_text_newline_and_encodingc
srtjdd��'�|j�j�|j�j�WdQRX|j�j��fdd�}|jt|�dS)Nr2rc	s��WdQRXdS)Nrr)r�rrr(�szQTestSpooledTemporaryFile.test_context_manager_before_rollover.<locals>.use_closed)rrOr|r3r*r6rr�)rr(r)r�r�$test_context_manager_before_rolloversz=TestSpooledTemporaryFile.test_context_manager_before_rolloverc
s�tjdd��N�|j�j��jd��j�|j�j�|j�j�WdQRX|j�j��fdd�}|jt	|�dS)Nr2rsabc
c	s��WdQRXdS)Nrr)r�rrr(�szQTestSpooledTemporaryFile.test_context_manager_during_rollover.<locals>.use_closed)
rrOr|r3rk�flushr6r*rr�)rr(r)r�r�$test_context_manager_during_rollover�s

z=TestSpooledTemporaryFile.test_context_manager_during_rolloverc
s�tjdd���jd��j�|j�j���|j�j�WdQRX|j�j��fdd�}|jt	|�dS)Nr2rsabc
c	s��WdQRXdS)Nrr)r�rrr(�szPTestSpooledTemporaryFile.test_context_manager_after_rollover.<locals>.use_closed)
rrOrkrUr6r3r|r*rr�)rr(r)r�r�#test_context_manager_after_rollover�s

z<TestSpooledTemporaryFile.test_context_manager_after_rollovercCstjdd�}|jd�|jd�|j�|j|j�|j|jj	�d�tjdd�}|jd�|jd�|j|j�|j|jj	�d�tjdd�}|jd�|jd�|j
|j�tr|jtj
|j��jd�dS)	Nr2r4sabcdefg
rr�sabcdre)rrOrkr�truncater|r3r1�_file�getvaluer6r�r,�fstatrF�st_size)rr�rrr�!test_truncate_with_size_parameter�s"






z:TestSpooledTemporaryFile.test_truncate_with_size_parameter)rrrr�r�r$r9r:r?rDrErGrHrIrKrOrQrSrTrVrWr^rrrrr1�s&		
		r1c@sLeZdZdd�Zdd�Zdd�Zdd�Zd	d
�ZdS)�TestTemporaryFilecCstj�dS)N)rrE)rrrrr��szTestTemporaryFile.test_basicc	Csetj�}tjd|�}|jd�ytj|�Wn"|j�tj|��YnXdS)Nr:sblat)rrGrErkr,r�rj)rr:r�rrr�test_has_no_name�s


z"TestTemporaryFile.test_has_no_namecCs;tj�}|jd�|j�|j�|j�dS)Nsabc
)rrErkrj)rr�rrrr'�s



z%TestTemporaryFile.test_multiple_closecsV�fdd�}|dd�|dd�|dddd	�|d
ddd�dS)
Nc
sPtj||��7}|j|�|jd��j||j��WdQRXdS)Nr)rrErkrr1rn)�inputr�r��fileobj)rrr�	roundtrip�s

z;TestTemporaryFile.test_mode_and_encoding.<locals>.roundtrips1234zw+bzabdc
zw+uΛrMzutf-16zfoo
rRrr)rrcr)rr�test_mode_and_encoding�s


z(TestTemporaryFile.test_mode_and_encodingcs�g�tj���fdd�}tjdd|��Ktjddt��.|jttj�|jt	��d�WdQRXWdQRXdS)Ncs�j|��|�dS)N)rS)r�)r*r+rrrj�s
z0TestTemporaryFile.test_no_leak_fd.<locals>.closezos.closer,zio.openr)
r,rjrr-r�rrrEr1r/)rrjr)r*r+rr.�s	z!TestTemporaryFile.test_no_leak_fdN)rrrr�r`r'rdr.rrrrr_�s
	
r_c@s4eZdZdd�Zdd�Zdd�ZdS)�
NulledModulescGs3dd�|D�|_dd�|jD�|_dS)NcSsg|]}|j�qSr)rR)�.0�modrrr�
<listcomp>�s	z*NulledModules.__init__.<locals>.<listcomp>cSsg|]}|j��qSr)�copy)rfrrrrrhs	)�refs�contents)r�modulesrrrr��szNulledModules.__init__cCs3x,|jD]!}x|D]}d||<qWq
WdS)N)rj)rrrVrrrr%s
zNulledModules.__enter__cGsAx:t|j|j�D]#\}}|j�|j|�qWdS)N)�ziprjrk�clear�update)r�exc_inforrrrrr*s"
zNulledModules.__exit__N)rrrr�r%r*rrrrre�srec@s�eZdZdddddd�Zdd�Zdd	�Zejd
d��Zej	dd
��Z
dd�Zdd�Zdd�Z
dd�Zdd�ZdS)�TestTemporaryDirectoryNrrc	Cs�|dkrtj�}tjd|d|d|�}|j|j|||�|r}|j|j|||d�}d|_ttjj	|jd�d��}|j
d�WdQRX|S)Nr:rrrztest.txtr�sHello world!)rrKrPr@r9r�r�r,r-r�rk)rr:r;r<�recurser%�d1r�rrrr�s	$z TestTemporaryDirectory.do_createcCs\tj��}WdQRX|jt��}tjd|�WdQRX|j|jjtj�dS)Nr:)rrPrr�r1�	exception�errno�ENOENT)rr��cmrrr�test_mkdtemp_failures
z+TestTemporaryDirectory.test_mkdtemp_failurecCs�tj�}zl|jd|�}|jtjj|j�d|j�|j�|j	tjj|j�d|j�Wdtj
|�XdS)Nr:z$TemporaryDirectory %s does not existz*TemporaryDirectory %s exists after cleanup)rrGr�r6r,r-r"r9�cleanupr|r�)rr:rrrr�test_explicit_cleanup&s
z,TestTemporaryDirectory.test_explicit_cleanupcCs�|j�}|jdd�}tj|jtjj|jd��|j�|jtjj|j�d|j�|j	tjj|j�d�|j
tj|j�dgd�|j�dS)NrrrZfooz*TemporaryDirectory %s exists after cleanupz-Directory pointed to by a symlink was deletedztest.txtz>Contents of the directory pointed to by a symlink were deleted)r�r,�symlinkr9r-r�ryr|r"r6r1r�)rrsZd2rrr�(test_cleanup_with_symlink_to_a_directory3s%
z?TestTemporaryDirectory.test_cleanup_with_symlink_to_a_directorycCsctj�}zB|jd|�}|j}~|jtjj|�d|�Wdtj|�XdS)Nr:z*TemporaryDirectory %s exists after __del__)	rrGr�r9r|r,r-r"r�)rr:rr9rrr�test_del_on_collectionHs	z-TestTemporaryDirectory.test_del_on_collectionc
Cs�|j���}x�dD]�}djd|d	|�}tjd
|�\}}}|j�j�}|jtjj	|�d|�|jdd
�}|j
d|�|jd|�qWWdQRXdS)N�builtinsr,�shutilr�rr&a�if True:
                    import builtins
                    import os
                    import shutil
                    import sys
                    import tempfile
                    import warnings

                    tmp = tempfile.TemporaryDirectory(dir={dir!r})
                    sys.stdout.buffer.write(tmp.name.encode())

                    tmp2 = os.path.join(tmp.name, 'test_dir')
                    os.mkdir(tmp2)
                    with open(os.path.join(tmp2, "test.txt"), "w") as f:
                        f.write("Hello world!")

                    {mod}.tmp = tmp

                    warnings.filterwarnings("always", category=ResourceWarning)
                    r:rgz-cz*TemporaryDirectory %s exists after cleanupzutf-8�backslashreplacez
Exception z'ResourceWarning: Implicitly cleaning up)r~r,rr�rr&)r��formatr�assert_python_okro�stripr|r,r-r"rbr�)rr:rg�code�rc�out�err�tmp_namerrr�test_del_on_shutdownUs
z+TestTemporaryDirectory.test_del_on_shutdowncCs�|j���}djd|�}tjd|�\}}}|j�j�}|jtjj	|�d|�|jdd�}|j
d|�|jd|�WdQRXdS)	Na�if True:
                import sys
                import tempfile
                import warnings

                def generator():
                    with tempfile.TemporaryDirectory(dir={dir!r}) as tmp:
                        yield tmp
                g = generator()
                sys.stdout.buffer.write(next(g).encode())

                warnings.filterwarnings("always", category=ResourceWarning)
                r:z-cz*TemporaryDirectory %s exists after cleanupzutf-8r�z
Exception z'ResourceWarning: Implicitly cleaning up)r�r�rr�ror�r|r,r-r"rbr�)rr:r�r�r�r�r�rrr�test_exit_on_shutdownus
z,TestTemporaryDirectory.test_exit_on_shutdowncCs�|j���}|jd|dd�}|j}tjdtfdd��%tjddt�~tj�WdQRX|jt	j
j|�d	|�WdQRXdS)
Nr:rr�Z
Implicitly�quietF�alwaysrz*TemporaryDirectory %s exists after __del__)r�r9rr#�ResourceWarningr&r'Z
gc_collectr|r,r-r")rr:rr9rrr�test_warnings_on_cleanup�s	z/TestTemporaryDirectory.test_warnings_on_cleanupcCs.|j�}|j�|j�|j�dS)N)r�ry)rrrrrr'�s

z*TestTemporaryDirectory.test_multiple_closec
Cse|j�}|�3}|jtjj|��|j||j�WdQRX|jtjj|��dS)N)r�r6r,r-r"r1r9r|)rrr9rrrr)�s
	z+TestTemporaryDirectory.test_context_manager)rrrr�rxrzrZskip_unless_symlinkr|Zcpython_onlyr}r�r�r�r'r)rrrrrqs



 rq�__main__)5rrur�r,rqr�rAr&�
contextlibrrwrZtestrZtest.supportrryrr�r�r�r�r�r��
startswithraZTestCaser
rrCrXrzr�r��contextmanagerr�r�r�r�r�rr
rrrr1rDrEr_rerqr�mainrrrr�<module>s\		/A*%
 � />l<~�>�