o
    HXh                    @   sJ  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Zd dlZd dlmZ d dlZd dlmZ d dlmZ d dlmZ d dlmZ e je jkZeedZejdrjd	Znd
ZG dd dejZG dd dejZG dd deZ G dd deZ!G dd deZ"G dd deZ#G dd deZ$ej%dd Z&dd Z'G dd dZ(G dd  d e(eZ)G d!d" d"eZ*G d#d$ d$eZ+G d%d& d&eZ,G d'd( d(e(eZ-G d)d* d*eZ.G d+d, d,eZ/G d-d. d.eZ0e j1e j2ur	G d/d0 d0eZ3G d1d2 d2Z4G d3d4 d4eZ5e6d5kr#e7  dS dS )6    N)mock)support)	os_helper)script_helper)warnings_helperspawnlZopenbsd0   d   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )TestLowLevelInternalsc                 C   s:   |  ttd |  ttd |  ttd  d S N     )assertIsstrtempfile_infer_return_typebytesself r   </opt/python-3.10.19/usr/lib/python3.10/test/test_tempfile.pytest_infer_return_type_singles'   s   z4TestLowLevelInternals.test_infer_return_type_singlesc                 C   s   |  ttdd |  ttdd | t tdd W d    n1 s*w   Y  | t tdd W d    d S 1 sFw   Y  d S r   r   r   r   r   r   assertRaises	TypeErrorr   r   r   r    test_infer_return_type_multiples,   s   "z6TestLowLevelInternals.test_infer_return_type_multiplesc                 C   s   |  ttd d |  ttdd  |  ttd d  |  ttdd  |  ttd d | t tdd d W d    n1 sIw   Y  | t tdd d W d    d S 1 sfw   Y  d S r   r   r   r   r   r   )test_infer_return_type_multiples_and_none4   s   "z?TestLowLevelInternals.test_infer_return_type_multiples_and_nonec                 C   s   |  tttd d S )N/)r   r   r   r   pathlibPathr   r   r   r   test_infer_return_type_pathlib?   s   z4TestLowLevelInternals.test_infer_return_type_pathlibc                 C   s  G dd d}|  tt|d |  tt|d |  ttd|d |  ttd|d |  ttd |d |  ttd |d | t td|d W d    n1 sew   Y  | t td|d W d    d S 1 sw   Y  d S )Nc                   @   s   e Zd Zdd Zdd ZdS )zCTestLowLevelInternals.test_infer_return_type_pathlike.<locals>.Pathc                 S   s
   || _ d S Npath)r   r#   r   r   r   __init__D   s   
zLTestLowLevelInternals.test_infer_return_type_pathlike.<locals>.Path.__init__c                 S   s   | j S r!   r"   r   r   r   r   
__fspath__G      zNTestLowLevelInternals.test_infer_return_type_pathlike.<locals>.Path.__fspath__N)__name__
__module____qualname__r$   r%   r   r   r   r   r   C   s    r   r      /r   r   r   )r   r   r   r   r   test_infer_return_type_pathlikeB   s   "z5TestLowLevelInternals.test_infer_return_type_pathlikeN)r'   r(   r)   r   r   r   r    r+   r   r   r   r   r
   &   s    r
   c                   @   s8   e Zd ZedZedZdd Zdd Zdd Z	d	S )
BaseTestCasez^[a-z0-9_-]{8}$s   ^[a-z0-9_-]{8}$c                 C   s*   t  | _| j  tjdtdtd d S )Nignoremktemp)categorymessagemodule)r   check_warnings_warnings_manager	__enter__warningsfilterwarningsRuntimeWarningr'   r   r   r   r   setUp]   s
   


zBaseTestCase.setUpc                 C   s   | j d d d  d S r!   )r3   __exit__r   r   r   r   tearDownc   s   zBaseTestCase.tearDownc           
      C   s  t j|\}}|d t| }|t|t| d  }|d ur7| t|t|tu s1t|t jr3tnt	d |d urL| t|t|tu rHtnt	d |d ura| t|t|tu r]tnt	d |||fdkrq| t|td | 
t j|t j|d||f  | 
||d||f  | 
||d||f  |t|t|t|  }t|tr| jn| j}	| |	|d||	jf  d S )Nzunexpected return typeNNNzdefault return type must be strzfile %r not in directory %rzfile %r does not begin with %rzfile %r does not end with %rz$random characters %r do not match %r)osr#   splitlenr   typer   
isinstancePathLiker   assertEqualabspath	str_checkb_check
assertTruematchpattern)
r   namedirpresufndirnbaseZnpreZnsufcheckr   r   r   	nameCheckf   sJ   


zBaseTestCase.nameCheckN)
r'   r(   r)   recompilerD   rE   r8   r:   rP   r   r   r   r   r,   X   s    

r,   c                   @   s   e Zd Zdd ZdS )TestExportsc                 C   sp   t j}ddddddddddddddd}g }|D ]}|d dkr)||vr)|| q| t|dkd|  d S )N   )NamedTemporaryFileTemporaryFilemkstempmkdtempr.   TMP_MAXgettempprefixgettempprefixb
gettempdirgettempdirbtempdirtemplateSpooledTemporaryFileTemporaryDirectoryr   _zunexpected keys: %s)r   __dict__appendrF   r>   )r   dictexpectedZunexpkeyr   r   r   test_exports   s0   
zTestExports.test_exportsN)r'   r(   r)   rh   r   r   r   r   rS      s    rS   c                       sR   e Zd ZdZ fddZdd Zdd Zdd	 Ze	e
ed
ddd Z  ZS )TestRandomNameSequencez6Test the internal iterator object _RandomNameSequence.c                       t  | _t   d S r!   )r   _RandomNameSequencersuperr8   r   	__class__r   r   r8         
zTestRandomNameSequence.setUpc                 C   s   t | j}| |ddd d S Nr   )nextrl   rP   )r   sr   r   r   test_get_eight_char_str   s   
z.TestRandomNameSequence.test_get_eight_char_strc                 C   sH   i }| j }ttD ]}t|}| |ddd | || d||< q	d S )Nr   rT   )rl   range
TEST_FILESrr   rP   assertNotIn)r   re   rl   irs   r   r   r   	test_many   s   
z TestRandomNameSequence.test_manyc                 C   s.   d}| j }|D ]}|d7 }|dkr d S qd S )Nr   rT      )rl   )r   rx   rl   rs   r   r   r   supports_iter   s   z$TestRandomNameSequence.supports_iterforkz!os.fork is required for this testc              
   C   s   t  \}}d }zFt  }|s*t | t |t| jd t | t d t| j}t 	|t
