Your IP : 216.73.216.213


Current Path : /opt/alt/python34/lib64/python3.4/test/__pycache__/
Upload File :
Current File : //opt/alt/python34/lib64/python3.4/test/__pycache__/test_tempfile.cpython-34.pyc

�
f f���@sddlZddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
mZddl
Z
ddlm
Z
mZeed�r�ddlZdZndZejejkZeed�Zejjd�rdZnd	ZGd
d�de
j�ZGdd
�d
e�ZGdd�de�ZGdd�de�ZGdd�de�ZGdd�de�Zej dd��Z!dd�Z"Gdd�d�Z#Gdd�de#e�Z$Gdd�de�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,d-�d-e�Z.nGd.d/�d/�Z/Gd0d1�d1e�Z0d2d3�Z1e2d4kr�e1�ndS)5�N)�mock)�support�
script_helper�stat��spawnlZopenbsd�0�dc@sCeZdZejd�Zdd�Zdd�Zdd�ZdS)	�BaseTestCasez^[a-z0-9_-]{8}$cCs?tj�|_|jj�tjddtdddt�dS)N�ignore�category�message�mktemp�module)r�check_warnings�_warnings_manager�	__enter__�warnings�filterwarnings�RuntimeWarning�__name__)�self�r�7/opt/alt/python34/lib64/python3.4/test/test_tempfile.py�setUp+s
zBaseTestCase.setUpcCs|jjddd�dS)N)r�__exit__)rrrr�tearDown1szBaseTestCase.tearDownc	Cstjj|�\}}|dt|��}|t|�t|�d�}|jtjj|�tjj|�d||f�|j||d||f�|j||d||f�|t|�t|�t|��}|j|jj|�d|�dS)Nzfile '%s' not in directory '%s'z"file '%s' does not begin with '%s'z file '%s' does not end with '%s'z1random string '%s' does not match ^[a-z0-9_-]{8}$)	�os�path�split�len�assertEqual�abspath�
assertTrue�	str_check�match)	r�name�dir�pre�suf�ndir�nbaseZnpreZnsufrrr�	nameCheck5s $&zBaseTestCase.nameCheckN)	r�
__module__�__qualname__�re�compiler$rrr,rrrrr
'sr
c@seZdZdd�ZdS)�TestExportscCs�tj}idd6dd6dd6dd6dd6dd6dd6dd	6dd
6dd6dd6dd
6}g}x:|D]2}|ddkrp||krp|j|�qpqpW|jt|�dkd|�dS)Nr�NamedTemporaryFile�
TemporaryFile�mkstemp�mkdtempr�TMP_MAX�
gettempprefix�
gettempdir�tempdir�template�SpooledTemporaryFile�TemporaryDirectoryr�_zunexpected keys: %s)�tempfile�__dict__�appendr#r )r�dict�expectedZunexp�keyrrr�test_exportsIs(	

zTestExports.test_exportsN)rr-r.rDrrrrr1Hsr1csseZdZdZ�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__rrrgszTestRandomNameSequence.setUpcCs)t|j�}|j|ddd�dS)N�)�nextrGr,)r�srrr�test_get_six_char_strksz,TestRandomNameSequence.test_get_six_char_strcCsfi}|j}xPtt�D]B}t|�}|j|ddd�|j||�d||<qWdS)NrJr)rG�range�
TEST_FILESrKr,�assertNotIn)rrArG�irLrrr�	test_manyps	z TestRandomNameSequence.test_manycCs>d}|j}x(|D] }|d7}|dkrPqqWdS)Nrr�)rG)rrQrGrLrrr�
supports_iter{s	

z$TestRandomNameSequence.supports_iter�forkz!os.fork is required for this testcCstj�\}}d}z�tj�}|sytj|�tj|t|j�jd��tj|�tjd�nt|j�}tj	|t
|��jd�}Wd|r�ytj|t
j�Wq�tk
r�Yq�Xntj|�tj|�X|j||�dS)N�asciir)r�piperU�close�writerKrG�encode�_exit�readr �decode�kill�signal�SIGKILL�OSErrorZassertNotEqual)rZread_fdZwrite_fd�pidZparent_valueZchild_valuerrr�test_process_awareness�s&
"
%

