a
    "h5d                     @   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mZ d dlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ d dlmZ d dl m!Z! d dl"m#Z#m$Z$m%Z%m&Z& d dl'm(Z( dZ)G d	d
 d
Z*dddZ+dS )    N)_parse_args)	findtestsruntestget_abs_module	is_failedSTDTESTSNOTTESTSPROGRESS_MIN_TIMEPassedFailed
EnvChangedSkippedResourceDeniedInterrupted
ChildError	DidNotRun)setup_tests)setup_pgo_tests)removepycountformat_duration	printlist)supportg      ^@c                   @   s   e Zd ZdZdd Zdd Zd7ddZd8d
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"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd9d1d2Zd3d4 Zd5d6 Zd0S ):Regrtestal  Execute a test suite.

    This also parses command-line options and modifies its behavior
    accordingly.

    tests -- a list of strings containing test names (optional)
    testdir -- the directory in which to look for tests (optional)

    Users other than the Python test suite will certainly want to
    specify testdir; if it's omitted, the directory containing the
    Python test suite is searched for.

    If the tests argument is omitted, the tests listed on the
    command-line will be used.  If that's empty, too, then all *.py
    files beginning with test_ will be used.

    The other default arguments (verbose, quiet, exclude,
    single, randomize, findleaks, use_resources, trace, coverdir,
    print_slow, and random_seed) allow programmers calling main()
    directly to set the values that would normally be set by flags
    on the command line.
    c                 C   s   d | _ g | _g | _g | _g | _g | _g | _g | _g | _g | _	g | _
d | _d| _g | _d | _t | _d| _d| _d | _d | _d | _d | _d | _d | _d S )NF    )nstestsselectedgoodbadskippedresource_deniedsenvironment_changedrun_no_tests
need_rerunrerunfirst_resultinterrupted
test_timestracertime	monotonic
start_time
test_counttest_count_widthnext_single_testnext_single_filenametestsuite_xmlwin_load_trackertmp_dirworker_test_nameself r8   =/opt/python-3.9.21/usr/lib/python3.9/test/libregrtest/main.py__init__6   s0    
zRegrtest.__init__c                 C   s<   t | jt | jB t | jB t | jB t | jB t | jB S N)setr   r    r!   r"   r#   r$   r6   r8   r8   r9   get_executeda   s    zRegrtest.get_executedFc              	   C   s  |j }t|ttfs*|s*| j|j|f t|trB| j| nt|t	rf| j
| | j| nt|tr~| j
| nvt|tr| j| n^t|tr|s| j| | j| n6t|tr| j| nt|trd| _ntd| |rt|ttfs| j| |j}|r~dd lm  m} |D ]D}z| j|| W n& |jyx   t|tj d  Y n0 q8d S )NTzinvalid test result: %rr   file)!name
isinstancer   r   r)   appendZduration_secr
   r   r   r!   r"   r   r   r#   r   r    r%   r   r$   r(   
ValueErrorremovexml_dataxml.etree.ElementTreeetreeElementTreer2   Z
fromstringZ
ParseErrorprintsys
__stderr__)r7   resultr&   	test_namerE   ETer8   r8   r9   accumulate_resultf   s@    






