o
     JAf r                     @   s  	 g d 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ZddlZddlZejZejejB ejB ZeedrIeejO ZeZeedrUeej O Zeedr^ej!Z!ndZ!dZ"e Z#d	d
 Z$dd Z%dd Z&G dd dZ'dd Z(dd Z)da*dd Z+dd Z,dd Z-dd Z.da/dd Z0dd  Z1d!d" Z2d>d$d%Z3d?d&d'Z4d(e"dfd)d*Z5G d+d, d,Z6G d-d. d.Z7			1d@dd2d3d4Z8ej9d5ksej:d6kre8Z;need7a<			dAdd2d8d9Z;G d:d; d;Z=G d<d= d=Z>dS )B)NamedTemporaryFileTemporaryFileSpooledTemporaryFileTemporaryDirectorymkstempmkdtempmktempTMP_MAXgettempprefixtempdir
gettempdirgettempprefixbgettempdirb    N)Random
O_NOFOLLOWO_BINARYr   i'  tmpc                 C   s&   zt |  W dS  ty   Y dS w )NFT)_oslstatOSError)fn r   /usr/lib/python3.10/tempfile.py_existsL   s   r   c                  G   s   	 d }| D ],}|d u rqt |tjrt|}t |tr'|tu r$tdt}q|tu r/tdt}q|d u rCtd u s?t ttrAtS tS |S )Nz1Can't mix bytes and non-bytes in path components.)
isinstancer   PathLikefspathbytesstr	TypeErrorr
   )argsreturn_typeargr   r   r   _infer_return_typeU   s&   

r#   c                 C   sf   	 t | ||}|d u r| }| d u r|tu rt} ntt} |d u r-|tu r*t }nt }| |||fS N)r#   r   templater   fsencoder   r   )prefixsuffixdiroutput_typer   r   r   _sanitize_paramsr   s   
r+   c                   @   s.   e Zd Z	 dZedd Zdd Zdd ZdS )	_RandomNameSequence%abcdefghijklmnopqrstuvwxyz0123456789_c                 C   s,   t  }|t| dd krt | _|| _| jS )N_rng_pid)r   getpidgetattr_Random_rngr.   )selfcur_pidr   r   r   rng   s
   z_RandomNameSequence.rngc                 C   s   | S r$   r   r3   r   r   r   __iter__   s   z_RandomNameSequence.__iter__c                 C   s   d | jj| jddS )N    )k)joinr5   choices
charactersr6   r   r   r   __next__   s   z_RandomNameSequence.__next__N)__name__
__module____qualname__r=   propertyr5   r7   r>   r   r   r   r   r,      s    
r,   c               	   C   s   	 g } dD ]}t |}|r| | qt jdkr-| t jdt jdddddg n| g d	 z
| t   W | S  t	t
fyP   | t j Y | S w )
N)TMPDIRTEMPTMPntz~\AppData\Local\Tempz%SYSTEMROOT%\Tempzc:\tempzc:\tmpz\tempz\tmp)z/tmpz/var/tmpz/usr/tmp)r   getenvappendnameextendpath
expanduser
expandvarsgetcwdAttributeErrorr   curdir)dirlistenvnamedirnamer   r   r   _candidate_tempdir_list   s&   


rT   c                  C   s   	 t  } t }|D ]~}|tjkrtj|}tdD ]l}t| }tj||}z0t	|t
d}zzt|d W t| nt| w W t| nt| w |W     S  ty`   Y q ty}   tjdkrztj|rzt|tjrzY qY  n
 ty   Y  nw q	ttjd| )Nd     s   blatrF   z)No usable temporary directory found in %s)r,   rT   r   rP   rK   abspathrangenextr;   open_bin_openflagswritecloseunlinkFileExistsErrorPermissionErrorrI   isdiraccessW_OKr   FileNotFoundError_errnoENOENT)namerrQ   r)   seqrI   filenamefdr   r   r   _get_default_tempdir   sD   
rk   c                   C   L   	 t d u r$t  zt d u rt a W t  t S W t  t S t  w t S r$   )_name_sequence
_once_lockacquirer,   releaser   r   r   r   _get_candidate_names   s   
rq   c           
   	   C   s   	 t j| } t }|tu rtt j|}ttD ]J}t	|}t j
| || | }td| z	t ||d}	W n$ tyA   Y q ty\   t jdkr[t j| r[t | t jr[Y q w |	|f  S ttjd)Nztempfile.mkstemprV   rF   z#No usable temporary file name found)r   rK   rW   rq   r   mapr&   rX   r   rY   r;   _sysauditrZ   r_   r`   rI   ra   rb   rc   re   EEXIST)
r)   presufflagsr*   namesrh   rI   filerj   r   r   r   _mkstemp_inner   s0   r{   c                   C      	 t tS r$   )r   fsdecoder%   r   r   r   r   r	        
r	   c                   C   r|   r$   )r   r&   r%   r   r   r   r   r     r~   r   c                   C   rl   r$   )r
   rn   ro   rk   rp   r   r   r   r   _gettempdir  s   
