o
    f                     @   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 rtj|j|_qa| jrd	tj_ t !  | j"d urt#| j" | j$d urt%&| j$ t'| j(o| j(d
k | j)t_)tt drd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 )	zSet 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$    ^