o
    p̦iVe                     @   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	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 d dlmZ d dlmZ d dlmZ d dlmZ ejZG dd	 d	ejZed
krre  dS dS )    N)support)import_helper)	os_helper)script_helper)socket_helper)warnings_helperc                   @   s  e Zd Zedd Zedd Zdd Zdd Zd	d
 Zdd Z	e
d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$d% Ze
eed&d'd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Z d4d5 Z!d6d7 Z"d8d9 Z#d:d; Z$d<d= Z%d>d? Z&d@dA Z'dBdC Z(dDdE Z)dFdG Z*dHdI Z+dJdK Z,dLdM Z-G dNdO dOZ.G dPdQ dQZ/dRdS Z0dTdU Z1dVdW Z2e
eedXoeedYdZd[d\ Z3dld^d_Z4d`da Z5dbdc Z6ddde Z7dfdg Z8dhdi Z9djdk Z:d]S )mTestSupportc                 C   sD   t tj}tjddd| _tjddd| _t tj|d ks J d S )Nztest.support.warnings_helperz.*used in test_support.*)likeztest.test_supportz!.*You should NOT be seeing this.*   )lenwarningsfiltersr   ignore_deprecations_from_warnings_helper_token_test_support_tokenclsorig_filter_len r   (/usr/lib/python3.10/test/test_support.py
setUpClass   s   
zTestSupport.setUpClassc                 C   s4   t tj}t| j| j t tj|d ksJ d S )Nr
   )r   r   r   r   clear_ignored_deprecationsr   r   r   r   r   r   tearDownClass&   s   
zTestSupport.tearDownClassc                 C   sd   t jdd}t  t dt dd |D }W d   n1 s"w   Y  | t|d| dS )z9Test support.ignore_deprecations_from() silences warningsT)recordzYou should NOT be seeing this.c                 S      g | ]}t |jqS r   strmessage.0wr   r   r   
<listcomp>4       zDTestSupport.test_ignored_deprecations_are_silent.<locals>.<listcomp>Nr   )r   catch_warningsr   _warn_about_deprecationwarnDeprecationWarningassertEqualr   )selfwarning_objsmessagesr   r   r   $test_ignored_deprecations_are_silent/   s   z0TestSupport.test_ignored_deprecations_are_silentc                 C   s    t d | tjt jd d S )Nftplibfoo)r   import_moduleassertRaisesunittestSkipTestr(   r   r   r   test_import_module7   s   
zTestSupport.test_import_modulec                 C   s   t d d S )Nr,   )r   import_fresh_moduler2   r   r   r   test_import_fresh_module<   s   z$TestSupport.test_import_fresh_modulec                 C   s.   |  t| d| j | tjtj| d d S )Ntest_get_attributer-   )r'   r   get_attributer6   r/   r0   r1   r2   r   r   r   r6   ?   s   zTestSupport.test_get_attributezfailing buildbotsc                 C   s   |  t tj d S N)r'   r   get_original_stdoutsysstdoutr2   r   r   r   test_get_original_stdoutD   s   z$TestSupport.test_get_original_stdoutc                 C   s2   dd l }| dtj td | dtj d S )Nr   sched)r=   assertInr:   modulesr   unloadassertNotIn)r(   r=   r   r   r   test_unloadH   s   
zTestSupport.test_unloadc                 C   sX   t tddd}W d    n1 sw   Y  tt | tjt tt d S )Nr    utf-8encoding)openTESTFNr   unlinkassertFalseospathexists)r(   fr   r   r   test_unlinkN   s   
zTestSupport.test_unlinkc                 C   s`  t jd }tj|d}t| t| t | | tj| t	
t	dd t | W d    n1 s;w   Y  t| t| t|tjtjB  t	
t	dd t | W d    n1 skw   Y  | tj| t| t| t|d t	
t	dd t | W d    n1 sw   Y  | tj| d S )Ndsubdirverboser   )r   rG   rJ   rK   joinmkdirrmtreerI   rL   r   	swap_attrchmodstatS_IRUSRS_IXUSR)r(   dirpath
subdirpathr   r   r   test_rmtreeU   s.   







zTestSupport.test_rmtreec              	   C   s   t d }t|ddd}td|d W d    n1 sw   Y  tjdtj t	  z(t
t }| t tj tt  | t tj W tjd= t| td d S tjd= t| td w )	Nz.pyr    rC   rD   zfoo = 1filer   __pycache__)rG   rF   printr:   rK   insertrJ   curdir	importlibinvalidate_caches
__import__r>   r?   r   forgetrA   r   rH   rT   )r(   mod_filenamerM   modr   r   r   test_forgetm   s"   


zTestSupport.test_forgetc                 C   s   t tjdf}|  d S )Nr   )socketcreate_serverr   HOSTcloser(   sr   r   r   	test_HOST~   s   zTestSupport.test_HOSTc                 C   s$   t  }tt j|f}|  d S r8   )r   find_unused_portrj   rk   rl   rm   )r(   portro   r   r   r   test_find_unused_port   s   z!TestSupport.test_find_unused_portc                 C   s&   t   }t| |  |  d S r8   )rj   r   	bind_portlistenrm   rn   r   r   r   test_bind_port   s   
zTestSupport.test_bind_portc              	   C   s   t  }tj|}zEtj|d}| tj| t	|}| 
|| | tj| W d   n1 s:w   Y  | tj| W t| dS t| w )z8Test that temp_dir() creates and destroys its directory.tempN)tempfilemkdtemprJ   rK   realpathrR   rI   isdirr   temp_dirr'   
assertTruerT   )r(   
parent_dirrK   	temp_pathr   r   r   test_temp_dir   s   zTestSupport.test_temp_dirc                 C   sP   t  }| tj| W d   n1 sw   Y  | tj| dS )zTest passing no path.N)r   r|   r}   rJ   rK   r{   rI   )r(   r   r   r   r   test_temp_dir__path_none   s   
z$TestSupport.test_temp_dir__path_nonec              	   C   sl   dd }t  }tj|}z!| tj| | t|| | tj| W t	
| dS t	
| w )z-Test passing a directory that already exists.c                 S   (   t | }td1 sw   Y  d S Nzshould not get here)r   r|   	Exception)rK   r   r   r   r   call_temp_dir      zMTestSupport.test_temp_dir__existing_dir__quiet_default.<locals>.call_temp_dirN)rx   ry   rJ   rK   rz   r}   r{   r/   FileExistsErrorshutilrT   )r(   r   rK   r   r   r   *test_temp_dir__existing_dir__quiet_default   s   z6TestSupport.test_temp_dir__existing_dir__quiet_defaultc              	   C   s   t  }tj|}zIt -}tj|dd}| 	|| W d   n1 s(w   Y  dd |j
D }W d   n1 s?w   Y  | tj| W t| nt| w | 	t|d| |d }| |d|d	| dS )
z=Test passing a directory that already exists with quiet=True.TquietNc                 S   r   r   r   r   r   r   r   r!      r"   zGTestSupport.test_temp_dir__existing_dir__quiet_true.<locals>.<listcomp>   r   z5tests may fail, unable to create temporary directory : )rx   ry   rJ   rK   rz   r   check_warningsr   r|   r'   r   r}   r{   r   rT   r   
startswith)r(   rK   recorderr   r   r%   r   r   r   'test_temp_dir__existing_dir__quiet_true   s$   

z3TestSupport.test_temp_dir__existing_dir__quiet_trueforkztest requires os.forkc                 C   s   t dtd dS )z?Test that a forked child process does not remove the directory.-ca  
            import os
            from test import support
            from test.support import os_helper
            with os_helper.temp_cwd() as temp_path:
                pid = os.fork()
                if pid != 0:
                    # parent process

                    # wait for the child to terminate
                    support.wait_process(pid, exitcode=0)

                    # Make sure that temp_path is still present. When the child
                    # process leaves the 'temp_cwd'-context, the __exit__()-
                    # method of the context must not remove the temporary
                    # directory.
                    if not os.path.isdir(temp_path):
                        raise AssertionError("Child removed temp_path.")
        N)r   assert_python_oktextwrapdedentr2   r   r   r   test_temp_dir__forked_child   s   z'TestSupport.test_temp_dir__forked_childc              	   C   s   t  }t +}t|}| || | t  | W d    n1 s'w   Y  W d    n1 s6w   Y  | t  | d S r8   )rJ   getcwdr   r|   