zRegrtest.accumulate_resultr   c                 C   st   | }|   }|d ur(d|dd| }t | j }tjt|d}| d| }|rd|d d }t|dd d S )Nz
load avg: z.2f )ZsecondsT)flush)
getloadavgr+   r,   r-   datetimeZ	timedeltaintrI   )r7   lineemptyZload_avg	test_timer8   r8   r9   log   s    zRegrtest.logc                 C   sh   | j jrd S || j | j }t| jt| j }|rN| j jsN| d| }| d| d|  d S )N/[z] )	r   quietr/   r.   lenr    r#   pgorZ   )r7   
test_indextextrW   Zfailsr8   r8   r9   display_progress   s    zRegrtest.display_progressc           
      C   s   t tjdd  fi |}|jr,g  t_| _|j}|d ur`ddlm	} ||j\}}||_|| _
t|j |jr|j\}}}|dk s|dk rd}	t|	tjdd td |jrtj|j|_|| _d S )Nr   r   )parse_worker_argszwInvalid values for the --huntrleaks/-R parameters. The number of warmups and repetitions must be at least 1 each (1:1).Tr?   rS      )r   rJ   argvxmlpathr   Zjunit_xml_listr2   worker_argstest.libregrtest.runtest_mprc   r5   r   args
huntrleaksrI   stderrexittempdirospath
expanduserr   )
r7   kwargsr   rh   rc   rM   warmuprepetitions_msgr8   r8   r9   
parse_args   s&    

zRegrtest.parse_argsc                 C   s  || _ | jjrxtj| jd| _zDt| jd$}|	 
 }|g| _ W d    n1 sX0    Y  W n tyv   Y n0 | jjrg | _ td}ttjtj| jjT}|D ]>}|ddd }|
 }||}|d ur| j |  qW d    n1 s0    Y  t| j  | jjr.t| j td d  }t }| jjr| jjD ]$}	|	|v rl||	 ||	 qTg | j_| jjrt | jjt! t" }
nt | jj||}
| jjs| j p| jjp|
| _#n| j | _#| jjr0| j#d d | _#z"|
$| j#d }|
|d  | _%W n t&y.   Y n0 | jj'rz| j#d | j#$| jj'= W n* t(y   t)d| jj' t*j+d Y n0 | jj,r| jj-d u rt./d	| j_-t.0| jj- t.1| j# d S )
NZ
pynexttestrz\btest_[a-zA-Z0-9_]+\b#r   r   z1Couldn't find starting test (%s), using all testsr>   i )2r   r   singlero   rp   joinr4   r1   openreadstripOSErrorfromfilerecompiler   SAVEDCWDsplitsearchrB   groupr   r_   r   r   r   copyZexcluderj   rD   addZtestdirr   listr<   r   indexr0   
IndexErrorstartrC   rI   rJ   rl   	randomizerandom_seedrandom	randrangeseedshuffle)r7   r   fpZ	next_testZregexrW   matchZstdtestsZnottestsargZalltestsposr8   r8   r9   
find_tests   sr    *


2










zRegrtest.find_testsc                 C   s   | j D ]}t| qd S r;   )r   rI   )r7   r@   r8   r8   r9   
list_tests  s    
zRegrtest.list_testsc                 C   sX   |D ]N}t |tjjrqt |tjr0| | qt |tjrt|rt	|
  qd S r;   )rA   unittestloaderZ_FailedTestZ	TestSuite_list_casesZTestCaser   Z
match_testrI   id)r7   suitetestr8   r8   r9   r     s    
zRegrtest._list_casesc              	   C   s   dt _t | jj| jj | jD ]L}t| j|}ztj	
|}| | W q  tjyj   | j| Y q 0 q | jrttjd ttt| jddtjd t| jtjd d S )NFr>   r   skipped:)r   verboseZset_match_testsr   match_testsZignore_testsr   r   r   ZdefaultTestLoaderZloadTestsFromNamer   ZSkipTestr!   rB   rI   rJ   rl   r   r^   r   )r7   rM   Zabstestr   r8   r8   r9   
list_cases  s    
zRegrtest.list_casesc           
      C   s  d| j _d| j _d| j _|  | _|   | d t| j}| j	  |D ] }|j
}| j| |jplg }|jpvg }dd |D }dd |D }d| j _| j j}|s|r
| j jd u rg | j _| j j| | j j| dd| j j }	| d	| d
|	 d n| d	| d t| j |}|| j _| j|dd t|trL qPqL| jrxttt| jdd t| j |   d S )NTFz'Re-running failed tests in verbose modec                 S   s   g | ]^}}| d d qS rQ   r   r   .0Ztest_full_nameru   r8   r8   r9   
<listcomp>A      z/Regrtest.rerun_failed_tests.<locals>.<listcomp>c                 S   s   g | ]^}}| d d qS r   r   r   r8   r8   r9   r   B  r   z
matching: , zRe-running z in verbose mode ()z in verbose mode)r&   r   zfailed again:)r   r   failfastZverbose3get_tests_resultr'   rZ   r   r%   clearr@   r&   rB   errorsfailuresr   extendr{   r   rP   rA   r   r    rI   r   r^   r   display_result)
r7   Z
rerun_listrL   rM   r   r   Zerror_namesZfailure_namesZorig_match_testsZmatchingr8   r8   r9   rerun_failed_tests0  sD    









zRegrtest.rerun_failed_testsc                 C   s
  | j jrd S t  td|    | jr0td t| j|   }|rht  ttt	|dd t
| | jr| j jst  | js| js| jst	| jdkrtddd ttt	| jdd	 | j jr| jjd
d t  td | jd d D ]\}}td|t|f  q| jr>t  ttt	| jdd t
| j | jrpt  tdtt	| jd t
| j | jr| j jst  ttt	| jdd t
| j | jrt  tdtt	| jd  t
| j | jrt  ttt	| jdd t
| j d S )Nz== Tests result: %s ==z(Test suite interrupted by signal SIGINT.r   zomitted:r   ZAllrQ   )endzOK.T)reversez10 slowest tests:
   z- %s: %szfailed:z%{} altered the execution environment:r   z%s:zre-run testzrun no tests:)r   r_   rI   r   r(   r<   r   r=   r   r^   r   r   r]   r    r!   Z
print_slowr)   sortr   r#   formatr&   r$   )r7   ZomittedrY   r   r8   r8   r9   r   \  sd    




zRegrtest.display_resultc                 C   s  | j jr dd l}|jddd| _tj }d}| j jrJ|dt| j j 7 }| 	| d }t
| jdD ]\}}t }|}|rd||f }| || | jrd	}	tt }
| jj|	t |
d
 |
d }nt| j |}| | t|tr q|t|}t | }|tkrd|t|f }nt|tr.d }tj D ]&}||vr8|dr8t| q8| j jrdt|| j rd q|qd|rt| d S )Nr   FT)tracer   zRun tests sequentiallyz (timeout: %s)r   z%s -- %szDresult = runtest(self.ns, test_name); self.accumulate_result(result))globalslocalsrL   z%s in %sztest.) r   r   ZTracer*   rJ   moduleskeystimeoutr   rZ   	enumerater   r+   r,   rb   dictr   Zrunctxr   r   rP   rA   r   strr	   r
   
startswithr   Zunloadr   r   rI   )r7   r   Zsave_modulesrv   Zprevious_testr`   rM   r-   ra   cmdr   rL   rY   moduler8   r8   r9   run_tests_sequential  sJ    