z-TestRandomNameSequence.test_process_awareness)
rr-r.�__doc__rrMrRrT�unittest�
skipUnless�hasattrrrcrr)rIrrEds
rEc@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 �assertIsInstance�str)r�cand�crrr�test_nonempty_list�s
z+TestCandidateTempdirList.test_nonempty_listcCs�tj���}x<dD]4}tj|�}|stjj|�||<qqWtj�}x<dD]4}tj|�}|s�t�n|j	||�qaWytj
�}Wn!ttfk
r�tj
}YnX|j	||�WdQXdS)N�TMPDIR�TEMP�TMP)rprqrr)rprqrr)r�EnvironmentVarGuardr�getenvrr"r>ri�
ValueError�assertIn�getcwd�AttributeErrorra�curdir)r�envZenvname�dirnamermrrr�test_wanted_dirs�s 

	z)TestCandidateTempdirList.test_wanted_dirsN)rr-r.rdror|rrrrrh�s	rhc@s"eZdZdZdd�ZdS)�TestGetDefaultTempdirzTest _get_default_tempdir().c*s>tj��,��fdd�}tjtd|���tj�|jtj��g�dd��tjtd���>|j	t
��tj�WdQX|jtj��g�WdQXtj���fdd�}tjtd|��>|j	t
��tj�WdQX|jtj��g�WdQXWdQXWdQXdS)	Ncs�gS)Nrr)�our_temp_directoryrr�our_candidate_list�szKTestGetDefaultTempdir.test_no_files_left_behind.<locals>.our_candidate_listric_s
t��dS)N)ra)�args�kwargsrrr�
raise_OSError�szFTestGetDefaultTempdir.test_no_files_left_behind.<locals>.raise_OSError�opencs�||�}�|_|S)N)rY)r�r��fp)r�r�rr�
bad_writer�s	zCTestGetDefaultTempdir.test_no_files_left_behind.<locals>.bad_writer)r>r<r�	swap_attrZ_get_default_tempdirr!r�listdir�io�assertRaises�FileNotFoundErrorr�)rrr�r)r�r~r�r�test_no_files_left_behind�s"

	z/TestGetDefaultTempdir.test_no_files_left_behindN)rr-r.rdr�rrrrr}�sr}c@s.eZdZdZdd�Zdd�ZdS)�TestGetCandidateNamesz0Test the internal function _get_candidate_names.cCs#tj�}|j|tj�dS)N)r>�_get_candidate_namesrkrF)r�objrrr�test_retval�sz!TestGetCandidateNames.test_retvalcCs/tj�}tj�}|j||k�dS)N)r>r�r#)r�a�brrr�test_same_thing�sz%TestGetCandidateNames.test_same_thingN)rr-r.rdr�r�rrrrr��sr�ccsFtj�}z%tjtd|��
dVWdQXWdtj|�XdS)Nr9)r>r5rr�Zrmtree)r'rrr�_inside_empty_temp_dirs
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_namess	r�c@s4eZdZdd�Zdd�Zdd�ZdS)�TestBadTempdircCs�t���tjtj�j}}|tjtjBtjBM}tj	tj|�zhtj
tjtj�r}|jd�n|j
t��|j�WdQX|jtjtj�g�Wdtj	tj|�XWdQXdS)Nz!can't set the directory read-only)r�rrr>r9�st_mode�S_IWUSR�S_IWGRP�S_IWOTH�chmod�access�W_OKZskipTestr��PermissionError�	make_tempr!r�)rZoldmode�moderrr�test_read_only_directorys
 z'TestBadTempdir.test_read_only_directorycCsht��Ytjjtjd�}tjtd|��%|jt	��|j
�WdQXWdQXWdQXdS)N�nonexistentr9)r�rr�joinr>r9rr�r�r�r�)rr9rrr�test_nonexisting_directory%s

