o
    fM                  	   @   s  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
ejdkr0dZndZdee Zed ZejdkrMd dlZedeZdZejd	kre jd
kr~ed Zz	ee  W n	 eyq   Y n1w edee f  dZn#ejdkrz	de  W n ey   ede d ZY nw 	 dZdD ]ZzeeeekreW n	 ey   Y qw eZ e  Z!dZ"dD ]Zz	ee  W q ey   eee Z"Y  nw eree Z#ndZ#e#peZ$dd Z%da&dd Z'dd Z(da)dd Z*dd Z+dd Z,ej-dr.dGd!d"Z.d#d$ Z/d%d& Z0d'd( Z1d)d* Z2nej,Z/ej3Z0d+d( Z1d,d* Z2d-d. Z3d/d0 Z4ej5dHd1d2Z6ej5dGd3d4Z7ej5dId6d7Z8d8d9 Z9ej5d:d; Z:d<d= Z;G d>d? d?Z<d@dA Z=e>edBrej5dCdD Z?G dEdF dFej@jAZBdS )J    Njavaz$testz@testz	{}_{}_tmpu   -àòɘŁğdarwinNFDnt   u   -共Ł♡ͣztWARNING: The filename %r CAN be encoded by the filesystem encoding (%s). Unicode filename tests may not be effective   s   -surrogateescape )   æu   İu   Łu   φu   Кu   אu   ،u   تu   ก    u   €)s   wr   s   s   s   c                  C   s8   t td} z|  W |   tt S |   tt w )z`
    Create an invalid file descriptor by opening and closing a file and return
    its fd.
    wb)openTESTFNfilenocloseunlinkfile r   -/usr/lib/python3.10/test/support/os_helper.pymake_bad_fd   s   


r   c               
   C   s^   t d urt S td } z
tt|  d}W n tttfy%   d}Y |a |S w t|  |a |S )Ncan_symlinkTF)_can_symlinkr   ossymlinkOSErrorNotImplementedErrorAttributeErrorremove)symlink_pathcanr   r   r   r      s   
r   c                 C       t  }d}|r	| S t|| S )z8Skip decorator for tests that require functional symlinkz*Requires functional symlink implementation)r   unittestskiptestokmsgr   r   r   skip_unless_symlink      r(   c            	   	   C   sL  dd l } td ur
tS ttdsd}|a|S dd l}|  }| j|d\}}zsttdD}z0t	|dd t	|dd	 t	|
 dd | }td
|}|d u pZt|ddk}W n tyg   d}Y nw W d    n1 srw   Y  W tt t| t| |a|S W tt t| t| |a|S tt t| t| w )Nr   setxattrFdirr   s	   user.test    s   trusted.foos   42z2.6.(\d{1,2})   '   )tempfile
_can_xattrhasattrr   platformmkdtempmkstempr   r   r*   r   releaserematchintgroupr   r   rmdir)	r0   r    r3   tmp_dirtmp_fptmp_namefpkernel_versionmr   r   r   	can_xattr   sL   

rB   c                 C   r!   )zDSkip decorator for tests that require functional extended attributesz(no non-broken extended attribute support)rB   r"   r#   r$   r   r   r   skip_unless_xattr   r)   rC   c              	   C   s(   zt |  W d S  ttfy   Y d S w N)_unlinkFileNotFoundErrorNotADirectoryErrorfilenamer   r   r   r      s
   r   winFc                 C   s   | | |r	|}nt j|\}}|pd}d}|dk r9t |}|r&|s,d S ||v s,d S t| |d9 }|dk stjd| tdd d S )N.gMbP?g      ?r   z)tests may fail, delete still pending for    
stacklevel)	r   pathsplitlistdirtimesleepwarningswarnRuntimeWarning)funcpathnamewaitalldirnamenametimeoutLr   r   r   _waitfor   s$   