r   c                   C      	 t t S r$   )r   r}   r   r   r   r   r   r   )     r   c                   C   r   r$   )r   r&   r   r   r   r   r   r   -  r   r   Fc                 C   s4   	 t || |\}} }}|rt}nt}t||| ||S r$   )r+   _text_openflagsr[   r{   )r(   r'   r)   textr*   rx   r   r   r   r   1  s   r   c              	   C   s   	 t || |\}} }}t }|tu rttj|}ttD ]F}t|}tj	
||| |  }td| zt|d W |  S  tyG   Y q tyb   tjdkratj	|rat|tjraY q w ttjd)Nztempfile.mkdtemp  rF   z(No usable temporary directory name found)r+   rq   r   rr   r   r&   rX   r   rY   rK   r;   rs   rt   mkdirr_   r`   rI   ra   rb   rc   re   ru   )r(   r'   r)   r*   ry   rh   rI   rz   r   r   r   r   X  s0   
r   r8   c                 C   s^   	 |d u rt  }t }ttD ]}t|}tj||| |  }t|s(|  S qt	t
jd)Nz"No usable temporary filename found)r   rq   rX   r   rY   r   rK   r;   r   r_   re   ru   )r(   r'   r)   ry   rh   rI   rz   r   r   r   r     s   r   c                   @   sL   e Zd Z	 dZdZdddZejdkr ejfddZ	d	d
 Z
dS dd Z	dS )_TemporaryFileCloserNFTc                 C   s   || _ || _|| _d S r$   )rz   rI   deleter3   rz   rI   r   r   r   r   __init__  s   
z_TemporaryFileCloser.__init__rF   c                 C   sX   | j s(| jd ur*d| _ z| j  W | jr|| j d S d S | jr'|| j w w d S d S NT)close_calledrz   r]   r   rI   )r3   r^   r   r   r   r]     s   
z_TemporaryFileCloser.closec                 C      |    d S r$   )r]   r6   r   r   r   __del__     z_TemporaryFileCloser.__del__c                 C   s   | j sd| _ | j  d S d S r   )r   rz   r]   r6   r   r   r   r]     s   T)r?   r@   rA   rz   r   r   r   rI   r^   r]   r   r   r   r   r   r     s    


r   c                   @   s@   e Zd Z	 dddZdd Zdd Zdd	 Zd
d Zdd ZdS )_TemporaryFileWrapperTc                 C   s$   || _ || _|| _t|||| _d S r$   )rz   rI   r   r   _closerr   r   r   r   r     s   z_TemporaryFileWrapper.__init__c                    s^   | j d }t||}t|dr"| t  fdd}| j|_|}t|ts-t| || |S )Nrz   __call__c                     s    | i |S r$   r   )r    kwargsfuncr   r   func_wrapper  s   z7_TemporaryFileWrapper.__getattr__.<locals>.func_wrapper)	__dict__r0   hasattr
_functoolswrapsr   r   intsetattr)r3   rI   rz   ar   r   r   r   __getattr__  s   



z!_TemporaryFileWrapper.__getattr__c                 C   s   | j   | S r$   )rz   	__enter__r6   r   r   r   r     s   
z_TemporaryFileWrapper.__enter__c                 C   s   | j |||}|   |S r$   )rz   __exit__r]   )r3   excvaluetbresultr   r   r   r     s   z_TemporaryFileWrapper.__exit__c                 C   s   	 | j   d S r$   )r   r]   r6   r   r   r   r]     s   z_TemporaryFileWrapper.closec                 c   s    | j D ]}|V  qd S r$   )rz   )r3   liner   r   r   r7      s   
z_TemporaryFileWrapper.__iter__Nr   )	r?   r@   rA   r   r   r   r   r]   r7   r   r   r   r   r     s    
r   w+bTerrorsc             	      s   	 t  \ ttjdkr|rtjO d| vr"t|}d  fdd}	z,tj | |||||	d}
zt|
d|
}t|d|}|_t	|
|W W S    |

      d urotjdkrj|sot  )NrF   bc                     s   t  \}|S r$   )r{   )r    rj   r)   rx   rI   r*   r'   r(   r   r   opener*  s   z"NamedTemporaryFile.<locals>.opener	bufferingnewlineencodingr   r   bufferraw)r+   r[   r   rI   O_TEMPORARY_iotext_encodingrZ   r0   r   r]   r^   )moder   r   r   r(   r'   r)   r   r   r   rz   r   r   r   r   r   
  s4   



r   posixcygwin	O_TMPFILEc             	      s   	 d| vr
t |}t \ ttrTd  fdd}zt j | |||||d}	t|	d|	}
t|
d|
}
|
_|	W S  tyK   daY n	 t	yS   Y nw d  fdd}t j | |||||d}	t|	d|	}
t|
d|
}
|
_|	S )	Nr   c                     s$   t jB t j @ }t  |dS )NrV   )r   r   O_CREATrZ   )r    flags2)r)   rj   rx   r   r   r   d  s   zTemporaryFile.<locals>.openerr   r   r   Fc               
      sJ   t  \}zt| W S  ty$ } zt  d }~ww r$   )r{   r   r^   BaseExceptionr]   )r    rI   er)   rj   rx   r*   r'   r(   r   r   r     s   
)
r   r   r+   r[   _O_TMPFILE_WORKSrZ   r0   rI   IsADirectoryErrorr   )r   r   r   r   r(   r'   r)   r   r   rz   r   r   r   r   r   J  s@   


	r   c                   @   s  e Zd Z	 dZ			d7ddddZeejZd	d
 Z	dd Z
dd Zdd Zdd Zdd Zedd Zedd Zedd Zdd Zdd Zdd  Zed!d" Zed#d$ Zed%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd8d1d2Zd3d4 Zd5d6 Z dS )9r   Fr   r   r   Nr   c	          
   	   C   s^   d|v r
t  | _nt |}t jt  ||	|d| _|| _d| _||||||||	d| _d S )Nr   )r   r   r   F)r   r   r(   r'   r   r   r)   r   )r   BytesIO_filer   TextIOWrapper	_max_size_rolled_TemporaryFileArgs)
r3   max_sizer   r   r   r   r(   r'   r)   r   r   r   r   r     s   

zSpooledTemporaryFile.__init__c                 C   s4   | j rd S | j}|r| |kr|   d S d S d S r$   )r   r   tellrollover)r3   rz   r   r   r   r   _check  s
   
zSpooledTemporaryFile._checkc                 C   sv   | j rd S | j}tdi | j }| _| `| }t|dr)|j| 	  n||	  |
|d d| _ d S )Nr   r   Tr   )r   r   r   r   r   r   r   r\   detachgetvalueseek)r3   rz   newfileposr   r   r   r     s   