change_cwdr'   )r(   original_cwdr   new_cwdr   r   r   test_change_cwd   s   
zTestSupport.test_change_cwdc                 C   sh   t  }dd }t }t j|d}| t|| W d   n1 s%w   Y  | t  | dS )z&Test passing a non-existent directory.c                 S   r   r   )r   r   r   )rK   r   r   r   r   call_change_cwd   r   zFTestSupport.test_change_cwd__non_existent_dir.<locals>.call_change_cwddoes_not_existN)	rJ   r   r   r|   rK   rR   r/   FileNotFoundErrorr'   )r(   r   r   r~   non_existent_dirr   r   r   !test_change_cwd__non_existent_dir   s   
z-TestSupport.test_change_cwd__non_existent_dirc              
   C   s   t  }t P}t j|d}t 5}tj|dd}| 	|| | 	t  | W d   n1 s5w   Y  dd |j
D }W d   n1 sLw   Y  W d   n1 s[w   Y  | 	t|d| |d }| |d	|d
| dS )z6Test passing a non-existent directory with quiet=True.r   Tr   Nc                 S   r   r   r   r   r   r   r   r!     r"   zMTestSupport.test_change_cwd__non_existent_dir__quiet_true.<locals>.<listcomp>r   r   Btests may fail, unable to change the current working directory to r   )rJ   r   r   r|   rK   rR   r   r   r   r'   r   r   r}   r   )r(   r   r~   bad_dirr   r   r   r%   r   r   r   -test_change_cwd__non_existent_dir__quiet_true   s&   


