o
    éT•j²  ã                   @   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mZ d dlm	Z	m
Z
 zd dlZW n ey7   dZY nw d dlmZmZ dZdd„ Zdd	„ Zd
d„ ZdS )é    N)Úsupport)ÚTESTFN_UNDECODABLEÚFS_NONASCII)Úsetup_unraisable_hookÚsetup_threading_excepthookÚPYTHONREGRTEST_UNICODE_GUARDc           	   	   C   sj  zt j ¡ }W n ttfy   d }Y n-w tjd|d� g }ttdƒr)| 	tj
¡ ttdƒr4| 	tj¡ |D ]
}tj|d|d� q6tƒ  tƒ  t t j¡ | jr\t j dtj | j¡¡ t j ¡ D ](}t|dƒr{t|jƒD ]\}}tj |¡|j|< qmt|dd ƒr‰tj |j¡|_qa| jr•d	tj_ t  !¡  | j"d ur t #| j"¡ | j$d ur«t% &| j$¡ t '| j(o´| j(d
k¡ | j)t_)tt dƒrÈdd„ }t  *|¡ t+ƒ  t,ƒ  | j-d u�rt.tj/| j-d ƒt_/t.tj0| j-d ƒt_0t1tj2| j-ƒt_2t1tj3| j-ƒt_3t1tj/| j-ƒt_/t1tj0| j-ƒt_0| j4�rddl5m6} d|_7t8�r'tj9�r'tj: ;t< =¡ t8¡ d S t>�r3tj? ;t<t>¡ d S d S )NT)Úall_threadsÚfileÚSIGALRMÚSIGUSR1)Úchainr	   r   Ú__path__Ú__file__Fé   Úaddaudithookc                 S   s   d S ©N© )ÚnameÚargsr   r   ú-/usr/lib/python3.10/test/libregrtest/setup.pyÚ_test_audit_hookU   s   z%setup_tests.<locals>._test_audit_hooké(   é   )ÚRegressionTestResult)@ÚsysÚ
__stderr__ÚfilenoÚ
ValueErrorÚAttributeErrorÚfaulthandlerÚenableÚhasattrÚsignalÚappendr
   r   ÚregisterÚ_adjust_resource_limitsÚreplace_stdoutr   Úrecord_original_stdoutÚstdoutÚtestdirÚpathÚinsertÚosÚabspathÚmodulesÚvaluesÚ	enumerater   Úgetattrr   Ú
huntrleaksÚunittestÚBaseTestSuiteÚ_cleanupÚ_deactivate_opcacheÚmemlimitÚset_memlimitÚ	thresholdÚgcÚset_thresholdÚsuppress_msvcrt_assertsÚverboseÚuse_resourcesr   r   r   ÚtimeoutÚmaxÚSHORT_TIMEOUTÚLONG_TIMEOUTÚminÚLOOPBACK_TIMEOUTÚINTERNET_TIMEOUTÚxmlpathÚtest.support.testresultr   ÚUSE_XMLr   Úsupports_bytes_environÚenvironbÚ
setdefaultÚUNICODE_GUARD_ENVÚencoder   Úenviron)	ÚnsÚ	stderr_fdÚsignalsÚsignumÚmoduleÚindexr*   r   r   r   r   r   Úsetup_tests   sl   û


€



ÿrU   c                     sZ   t j‰ zˆ  ¡ } W n
 ty   Y dS w t| dˆ jdddd�t _‡ fdd„}t |¡ dS )	zˆSet stdout encoder error handler to backslashreplace (as stderr error
    handler) to avoid UnicodeEncodeError when printing a tracebackNÚwÚbackslashreplaceFÚ
)ÚencodingÚerrorsÚclosefdÚnewlinec                      s   t j ¡  ˆ t _d S r   )r   r(   Úcloser   ©r(   r   r   Úrestore_stdout‡   s   

z&replace_stdout.<locals>.restore_stdout)r   r(   r   r   ÚopenrY   Úatexitr$   )Úfdr_   r   r^   r   r&   s   s   úür&   c                  C   sØ   zddl } ddl m}m} W n
 ty   Y dS w |  |¡\}}d}||k rh||k rjt||ƒ}z|  |||f¡ td|› d|› �ƒ W dS  tt	fyg } ztd|› d|› d	|› d
�ƒ W Y d}~dS d}~ww dS dS )z5Adjust the system resource limits (ulimit) if needed.r   N)ÚRLIMIT_NOFILEÚRLIM_INFINITYi   zRaised RLIMIT_NOFILE: z -> z#Unable to raise RLIMIT_NOFILE from z to z: Ú.)
Úresourcerc   rd   ÚImportErrorÚ	getrlimitrC   Ú	setrlimitÚprintr   ÚOSError)rf   rc   rd   Úfd_limitÚmax_fdsÚdesired_fdsÚnew_fd_limitÚerrr   r   r   r%   �   s,   ÿ

ÿÿ€ÿûr%   )ra   r   r,   r"   r   r3   Útestr   Útest.support.os_helperr   r   r:   rg   Útest.libregrtest.utilsr   r   rL   rU   r&   r%   r   r   r   r   Ú<module>   s$    ÿ^