z)TestBadTempdir.test_nonexisting_directorycCs�t��rtjjtjd�}t|d�j�tj	td|��+|j
ttf��|j
�WdQXWdQXWdQXdS)N�file�wbr9)r�rrr�r>r9r�rXrr�r��NotADirectoryErrorr�r�)rr9rrr�test_non_directory,s
z!TestBadTempdir.test_non_directoryN)rr-r.r�r�r�rrrrr�sr�c@s�eZdZdZGdd�d�Zdddddd�Zd	d
�Zdd�Zd
d�Ze	j
ed�dd��Ze	j
e
d�dd��Ze	j
ed�dd��Zdd�Zdd�Zdd�ZdS)�TestMkstempInnerz*Test the internal function _mkstemp_inner.c@sXeZdZejZejZej	Z
ejZdd�Z
dd�Zdd�ZdS)zTestMkstempInner.mkstempedcCsC|r|j}n	|j}tj||||�\|_|_dS)N)�_bflags�_tflagsr>�_mkstemp_inner�fdr&)rr'r(r)�bin�flagsrrr�__init__>s	z#TestMkstempInner.mkstemped.__init__cCstj|j|�dS)N)rrYr�)rrlrrrrYDsz TestMkstempInner.mkstemped.writecCs$|j|j�|j|j�dS)N)�_closer��_unlinkr&)rrrr�__del__Gsz"TestMkstempInner.mkstemped.__del__N)rr-r.r>�_bin_openflagsr��_text_openflagsr�rrXr��unlinkr�r�rYr�rrrr�	mkstemped8s				r�NrJrcCsP|dkrtj�}n|j||||�}|j|j|||�|S)N)r>r8r�r,r&)rr'r(r)r�r�rrr�	do_createKs
zTestMkstempInner.do_createcCs�|j�jd�|jdd�jd�|jdd�jd�|jdddd�jd�|jdddd�jd�dS)Nsblatr(r�r)r��aaz.txt)r�rY)rrrr�
test_basicSs
zTestMkstempInner.test_basiccCs=ttt��}x$|D]}|jdd�||<qWdS)Nr(r�)�listrNrOr�)r�extantrQrrr�test_basic_many[s
z TestMkstempInner.test_basic_manycCs>tj�}z|jd|�jd�Wdtj|�XdS)Nr'sblat)r>r5r�rYr�rmdir)rr'rrr�test_choose_directoryasz&TestMkstempInner.test_choose_directoryzos.stat not availablecCsj|j�}tjtj|j�j�}d}tjdkrV|d?}|d}n|j||�dS)	Ni��win32�r��@�	�I)	r�r�S_IMODErr&r��sys�platformr!)rr�r�rB�userrrr�test_file_modeis

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�r!r�get_inheritabler��__file__�	NameErrorr��argvrr�r{r"r��
executabler�P_WAITrj)rr�r�r��meZtesterZ	decoratedZretvalrrr�test_noinheritws(		


$	

	$zTestMkstempInner.test_noinheritztext mode not availablecCsh|jdd�}|jd�|jd�tj|jdtj�|jtj|jd�d�dS)Nr�rsblatsextra
rSsblat)r�rYr�lseekr��SEEK_SETr!r\)r�frrr�
test_textmode�s


zTestMkstempInner.test_textmodecCs"tjtj�tjdtj�S)NrJ)r>r�r8r:r�)rrrrr��szTestMkstempInner.make_tempcCs�t���tddd��o|j�\}}tj|�|j|jd��|j�\}}tj|�|j|jd��WdQXWdQXdS)N�aaa�bbb)r�r�r�rrXr#�endswith)rZfd1Zname1Zfd2Zname2rrr�!test_collision_with_existing_file�s


z2TestMkstempInner.test_collision_with_existing_filecCs�t��utddd��\tj�}|j|jd��|j�\}}tj|�|j|jd��WdQXWdQXdS)Nr�r�)	r�r�r>r5r#r�r�rrX)rr'r�r&rrr�&test_collision_with_existing_directory�s