zRegrtest.run_tests_sequentialc                 c   s6   |D ]*}|V  | j r d S | jjr| jr d S qq d S r;   )r    r   fail_env_changedr#   )r7   r   rM   r8   r8   r9   _test_forever  s    zRegrtest._test_foreverc                 C   sx   t dt gtj R   t dtjdddtj  t dt  t	 }|rZt d| t dt
dt f  d S )	Nz==T)Zaliasedz	%s-endianz== cwd:z== CPU count:z== encodings: locale=%s, FS=%sF)rI   platformZpython_implementationrJ   versionr   	byteorderro   getcwd	cpu_countlocalegetpreferredencodinggetfilesystemencoding)r7   r   r8   r8   r9   display_header  s    
zRegrtest.display_headerc                 C   s   g }| j r|d n@| jjr0| jr0|d n&t| j| j | j| j| jfsV|d | jrf|d |st|d d	|}| j
rd| j
|f }|S )NFAILUREzENV CHANGEDzNO TEST RUNZINTERRUPTEDSUCCESSr   z
%s then %s)r    rB   r   r   r#   anyr   r!   r(   r{   r'   )r7   rL   r8   r8   r9   r     s"    



zRegrtest.get_tests_resultc              
   C   s  | j js.| j js6| j js6| j js6| js6| j js6|   | j jrh| j j\}}}|dk rhd}t	|t
jdd | j jr~t	d| j j | j jr| t| j| _d| _d| _n.t| j| _dt| j| _t| jd | _| j jrd	d
lm} t
jdkrP| jd u rPd	dlm} z| | _W n4 tyN } zt	d|  W Y d }~n
d }~0 0 z(||  W | jd ur| j   d | _n| jd ur| j   d | _0 n| !  d S )N   zhWARNING: Running tests with --huntrleaks/-R and less than 3 warmup repetitions can give false positives!Trd   zUsing random seedr   z/{}r   r   )run_tests_multiprocesswin32)WindowsLoadTrackerz%Failed to create WindowsLoadTracker: )"r   headerr_   r]   rz   r   rj   r   rk   rI   rJ   stdoutr   r   Zforeverr   r   r   r.   r/   iterr   r^   Zuse_mpri   r   r   r5   Ztest.libregrtest.win_utilsr   r3   PermissionErrorcloser   )r7   rs   rt   ru   rv   r   r   errorr8   r8   r9   	run_tests  sV    
$



