a
    kxgs`                     @   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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STDTESTSNOTTESTSPASSEDFAILEDENV_CHANGEDSKIPPEDRESOURCE_DENIEDINTERRUPTEDCHILD_ERRORTEST_DID_NOT_RUNTIMEOUTPROGRESS_MIN_TIMEformat_test_result	is_failed)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 | _	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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 r9   +/usr/lib/python3.9/test/libregrtest/main.py__init__6   s.    
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&   r7   r9   r9   r:   get_executed`   s    zRegrtest.get_executedFc              	   C   s  |j }|j}|ttfvr.|s.| j|j|f |tkrD| j| n|t	tfv rb|s| j
| n|tkrx| j| n|tkr| j| nj|tkr| j| | j| nH|tkr| j| n2|tkrd| _n"|tkr| j
| ntd| |r|t	tthvr| j
| |j}|rdd lm  m} |D ]D}z| j|| W n& |jy|   t|tj d  Y n0 q<d S )NTzinvalid test result: %rr   file)!	test_nameresultr   r   r*   append	test_timer   r!   r	   r"   r
   r%   r   r#   r   r$   r   r&   r)   r   
ValueErrorremovexml_dataxml.etree.ElementTreeetreeElementTreer3   Z
fromstringZ
ParseErrorprintsys
__stderr__)r8   rB   r'   rA   okrG   ETer9   r9   r:   accumulate_resulte   sD    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intrK   )r8   lineemptyZload_avgrD   r9   r9   r:   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   quietr0   r/   lenr"   r%   pgorZ   )r8   
test_indextextrX   Zfailsr9   r9   r:   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@   rT      )r   rL   argvxmlpathr   Zjunit_xml_listr3   worker_argstest.libregrtest.runtest_mprc   r6   r   args
huntrleaksrK   stderrexittempdirospath
expanduserr   )
r8   kwargsr   rh   rc   rA   warmuprepetitions_msgr9   r9   r:   
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   joinr5   r2   openreadstripOSErrorZfromfilerecompiler   SAVEDCWDsplitsearchrC   groupr   r_   r   r   r   copyZexcluderj   rF   addZtestdirr   listr=   r    indexr1   
IndexErrorstartrE   rK   rL   rl   	randomizerandom_seedrandomZ	randrangeZseedZshuffle)r8   r   fpZ	next_testZregexrX   matchZstdtestsZnottestsargZalltestsposr9   r9   r:   
find_tests   sr    *


2