z7TestMkstempInner.test_collision_with_existing_directory)rr-r.rdr�r�r�r�r�rerf�has_statr��
has_spawnlr��has_textmoder�r�r�r�rrrrr�5s&
r�c@s.eZdZdZdd�Zdd�ZdS)�TestGetTempPrefixzTest gettempprefix().cCs9tj�}|j|t�|jt|�dk�dS)Nr)r>r7rkrlr#r )r�prrr�test_sane_template�sz$TestGetTempPrefix.test_sane_templatecCs�tj�d}tjdd�}zOtjj||�}tj|tjtjB�}tj	|�tj
|�Wdtj|�XdS)Nz
xxxxxx.xxx�prefixrJ)r>r7r5rrr�r��O_RDWR�O_CREATrXr�r�)rr��dr�rrr�test_usable_template�s
z&TestGetTempPrefix.test_usable_templateN)rr-r.rdr�r�rrrrr��sr�c@sFeZdZdZdd�Zdd�Zdd�Zdd	�Zd
S)�TestGetTempDirzTest gettempdir().cCs_tj�}|jtjj|�p0|tjkd|�|jtjj|�d|�dS)Nz%s is not an absolute pathz%s is not a directory)r>r8r#rr�isabsry�isdir)rr'rrr�test_directory_exists�s
$z$TestGetTempDir.test_directory_existscCs'tj�}|jd�|j�dS)Nsblat)r>r2rYrX)rr�rrr�test_directory_writable�s
z&TestGetTempDir.test_directory_writablecCs/tj�}tj�}|j||k�dS)N)r>r8r#)rr�r�rrrr��szTestGetTempDir.test_same_thingcCsytjd�}tjd}t_z9tj��'}||d<|jtj�|�WdQXWd|t_tj|�XdS)Nz-Temprp)r>r5r9rrsr!r8r�)rZcase_sensitive_tempdirZ_tempdirrzrrr�test_case_sensitives
 	z"TestGetTempDir.test_case_sensitiveN)rr-r.rdr�r�r�r�rrrrr��s
	
r�c@sCeZdZdZddddd�Zdd�Zdd	�ZdS)
�TestMkstempzTest mkstemp().NrJc	Cs�|dkrtj�}ntjd|d|d|�\}}tjj|�\}}tjj|�}|j||d||f�z|j||||�Wdtj	|�tj
|�XdS)Nr'r��suffixz+Directory '%s' incorrectly returned as '%s')r>r8r4rrrr"r!r,rXr�)	rr'r(r)r�r&r*r+Zadirrrrr�s$
zTestMkstemp.do_createcCsj|j�|jdd�|jdd�|jdddd�|jdddd�|jdd�dS)	Nr(r�r)r�r�z.txtr'�.)r�)rrrrr�!s
zTestMkstemp.test_basiccCs5tj�}z|jd|�Wdtj|�XdS)Nr')r>r5r�rr�)rr'rrrr�*sz!TestMkstemp.test_choose_directory)rr-r.rdr�r�r�rrrrr�s	r�c@s�eZdZdZdd�Zddddd�Zdd	�Zd
d�Zdd
�Ze	j
ed�dd��Zdd�Z
dd�ZdS)�TestMkdtempzTest mkdtemp().cCs
tj�S)N)r>r5)rrrrr�6szTestMkdtemp.make_tempNrJcCsv|dkrtj�}ntjd|d|d|�}y|j||||�|SWntj|��YnXdS)Nr'r�r�)r>r8r5r,rr�)rr'r(r)r&rrrr�9s
zTestMkdtemp.do_createcCs�tj|j��tj|jdd��tj|jdd��tj|jdddd��tj|jdddd��dS)Nr(r�r)r�r�z.txt)rr�r�)rrrrr�Es
zTestMkdtemp.test_basicc
Csuttt��}z+x$|D]}|jdd�||<qWWdx-|D]%}t|t�rGtj|�qGqGWXdS)Nr(r�)r�rNrOr��
isinstancerlrr�)rr�rQrrrr�Ms

zTestMkdtemp.test_basic_manycCs>tj�}ztj|jd|��Wdtj|�XdS)Nr')r>r5rr�r�)rr'rrrr�Xsz!TestMkdtemp.test_choose_directoryzos.stat not availablecCs�|j�}zetjtj|�j�}|dM}d}tjdkr`|d?}|d	}n|j||�Wdtj|�XdS)
Ni�i�r�r�rr�r�r�r�)	r�rr�rr�r�r�r!r�)rr'r�rBr�rrr�	test_mode`s


zTestMkdtemp.test_modecCs�t��utddd��\tjdd�}|j�|j|jjd��tj�}|j|jd��WdQXWdQXdS)Nr�r��deleteF)	r�r�r>r2rXr#r&r�r5)rr�r'rrrr�rs