|d}W |rDtj|dd t | t | n|rXtj|dd t | t | w | || d S )Nasciir   )exitcode)r<   piper|   closewriterr   rl   encode_exitreadr>   decoder   Zwait_processassertNotEqual)r   Zread_fdZwrite_fdpidZparent_valueZchild_valuer   r   r   test_process_awareness   s(   





z-TestRandomNameSequence.test_process_awareness)r'   r(   r)   __doc__r8   rt   ry   r{   unittest
skipUnlesshasattrr<   r   __classcell__r   r   rn   r   ri      s    
ri   c                   @       e Zd ZdZdd Zdd ZdS )TestCandidateTempdirListz3Test the internal function _candidate_tempdir_list.c                 C   s4   t  }| t|dk |D ]}| |t qd S Nr   )r   _candidate_tempdir_listassertFalser>   assertIsInstancer   )r   candcr   r   r   test_nonempty_list   s
   z+TestCandidateTempdirList.test_nonempty_listc              
   C   s   t  P}dD ]}t|}|stj|||< qt }dD ]}t|}|s*t| 	|| qzt
 }W n ttfyE   tj}Y nw | 	|| W d    d S 1 sWw   Y  d S )N)TMPDIRTEMPTMP)r   EnvironmentVarGuardr<   getenvr#   rC   r   r   
ValueErrorassertIngetcwdAttributeErrorOSErrorcurdir)r   envenvnamedirnamer   r   r   r   test_wanted_dirs   s$   



"z)TestCandidateTempdirList.test_wanted_dirsN)r'   r(   r)   r   r   r   r   r   r   r   r      s    	r   c                   @   s   e Zd ZdZdd ZdS )TestGetDefaultTempdirzTest _get_default_tempdir().c                    s~  t    fdd}tt d| t   | t g  dd }ttd|* | t	 t   W d    n1 s@w   Y  | t g  W d    n1 sXw   Y  ttd|* | t	 t   W d    n1 syw   Y  | t g  W d    n1 sw   Y  W d    n1 sw   Y  W d    d S W d    d S 1 sw   Y  d S )Nc                      s    gS r!   r   r   Zour_temp_directoryr   r   our_candidate_list  r&   zKTestGetDefaultTempdir.test_no_files_left_behind.<locals>.our_candidate_listr   c                  _   s   t  r!   )r   )argskwargsr   r   r   raise_OSError#  r&   zFTestGetDefaultTempdir.test_no_files_left_behind.<locals>.raise_OSErroropenr   )
r   ra   r   	swap_attr_get_default_tempdirrB   r<   listdirr   FileNotFoundError)r   r   r   r   r   r   test_no_files_left_behind  s2   


"z/TestGetDefaultTempdir.test_no_files_left_behindN)r'   r(   r)   r   r   r   r   r   r   r     s    r   c                   @   r   )TestGetCandidateNamesz0Test the internal function _get_candidate_names.c                 C   s   t  }| |t j d S r!   )r   _get_candidate_namesr   rk   )r   objr   r   r   test_retval6  s   z!TestGetCandidateNames.test_retvalc                 C   s"   t  }t  }| ||u  d S r!   )r   r   rF   )r   abr   r   r   test_same_thing;  s   z%TestGetCandidateNames.test_same_thingN)r'   r(   r)   r   r   r   r   r   r   r   r   3  s    r   c               	   c   sl    t  } z*tt d|  d V  W d    n1 sw   Y  W t|  d S W t|  d S t|  w )Nr^   )r   rX   r   r   r   rmtreerJ   r   r   r   _inside_empty_temp_dirC  s   r   c                     s   t td fddS )Nr   c                      s   t  S r!   )iterr   namesr   r   <lambda>P  s    z'_mock_candidate_names.<locals>.<lambda>)r   r   r   r   r   r   r   _mock_candidate_namesM  s   
r   c                   @   $   e Zd Zdd Zdd Zdd ZdS )TestBadTempdirc                 C   s   t  g ttjj }}|tjtjB tjB  M }t	tj| z9t
tjtjr.| d | t |   W d    n1 sBw   Y  | ttjg  W t	tj| nt	tj| w W d    d S 1 smw   Y  d S )Nz!can't set the directory read-only)r   r<   statr   r^   st_modeS_IWUSRS_IWGRPS_IWOTHchmodaccessW_OKskipTestr   PermissionError	make_temprB   r   )r   Zoldmodemoder   r   r   test_read_only_directoryU  s   

""z'TestBadTempdir.test_read_only_directoryc              
   C   s   t  I tjtjd}ttd|! | t	 | 
  W d    n1 s(w   Y  W d    n1 s7w   Y  W d    d S W d    d S 1 sOw   Y  d S )Nnonexistentr^   )r   r<   r#   joinr   r^   r   r   r   r   r   r   r^   r   r   r   test_nonexisting_directoryc  s   
"z)TestBadTempdir.test_nonexisting_directoryc              
   C   s   t  R tjtjd}t|d  t	td|# | 
ttf |   W d    n1 s1w   Y  W d    n1 s@w   Y  W d    d S W d    d S 1 sXw   Y  d S )Nfilewbr^   )r   r<   r#   r   r   r^   r   r   r   r   r   NotADirectoryErrorr   r   r   r   r   r   test_non_directoryj  s   
"z!TestBadTempdir.test_non_directoryN)r'   r(   r)   r   r   r   r   r   r   r   r   S  s    r   c                   @   s   e Zd ZdZG dd dZdddZdd	 Zd
d Zdd Zdd Z	dd Z
eeddd Zeeddd Zdd Zdd Zdd ZdS )TestMkstempInnerz*Test the internal function _mkstemp_inner.c                   @   s<   e Zd ZejZejZej	Z
ejZdd Zdd Zdd ZdS )zTestMkstempInner.mkstempedc                 C   s>   |r| j }n| j}t|||}t|||||\| _| _d S r!   )_bflags_tflagsr   r   _mkstemp_innerfdrI   )r   rJ   rK   rL   binflagsoutput_typer   r   r   r$   |  s   z#TestMkstempInner.mkstemped.__init__c                 C   s   t | j| d S r!   )r<   r   r   )r   r   r   r   r   r     s   z TestMkstempInner.mkstemped.writec                 C   s   |  | j | | j d S r!   )_closer   _unlinkrI   r   r   r   r   __del__  s   z"TestMkstempInner.mkstemped.__del__N)r'   r(   r)   r   _bin_openflagsr   _text_openflagsr   r<   r   r   unlinkr   r$   r   r   r   r   r   r   	mkstempedv  s    r   NrT   c                 C   sr   t |||}|d u r|tu rt  }nt  }|d u r| }|d u r&| }| ||||}| |j||| |S r!   )r   r   r   r\   r]   r   rP   rI   )r   rJ   rK   rL   r   r   r   r   r   r   	do_create  s   
zTestMkstempInner.do_createc                 C   s^   |   d | j ddd | j ddd | j dddd | j dddd d S )	N   blatr   rK   r   rL   rK   rL   aa.txt)r   r   r   r   r   r   
test_basic  
   zTestMkstempInner.test_basicc                 C   s2  t  }| j|ddd | j|ddd | j|ddd | j|dddd | j|dd	dd | t | jd