r^   c                 C      t tj|  d S rD   )r^   r   r   rH   r   r   r   rE        rE   c                 C   r_   rD   )r^   r   r;   rZ   r   r   r   _rmdir  r`   rb   c                    s>   ddl m   fddt| dd t fdd|  d S )	Nr   
_force_runc                    s    | t j| D ]L}t j| |}zt |j}W n ty7 } ztd||f tj	d d}W Y d }~nd }~ww t
|rLt|dd  |t j| q |t j| qd S )Nz-support.rmtree(): os.lstat(%r) failed with %sr   r   TrY   )r   rQ   rO   joinlstatst_moder   printsys
__stderr__statS_ISDIRr^   r;   r   )rO   r[   fullnamemodeexcrd   _rmtree_innerr   r   rr     s$   
_rmtree.<locals>._rmtree_innerTre   c                    s    | t j| S rD   )r   r;   )prc   r   r   <lambda>.  s    z_rmtree.<locals>.<lambda>)test.supportrd   r^   rO   r   rq   r   _rmtree  s   rx   c                 C   s^   zdd l }W n
 ty   Y | S w |t| d }|jj| |t|}|r-|d | S | S )Nr   r   )ctypesImportErrorcreate_unicode_bufferlenwindllkernel32GetLongPathNameW)rO   ry   bufferlengthr   r   r   	_longpath0  s   r   c                    sN   dd l }z||  W d S  ty   Y nw  fdd  |  t|  d S )Nr   c              	      s   ddl m} || tj| D ]5}tj| |}zt|j}W n ty)   d}Y nw t	
|r; | || tj| q|| tj| qd S )Nr   rc   )rv   rd   r   rQ   rO   rf   rg   rh   r   rl   rm   r;   r   )rO   rd   r[   rn   ro   rr   r   r   rr   I  s   
rs   )shutilrmtreer   r   r;   )rO   r   r   r   r   rx   A  s   
c                 C      | S rD   r   rw   r   r   r   r   Y     c                 C   $   zt |  W d S  ty   Y d S w rD   )rb   rF   ra   r   r   r   r;   ]  
   r;   c                 C   r   rD   )rx   rF   rw   r   r   r   r   d  r   r   c              
   c   s    ddl }d}| du r| } d}tj| } n-z	t|  d}W n# tyD } z|s, tjd| d| t	dd W Y d}~nd}~ww |rKt
 }z| V  W |r^|t
 kr`t|  dS dS dS |ro|t
 krpt|  w w w )	a  Return a context manager that creates a temporary directory.

    Arguments:

      path: the directory to create temporarily.  If omitted or None,
        defaults to creating a temporary directory using tempfile.mkdtemp.

      quiet: if False (the default), the context manager raises an exception
        on error.  Otherwise, if the path is specified and cannot be
        created, only a warning is issued.

    r   NFTz5tests may fail, unable to create temporary directory :    rM   )r0   r4   r   rO   realpathmkdirr   rT   rU   rV   getpidr   )rO   quietr0   dir_createdrp   pidr   r   r   temp_dirk  s>   

r   c              
   c   s    t  }zt t j|  W n# ty3 } z|s tjd| d| tdd W Y d}~nd}~ww zt  V  W t | dS t | w )ag  Return a context manager that changes the current working directory.

    Arguments:

      path: the directory to use as the temporary current working directory.

      quiet: if False (the default), the context manager raises an exception
        on error.  Otherwise, it issues only a warning and keeps the current
        working directory the same.

    zBtests may fail, unable to change the current working directory to r   r   rM   N)	r   getcwdchdirrO   r   r   rT   rU   rV   )rO   r   	saved_dirrp   r   r   r   
change_cwd  s&   r   tempcwdc              	   c   sv    t | |d*}t||d}|V  W d   n1 sw   Y  W d   dS W d   dS 1 s4w   Y  dS )a  
    Context manager that temporarily creates and changes the CWD.

    The function temporarily changes the current working directory
    after creating a temporary directory in the current directory with
    name *name*.  If *name* is None, the temporary directory is
    created using tempfile.mkdtemp.

    If *quiet* is False (default) and it is not possible to
    create or change the CWD, an error is raised.  If *quiet* is True,
    only a warning is raised and the original CWD is used.

    )rO   r   )r   N)r   r   )r[   r   	temp_pathcwd_dirr   r   r   temp_cwd  s   "r   c                 C   s(   t | t jt jB t jB }t | dS )z>Create an empty file. If the file already exists, truncate it.N)r   r   O_WRONLYO_CREATO_TRUNCr   )rI   fdr   r   r   create_empty_file  s   r   c              	   c   sD    t j| s	J t | t j}z|V  W t | dS t | w )z&Open a file descriptor to a directory.N)r   rO   isdirr   O_RDONLYr   )rO   dir_fdr   r   r   open_dir_fd  s   r   c              	   C   s   ddl }|j| d0}|j}| }||kr| }ztj||W W  d   S  ty9   Y W d   dS w 1 s=w   Y  dS )zODetects if the file system for the specified directory
    is case-insensitive.r   Nr+   F)	r0   NamedTemporaryFiler[   upperlowerr   rO   samefilerF   )	directoryr0   base	base_path	case_pathr   r   r   fs_is_case_insensitive  s   r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	FakePathz.Simple implementing of the path protocol.
    c                 C   s
   || _ d S rD   rw   )selfrO   r   r   r   __init__     