zSpooledTemporaryFile.rolloverc                 C   s   | j jrtd| S )Nz%Cannot enter context with closed file)r   closed
ValueErrorr6   r   r   r   r     s   zSpooledTemporaryFile.__enter__c                 C      | j   d S r$   r   r]   r3   r   r   r   r   r   r   r        zSpooledTemporaryFile.__exit__c                 C   
   | j  S r$   )r   r7   r6   r   r   r   r7        
zSpooledTemporaryFile.__iter__c                 C   r   r$   r   r6   r   r   r   r]     r   zSpooledTemporaryFile.closec                 C      | j jS r$   )r   r   r6   r   r   r   r        zSpooledTemporaryFile.closedc                 C   r   r$   )r   r   r6   r   r   r   r     r   zSpooledTemporaryFile.encodingc                 C   r   r$   )r   r   r6   r   r   r   r     r   zSpooledTemporaryFile.errorsc                 C   s   |    | j S r$   )r   r   filenor6   r   r   r   r     s   
zSpooledTemporaryFile.filenoc                 C   r   r$   )r   flushr6   r   r   r   r     r   zSpooledTemporaryFile.flushc                 C   r   r$   )r   isattyr6   r   r   r   r     r   zSpooledTemporaryFile.isattyc                 C   s(   z| j jW S  ty   | jd  Y S w )Nr   )r   r   rO   r   r6   r   r   r   r     s
   
