Your IP : 216.73.216.85


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.pyc



��Yf���@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�7/opt/alt/python35/lib64/python3.5/test/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�nbaseZnpreZnsufZcheckrrr�	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)rrrrVrrrrrBrsrBcsseZdZdZ�fdd�Zdd�Zdd�Zdd	�Zej	e
ed
�d�dd
��Z�S)�TestRandomNameSequencez6Test the internal iterator object _RandomNameSequence.cs tj�|_t�j�dS)N)r�_RandomNameSequence�r�superr))r)�	__class__rrr)�szTestRandomNameSequence.setUpcCs)t|j�}|j|ddd�dS)Nr)�nextrYr?)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)rY�range�
TEST_FILESr\r?�assertNotIn)rrSrY�ir]rrr�	test_many�s	z TestRandomNameSequence.test_manycCs;d}|j}x%|D]}|d7}|dkrPqWdS)Nrr�)rY)rrbrYr]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,�piperf�close�writer\rY�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)
rrr�__doc__r)r^rcre�unittest�
skipUnless�hasattrr,rurr)r[rrW�s
rWc@s.eZdZdZdd�Zdd�ZdS)�TestCandidateTempdirListz3Test the internal function _candidate_tempdir_list.cCsJtj�}|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�AttributeErrorrr�curdir)r�envZenvname�dirnamer~rrr�test_wanted_dirs�s 

z)TestCandidateTempdirList.test_wanted_dirsN)rrrrvr�r�rrrrrz�s	rzc@s"eZdZdZdd�ZdS)�TestGetDefaultTempdirzTest _get_default_tempdir().c-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)rr)�args�kwargsrrr�
raise_OSErrorszFTestGetDefaultTempdir.test_no_files_left_behind.<locals>.raise_OSError�opencs�||�}�|_|S)N)rj)r�r��fp)�	orig_openr�rr�
bad_writers	zCTestGetDefaultTempdir.test_no_files_left_behind.<locals>.bad_writer)rrOr�	swap_attrZ_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)rrrrvr�rrrrr��sr�c@s.eZdZdZdd�Zdd�ZdS)�TestGetCandidateNamesz0Test the internal function _get_candidate_names.cCs#tj�}|j|tj�dS)N)r�_get_candidate_namesr}rX)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)rrrrvr�r�rrrrr� sr�ccsGtj�}z&tjtd|��
dVWdQRXWdtj|�XdS)NrL)rrFrr��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,rrrL�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�nonexistentrL)r�r,r-�joinrrLrr�rr�r�)rrLrrr�test_nonexisting_directoryPs

z)TestBadTempdir.test_nonexisting_directorycCs�t��ttjjtjd�}t|d�j�tj	td|��,|j
ttf��|j
�WdQRXWdQRXWdQRXdS)N�file�wbrL)r�r,r-r�rrLr�rirr�r�NotADirectoryErrorr�r�)rrLrrr�test_non_directoryWs
z!TestBadTempdir.test_non_directoryN)rrrr�r�r�rrrrr�@sr�c@s�eZdZdZGdd�d�Zdddddd�Zdd	�Zd
d�Zdd
�Zdd�Z	e
jed�dd��Z
e
jed�dd��Ze
jed�dd��Zdd�Zdd�Zdd�ZdS)�TestMkstempInnerz*Test the internal function _mkstemp_inner.c@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,rjr�)rrrrrrjpsz TestMkstempInner.mkstemped.writecCs$|j|j�|j|j�dS)N)�_closer��_unlinkr9)rrrr�__del__ssz"TestMkstempInner.mkstemped.__del__N)rrrr�_bin_openflagsr��_text_openflagsr�r,rir��unlinkr�r�rjr�rrrr�	mkstempedcs				r�NrcCs�tj|||�}|dkrH|tkr<tj�}ntj�}|dkr]|�}|dkrr|�}|j||||�}|j|j|||�|S)N)rrrrJrKr�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�rj)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)rrKr�rjrr)rZdir_brrr�test_basic_with_bytes_names�s%%&&z,TestMkstempInner.test_basic_with_bytes_namescCs=ttt��}x$|D]}|jdd�||<qWdS)Nr;r�)�listr_r`r�)r�extantrbrrr�test_basic_many�s
z TestMkstempInner.test_basic_manycCs>tj�}z|jd|�jd�Wdtj|�XdS)Nr:sblat)rrFr�rjr,�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�rT�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
rdsblat)r�rjr,�lseekr��SEEK_SETr1rm)r�frrr�
test_textmode�s


