o
    ^hM                  	   @   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   -àòɘŁğdarwinZ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   (  /home/conda/feedstock_root/build_artifacts/python-split_1761171407825/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehol/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)Z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)	r.   r   r1   Ztmp_dirZtmp_fpZtmp_namefpZkernel_versionmr   r   r   	can_xattr   sL   

r<   c                 C   r   )zDSkip decorator for tests that require functional extended attributesz(no non-broken extended attribute support)r<   r    r!   r"   r   r   r   skip_unless_xattr   r'   r=   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$   



rX   c                 C      t tj|  d S r>   )rX   r   r   rB   r   r   r   r?        r?   c                 C   rY   r>   )rX   r   r9   rT   r   r   r   _rmdir  rZ   r\   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rS   )r   rK   rI   joinlstatst_moder   printsys
__stderr__statS_ISDIRrX   r9   r   )rI   rU   fullnamemodeexcr^   _rmtree_innerr   r   rl     s$   
_rmtree.<locals>._rmtree_innerTr_   c                    s    | t j| S r>   )r   r9   )pr]   r   r   <lambda>.  s    z_rmtree.<locals>.<lambda>)test.supportr^   rX   rI   r   rk   r   _rmtree  s   rr   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ImportErrorZcreate_unicode_bufferlenZwindllZkernel32ZGetLongPathNameW)rI   rs   bufferlengthr   r   r   	_longpath0  s   rx   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   r]   )rp   r^   r   rK   rI   r`   ra   rb   r   rf   rg   r9   r   )rI   r^   rU   rh   ri   rl   r   r   rl   I  s   
rm   )shutilrmtreer   r   r9   )rI   rz   r   ry   r   rr   A  s   
c                 C      | S r>   r   rq   r   r   r   rx   Y     c                 C   $   zt |  W d S  ty   Y d S w r>   )r\   r@   r[   r   r   r   r9   ]  
   r9   c                 C   r~   r>   )rr   r@   rq   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 :    rG   )r.   r2   r   rI   realpathmkdirr   rN   rO   rP   getpidr{   )rI   quietr.   Zdir_createdrj   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   rG   N)	r   getcwdchdirrI   r   r   rN   rO   rP   )rI   r   Z	saved_dirrj   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.

    )rI   r   )r   N)r   r   )rU   r   Z	temp_pathZ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   )rC   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   rI   isdirr   O_RDONLYr   )rI   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)	r.   NamedTemporaryFilerU   upperlowerr   rI   samefiler@   )Z	directoryr.   base	base_pathZ	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 r>   rq   )selfrI   r   r   r   __init__     
zFakePath.__init__c                 C   s   d| j dS )Nz
<FakePath >rq   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 r>   )
isinstancerI   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.
    )linuxZfreebsdz/proc/self/fdr,      sysconfSC_OPEN_MAXNwin32r   )rd   r1   
startswithr   rK   ru   r@   r0   r   r   msvcrtZCrtSetReportModer   rt   ZCRT_WARNZ	CRT_ERRORZ
CRT_ASSERTrangeduperrnoEBADFr   )	namesMAXFDZ	old_modesr   Z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   Z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 r>   )r   environ_environ_changedr   r   r   r   r   B  s   
zEnvironmentVarGuard.__init__c                 C   s
   | j | S r>   )r   r   envvarr   r   r   __getitem__F  r   zEnvironmentVarGuard.__getitem__c                 C   s*   || j vr| j|| j |< || j|< d S r>   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 r>   r   r   r   r   r   __delitem__O  s
   

zEnvironmentVarGuard.__delitem__c                 C   s
   | j  S r>   )r   keysr   r   r   r   r   V  r   zEnvironmentVarGuard.keysc                 C   
   t | jS r>   )iterr   r   r   r   r   __iter__Y  r   zEnvironmentVarGuard.__iter__c                 C   r   r>   )ru   r   r   r   r   r   __len__\  r   zEnvironmentVarGuard.__len__c                 C   s   || |< d S r>   r   r   r   r   r   set_  s   zEnvironmentVarGuard.setc                 C   s
   | |= d S r>   r   r   r   r   r   unsetb  r   zEnvironmentVarGuard.unsetc                 C   r|   r>   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 r>   )r   itemsr   r   r   )r   Z
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   r5   rf   rd   rL   r    rN   rU   ZTESTFN_ASCIIformatr   ZTESTFN_UNICODEr1   unicodedata	normalizeZTESTFN_UNENCODABLEgetwindowsversionencodegetfilesystemencodingUnicodeEncodeErrorrc   decodeUnicodeDecodeErrorZFS_NONASCII	characterfsdecodefsencodeUnicodeErrorr   ZSAVEDCWDZTESTFN_UNDECODABLEZTESTFN_NONASCIIr   r   r   r   r&   r/   r<   r=   r   r   rX   r?   r\   rr   rx   r9   r{   contextmanagerr   r   r   r   r   r   r   r   r0   r   abcMutableMappingr   r   r   r   r   <module>   s    




!
!
!
(

<
	