z-TestMkdtemp.test_collision_with_existing_filecCsqt��btddd��Itj�}|j|jd��tj�}|j|jd��WdQXWdQXdS)Nr�r�)r�r�r>r5r#r�)rZdir1Zdir2rrrr�}s
z2TestMkdtemp.test_collision_with_existing_directory)rr-r.rdr�r�r�r�r�rerfr�r�r�r�rrrrr�3sr�csweZdZdZ�fdd�Z�fdd�ZGdd�d�Zddd	d
�Zdd�Zd
d�Z	�S)�
TestMktempzTest mktemp().cs tj�|_t�j�dS)N)r>r5r'rHr)r)rIrrr�szTestMktemp.setUpcs6|jr%tj|j�d|_nt�j�dS)N)r'rr�rHr)r)rIrrr�s	zTestMktemp.tearDownc@s:eZdZejZejZdd�Z	dd�Z
dS)zTestMktemp.mktempedcCsGtjd|d|d|�|_tjtj|j|jd��dS)Nr'r�r�i�)r>rr&rrXr�r�)rr'r(r)rrrr��s!zTestMktemp.mktemped.__init__cCs|j|j�dS)N)r�r&)rrrrr��szTestMktemp.mktemped.__del__N)rr-r.rr�r�r>r�r�r�r�rrrr�mktemped�s		r�rJcCs8|j|j||�}|j|j|j||�|S)N)r�r'r,r&)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�rNrOr�)rr�rQrrrrR�s
zTestMktemp.test_many)
rr-r.rdrrr�r�r�rRrr)rIrr��s
r�c@s�eZdZdZdddddd�Zdd�Zd	d
�Zdd�Zd
d�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dS)�TestNamedTemporaryFilezTest NamedTemporaryFile().NrJTc	Cs\|dkrtj�}ntjd|d|d|d|�}|j|j|||�|S)Nr'r�r�r�)r>r8r2r,r&)rr'r(r)r�r�rrrr��s	z TestNamedTemporaryFile.do_createcCsZ|j�|jdd�|jdd�|jdddd�|jdddd�dS)Nr(r�r)r�r�z.txt)r�)rrrrr��s

z!TestNamedTemporaryFile.test_basiccCss|j�}tj|�}|j}|j}~|d�~|d�~tjdd�ro|j|��ndS)NsfoosbarZcpythonT)r��weakref�refrYrZcheck_impl_detail�assertIsNone)rr��wrrYZwrite2rrr�test_method_lookup�s		

z)TestNamedTemporaryFile.test_method_lookupcssdddg��fdd�}x1t|��D] \}}|j|�|�q1W|j|t��d�dS)Nsspam
seggs
sbeans
cs9tjdd�}|jdj���|jd�|S)Nr�zw+b�r)r>r2rYr��seek)r�)�linesrr�	make_file�s
z3TestNamedTemporaryFile.test_iter.<locals>.make_filer)�	enumerater!r )rrrQ�lr)rr�	test_iter�s
z TestNamedTemporaryFile.test_itercCs6tj�}|jtjj|j�d|j�dS)Nz$NamedTemporaryFile %s does not exist)r>r2r#rr�existsr&)rr�rrr�test_creates_named�sz)TestNamedTemporaryFile.test_creates_namedcCsttj�}zStjd|�}|jd�|j�|jtjj|j	�d|j	�Wdtj
|�XdS)Nr'sblatz(NamedTemporaryFile %s exists after close)r>r5r2rYrXrjrrrr&r�)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
|�ntj|�XdS)Nr'r�Fsblatz)NamedTemporaryFile %s missing after close)r>r5r2r&rYrXr#rrrr�r�)rr'�tmpr�rrr�test_dis_del_on_closes	

z,TestNamedTemporaryFile.test_dis_del_on_closecCs;tj�}|jd�|j�|j�|j�dS)Nsabc
)r>r2rYrX)rr�rrr�test_multiple_closes



z*TestNamedTemporaryFile.test_multiple_closec
sstj��#�|jtjj�j��WdQX|jtjj�j���fdd�}|jt	|�dS)Ncs��WdQXdS)Nrr)r�rr�
use_closed%sz?TestNamedTemporaryFile.test_context_manager.<locals>.use_closed)
r>r2r#rrrr&rjr�ru)rrr)r�r�test_context_manager s
"z+TestNamedTemporaryFile.test_context_managercs�g�tj���fdd�}tjdd|��Jtjddt��.|jttj�|jt	��d�WdQXWdQXdS)Ncs�j|��|�dS)N)r@)r�)�closed�os_closerrrX.s
z5TestNamedTemporaryFile.test_no_leak_fd.<locals>.closezos.close�side_effectzio.openr)
rrXr�patchrur�r>r2r!r )rrXr)rrr�test_no_leak_fd*s	z&TestNamedTemporaryFile.test_no_leak_fd)rr-r.rdr�r�rrr	r
rr
rrrrrrr��s