ddd W d    n1 sRw   Y  | t | j|d
dd W d    n1 sqw   Y  | t | j|dd
dd W d    d S 1 sw   Y  d S )Nr   rJ   rL   r      arJ   rK      brJ   rK   rL      aa   .txtr   )r   r]   r   r   r   r   )r   Zdir_br   r   r   test_basic_with_bytes_names  s   "z,TestMkstempInner.test_basic_with_bytes_namesc                 C   s*   t tt}|D ]
}| jdd||< qd S Nr   r   )listru   rv   r   r   Zextantrx   r   r   r   test_basic_many  s   z TestMkstempInner.test_basic_manyc              	   C   s`   t  }z!| j|dd | jt|dd W t  t	| d S t  t	| w Nr   r   )
r   rX   r   r   r   r   r   
gc_collectr<   rmdirr   rJ   r   r   r   test_choose_directory  s   z&TestMkstempInner.test_choose_directoryc                 C   sJ   |   }tt|jj}d}tjdkr|d? }|d }| || d S )N  win32   I   )	r   r   S_IMODEr<   rI   r   sysplatformrB   )r   r   r   rf   userr   r   r   test_file_mode  s   
zTestMkstempInner.test_file_modezos.spawnl not availablec                 C   s   t jrd}nd}|  }| t|jd d|j }zt}W n ty-   t	j
d }Y nw tjtjtj|d}t	jdkrLdt	j }d| }nt	j}ttjt	j||||}| |dk d	|   | |dkd
|  d S )NvqFz%dr   ztf_inherit_check.pyr  z"%s"z$child process caught fatal signal %dz child process reports failure %d)r   verboser   rB   r<   get_inheritabler   __file__	NameErrorr  argvr#   r   r   rC   r  
executabler   P_WAITr   )r   r
  r   r   meZtesterZ	decoratedZretvalr   r   r   test_noinherit  s.   




zTestMkstempInner.test_noinheritztext mode not availablec                 C   sL   | j dd}|d |d t|jdtj | t|jdd d S )Nr   )r   s   blats   extra
rz   r   )r   r   r<   lseekr   SEEK_SETrB   r   r   fr   r   r   test_textmode  s
   

zTestMkstempInner.test_textmodec                 C   s   t t  t  dt jtS rq   )r   r   r\   rZ   r   r   r   r   r   r   r     s   
zTestMkstempInner.make_tempc              	   C   s   t  M tddd. |  \}}t| | |d |  \}}t| | |d W d    n1 s;w   Y  W d    d S W d    d S 1 sSw   Y  d S Naaabbb)r   r   r   r<   r   rF   endswith)r   fd1Zname1fd2Zname2r   r   r   !test_collision_with_existing_file  s   


Pz2TestMkstempInner.test_collision_with_existing_filec              	   C   s   t  F tddd' t }| |d |  \}}t| | |d W d    n1 s4w   Y  W d    d S W d    d S 1 sLw   Y  d S r  )	r   r   r   rX   rF   r  r   r<   r   )r   rJ   r   rI   r   r   r   &test_collision_with_existing_directory  s   

Pz7TestMkstempInner.test_collision_with_existing_directory)NNNrT   )r'   r(   r)   r   r   r   r   r   r   r   r	  r   r   
has_spawnlr  has_textmoder  r   r   r!  r   r   r   r   r   s  s     




%

r   c                   @   r   )TestGetTempPrefixzTest gettempprefix().c                 C   sL   t  }| |t | t|d t  }| |t | t|d d S r   )r   rZ   r   r   ZassertGreaterr>   r[   r   )r   pZpbr   r   r   test_sane_template%  s   z$TestGetTempPrefix.test_sane_templatec              	   C   sl   t  d }t jdd}z#tj||}t|tjtjB }t	| t
| W t| d S t| w )Nz
xxxxxx.xxxr   prefix)r   rZ   rX   r<   r#   r   r   O_RDWRO_CREATr   r   r   )r   r%  dr   r   r   r   test_usable_template1  s   
z&TestGetTempPrefix.test_usable_templateN)r'   r(   r)   r   r&  r,  r   r   r   r   r$  "  s    r$  c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )TestGetTempDirzTest gettempdir().c                 C   sT   t  t  fD ]}| tj|p|tjkd|  | tj|d|  qd S )Nz%r is not an absolute pathz%r is not a directory)	r   r\   r]   rF   r<   r#   isabsr   isdirr   r+  r   r   r   test_directory_existsE  s   z$TestGetTempDir.test_directory_existsc                 C   s8   t  }|d W d    d S 1 sw   Y  d S )Nr   )r   rU   r   )r   r   r   r   r   test_directory_writableN  s   
"z&TestGetTempDir.test_directory_writablec                 C   sP   t  }t  }t  }| ||u  | t|t| | |t| d S r!   )	r   r\   r]   rF   r   r?   rB   r<   fsdecode)r   r   r   r   r   r   r   r   W  s   zTestGetTempDir.test_same_thingc              	   C   s   t d}t jd }t _z6t }||d< | t  | W d    n1 s(w   Y  W |t _t| d S W |t _t| d S |t _t| w )Nz-Tempr   )r   rX   r^   r   r   rB   r\   r   )r   Zcase_sensitive_tempdirZ_tempdirr   r   r   r   test_case_sensitivea  s   

z"TestGetTempDir.test_case_sensitiveN)r'   r(   r)   r   r1  r2  r   r4  r   r   r   r   r-  B  s    		
r-  c                   @   s:   e Zd ZdZdddZdd Zdd Zd	d
 Zdd ZdS )TestMkstempzTest mkstemp().Nc           
   	   C   s   t |||}|d u r|tu rt  }nt  }|d u r| }|d u r&| }t j|||d\}}tj|\}}tj	|}	| 
|	|d|	|f  z| |||| W t| t| d S t| t| w )NrJ   r(  suffixz+Directory '%s' incorrectly returned as '%s')r   r   r   r\   r]   rW   r<   r#   r=   rC   rB   rP   r   r   )
r   rJ   rK   rL   r   r   rI   rM   rN   Zadirr   r   r   r   s  s*   



zTestMkstemp.do_createc                 C   sL   |    | j dd | j dd | j ddd | j ddd | j dd	 d S )
Nr   r   r   r   r   r   r   .r   r   r   r   r   r   r     s   zTestMkstemp.test_basicc                 C   s  t  }| j|dd | j|dd | j|dd | j|ddd | j|ddd | jd	d
 | t | jdddd W d    n1 sGw   Y  | t | jd	ddd W d    n1 sdw   Y  | t | jd	ddd W d    d S 1 sw   Y  d S )Nr   r   r   r   r   r   r   r      .r   r8  r   r   )r   r]   r   r   r   r0  r   r   r   r     s    "z'TestMkstemp.test_basic_with_bytes_namesc              	   C   sD   t  }z| j|d | jt|d W t| d S t| w Nr   )r   rX   r   r   r   r<   r   r   r   r   r   r     s
   z!TestMkstemp.test_choose_directoryc                 C   sV  t j}| t jttd f zt  \}}t| t| | |t t 	 t _| t jt
 | t  t | t 	 t
 t  \}}t| t| | |t
 t jdd\}}t| t| | |t t jdd\}}t| t| | |t t jt  d\}}t| t| | |t W |t _d S |t _w )Nr   r7  z
test-temp-r'  r   )r   r^   r   r   r?   rW   r<   r   r   r]   r   r\   )r   orig_tempdirr   r#   r   r   r   .test_for_tempdir_is_bytes_issue40701_api_warts  s8   










z:TestMkstemp.test_for_tempdir_is_bytes_issue40701_api_wartsr;   )	r'   r(   r)   r   r   r   r   r   r>  r   r   r   r   r5  p  s    
		r5  c                   @   s|   e Zd ZdZdd ZdddZdd Zd	d
 Zdd Zdd Z	dd Z
eejdkddd Zdd Zdd Zdd ZdS )TestMkdtempzTest mkdtemp().c                 C   s   t  S r!   )r   rX   r   r   r   r   r     s   zTestMkdtemp.make_tempNc                 C   s   t |||}|d u r|tu rt  }nt  }|d u r| }|d u r&| }t j|||d}z| |||| |W S    t|  )Nr6  )	r   r   r   r\   r]   rX   rP   r<   r   )r   rJ   rK   rL   r   rI   r   r   r   r     s    

zTestMkdtemp.do_createc                 C   s^   t |   t | jdd t | jdd t | jddd t | jddd d S Nr   r   r   r   r   r   r   )r<   r   r   r   r   r   r   r     r   zTestMkdtemp.test_basicc                 C   s4  t  }t| j|d t| j|dd t| j|dd t| j|ddd t| j|ddd | t t| j|d	dd W d    n1 sRw   Y  | t t| j|dd
d W d    n1 srw   Y  | t t| jdddd W d    d S 1 sw   Y  d S )Nr   r   r   r   r   r   r   r   r   r   r   )r   r]   r<   r   r   r   r   r0  r   r   r   r     s   "z'TestMkdtemp.test_basic_with_bytes_namesc              
   C   sl   t tt}z|D ]
}| jdd||< q	W |D ]}t|tr#t| qd S |D ]}t|tr4t| q(w r   )r   ru   rv   r   r@   r   r<   r   r   r   r   r   r     s   



zTestMkdtemp.test_basic_manyc              	   C   sP   t  }zt| j|d t| jt|d W t| d S t| w r;  )r   rX   r<   r   r   r   r   r   r   r   r   r   
  s
   z!TestMkdtemp.test_choose_directoryc              	   C   sj   |   }z*tt|j}|dM }d}tjdkr!|d? }|d }| || W t| d S t| w )Ni  i  r  r  r  )	r   r   r  r<   r   r  r  rB   r   )r   rJ   r   rf   r  r   r   r   	test_mode  s   
zTestMkdtemp.test_modentzOnly on Windows.c              	   C   s   |   }ztjd|gdd }W t| nt| w | }t }d}|  D ]+}d }|	|r=|
| }n|rK|d d  rK| }|rW||dd  q,| d | d S )	Nz
icacls.exeZoemencodingFrT   :r   zBUILTIN\Users)r   
subprocesscheck_outputcasefoldr<   r   setstrip
splitlines
startswithremoveprefixisspaceadd	partitionrw   )r   rJ   outZusersZ
found_userlineZaclr   r   r   test_mode_win32$  s"   
zTestMkdtemp.test_mode_win32c              	   C   s   t  F tddd' tjdd}|  | |jd t }| |d W d    n1 s4w   Y  W d    d S W d    d S 1 sLw   Y  d S )Nr  r  F)delete)	r   r   r   rU   r   rF   rI   r  rX   )r   r   rJ   r   r   r   r   ?  s   
Pz-TestMkdtemp.test_collision_with_existing_filec              	   C   s   t  ? tddd  t }| |d t }| |d W d    n1 s-w   Y  W d    d S W d    d S 1 sEw   Y  d S r  )r   r   r   rX   rF   r  )r   dir1Zdir2r   r   r   r!  J  s   
Pz2TestMkdtemp.test_collision_with_existing_directoryc                 C   s  t j}| t jttd f zut  }t| | |t t  t _| t jt	 | t 
 t | t  t	 t  }t| | |t	 t jdd}t| | |t t jdd}t| | |t t jt 
 d}t| | |t W |t _d S |t _w )Nz-dirr<  ztest-mkdtemp-r'  r   )r   r^   r   r   r?   rX   r<   r   r]   r   r\   )r   r=  r#   r   r   r   r>  T  s.   





z:TestMkdtemp.test_for_tempdir_is_bytes_issue40701_api_wartsr;   )r'   r(   r)   r   r   r   r   r   r   r   rA  r   r   r<   rI   rS  r   r!  r>  r   r   r   r   r?    s    
	

r?  c                       sT   e Zd ZdZ fddZ fddZG dd dZdd	d
Zdd Zdd Z	  Z
S )
TestMktempzTest mktemp().c                    rj   r!   )r   rX   rJ   rm   r8   r   rn   r   r   r8   t  rp   zTestMktemp.setUpc                    s&   | j rt| j  d | _ t   d S r!   )rJ   r<   r   rm   r:   r   rn   r   r   r:   x  s   zTestMktemp.tearDownc                   @   s(   e Zd ZejZejZdd Z	dd Z
dS )zTestMktemp.mktempedc                 C   s.   t j|||d| _tt| j| jd d S )Nr6  r  )r   r.   rI   r<   r   r   r   )r   rJ   rK   rL   r   r   r   r$     s   zTestMktemp.mktemped.__init__c                 C   s   |  | j d S r!   )r   rI   r   r   r   r   r     s   zTestMktemp.mktemped.__del__N)r'   r(   r)   r<   r   r   r   r   r   r$   r   r   r   r   r   mktemped~  s
    rW  r   c                 C   s(   |  | j||}| |j| j|| |S r!   )rW  rJ   rP   rI   )r   rK   rL   r   r   r   r   r     s   zTestMktemp.do_createc                 C   @   |    | j dd | j dd | j ddd | j ddd d S r@  r9  r   r   r   r   r     
   zTestMktemp.test_basicc                 C   s4   t tt}|D ]
}| jdd||< q~t  d S r   )r   ru   rv   r   r   r   r   r   r   r   ry     s
   zTestMktemp.test_many)r   r   )r'   r(   r)   r   r8   r:   rW  r   r   ry   r   r   r   rn   r   rV  o  s    
rV  c                   @   sr   e Zd Z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dd ZdS )TestNamedTemporaryFilezTest NamedTemporaryFile().Nr   Tc                 C   s8   |d u rt  }t j||||d}| |j||| |S )N)rJ   r(  r7  rT  )r   r\   rU   rP   rI   )r   rJ   rK   rL   rT  r   r   r   r   r     s   
z TestNamedTemporaryFile.do_createc                 C   rX  r@  r9  r   r   r   r   r     rY  z!TestNamedTemporaryFile.test_basicc                 C   sT   |   }t|}|j}|j}~|d ~|d ~tjddr(| |  d S d S )Ns   foos   barT)Zcpython)r   weakrefrefr   r   Zcheck_impl_detailassertIsNone)r   r  wrr   Zwrite2r   r   r   test_method_lookup  s   
z)TestNamedTemporaryFile.test_method_lookupc                    sP   g d  fdd}t | D ]\}}| | |  q| |t d  d S )N)s   spam
s   eggs
s   beans
c                     s*   t jdd} | d  | d | S )Nw+br   r   r   )r   rU   r   r   seekr  linesr   r   	make_file  s   
z3TestNamedTemporaryFile.test_iter.<locals>.make_filerT   )	enumeraterB   r>   )r   rf  rx   lr   rd  r   	test_iter  s
   z TestNamedTemporaryFile.test_iterc                 C   s(   t  }| tj|jd|j  d S )Nz$NamedTemporaryFile %s does not exist)r   rU   rF   r<   r#   existsrI   r  r   r   r   test_creates_named  s   z)TestNamedTemporaryFile.test_creates_namedc              	   C   s   t  }z:t j|d}|d W d    n1 sw   Y  | t|g  | tj	|j
d|j
  W t| d S t| w )Nr   r   z(NamedTemporaryFile %s exists after close)r   rX   rU   r   rB   r<   r   r   r#   rj  rI   r   r   rJ   r  r   r   r   test_del_on_close  s   z(TestNamedTemporaryFile.test_del_on_closec              	   C   s   t  }d }z2t j|dd}|j}|d |  | tj	|jd|j  W |d ur2t
| t| d S |d urBt
| t| w )NF)rJ   rT  r   z)NamedTemporaryFile %s missing after close)r   rX   rU   rI   r   r   rF   r<   r#   rj  r   r   )r   rJ   tmpr  r   r   r   test_dis_del_on_close  s    


z,TestNamedTemporaryFile.test_dis_del_on_closec                 C   .   t  }|d |  |  |  d S N   abc
)r   rU   r   r   r  r   r   r   test_multiple_close  
   
z*TestNamedTemporaryFile.test_multiple_closec                    sl   t   | tj j W d    n1 sw   Y  | tj j  fdd}| t	| d S )Nc                      *    	 W d    d S 1 sw   Y  d S r!   r   r   rc  r   r   
use_closed     "z?TestNamedTemporaryFile.test_context_manager.<locals>.use_closed)
r   rU   rF   r<   r#   rj  rI   r   r   r   r   rv  r   rc  r   test_context_manager	  s   
z+TestNamedTemporaryFile.test_context_managerc                 C      t  }| tj| | t t jd|d W d    n1 s"w   Y  | t t jd|d W d    n1 s>w   Y  | 	t
|g  d S Nr^  )r   rJ      )r   rX   
addCleanupr   r   r   r   rU   r   rB   r<   r   r   r   r   r   test_bad_mode     z$TestNamedTemporaryFile.test_bad_modec                 C   f   t  }| tj| | t t jdd|d W d    n1 s#w   Y  | t	
|g  d S Nwzbad-encoding)rD  rJ   )r   rX   r}  r   r   r   LookupErrorrU   rB   r<   r   r   r   r   r   test_bad_encoding     z(TestNamedTemporaryFile.test_bad_encodingc              
   C   s   t  }| tj| td?}tdt '}t |_	| 
t t j|d W d    n1 s4w   Y  W d    n1 sCw   Y  W d    n1 sRw   Y  | j  | t|g  d S )Nztempfile._TemporaryFileWrapperzio.openr   )r   rX   r}  r   r   r   patch	mock_openKeyboardInterruptside_effectr   rU   r   assert_calledrB   r<   r   )r   rJ   Zmock_ntfr  r   r   r   test_unexpected_error#  s     z,TestNamedTemporaryFile.test_unexpected_error)Nr   r   T)r'   r(   r)   r   r   r   r_  ri  rk  rm  ro  rs  ry  r~  r  r  r   r   r   r   rZ    s    


	rZ  c                   @   s   e Zd Z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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S ).TestSpooledTemporaryFilezTest SpooledTemporaryFile().r   Nr   c                 C   s&   |d u rt  }t j||||d}|S )N)max_sizerJ   r(  r7  )r   r\   r`   )r   r  rJ   rK   rL   r   r   r   r   r   3  s   z"TestSpooledTemporaryFile.do_createc                 C   s4   |   }| |j | j dddd}| |j d S )Nr	   r   r   )r  rK   rL   )r   r   _rolledr  r   r   r   r   ;  s   z#TestSpooledTemporaryFile.test_basicc              	   C   s   t  }zIt jd|d}| |j |d | |j |j}|  | 	t