zFakePath.__init__c                 C   s   d| j dS )Nz
<FakePath >rw   r   r   r   r   __repr__  s   zFakePath.__repr__c                 C   s0   t | jtst | jtrt| jtr| j| jS rD   )
isinstancerO   BaseExceptiontype
issubclassr   r   r   r   
__fspath__  s   

zFakePath.__fspath__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r     s
    r   c            	      C   s  t jdrztd} t| d W S  ty   Y nw d}ttdr4ztd}W n	 t	y3   Y nw d}t jdkrez	d	dl
}|j W n ttfyO   Y nw i }|j|j|jfD ]
}||d	||< qZzMd	}t|D ],}zt|}W n t	y } z|jtjkr W Y d}~qld}~ww t| |d7 }qlW |dur|j|j|jfD ]
}||||  q|S |dur|j|j|jfD ]}||||  qw w )
z/Count the number of open file descriptors.
    )linuxfreebsdz/proc/self/fdr.      sysconfSC_OPEN_MAXNwin32r   )rj   r3   
startswithr   rQ   r|   rF   r2   r   r   msvcrtCrtSetReportModer   rz   CRT_WARN	CRT_ERROR
CRT_ASSERTrangeduperrnoEBADFr   )	namesMAXFD	old_modesr   report_typecountr   fd2er   r   r   fd_count  sv   






r   umaskc              	   c   s0    t | }zdV  W t | dS t | w )z8Context manager that temporarily sets the process umask.N)r   r   )r   oldmaskr   r   r   
temp_umask3  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d Zdd Zdd Zdd ZdS )EnvironmentVarGuardz_Class to help protect the environment variable properly.  Can be used as
    a context manager.c                 C   s   t j| _i | _d S rD   )r   environ_environ_changedr   r   r   r   r   B  s   
zEnvironmentVarGuard.__init__c                 C   s
   | j | S rD   )r   r   envvarr   r   r   __getitem__F  r   zEnvironmentVarGuard.__getitem__c                 C   s*   || j vr| j|| j |< || j|< d S rD   r   r   getr   r   valuer   r   r   __setitem__I  s   
zEnvironmentVarGuard.__setitem__c                 C   s6   || j vr| j|| j |< || jv r| j|= d S d S rD   r   r   r   r   r   __delitem__O  s
   

zEnvironmentVarGuard.__delitem__c                 C   s
   | j  S rD   )r   keysr   r   r   r   r   V  r   zEnvironmentVarGuard.keysc                 C   
   t | jS rD   )iterr   r   r   r   r   __iter__Y  r   zEnvironmentVarGuard.__iter__c                 C   r   rD   )r|   r   r   r   r   r   __len__\  r   zEnvironmentVarGuard.__len__c                 C   s   || |< d S rD   r   r   r   r   r   set_  s   zEnvironmentVarGuard.setc                 C   s
   | |= d S rD   r   r   r   r   r   unsetb  r   zEnvironmentVarGuard.unsetc                 C   r   rD   r   r   r   r   r   	__enter__e  r   zEnvironmentVarGuard.__enter__c                 G   sF   | j  D ]\}}|d u r|| jv r| j|= q|| j|< q| jt_d S rD   )r   itemsr   r   r   )r   
ignore_exckvr   r   r   __exit__h  s   
zEnvironmentVarGuard.__exit__N)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   =  s    r   )F)NF)r   F)Ccollections.abccollections
contextlibr   r   r7   rl   rj   rR   r"   rT   r[   TESTFN_ASCIIformatr   TESTFN_UNICODEr3   unicodedata	normalizeTESTFN_UNENCODABLEgetwindowsversionencodegetfilesystemencodingUnicodeEncodeErrorri   decodeUnicodeDecodeErrorFS_NONASCII	characterfsdecodefsencodeUnicodeErrorr   SAVEDCWDTESTFN_UNDECODABLETESTFN_NONASCIIr   r   r   r   r(   r1   rB   rC   r   r   r^   rE   rb   rx   r   r;   r   contextmanagerr   r   r   r   r   r   r   r   r2   r   abcMutableMappingr   r   r   r   r   <module>   s    




!
!
!
(

<
	