r�c@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().rNrJc	CsC|dkrtj�}ntjd|d|d|d|�}|S)N�max_sizer'r�r�)r>r8r;)rrr'r(r)r�rrrr�=s$z"TestSpooledTemporaryFile.do_createcCsN|j�}|j|j�|jdddddd�}|j|j�dS)Nrr	r(r�r)z.txt)r�rj�_rolled)rr�rrrr�Es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)r>r5r;rjrrYr#r&rXr�rlrrrr�)rr'r��filenamerrrr
Ls	
$z*TestSpooledTemporaryFile.test_del_on_closecCsj|jdd�}|j|j�x1td�D]#}|jdd�|jd�q/W|j|j�dS)Nr�rr�xrSsxxxxxxxxxxxxxxxxxxxx)r�rjrrNrrY)rr�rQrrr�test_rewrite_small[sz+TestSpooledTemporaryFile.test_rewrite_smallcCs}|jdd�}|j|j�|jd�|j|j�|jd�|j|j�|jd�|j|j�dS)NrrrrSrsxxxxxxxxxxxxxxxxxxxxs
xxxxxxxxxx)r�rjrrYr#)rr�rrr�test_write_sequentialds


z.TestSpooledTemporaryFile.test_write_sequentialcCsF|j�}|jd�|jd�|j�}|j|d�dS)Nr�y�zrsxyz)rrr )r��
writelinesrr\r!)rr�Zbufrrr�test_writelinesps


z(TestSpooledTemporaryFile.test_writelinescCsP|jdd�}|jd
�|j|j�|jd�|j|j�dS)Nr�#rrSrr�xxxxxxxxxxxxxxxxxxxx�
xxxxxxxxxx�xxxxx)r$r%r&)r�r!rjrrYr#)rr�rrr�test_writelines_sequentialxs


z3TestSpooledTemporaryFile.test_writelines_sequentialcCsc|jdd�}|j|j�|jdd�|j|j�|jd�|j|j�dS)Nrrr	rr)r�rjrrrYr#)rr�rrr�test_sparse�s
z$TestSpooledTemporaryFile.test_sparsecCsO|jdd�}|j|j�|j|j�dk�|j|j�dS)Nrrr)r�rjrr#�fileno)rr�rrr�test_fileno�sz$TestSpooledTemporaryFile.test_filenocCsKtj�}|jd�|j|j�|j�|j�|j�dS)Nsabc
)r>r;rYrjrrX)rr�rrr�#test_multiple_close_before_rollover�s


z<TestSpooledTemporaryFile.test_multiple_close_before_rollovercCsQtjdd�}|jd�|j|j�|j�|j�|j�dS)Nrrsabc
)r>r;rYr#rrX)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)Nrr�ar#�br�F)r�r\rYrr!)rr�r\rYrrrr�test_bound_methods�s			
z+TestSpooledTemporaryFile.test_bound_methodscCs
tjdd�}|jd�|j|j�|j|jd�|j|j�|j	t
��|jWdQX|j	t
��|jWdQX|jd�|j
|j�|j|jd�|j|j�|j	t
��|jWdQX|j	t
��|jWdQXdS)Nrrrzw+bzrb+s
xxxxxxxxxx)r>r;rYrjrr!r�r�r&r�rx�newlines�encodingr#�assertIsNotNone)rr�rrr�test_properties�s"




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+rrzabc
rzdef
zabc
def
zxyzzy
zabc
def
xyzzy
zfoobar
zabc
def
xyzzy
foobar
)r>r;rYrr!r\rjrr�r�r&r1r2r#r3r�linesep)rr�rrr�test_text_mode�s.







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+rr�newlinerJr2zutf-8uΛ
ruΛrSz
u(ΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛu(ΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛΛ)r>r;rYrr!r\rjrr�r�r&r1r2r#r3)rr�rrr�test_text_newline_and_encoding�s$


z7TestSpooledTemporaryFile.test_text_newline_and_encodingc	sqtjdd��'�|j�j�|j�j�WdQX|j�j��fdd�}|jt|�dS)Nrrcs��WdQXdS)Nrr)r�rrr�szQTestSpooledTemporaryFile.test_context_manager_before_rollover.<locals>.use_closed)r>r;rjrrr#r�ru)rrr)r�r�$test_context_manager_before_rollover�sz=TestSpooledTemporaryFile.test_context_manager_before_rolloverc	s�tjdd��N�|j�j��jd��j�|j�j�|j�j�WdQX|j�j��fdd�}|jt	|�dS)Nrrsabc
cs��WdQXdS)Nrr)r�rrrszQTestSpooledTemporaryFile.test_context_manager_during_rollover.<locals>.use_closed)
r>r;rjrrY�flushr#rr�ru)rrr)r�r�$test_context_manager_during_rollovers