zRegrtest.run_testsc                 C   s   | j rV| jrJt| j d }|| jd  W d    qV1 s>0    Y  nt| j  | jrz| j }|jdd| j	j
d t  t | j }tdt|  td|    | j	jrtdt   d S )Nw
T)Zshow_missingZsummarycoverdirzTotal duration: %szTests result: %szleaks %d)r1   r0   r|   writero   unlinkr*   resultsZwrite_resultsr   r   rI   r+   r,   r-   r   r   Zrunleakssystemgetpid)r7   r   rx   Zdurationr8   r8   r9   finalize*  s     0
zRegrtest.finalizec           
   
   C   s  | j js| jsd S dd lm  m} |d}dddd}| jD ]L}|| |D ]8}z ||  t|	|d7  < W qR t
y   Y qR0 qRq@| D ]\}}||t| qtjtj| j j}t|d*}||D ]}	||	 qW d    n1 s0    Y  d S )Nr   Z
testsuites)r   r   r   wb)r   rg   r2   rF   rG   rH   ZElementrB   rV   getrC   itemsr<   r   ro   rp   r{   r   r   r|   Ztostringlistr   )
r7   rN   rootZtotalsr   kvrg   fsr8   r8   r9   save_xml_result?  s$    


 
zRegrtest.save_xml_resultc                 C   st   | j jr| j j| _| js`t rVtd| _| jd u rBtd| _tj| jd| _n
t	
 | _tj| j| _d S )NZabs_builddirZsrcdirZbuild)r   rn   r4   	sysconfigZis_python_buildZget_config_varro   rp   r{   tempfile
gettempdirabspathr6   r8   r8   r9   set_temp_dirX  s    


zRegrtest.set_temp_dirc                 C   sV   t j| jdd t  }| jd ur.d|}n
d|}|tj7 }t j	| j|}|S )NT)exist_okztest_python_worker_{}ztest_python_{})
ro   makedirsr4   r   r5   r   r   ZFS_NONASCIIrp   r{   )r7   pidtest_cwdr8   r8   r9   create_temp_dirn  s    


zRegrtest.create_temp_dirc                 C   sz   dd l }tj|| jd}td| j  | |D ]>}tj|r^td|  t	| q6td|  t
| q6d S )Nr   ztest_python_*zCleanup %s directoryzRemove directory: %szRemove file: %s)globro   rp   r{   escaper4   rI   isdirr   rmtreer   )r7   r   rp   r@   r8   r8   r9   cleanup}  s    zRegrtest.cleanupNc              
   K   s   |  | |   | jjr,|   td |  }zFtj|dd$ || j_	| 
|| W d    n1 sn0    Y  W n> ty } z&tjtdd t|j W Y d }~n
d }~0 0 d S )Nr   T)r]   )rm   )rw   r   r   r   rJ   rm   r   r   Ztemp_cwdrn   _main
SystemExitfaulthandlerZdump_traceback_laterEXIT_TIMEOUTcode)r7   r   rr   r   excr8   r8   r9   main  s    

.zRegrtest.mainc                 C   s.   | j d ur| j  S ttdr*t d S d S )NrT   r   )r3   rT   hasattrro   r6   r8   r8   r9   rT     s
    


zRegrtest.getloadavgc                 C   s  | j d ur$ddlm} || j| j  | jjr4td | jjt_| jj	t_
t| j | | | jjrv|   td | jjr|   td |   |   | jjr| jr|   |   |   | jrtd | jrtd | jjr| jrtd td d S )Nr   )run_tests_workerzPress any key to continue...re      r   )r5   ri   r  r   waitinputr_   r   ZPGOZpgo_extendedZPGO_EXTENDEDr   r   r   rJ   rm   r   r   r   Zverbose2r    r   r   r   r(   r   r#   )r7   r   rr   r  r8   r8   r9   r     s8    









zRegrtest._main)F)r   )N)__name__
__module____qualname____doc__r:   r=   rP   rZ   rb   rw   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  rT   r   r8   r8   r8   r9   r      s4   +
'
I
,;7	1
	r   c                 K   s   t  jf d| i| dS )zRun the Python suite.r   N)r   r  )r   rr   r8   r8   r9   r    s    r  )N),rU   r  r   ro   r   r   r   rJ   r   r   r+   r   Ztest.libregrtest.cmdliner   Ztest.libregrtest.runtestr   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   Ztest.libregrtest.setupr   Ztest.libregrtest.pgor   Ztest.libregrtest.utilsr   r   r   r   r   r   r  r   r  r8   r8   r8   r9   <module>   s2   D     A