zTestMkstempInner.test_textmodecCs(tjtj�tj�dtjt�S)Nr)rr�rJrHr�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,rir6�endswith)rZfd1Zname1Zfd2Zname2rrr�!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�rrFr6r�r�r,ri)rr:r�r9rrr�&test_collision_with_existing_directorys

z7TestMkstempInner.test_collision_with_existing_directory)rrrrvr�r�r�r�r�r�rwrx�has_statr��
has_spawnlr��has_textmoder�r�r�r�rrrrr�`s&
r�c@s.eZdZdZdd�Zdd�ZdS)�TestGetTempPrefixzTest gettempprefix().cCshtj�}|j|t�|jt|�d�tj�}|j|t�|jt|�d�dS)Nr)rrHr}rZ
assertGreaterr/rIr)r�pZpbrrr�test_sane_templatesz$TestGetTempPrefix.test_sane_templatecCs�tj�d}tjdd�}zOtjj||�}tj|tjtjB�}tj	|�tj
|�Wdtj|�XdS)Nz
xxxxxx.xxx�prefixr)rrHrFr,r-r�r��O_RDWR�O_CREATrir�r�)rr��dr�rrr�test_usable_templates
z&TestGetTempPrefix.test_usable_templateN)rrrrvr�rrrrrr�sr�c@sFeZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�TestGetTempDirzTest gettempdir().cCsvxotj�tj�fD]U}|jtjj|�pC|tjkd|�|jtjj|�d|�qWdS)Nz%r is not an absolute pathz%r is not a directory)	rrJrKr6r,r-�isabsr��isdir)rrrrr�test_directory_exists1s
$z$TestGetTempDir.test_directory_existscCs'tj�}|jd�|j�dS)Nsblat)rrCrjri)rr�rrr�test_directory_writable:s
z&TestGetTempDir.test_directory_writablecCsptj�}tj�}tj�}|j||k�|jt|�t|��|j|tj|��dS)N)	rrJrKr6rsr0r1r,�fsdecode)rr�r�rrrrr�DszTestGetTempDir.test_same_thingcCsztjd�}tjd}t_z:tj��'}||d<|jtj�|�WdQRXWd|t_tj|�XdS)Nz-Tempr�)rrFrLrr�r1rJr�)rZcase_sensitive_tempdirZ_tempdirr�rrr�test_case_sensitiveNs
!	z"TestGetTempDir.test_case_sensitiveN)rrrrvrrr�rrrrrr.s
	

rc@sOeZdZdZddddd�Zdd�Zdd�Zd	d
�ZdS)�TestMkstempzTest mkstemp().Nc
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')rrrrJrKrEr,r-r.r2r1r?rir�)
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)rrKr�rr)rrrrrr��s##z'TestMkstemp.test_basic_with_bytes_namescCs5tj�}z|jd|�Wdtj|�XdS)Nr:)rrFr�r,r�)rr:rrrr��sz!TestMkstemp.test_choose_directory)rrrrvr�r�r�r�rrrrr	]s
	r	c@s�eZdZdZdd�Zddddd�Zdd�Zd	d
�Zdd�Zd
d�Z	e
jed�dd��Z
dd�Zdd�ZdS)�TestMkdtempzTest mkdtemp().cCs
tj�S)N)rrF)rrrrr��szTestMkdtemp.make_tempNcCs�tj|||�}|dkrH|tkr<tj�}ntj�}|dkr]|�}|dkrr|�}tjd|d|d|�}y|j||||�|SWntj|��YnXdS)Nr:r�r
)	rrrrJrKrFr?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)rrKr,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_r`r�r3rr,r�)rr�rbrrrr��s