z=TestSpooledTemporaryFile.test_context_manager_during_rolloverc	s�tjdd���jd��j�|j�j���|j�j�WdQX|j�j��fdd�}|jt	|�dS)Nrrsabc
cs��WdQXdS)Nrr)r�rrrszPTestSpooledTemporaryFile.test_context_manager_after_rollover.<locals>.use_closed)
r>r;rYr:r#rrjrr�ru)rrr)r�r�#test_context_manager_after_rollovers

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�ndS)	Nrrsabcdefg
rr�sabcdrS)r>r;rYr�truncaterjrr!Z_file�getvaluer#r�r�fstatr)�st_size)rr�rrr�!test_truncate_with_size_parameters"






z:TestSpooledTemporaryFile.test_truncate_with_size_parameter)rr-r.rdr�r�r
rrr"r'r(r*r+r,r0r4r6r8r9r;r<rBrrrrr:s(		
		rc@sReZdZdZdd�Zdd�Zdd�Zdd	�Zd
d�ZdS)
�TestTemporaryFilezTest TemporaryFile().cCstj�dS)N)r>r3)rrrrr�;szTestTemporaryFile.test_basicc	Csetj�}tjd|�}|jd�ytj|�Wn"|j�tj|��YnXdS)Nr'sblat)r>r5r3rYrr�rX)rr'r�rrr�test_has_no_name@s


z"TestTemporaryFile.test_has_no_namecCs;tj�}|jd�|j�|j�|j�dS)Nsabc
)r>r3rYrX)rr�rrrr
Ps



z%TestTemporaryFile.test_multiple_closecsV�fdd�}|dd�|dd�|dddd	�|d
ddd�dS)
Nc
sOtj||��7}|j|�|jd��j||j��WdQXdS)Nr)r>r3rYrr!r\)�inputr�r�Zfileobj)rrr�	roundtrip[s

z;TestTemporaryFile.test_mode_and_encoding.<locals>.roundtrips1234zw+bzabdc
zw+uΛr2zutf-16zfoo
r7rJr)rrFr)rr�test_mode_and_encodingYs