z9TestSupport.test_change_cwd__non_existent_dir__quiet_truec              	   C   s   t d }t '}tj|dd W d   n1 sw   Y  dd |jD }W d   n1 s2w   Y  | t|d| |d }| |	d	|d
| dS )z0Check the warning message when os.chdir() fails._does_not_existT)rK   r   Nc                 S   r   r   r   r   r   r   r   r!     r"   z>TestSupport.test_change_cwd__chdir_warning.<locals>.<listcomp>r   r   r   r   )
rG   r   r   r   r   r   r'   r   r}   r   )r(   rK   r   r*   msgr   r   r   test_change_cwd__chdir_warning  s   

z*TestSupport.test_change_cwd__chdir_warningc                 C   sr   t  }tjtd | t jt  t W d    n1 s!w   Y  | t j	t | t  | d S )Nname)
rJ   r   r   temp_cwdrG   r'   rK   basenamerI   rL   )r(   herer   r   r   test_temp_cwd#  s   zTestSupport.test_temp_cwdc                 C   sv   t  }tjdd}| || | t j| | t  | W d   n1 s,w   Y  | t  | dS )z Test passing None to temp_cwd().Nr   )	rJ   r   r   r   assertNotEqualr}   rK   r{   r'   )r(   r   r   r   r   r   test_temp_cwd__name_none+  s   z$TestSupport.test_temp_cwd__name_nonec                 C   s   |  tddddd d S )N   r
   r   )r   r
   r   z{1: 1, 2: 2, 3: 3})r'   r   sortdictr2   r   r   r   test_sortdict4  s   zTestSupport.test_sortdictc                 C   sT   t  }| t}t|d W d    n1 sw   Y  | |jjtj	 d S )Ns   foo)
r   make_bad_fdr/   OSErrorrJ   writer'   	exceptionerrnoEBADF)r(   fdcmr   r   r   test_make_bad_fd7  s
   zTestSupport.test_make_bad_fdc                 C   sN   t j| dddd | t t | d W d    d S 1 s w   Y  d S )Nz	def classr      )linenooffsetzx=1)r   check_syntax_errorr/   AssertionErrorr2   r   r   r   test_check_syntax_error=  s   "z#TestSupport.test_check_syntax_errorc                 C   sB   dd l }td |d W d    d S 1 sw   Y  d S )Nr   pprint)rc   r   CleanImportr.   )r(   rc   r   r   r   test_CleanImportB  s   "zTestSupport.test_CleanImportc                 C   sh   t dd | dtj | dtj W d    n1 sw   Y  | dtj | dtj d S )Nr-   bar)r   DirsOnSysPathr>   r:   rK   rA   r2   r   r   r   test_DirsOnSysPathG  s   zTestSupport.test_DirsOnSysPathc                 C   sD   t  }td W d    n1 sw   Y  | | d d S )Nhellohello
)r   captured_stdoutr`   r'   getvalue)r(   r;   r   r   r   test_captured_stdoutN  s   

z TestSupport.test_captured_stdoutc                 C   sJ   t  }tdtjd W d    n1 sw   Y  | | d d S )Nr   r]   r   )r   captured_stderrr`   r:   stderrr'   r   )r(   r   r   r   r   test_captured_stderrS  s   
z TestSupport.test_captured_stderrc                 C   sR   t  }|d |d t }W d    n1 sw   Y  | |d d S )Nr   r   r   )r   captured_stdinr   seekinputr'   )r(   stdincapturedr   r   r   test_captured_stdinX  s   