|g  t|tsF| t
j|d|  W t
| d S W t
| d S t
| w )N
   )r  rJ   s   blat blat blat blat blat z*SpooledTemporaryFile %s exists after close)r   rX   r`   r   r  r   rF   rI   r   rB   r<   r   r@   intr#   rj  r   )r   rJ   r  filenamer   r   r   rm  B  s    

z*TestSpooledTemporaryFile.test_del_on_closec                 C   sL   | j dd}| |j tdD ]}|dd |d q| |j d S )N   r     r      xxxxxxxxxxxxxxxxxxxx)r   r   r  ru   rb  r   )r   r  rx   r   r   r   test_rewrite_smallS  s   z+TestSpooledTemporaryFile.test_rewrite_smallc                 C   s^   | j dd}| |j |d | |j |d | |j |d | |j d S )Nr  r  r  
   xxxxxxxxxx   x)r   r   r  r   rF   r  r   r   r   test_write_sequential\  s   


z.TestSpooledTemporaryFile.test_write_sequentialc                 C   s@   |   }|d |d}| |d | }| |d d S )N)r     y   zr   s   xyz)r   
writelinesrb  rB   r   )r   r  posbufr   r   r   test_writelinesh  s   

z(TestSpooledTemporaryFile.test_writelinesc                 C   s<   | j dd}|d | |j |d | |j d S )N#   r  )r  r  s   xxxxxr  )r   r  r   r  r   rF   r  r   r   r   test_writelines_sequentialq  s
   

z3TestSpooledTemporaryFile.test_writelines_sequentialc                 C   sV   | j dd}| |j |dd}| |d | |j |d | |j d S )Nr  r  r	   r   r  )r   r   r  rb  rB   r   rF   )r   r  r  r   r   r   test_sparsez  s   
z$TestSpooledTemporaryFile.test_sparsec                 C   s:   | j dd}| |j | | dk | |j d S )Nr  r  r   )r   r   r  rF   filenor  r   r   r   test_fileno  s   z$TestSpooledTemporaryFile.test_filenoc                 C   s:   t  }|d | |j |  |  |  d S rq  )r   r`   r   r   r  r   r  r   r   r   #test_multiple_close_before_rollover  s   
z<TestSpooledTemporaryFile.test_multiple_close_before_rolloverc                 C   s>   t jdd}|d | |j |  |  |  d S )NrT   r  rr  )r   r`   r   rF   r  r   r  r   r   r   "test_multiple_close_after_rollover  s   
z;TestSpooledTemporaryFile.test_multiple_close_after_rolloverc                 C   sL   | j dd}|j}|j}|j}|d |d |dd | |dd d S )Nr  r  s#   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaas#   bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbr   F   sF   aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb)r   r   r   rb  rB   )r   r  r   r   rb  r   r   r   test_bound_methods  s   
z+TestSpooledTemporaryFile.test_bound_methodsc                 C   s  t jdd}|d | |j | |jd | |j | 	t
 |j W d    n1 s1w   Y  | 	t
 |j W d    n1 sIw   Y  | 	t
 |j W d    n1 saw   Y  |d | |j | |jd | |j | 	t
 |j W d    n1 sw   Y  | 	t
 |j W d    n1 sw   Y  | 	t
 |j W d    d S 1 sw   Y  d S )Nr  r  r  r`  r  zrb+)r   r`   r   r   r  rB   r   r]  rI   r   r   newlinesrD  errorsrF   assertIsNotNoner  r   r   r   test_properties  s6   

"z(TestSpooledTemporaryFile.test_propertiesc                 C   sH  t jdddd}|d |d | | d |d |d | | d | |j | |jd | 	|j
 | |jtj | |jd | |jd	 |d
 |d | | d |d |d | | d | |j | |jd | |j
 | |jtj | |jd | |jd	 d S )Nw+r  utf-8)r   r  rD  zabc
r   zdef
zabc
def
strictzxyzzy
zabc
def
xyzzy
zfoobar
zabc
def
xyzzy
foobar
)r   r`   r   rb  rB   r   r   r  r   r]  rI   r  r<   lineseprD  r  rF   r  r  r   r   r   test_text_mode  s6   