zTestMkdtemp.test_basic_manycCs>tj�}ztj|jd|��Wdtj|�XdS)Nr:)rrFr,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�rTr�rrr�	test_mode�s


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

z-TestMkdtemp.test_collision_with_existing_filecCsst��ctddd��Itj�}|j|jd��tj�}|j|jd��WdQRXWdQRXdS)Nr�r�)r�r�rrFr6r�)rZdir1Zdir2rrrr��s
z2TestMkdtemp.test_collision_with_existing_directory)rrrrvr�r�r�r�r�r�rwrxr�rr�r�rrrrr
�sr
csweZdZdZ�fdd�Z�fdd�ZGdd�d�Zddd	d
�Zdd�Zd
d�Z	�S)�
TestMktempzTest mktemp().cs tj�|_t�j�dS)N)rrFr:rZr))r)r[rrr)szTestMktemp.setUpcs3|jr"tj|j�d|_t�j�dS)N)r:r,r�rZr+)r)r[rrr+s		zTestMktemp.tearDownc@s:eZdZejZejZdd�Z	dd�Z
dS)zTestMktemp.mktempedcCsGtjd|d|d|�|_tjtj|j|jd��dS)Nr:r�r
i�)rr!r9r,rir�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_r`r�)rr�rbrrrrc1s
zTestMktemp.test_many)
rrrrvr)r+rr�r�rcrr)r[rrs
rc@s�eZdZdZdddddd�Zdd�Zd	d
�Zdd�Zd
d�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dd�ZdS)�TestNamedTemporaryFilezTest NamedTemporaryFile().NrTc	CsY|dkrtj�}tjd|d|d|d|�}|j|j|||�|S)Nr:r�r
r)rrJrCr?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�refrjrZcheck_impl_detail�assertIsNone)rr��wrrjZwrite2rrr�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)rrCrjr��seek)r�)�linesrr�	make_filels
z3TestNamedTemporaryFile.test_iter.<locals>.make_filer)�	enumerater1r/)rrrb�lr)rr�	test_iterhs
z TestNamedTemporaryFile.test_itercCs6tj�}|jtjj|j�d|j�dS)Nz$NamedTemporaryFile %s does not exist)rrCr6r,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)rrFrCrjrir|r,r-rr9r�)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)rrFrCr9rjrir6r,r-rr�r�)rr:�tmpr�rrr�test_dis_del_on_close�s	


z,TestNamedTemporaryFile.test_dis_del_on_closecCs;tj�}|jd�|j�|j�|j�dS)Nsabc
)rrCrjri)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)
rrCr6r,r-rr9r|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)rR)r�)�closed�os_closerrri�s
z5TestNamedTemporaryFile.test_no_leak_fd.<locals>.closezos.close�side_effectzio.openr)
r,rir�patchr�rrrCr1r/)rrir)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:�)rrFZ
addCleanuprr�rr�rCrr1r,r�)rr:rrr�
test_bad_mode�sz$TestNamedTemporaryFile.test_bad_mode)rrrrvr�r�rrrr r"r#r%r*r,rrrrrCs



rc@seZdZdZdddddd�Zdd�Zd	d
�Zdd�Zd
d�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�ZdS)+�TestSpooledTemporaryFilezTest SpooledTemporaryFile().rNrc	Cs@|dkrtj�}tjd|d|d|d|�}|S)N�max_sizer:r�r
)rrJrN)rr.r:r;r<r�rrrr��s$z"TestSpooledTemporaryFile.do_createcCsN|j�}|j|j�|jdddddd�}|j|j�dS)Nr.r	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)Nr.�
r:sblat �z*SpooledTemporaryFile %s exists after close)rrFrNr|r/rjr6r9rir3rr,r-rr�)rr:r��filenamerrrr �s	
$z*TestSpooledTemporaryFile.test_del_on_closecCsj|jdd�}|j|j�x1td�D]#}|jdd�|jd�q/W|j|j�dS)Nr.�r1r�xrdsxxxxxxxxxxxxxxxxxxxx)r�r|r/r_rrj)rr�rbrrr�test_rewrite_small�sz+TestSpooledTemporaryFile.test_rewrite_smallcCs}|jdd�}|j|j�|jd�|j|j�|jd�|j|j�|jd�|j|j�dS)Nr.r3r4rdr0sxxxxxxxxxxxxxxxxxxxxs
xxxxxxxxxx)r�r|r/rjr6)rr�rrr�test_write_sequential�s


z.TestSpooledTemporaryFile.test_write_sequentialcCsF|j�}|jd�|jd�|j�}|j|d�dS)Nr4�y�zrsxyz)r4r7r8)r��
writelinesrrmr1)rr�Zbufrrr�test_writelines�s


z(TestSpooledTemporaryFile.test_writelinescCsP|jdd�}|jd
�|j|j�|jd�|j|j�dS)Nr.�#r4rdr0r1�xxxxxxxxxxxxxxxxxxxx�
xxxxxxxxxx�xxxxx)r<r=r>)r�r9r|r/rjr6)rr�rrr�test_writelines_sequential�s


z3TestSpooledTemporaryFile.test_writelines_sequentialcCsc|jdd�}|j|j�|jdd�|j|j�|jd�|j|j�dS)Nr.r3r	rr4)r�r|r/rrjr6)rr�rrr�test_sparses
z$TestSpooledTemporaryFile.test_sparsecCsO|jdd�}|j|j�|j|j�dk�|j|j�dS)Nr.r3r)r�r|r/r6�fileno)rr�rrr�test_filenosz$TestSpooledTemporaryFile.test_filenocCsKtj�}|jd�|j|j�|j�|j�|j�dS)Nsabc
)rrNrjr|r/ri)rr�rrr�#test_multiple_close_before_rollovers


z<TestSpooledTemporaryFile.test_multiple_close_before_rollovercCsQtjdd�}|jd�|j|j�|j�|j�|j�dS)Nr.rsabc
)rrNrjr6r/ri)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)Nr.r3r�r;r�r�F)r�rmrjrr1)rr�rmrjrrrr�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)Nr.r0r4zw+bzrb+s
xxxxxxxxxx)rrNrjr|r/r1r�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+r.r0zabc
rzdef
zabc
def
zxyzzy
zabc
def
xyzzy
zfoobar
zabc
def
xyzzy
foobar
)rrNrjrr1rmr|r/r�rr9rGrHr6rIr,�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+r.r0�newlinerrHzutf-8uΛ
ruΛrdz
u(ΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛu(ΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛ)rrNrjrr1rmr|r/r�rr9rGrHr6rI)rr�rrr�test_text_newline_and_encodingjs$


z7TestSpooledTemporaryFile.test_text_newline_and_encodingc
srtjdd��'�|j�j�|j�j�WdQRX|j�j��fdd�}|jt|�dS)Nr.rc	s��WdQRXdS)Nrr)r�rrr$�szQTestSpooledTemporaryFile.test_context_manager_before_rollover.<locals>.use_closed)rrNr|r/r&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)Nr.rsabc
c	s��WdQRXdS)Nrr)r�rrr$�szQTestSpooledTemporaryFile.test_context_manager_during_rollover.<locals>.use_closed)
rrNr|r/rj�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)Nr.rsabc
c	s��WdQRXdS)Nrr)r�rrr$�szPTestSpooledTemporaryFile.test_context_manager_after_rollover.<locals>.use_closed)
rrNrjrPr6r/r|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)	Nr.r0sabcdefg
rr�sabcdrd)rrNrjr�truncater|r/r1Z_file�getvaluer6r�r,�fstatrA�st_size)rr�rrr�!test_truncate_with_size_parameter�s"






z:TestSpooledTemporaryFile.test_truncate_with_size_parameter)rrrrvr�r�r r5r6r:r?r@rBrCrDrFrJrLrNrOrQrRrXrrrrr-�s(		
		r-c@sReZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�ZdS)
�TestTemporaryFilezTest TemporaryFile().cCstj�dS)N)rrD)rrrrr��szTestTemporaryFile.test_basicc	Csetj�}tjd|�}|jd�ytj|�Wn"|j�tj|��YnXdS)Nr:sblat)rrFrDrjr,r�ri)rr:r�rrr�test_has_no_name�s


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


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

z;TestTemporaryFile.test_mode_and_encoding.<locals>.roundtrips1234zw+bzabdc
zw+uΛrHzutf-16zfoo
rMrr)rr\r)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)rR)r�)r&r'rrri�s
z0TestTemporaryFile.test_no_leak_fd.<locals>.closezos.closer(zio.openr)
r,rirr)r�rrrDr1r/)rrir)r&r'rr*�s	z!TestTemporaryFile.test_no_leak_fdN)	rrrrvr�rZr#r]r*rrrrrY�s	
rYc@s4eZdZdd�Zdd�Zdd�ZdS)�
NulledModulescGs3dd�|D�|_dd�|jD�|_dS)NcSsg|]}|j�qSr)rQ)�.0�modrrr�
<listcomp>�s	z*NulledModules.__init__.<locals>.<listcomp>cSsg|]}|j��qSr)�copy)r_rrrrras	)�refs�contents)r�modulesrrrr��szNulledModules.__init__cCs3x,|jD]!}x|D]}d||<qWq
WdS)N)rc)rrrUrrrr%s
zNulledModules.__enter__cGsAx:t|j|j�D]#\}}|j�|j|�qWdS)N)�ziprcrd�clear�update)r�exc_inforrrrrr*s"
zNulledModules.__exit__N)rrrr�r%r*rrrrr^�sr^c@s�eZdZdZdddddd�Zdd�Zd	d
�Zejdd��Z	ej
d
d��Zdd�Zdd�Z
dd�Zdd�Zdd�ZdS)�TestTemporaryDirectoryzTest TemporaryDirectory().Nrrc	Cs�|dkrtj�}tjd|d|d|�}|j|j|||�|r}|j|j|||d�}d|_ttjj	|jd�d��}|j
d�WdQRX|S)Nr:r�r
rztest.txtr�sHello world!)rrJrOr?r9r�r�r,r-r�rj)rr:r;r<�recurser!�d1r�rrrr�s	$z TestTemporaryDirectory.do_createcCs\tj��}WdQRX|jt��}tjd|�WdQRX|j|jjtj�dS)Nr:)rrOrr�r1Z	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)rrFr�r6r,r-rr9�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)NrkrZfooz*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�rqr|rr6r1r�)rrlZd2rrr�(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__)	rrFr�r9r|r,r-rr�)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:r`z-cz*TemporaryDirectory %s exists after cleanupzutf-8�backslashreplacez
Exception z'ResourceWarning: Implicitly cleaning up)rvr,rwr�rr&)r��formatr�assert_python_okrn�stripr|r,r-rrar�)rr:r`�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-8rxz
Exception z'ResourceWarning: Implicitly cleaning up)r�ryrrzrnr{r|r,r-rrar�)rr:r|r}r~rr�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:rk�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�rq)rrrrrr#�s

z*TestTemporaryDirectory.test_multiple_closec
Cse|j�}|�3}|jtjj|��|j||j�WdQRX|jtjj|��dS)N)r�r6r,r-rr1r9r|)rrr9rrrr%�s
	z+TestTemporaryDirectory.test_context_manager)rrrrvr�rprrrZskip_unless_symlinkrtZcpython_onlyrur�r�r�r#r%rrrrrjs



 rj�__main__)5rrmr�r,rpr�r@r&�
contextlibrrwrZtestrZtest.supportrryrr�r�r�r�r�r��
startswithr`ZTestCaser
rrBrWrzr�r��contextmanagerr�r�r�r�r�rr	r
rrr-rCrDrYr^rjr�mainrrrr�<module>s\		/A*%
 � />l<~�>