zTestSupport.test_captured_stdinc                 C   s   t   d S r8   )r   
gc_collectr2   r   r   r   test_gc_collect`  s   zTestSupport.test_gc_collectc                 C   s   |  t t d S r8   )assertIsInstancer   python_is_optimizedboolr2   r   r   r   test_python_is_optimizedc  s   z$TestSupport.test_python_is_optimizedc                 C   s  G dd d}| }d|_ t|dd}| |j d | |d W d    n1 s,w   Y  | |j d t|dd}| |jd | | W d    n1 sVw   Y  | t|d t|dd
 |`W d    n1 sww   Y  | t|d d S )Nc                   @   s   e Zd ZdS )z'TestSupport.test_swap_attr.<locals>.ObjN)__name__
__module____qualname__r   r   r   r   Objg  s    r   r   xr   y)r   r   rU   r'   r   assertIsNonerI   hasattr)r(   r   objr   r   r   r   r   test_swap_attrf  s"   zTestSupport.test_swap_attrc                 C   s   ddi}t |dd}| |d d | |d W d    n1 s$w   Y  | |d d t |dd}| |d d | | W d    n1 sPw   Y  | d| t |dd |d= W d    n1 spw   Y  | d| d S )Nr   r   r   r   )r   	swap_itemr'   r   rA   )r(   Dr   r   r   r   r   test_swap_itemw  s   zTestSupport.test_swap_itemc                   @      e Zd ZdZdZdZdZdS )zTestSupport.RefClassN)r   r   r   
attribute1
attribute2_hidden_attribute1__magic_1__r   r   r   r   RefClass  
    r   c                   @   r   )zTestSupport.OtherClassN)r   r   r   r   
attribute3r   __magic_2__r   r   r   r   
OtherClass  r   r   c                 C   sB   t | j| j}| dh| t | j| j}| ddh| d S )Nr   r   r   )r   detect_api_mismatchr   r   r'   )r(   missing_itemsr   r   r   test_detect_api_mismatch  s   z$TestSupport.test_detect_api_mismatchc                 C   sP   g d}t j| j| j|d}| t | t j| j| j|d}| t | d S )N)r   r   r   not_in_either)ignore)r   r   r   r   r'   set)r(   r   r   r   r   r    test_detect_api_mismatch__ignore  s   

z,TestSupport.test_detect_api_mismatch__ignorec                 C   sX   dh}dh}t j| t||d ddh}h d}t j| td||d | tt j| t d S )Ntempdirtemplate)extranot_exportedTextTestResultinstallHandler>   
load_testsTestProgramBaseTestSuite)zunittest.resultzunittest.casezunittest.suitezunittest.loaderzunittest.mainzunittest.runnerzunittest.signalszunittest.async_case)r   check__all__rx   r0   r/   r   )r(   r   r   r   r   r   test_check__all__  s    	zTestSupport.test_check__all__waitpidWNOHANGz need os.waitpid() and os.WNOHANGc                 C   s   t   t }|dkrtd t }t t j }t j}zHdt _t	
 }	 t |kr3| d tj}z|t_t   W |t_n|t_w t jrKntd q(d| }| ||  | t j W |t _n|t _w t   d S )Nr   FTtimeoutg?z2Warning -- reap_children() reaped child process %s)r   reap_childrenrJ   r   _exittime	monotonicSHORT_TIMEOUTenvironment_alteredioStringIOfailr:   
__stderr__sleepr>   r   r}   )r(   pidt0deadlinewas_alteredr   
old_stderrr   r   r   r   test_reap_children  s6   



zTestSupport.test_reap_childrenNc                 C   s   d| d| d}t jg|d|}dd tj D }tj|tjtjd|d}|d u r/|}| 	|j
 t| | 	|jd	 d S )