z'TestSpooledTemporaryFile.test_text_modec                 C   s  t jdddddd}|d |d | | d | |j | |jd | 	|j
 | |j | |jd | |jd |d	 |d
 |d | | d | |j | |jd | |j
 | |j | |jd | |jd d S )Nr  r  r   r  r-   )r   r  newlinerD  r  u   Λ
r   u   ΜΜΜΜΜΜΜΜΜΜ
u(   ΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝuB   Λ
ΜΜΜΜΜΜΜΜΜΜ
ΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝΝ)r   r`   r   rb  rB   r   r   r  r   r]  rI   r  r  rD  r  rF   r  r   r   r   test_text_newline_and_encoding  s2   





z7TestSpooledTemporaryFile.test_text_newline_and_encodingc                    sl   t jdd |  j |  j W d    n1 sw   Y  |  j  fdd}| t| d S )NrT   r  c                      ru  r!   r   r   rc  r   r   rv    rw  zQTestSpooledTemporaryFile.test_context_manager_before_rollover.<locals>.use_closed)r   r`   r   r  closedrF   r   r   rx  r   rc  r   $test_context_manager_before_rollover  s   z=TestSpooledTemporaryFile.test_context_manager_before_rolloverc                    s   t jdd# |  j  d    |  j |  j W d    n1 s,w   Y  |  j  fdd}| t	| d S )NrT   r  rr  c                      ru  r!   r   r   rc  r   r   rv    rw  zQTestSpooledTemporaryFile.test_context_manager_during_rollover.<locals>.use_closed)
r   r`   r   r  r   flushrF   r  r   r   rx  r   rc  r   $test_context_manager_during_rollover	  s   
z=TestSpooledTemporaryFile.test_context_manager_during_rolloverc                    s   t jdd  d    |  j   |  j W d    n1 s(w   Y  |  j  fdd}| t	| d S )NrT   r  rr  c                      ru  r!   r   r   rc  r   r   rv     rw  zPTestSpooledTemporaryFile.test_context_manager_after_rollover.<locals>.use_closed)
r   r`   r   r  rF   r  r   r  r   r   rx  r   rc  r   #test_context_manager_after_rollover  s   
z<TestSpooledTemporaryFile.test_context_manager_after_rolloverc                 C   s   t jdd}|d |d |  | |j | |j	 d t jdd}|d |d | |j | |j	 d t jdd}|d |d | 
|j | t| jd d S )	Nr  r  s   abcdefg
r   r      s   abcdrz   )r   r`   r   rb  truncater   r  rB   _filegetvaluerF   r<   fstatr  st_sizer  r   r   r   !test_truncate_with_size_parameter%  s    





z:TestSpooledTemporaryFile.test_truncate_with_size_parameterc                 C   s   |  tjt tj d S r!   )r   r   r`   r   typesGenericAliasr   r   r   r   test_class_getitem:  s   z+TestSpooledTemporaryFile.test_class_getitem)r   Nr   r   )r'   r(   r)   r   r   r   rm  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r   r   r   r  0  s,    
					 r  c                   @   sH   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dS )TestTemporaryFilezTest TemporaryFile().c                 C   s   t   d S r!   )r   rV   r   r   r   r   r   C  s   zTestTemporaryFile.test_basicc                 C   sJ   t  }t j|d}|d zt| W d S    |  t|  r   )r   rX   rV   r   r<   r   r   rl  r   r   r   test_has_no_nameH  s   