z(TestTemporaryFile.test_mode_and_encodingcs�g�tj���fdd�}tjdd|��Jtjddt��.|jttj�|jt	��d�WdQXWdQXdS)Ncs�j|��|�dS)N)r@)r�)rrrrrXjs
z0TestTemporaryFile.test_no_leak_fd.<locals>.closezos.closerzio.openr)
rrXrrrur�r>r3r!r )rrXr)rrrrfs	z!TestTemporaryFile.test_no_leak_fdN)	rr-r.rdr�rDr
rGrrrrrrC8s	
rCc@s4eZdZdd�Zdd�Zdd�ZdS)�
NulledModulescGs3dd�|D�|_dd�|jD�|_dS)NcSsg|]}|j�qSr)r?)�.0�modrrr�
<listcomp>xs	z*NulledModules.__init__.<locals>.<listcomp>cSsg|]}|j��qSr)�copy)rIr�rrrrKys	)�refs�contents)r�modulesrrrr�wszNulledModules.__init__cCs3x,|jD]!}x|D]}d||<qWq
WdS)N)rM)rr�rCrrrr{s
zNulledModules.__enter__cGsAx:t|j|j�D]#\}}|j�|j|�qWdS)N)�ziprMrN�clear�update)r�exc_infor�rnrrrr�s"
zNulledModules.__exit__N)rr-r.r�rrrrrrrHvsrHc@s�eZdZdZdddddd�Zdd�Zd	d
�Zejdd��Z	ej
d
d��Zdd�Zdd�Z
dd�Zdd�Zdd�ZdS)�TestTemporaryDirectoryzTest TemporaryDirectory().NrJrc	Cs�|dkrtj�}ntjd|d|d|�}|j|j|||�|r�|j|j|||d�}d|_nttjj	|jd�d��}|j
d�WdQX|S)Nr'r�r�rztest.txtr�sHello world!)r>r8r<r,r&r�r�rrr�rY)rr'r(r)�recurser�d1r�rrrr��s$z TestTemporaryDirectory.do_createcCsZtj��}WdQX|jt��}tjd|�WdQX|j|jjtj�dS)Nr')r>r<r�r�r!Z	exception�errno�ENOENT)rr��cmrrr�test_mkdtemp_failure�s
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)r>r5r�r#rrrr&�cleanuprjr�)rr'r�rrr�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)NrUrZfooz*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�symlinkr&rr�r[rjrr#r!r�)rrVZd2rrr�(test_cleanup_with_symlink_to_a_directory�s%
z?TestTemporaryDirectory.test_cleanup_with_symlink_to_a_directorycCsctj�}zB|jd|�}|j}~|jtjj|�d|�Wdtj|�XdS)Nr'z*TemporaryDirectory %s exists after __del__)	r>r5r�r&rjrrrr�)rr'r�r&rrr�test_del_on_collection�s	z-TestTemporaryDirectory.test_del_on_collectionc
Cs�|j���}x�dD]�}djd|d	|�}tjd
|�\}}}|j�j�}|jtjj	|�d|�|jdd
�}|j
d|�|jd|�qWWdQXdS)N�builtinsr�shutilr�r>ra�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'rJz-cz*TemporaryDirectory %s exists after cleanupzutf-8�backslashreplacez
Exception z'ResourceWarning: Implicitly cleaning up)zbuiltinszosrazsysztempfilezwarnings)r��formatr�assert_python_okr]�striprjrrrrPrv)rr'rJ�code�rc�out�err�tmp_namerrr�test_del_on_shutdown�s
z+TestTemporaryDirectory.test_del_on_shutdowncCs�|j���}djd|�}tjd|�\}}}|j�j�}|jtjj	|�d|�|jdd�}|j
d|�|jd|�WdQXdS)	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-8rbz
Exception z'ResourceWarning: Implicitly cleaning up)r�rcrrdr]rerjrrrrPrv)rr'rfrgrhrirjrrr�test_exit_on_shutdown�s
z,TestTemporaryDirectory.test_exit_on_shutdowncCs�|j���}|jd|dd�}|j}tjdtfdd��%tjddt�~tj�WdQX|jt	j
j|�d	|�WdQXdS)
Nr'rU�Z
Implicitly�quietF�alwaysrz*TemporaryDirectory %s exists after __del__)r�r&rr�ResourceWarningrrZ
gc_collectrjrrr)rr'r�r&rrr�test_warnings_on_cleanups	z/TestTemporaryDirectory.test_warnings_on_cleanupcCs.|j�}|j�|j�|j�dS)N)r�r[)rr�rrrr
s

z*TestTemporaryDirectory.test_multiple_closec
Csd|j�}|�3}|jtjj|��|j||j�WdQX|jtjj|��dS)N)r�r#rrrr!r&rj)rr�r&rrrrs
	z+TestTemporaryDirectory.test_context_manager)rr-r.rdr�rZr\rZskip_unless_symlinkr^Zcpython_onlyr_rkrlrqr
rrrrrrT�s



 rTcCstjt�dS)N)rZrun_unittestrrrrr�	test_main$srr�__main__)3r>rWr�rr_r�r/r�
contextlibr�rerZtestrrrgrr�r�r�r�r�r��
startswithrOZTestCaser
r1rErhr}r��contextmanagerr�r�r�r�r�r�r�r�r�r�rr2r3rCrHrTrrrrrrr�<module>sZ		!A*&
 �,$U<v�>