zRegrtest.find_testsc                 C   s   | j D ]}t| qd S r<   )r    rK   )r8   namer9   r9   r:   
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<   )
isinstanceunittestloaderZ_FailedTestZ	TestSuite_list_casesZTestCaser   Z
match_testrK   id)r8   suitetestr9   r9   r:   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   Zmatch_testsZignore_testsr    r   r   ZdefaultTestLoaderZloadTestsFromNamer   ZSkipTestr#   rC   rK   rL   rl   r   r^   r   )r8   rA   Zabstestr   r9   r9   r:   
list_cases   s    
zRegrtest.list_casesc                 C   s   d| j _d| j _d| j _|  | _|   | d | jd d  | _| jD ]F}| d| d d| j _t	| j |}| j
|dd |jtkrJ qqJ| jrttt| jdd t| j |   d S )	NTFz'Re-running failed tests in verbose modezRe-running z in verbose mode)r'   r   zfailed again:)r   r   failfastZverbose3get_tests_resultr(   rZ   r"   r'   r   rQ   rB   r   rK   r   r^   r   display_result)r8   rA   rB   r9   r9   r:   rerun_failed_tests1  s$    




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AllrR   )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_   rK   r   r)   r=   r    r>   r   r^   r   r!   r]   r"   r#   Z
print_slowr*   sortr   r%   formatr'   r&   )r8   ZomittedrD   r   r9   r9   r:   r   K  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 |}| | |jtkr q|t|}t | }|tkrd|t|f }n|jtk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localsrB   z%s in %sztest.) r   r   ZTracer+   rL   moduleskeysZtimeoutr   rZ   	enumerater   r,   r-   rb   dictr   Zrunctxr   r   rQ   rB   r   r   r   r   
startswithr   Zunloadr   r   rK   )r8   r   Zsave_modulesrv   Zprevious_testr`   rA   r.   ra   cmdr   rB   rD   moduler9   r9   r:   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%   )r8   r   rA   r9   r9   r:   _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)rK   platformZpython_implementationrL   versionr   	byteorderro   getcwd	cpu_countlocaleZgetpreferredencodinggetfilesystemencoding)r8   r   r9   r9   r:   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 RUNr   SUCCESSz, z
%s then %s)r"   rC   r   r   r%   anyr!   r#   r)   r{   r(   )r8   rB   r9   r9   r:   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} ||  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)r   headerr_   r]   rz   r   rj   r   rk   rK   rL   stdoutr   r   Zforeverr   r   r    r/   r0   iterr   r^   Zuse_mpri   r   r   )r8   rs   rt   ru   rv   r   r9   r9   r:   	run_tests  s<    
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)r2   r1   r|   writero   unlinkr+   ZresultsZwrite_resultsr   r   rK   r,   r-   r.   r   r   Zrunleakssystemgetpid)r8   r   rx   Zdurationr9   r9   r:   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   errorsZfailureswb)r   rg   r3   rH   rI   rJ   ZElementrC   rW   getrE   itemsr=   strro   rp   r{   r   r   r|   Ztostringlistr   )
r8   rO   rootZtotalsr   kvrg   fsr9   r9   r:   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   r5   	sysconfigZis_python_buildZget_config_varro   rp   r{   tempfileZ
gettempdirabspathr7   r9   r9   r:   set_temp_dir6  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   makedirsr5   r   r6   r   r   ZFS_NONASCIIrp   r{   )r8   pidtest_cwdr9   r9   r:   create_temp_dirL  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{   escaper5   rK   isdirr   Zrmtreer   )r8   r   rp   r   r9   r9   r:   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   rL   rm   r   r   Ztemp_cwdrn   _main
SystemExitfaulthandlerZdump_traceback_laterEXIT_TIMEOUTcode)r8   r   rr   r   excr9   r9   r:   mainh  s    

.zRegrtest.mainc                 C   s.   | j d ur| j  S ttdr*t d S d S )NrU   r   )r4   rU   hasattrro   r7   r9   r9   r:   rU     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 tjdkr| j d u rddlm} z| | _W n2 ty } ztd|  W Y d }~n
d }~0 0 zJ|   |   | jjr| jr|   W | jd urZ| j  d | _n| jd urX| j  d | _0 |   |   | jr|td | jrtd | jj r| j!rtd	 td d S )
Nr   )run_tests_workerzPress any key to continue...Zwin32)WindowsLoadTrackerz%Failed to create WindowsLoadTracker: re      r   )"r6   ri   r   r   waitinputr_   r   ZPGOZpgo_extendedZPGO_EXTENDEDr   r   r   rL   rm   r   r   Ztest.libregrtest.win_utilsr   r4   FileNotFoundErrorrK   r   r   Zverbose2r"   r   closer   r   r)   r   r%   )r8   r   rr   r   r   errorr9   r9   r:   r     sR    






$





zRegrtest._main)F)r   )N)__name__
__module____qualname____doc__r;   r>   rQ   rZ   rb   rw   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rU   r   r9   r9   r9   r:   r      s4   *
)
I
;7	 
	r   c                 K   s   t  jf d| i| dS )zRun the Python suite.r   N)r   r   )r   rr   r9   r9   r:   r     s    r   )N).rV   r   r   ro   r   r   r   rL   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   r   r   Ztest.libregrtest.setupr   Ztest.libregrtest.pgor   Ztest.libregrtest.utilsr   r   r   r   r   r   r   r   r   r9   r9   r9   r:   <module>   s2   L     0