Nzfrom test.support import z; print(repr(z()))r   c                 S   s    i | ]\}}| d s||qS )PYTHON)r   )r   keyvaluer   r   r   
<dictcomp>  s    z-TestSupport.check_options.<locals>.<dictcomp>T)r;   r   universal_newlinesenvr   )r:   
executablerJ   environitems
subprocessrunPIPEDEVNULLr'   r;   rstriprepr
returncode)r(   argsfuncexpectedcodecmdr  procr   r   r   check_options  s   zTestSupport.check_optionsc                 C   s   g dgdgdgdgdgdgdgdgd	gd
gdgddgg dddgddgddgddgddgfD ]}| j |d | |d W d    n1 sGw   Y  q.| g dddg d S )Nz-B-sz-S-Ez-vz-bz-q-Iz-bbz-vvv-Wignore-Xdev)r3  r4  r5  faulthandler
importtimeshowrefcounttracemallocztracemalloc=3optsargs_from_interpreter_flags)r2  r1  r0  subTestr/  r(   r;  r   r   r    test_args_from_interpreter_flags  s6   z,TestSupport.test_args_from_interpreter_flagsc              	   C   sT   g dgdgdgfD ]}| j |d | |d W d    n1 s"w   Y  q	d S )Nz-Oz-OOz-OOOOr:  !optim_args_from_interpreter_flagsr=  r?  r   r   r   &test_optim_args_from_interpreter_flags  s   z2TestSupport.test_optim_args_from_interpreter_flagsc                 C   s  G dd d}|d}|d}t t dd  t g  | t | | t | t d d  | t | | t | t | gd  | t | | t | t dgd  | t | | t | t dgd  | t | | t | t dgd  | t | t d	gd  | t | t d
gd  | t | | t | t | | gd  | t | | t | t ddgd  | t | | t | W d    n1 sw   Y  t t dd  t jg d | t | | t | t d d  | t | | t | t d | g | t | | t | t d dg | t | | t | t d dg | t | | t | t d dg | t | t d d	g | t | t d d
g | t | | t | t d | | g | t | | t | t d ddg | t | | t | W d    d S 1 sw   Y  d S )Nc                   @   s   e Zd Zdd Zdd ZdS )z)TestSupport.test_match_test.<locals>.Testc                 S   s
   || _ d S r8   test_id)r(   rD  r   r   r   __init__+  s   
z2TestSupport.test_match_test.<locals>.Test.__init__c                 S   s   | j S r8   rC  r2   r   r   r   id.  s   z,TestSupport.test_match_test.<locals>.Test.idN)r   r   r   rE  rF  r   r   r   r   Test*  s    rG  z"test.test_os.FileTests.test_accessz'test.test_os.Win32ErrorTests.test_chdir_match_test_functest_osztest_*	filetests	FileTestsz*test_os.*.test_*test_access	DONTMATCH)ignore_patterns)r   rU   set_match_testsr}   
match_testrF  rI   )r(   rG  rL  
test_chdirr   r   r   test_match_test)  s~   
/$zTestSupport.test_match_testc              	   C   sN   t  }tttj}zt  }W t| nt| w | || d d S )Nr   )r   fd_countrJ   rF   __file__O_RDONLYrm   r'   )r(   startr   morer   r   r   test_fd_count  s   
zTestSupport.test_fd_countc                 C   sF   t  }tj}z|t_t| W |t_n|t_w | | | d S r8   )r  r  r:   r  r   print_warningr'   r   )r(   r   r+  r   r  r   r   r   check_print_warning  s   zTestSupport.check_print_warningc                 C   s   |  dd |  dd d S )Nr   zWarning -- msg
za
bzWarning -- a
Warning -- b
)rZ  r2   r   r   r   test_print_warning  s   zTestSupport.test_print_warningr8   );r   r   r   classmethodr   r   r+   r3   r5   r6   r0   skipr<   rB   rN   r\   ri   rp   rs   rv   r   r   r   r   
skipUnlessr   rJ   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r/  r@  rB  rR  rX  rZ  r[  r   r   r   r   r      st    





		

/"ir   __main__)r   rc   r  rJ   r   rj   rW   r"  r:   rx   r   r
  r0   r   testr   test.supportr   r   r   r   r   rG   TestCaser   r   mainr   r   r   r   <module>   s<         3