z"TestTemporaryFile.test_has_no_namec                 C   rp  rq  )r   rV   r   r   r  r   r   r   rs  X  rt  z%TestTemporaryFile.test_multiple_closec                    s@    fdd}|dd |dd |dddd	 |d
ddd d S )Nc                    sZ   t j|i |}||  |d  | |  W d    d S 1 s&w   Y  d S r   )r   rV   r   rb  rB   r   )inputr   r   fileobjr   r   r   	roundtripc  s
   

"z;TestTemporaryFile.test_mode_and_encoding.<locals>.roundtrips   1234r`  zabdc
r  u   Λzutf-16rC  zfoo
r   )r  r   )r   r  r   r   r   test_mode_and_encodinga  s
   

z(TestTemporaryFile.test_mode_and_encodingc                 C   rz  r{  )r   rX   r}  r   r   r   r   rV   r   rB   r<   r   r   r   r   r   r~  n  r  zTestTemporaryFile.test_bad_modec                 C   r  r  )r   rX   r}  r   r   r   r  rV   rB   r<   r   r   r   r   r   r  w  r  z#TestTemporaryFile.test_bad_encodingc                 C   s  t  }| tj| tddf tdQ}td<}td'}t |_| 	t t j
|d W d    n1 s>w   Y  W d    n1 sMw   Y  W d    n1 s\w   Y  W d    n1 skw   Y  W d    n1 szw   Y  |  | t|g  d S )Nztempfile._O_TMPFILE_WORKSFz	os.unlinkzos.openzos.closer   )r   rX   r}  r   r   r   r  r  r  r   rV   r  rB   r<   r   )r   rJ   Zmock_unlinkr  Z
mock_closer   r   r   r  ~  s4      z'TestTemporaryFile.test_unexpected_errorN)r'   r(   r)   r   r   r  rs  r  r~  r  r  r   r   r   r   r  @  s    		r  c                   @   r   )NulledModulesc                 G   s&   dd |D | _ dd | j D | _d S )Nc                 S   s   g | ]}|j qS r   )rc   ).0modr   r   r   
<listcomp>  s    z*NulledModules.__init__.<locals>.<listcomp>c                 S   s   g | ]}|  qS r   )copy)r  r\  r   r   r   r    s    )refscontents)r   modulesr   r   r   r$     s   zNulledModules.__init__c                 C   s"   | j D ]}|D ]}d ||< qqd S r!   )r  )r   r+  rg   r   r   r   r4     s
   