zSpooledTemporaryFile.modec                 C   s    z| j jW S  ty   Y d S w r$   )r   rI   rO   r6   r   r   r   rI     s
   
zSpooledTemporaryFile.namec                 C   r   r$   )r   newlinesr6   r   r   r   r     r   zSpooledTemporaryFile.newlinesc                 G      | j j| S r$   )r   readr3   r    r   r   r   r     r   zSpooledTemporaryFile.readc                 G   r   r$   )r   readliner   r   r   r   r     r   zSpooledTemporaryFile.readlinec                 G   r   r$   )r   	readlinesr   r   r   r   r     r   zSpooledTemporaryFile.readlinesc                 G   r   r$   )r   r   r   r   r   r   r   
  r   zSpooledTemporaryFile.seekc                 C   r   r$   )r   r   r6   r   r   r   r     r   zSpooledTemporaryFile.tellc                 C   s8   |d u r| j   d S || jkr|   | j | d S r$   )r   truncater   r   )r3   sizer   r   r   r     s
   
zSpooledTemporaryFile.truncatec                 C      | j }||}| | |S r$   )r   r\   r   )r3   srz   rvr   r   r   r\        

zSpooledTemporaryFile.writec                 C   r   r$   )r   
writelinesr   )r3   iterablerz   r   r   r   r   r     r   zSpooledTemporaryFile.writelines)r   r   r   NNNNNr$   )!r?   r@   rA   r   r   classmethod_typesGenericAlias__class_getitem__r   r   r   r   r7   r]   rB   r   r   r   r   r   r   r   rI   r   r   r   r   r   r   r   r\   r   r   r   r   r   r     sN    







r   c                   @   sb   e Zd Z	 		dddZedddZedddZd	d
 Zdd Zdd Z	dd Z
eejZdS )r   NFc                 C   s:   t |||| _|| _tj| | j| jd| | jd| _d S )NzImplicitly cleaning up {!r})warn_messageignore_errors)r   rI   _ignore_cleanup_errors_weakreffinalize_cleanupformat
_finalizer)r3   r(   r'   r)   ignore_cleanup_errorsr   r   r   r   1  s   
zTemporaryDirectory.__init__c                    s"    fdd}t j|d d S )Nc              	      s   t |d trEdd }z/|kr|tj| || z	t| W W d S  ttfy7    j|d Y nw W d S  tyD   Y d S w t |d trNd S sQ d S )Nr   c                 S   s4   zt | d W n	 ty   Y nw t | d d S )Nr   r   )r   chflagsrO   chmod)rK   r   r   r   
resetperms>  s   z?TemporaryDirectory._rmtree.<locals>.onerror.<locals>.resetpermsr   )	
issubclassr`   r   rK   rS   r^   r   _rmtreerd   )r   rK   exc_infor   clsr   rI   r   r   onerror<  s(   z+TemporaryDirectory._rmtree.<locals>.onerror)r  )_shutilrmtree)r   rI   r   r  r   r   r   r   :  s   zTemporaryDirectory._rmtreec                 C   s   | j ||d t|t d S Nr   )r   	_warningswarnResourceWarning)r   rI   r   r   r   r   r   r   Y  s   zTemporaryDirectory._cleanupc                 C   s   d | jj| jS )Nz	<{} {!r}>)r   	__class__r?   rI   r6   r   r   r   __repr__^  s   zTemporaryDirectory.__repr__c                 C   s   | j S r$   )rI   r6   r   r   r   r   a  s   zTemporaryDirectory.__enter__c                 C   r   r$   )cleanupr   r   r   r   r   d  r   zTemporaryDirectory.__exit__c                 C   s2   | j  stj| jr| j| j| jd d S d S r  )r   r   r   rK   existsrI   r   r   r6   r   r   r   r
  g  s   zTemporaryDirectory.cleanupNNNF)F)r?   r@   rA   r   r   r   r   r	  r   r   r
  r   r   r   r   r   r   r   r   %  s    
	r   r  )NNN)r   r   NNNNNT)r   r   NNNNN)?__all__	functoolsr   warningsr  ior   osr   shutilr  errnore   randomr   r1   sysrs   typesr   weakrefr   _threadallocate_lock_allocate_lockO_RDWRr   O_EXCLr   r   r   r[   r   r   r%   rn   r   r#   r+   r,   rT   rk   rm   rq   r{   r	   r   r
   r   r   r   r   r   r   r   r   r   rI   platformr   r   r   r   r   r   r   r   <module>   s~    




	, 

'( +?5
K 