zNulledModules.__enter__c                 G   s.   t | j| jD ]\}}|  || qd S r!   )zipr  r  clearupdate)r   exc_infor+  r   r   r   r   r9     s   zNulledModules.__exit__N)r'   r(   r)   r$   r4   r9   r   r   r   r   r    s    r  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e	j
dd Ze	j
dd Zeeedde	j
dd Zejdd Zej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eeddd,d- ZdS )0TestTemporaryDirectoryzTest TemporaryDirectory().Nr   rT   Fc           	      C   sJ   |d u rt  }t j||||d}| |j||| | |j||| |S )N)rJ   r(  r7  ignore_cleanup_errors)r   r\   ra   rP   rI   
do_create2)	r   rJ   rK   rL   recursedirsfilesr  rn  r   r   r   r     s   z TestTemporaryDirectory.do_createc              	   C   s   |r!t |D ]}tj|d| }t| | ||d || qt |D ]#}ttj|d| d}|d W d    n1 sCw   Y  q%d S )Nzdir%drT   z
test%d.txtr   s   Hello world!)ru   r<   r#   r   mkdirr  r   r   )r   r#   r  r  r  rx   rI   r  r   r   r   r    s   
z!TestTemporaryDirectory.do_create2c                 C   st   t  }W d    n1 sw   Y  | t}t j|d W d    n1 s*w   Y  | |jjtj d S r;  )r   ra   r   r   rB   	exceptionerrnoENOENT)r   r   cmr   r   r   test_mkdtemp_failure  s   
z+TestTemporaryDirectory.test_mkdtemp_failurec              	   C   sr   t  }z.| j|d}| tj|jd|j  |  | 	tj|jd|j  W t
| d S t
| w )Nr   z$TemporaryDirectory %s does not exist*TemporaryDirectory %s exists after cleanup)r   rX   r   rF   r<   r#   rj  rI   cleanupr   r   )r   rJ   r+  r   r   r   test_explicit_cleanup  s   z,TestTemporaryDirectory.test_explicit_cleanupc              	   C   s  t  z}| j|dd}t|j}| | d|d t|d d}|	d |
  W d   n1 s9w   Y  | tt|d	ttjd
d| | | tjd
d|d |
  | | d|d W d   dS 1 sw   Y  dS )z=Test that cleanup doesn't return an error when ignoring them.TrJ   r  TemporaryDirectory  does not exist
a_file.txtw+tHello world!
N*win1Unexpected number of files in TemporaryDirectory  existence state unexpectedz exists after cleanup)r   ra   r   r   r   rI   rF   rj  r   r   r  rB   r>   r   globr  r  r  rL  r   r   working_dirZtemp_dir	temp_pathZ	open_filer   r   r   "test_explict_cleanup_ignore_errors  s:   







"z9TestTemporaryDirectory.test_explict_cleanup_ignore_errorsc                 C   s   |   }| j dd}t|jtj|jd |  | tj|jd|j  | 	tj|jd | 
t|jdgd |  d S )Nr   r  Zfoor  z-Directory pointed to by a symlink was deletedz	test0.txtz>Contents of the directory pointed to by a symlink were deleted)r   r<   symlinkrI   r#   r   r  r   rj  rF   rB   r   )r   d1d2r   r   r   (test_cleanup_with_symlink_to_a_directory  s   z?TestTemporaryDirectory.test_cleanup_with_symlink_to_a_directoryc                    s  j dd}tj|d}t|d  tj|d}t| tdD ]̉  dK  jt	 dd	  fd
d}d |ddd W d    n1 sRw   Y  d |ddd W d    n1 smw   Y  d+ t
|  t|j}||dd t|j}||d||f  W d    n1 sw   Y  d+ t
|  t|j}||dd t|j}||d||f  W d    n1 sw   Y  W d    n1 sw   Y  q%W d    d S 1 sw   Y  d S )Nr   r  file1r   rU     r  03ora  c                       j dd}tj|jd}tj| ||d z
tj| dd W n	 ty)   Y nw zt|  W n	 ty;   Y nw t|j  |	  
tj|j d S Nr   r  r  target_is_directoryF)follow_symlinks)r   r<   r#   r   rI   r  r   NotImplementedErrorr   r  r   rj  targetr  r  r  r   r   r   r   test  $   zDTestTemporaryDirectory.test_cleanup_with_symlink_modes.<locals>.testnonexisting filenonexistingFr  nonexisting dirTexisting filez%03o != %03oexisting dir)r   r<   r#   r   r   r   r  ru   subTestformatr   r   r   rB   )r   r  r  rU  r  Zold_modeZnew_moder   r  r   test_cleanup_with_symlink_modes  sL   


"z6TestTemporaryDirectory.test_cleanup_with_symlink_modeschflagszrequires os.chflagsc              	      s  t jt jB    jdd}tj|d}t|d	  tj|d}t
|  fdd}d |d	d
d W d    n1 sIw   Y  d |d	dd W d    n1 sdw   Y  d& t|  t |j}||d
d t |j}|| W d    n1 sw   Y  d& t|  t |j}||dd t |j}|| W d    n1 sw   Y  W d    d S W d    d S 1 sw   Y  d S )Nr   r  r  r   rU  c                    r  r  )r   r<   r#   r   rI   r  r  r   r   r  r   rj  r  r   r   r   r   r  7  r  zDTestTemporaryDirectory.test_cleanup_with_symlink_flags.<locals>.testr  r  Fr  r  Tr	  r
  )r   UF_IMMUTABLEUF_NOUNLINKcheck_flagsr   r<   r#   r   r   r   r  r  r  st_flagsrB   )r   r  r  rU  r  Z	old_flagsZ	new_flagsr   r  r   test_cleanup_with_symlink_flags+  s>   

""z6TestTemporaryDirectory.test_cleanup_with_symlink_flagsc              	   C   sR   t  }z| j|d}|j}~| tj|d|  W t| d S t| w )Nr   *TemporaryDirectory %s exists after __del__)	r   rX   r   rI   r   r<   r#   rj  r   r   rJ   r+  rI   r   r   r   test_del_on_collection[  s   z-TestTemporaryDirectory.test_del_on_collectionc              	   C   s   t  g}| j|dd}t|j}| | d|d t|d d}|	d ~W d   n1 s6w   Y  | 
tt|d	ttjd
d| | 
| tjd
d|d W d   dS 1 snw   Y  dS )z@Test that ignoring errors works when TemporaryDirectory is gced.Tr  r  r  r  r  r  Nr  r  r  r  )r   ra   r   r   r   rI   rF   rj  r   r   rB   r>   r   r  r  r  r  rL  r  r   r   r   $test_del_on_collection_ignore_errorsh  s0   





"z;TestTemporaryDirectory.test_del_on_collection_ignore_errorsc                 C   s   |   B}dD ]6}dj||d}td|\}}}|  }| tj	|d|  |dd}| 
d| | d	| qW d    d S 1 sIw   Y  d S )
N)builtinsr<   shutilr  r   r5   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, "test0.txt"), "w") as f:
                        f.write("Hello world!")

                    {mod}.tmp = tmp

                    warnings.filterwarnings("always", category=ResourceWarning)
                    )rJ   r  -cr  r  backslashreplace
Exception 'ResourceWarning: Implicitly cleaning upr   r  r   assert_python_okr   rJ  r   r<   r#   rj  rw   r   )r   rJ   r  codercrQ  errtmp_namer   r   r   test_del_on_shutdown}  s   
"z+TestTemporaryDirectory.test_del_on_shutdownc                 C   s   t  a}dj|d}td|\}}}t|  }| 	t
t|dttjdd| | 	| tjdd|d |d	d
}| d| | d| | d| W d   dS 1 shw   Y  dS )z?Test ignoring errors works when a tempdir is gc'ed on shutdown.a  if True:
                import pathlib
                import sys
                import tempfile
                import warnings

                temp_dir = tempfile.TemporaryDirectory(
                    dir={working_dir!r}, ignore_cleanup_errors=True)
                sys.stdout.buffer.write(temp_dir.name.encode())

                temp_dir_2 = pathlib.Path(temp_dir.name) / "test_dir"
                temp_dir_2.mkdir()
                with open(temp_dir_2 / "test0.txt", "w") as test_file:
                    test_file.write("Hello world!")
                open_file = open(temp_dir_2 / "open_file.txt", "w")
                open_file.write("Hello world!")

                warnings.filterwarnings("always", category=ResourceWarning)
                )r  r  r  r  r  r  r  r  r  	ExceptionErrorr  N)r   ra   r  r   r   r   r   r   rJ  rB   r>   r   r  r  r  r  rL  rj  rw   r   )r   r  r!  __rQ  r#  r  r   r   r   "test_del_on_shutdown_ignore_errors  s,   


"z9TestTemporaryDirectory.test_del_on_shutdown_ignore_errorsc                 C   s   |   <}dj|d}td|\}}}|  }| tj	|d|  |dd}| 
d| | d| W d    d S 1 sCw   Y  d S )	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   r  r  r  r  r  r  r  )r   rJ   r!  r"  rQ  r#  r$  r   r   r   test_exit_on_shutdown  s   
"z,TestTemporaryDirectory.test_exit_on_shutdownc              	   C   s   |   D}| j |dd}|j}tjdtfdd tjdtd ~t  W d    n1 s/w   Y  | 	t
j|d|  W d    d S 1 sKw   Y  d S )	N   )rJ   r  Z
ImplicitlyF)quietalways)r/   r  )r   rI   r   r2   ResourceWarningr5   r6   r   r   r   r<   r#   rj  r  r   r   r   test_warnings_on_cleanup  s    

"z/TestTemporaryDirectory.test_warnings_on_cleanupc                 C   s$   |   }|  |  |  d S r!   )r   r  r0  r   r   r   rs    s   z*TestTemporaryDirectory.test_multiple_closec                 C   sb   |   }|}| tj| | ||j W d    n1 s!w   Y  | tj| d S r!   )r   rF   r<   r#   rj  rB   rI   r   )r   r+  rI   r   r   r   ry    s   z+TestTemporaryDirectory.test_context_managerc                 C   s   t dD ]k}|dK }| jt|ddT | jdddd}|0 tj|jdd	D ]\}}}|D ]}ttj	||| q.t|| q'|
  W d    n1 sQw   Y  | tj|j W d    n1 sjw   Y  qd S )
Nr  r  r  ra  r+  r|  r  r  r  Ftopdown)ru   r  r  r   r<   walkrI   r   r#   r   r  r   rj  )r   r   r+  rootr  r  rI   r   r   r   
test_modes  s   
z!TestTemporaryDirectory.test_modesc              
   C   s   t j}zDt|d  zt|| W n ty1 } z| d|dd|  W Y d }~nd }~ww t|d W t | d S W t | d S t | w )Nr  z chflags() doesn't support flags z#bz: r   )	r   ZTESTFNr   r   r<   r  r   r   r   )r   r   r  excr   r   r   r    s"   z"TestTemporaryDirectory.check_flagsc              	   C   s   t jt jB }| | | jdddd}|0 tj|jddD ]\}}}|D ]}ttj	
||| q%t|| q|  W d    n1 sHw   Y  | tj	|j d S )Nr+  r|  r0  Fr1  )r   r  r  r  r   r<   r3  rI   r  r#   r   r  r   rj  )r   r   r+  r4  r  r  rI   r   r   r   
test_flags  s   

z!TestTemporaryDirectory.test_flags)Nr   r   rT   rT   rT   F)rT   rT   rT   )r'   r(   r)   r   r   r  r  r  r  r   Zskip_unless_symlinkr  r  r   r   r   r<   r  r   Zcpython_onlyr  r  r%  r)  r*  r/  rs  ry  r5  r  r7  r   r   r   r   r    s<    




0.

 %r  __main__)8r   r  ior<   r   r  rQ   r5   
contextlibr   r  r[  rF  r   r   r  r   Ztest.supportr   r   r   r   r   r#  r   r"  r  rL  rv   ZTestCaser
   r,   rS   ri   r   r   r   contextmanagerr   r   r   r   r$  r-  r5  r?  rV  rZ  r  rU   rV   r  r  r  r'   mainr   r   r   r   <module>   st   
23@* 
	  0 .^ ">   M   
