o
    fd                     @   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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 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)support)	os_helperg      ^@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 r9   ,/usr/lib/python3.10/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%   r7   r9   r9   r:   get_executeda   s   zRegrtest.get_executedFc              	   C   sv  |j }t|ttfs|s| j|j|f t|tr!| j| nYt|t	r3| j
| | j| nGt|tr?| j
| n;t|trK| j| n/t|tr_|s^| j| | j| nt|trk| j| nt|trtd| _ntd| |rt|ttfs| j| |j}|rdd lm  m} |D ]}z| j|| W q |jy   t|tj d  w d S d S )NTzinvalid test result: %rr   file)!name
isinstancer   r   r*   appendduration_secr
   r    r   r"   r#   r   r   r$   r   r!   r&   r   r%   r)   
ValueErrorremovexml_dataxml.etree.ElementTreeetreeElementTreer3   
fromstring
ParseErrorprintsys
__stderr__)r8   resultr'   	test_namerG   ETer9   r9   r:   accumulate_resultf   sH   






zRegrtest.accumulate_resultr   c                 C   s   | }|   }|d urd|dd| }t | j }tt|d\}}t|d\}}d|||f }| d| }|rA|d d }t|dd d S )	Nz
load avg: z.2f <   z%d:%02d:%02dT)flush)
getloadavgr,   r-   r.   divmodintrM   )r8   lineemptyload_avg	test_timeminssecshoursr9   r9   r:   log   s   zRegrtest.logc                 C   sh   | j jrd S || j | j }t| jt| j }|r'| j js'| d| }| d| d|  d S )N/[z] )	r   quietr0   r/   lenr!   r$   pgorc   )r8   
test_indextextr\   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r0ddlm	} ||j\}}||_|| _
t|j |jrU|j\}}}|dk sF|dk rUd}	t|	tjdd td |jr`t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@   rX      )r   rN   argvxmlpathr   junit_xml_listr3   worker_argstest.libregrtest.runtest_mprm   r6   r   args
huntrleaksrM   stderrexittempdirospath
expanduserr   )
r8   kwargsr   rs   rm   rQ   warmuprepetitions_msgr9   r9   r:   
parse_args   s&   


zRegrtest.parse_argsc                 C   s  || _ | jjr<tj| jd| _z"t| jd}|	 
 }|g| _ W d    n1 s,w   Y  W n	 ty;   Y nw | jjrg | _ td}ttjtj| jj*}|D ]}|ddd }|
 }||}|d urv| j |  qWW d    n1 sw   Y  t| j  | jjrt| j td d  }t }| jjr| jjD ]}	|	|v r||	 ||	 qg | j_| jjrt | jjt! t" }
nt | jj||}
| jjs| j p| jjp|
| _#n| j | _#| jjr| j#d d | _#z|
$| j#d }|
|d  | _%W n
 t&y   Y nw | jj'r7z| j#d | j#$| jj'= W n t(y6   t)d| jj' t*j+d Y nw | jj,rY| jj-d u rJt./d	| j_-t.0| jj- t.1| j# d S d S )
N
pynexttestrz\btest_[a-zA-Z0-9_]+\b#r   r   z1Couldn't find starting test (%s), using all testsr?   i )2r   r   singlerz   r{   joinr5   r2   openreadstripOSErrorfromfilerecompiler   SAVEDCWDsplitsearchrC   groupr   rh   r   r   r   copyexcluderu   rF   addtestdirr   listr=   r   indexr1   
IndexErrorstartrE   rM   rN   rw   	randomizerandom_seedrandom	randrangeseedshuffle)r8   r   fp	next_testregexr\   matchstdtestsnottestsargalltestsposr9   r9   r:   
find_tests   s   









zRegrtest.find_testsc                 C   s   | j D ]}t| qd S r<   )r   rM   )r8   rA   r9   r9   r:   
list_tests  s   

zRegrtest.list_testsc                 C   sX   |D ]'}t |tjjrqt |tjr| | qt |tjr)t|r)t	|
  qd S r<   )rB   unittestloader_FailedTest	TestSuite_list_casesTestCaser   
match_testrM   id)r8   suitetestr9   r9   r:   r     s   
zRegrtest._list_casesc              	   C   s   dt _t | jj| jj | jD ]%}t| j|}ztj	
|}| | W q tjy5   | j| Y qw | jrWttjd ttt| jddtjd t| jtjd d S d S )NFr?   r   skipped:)r   verboseset_match_testsr   match_testsignore_testsr   r   r   defaultTestLoaderloadTestsFromNamer   SkipTestr"   rC   rM   rN   rw   r   rg   r   )r8   rQ   abstestr   r9   r9   r:   
list_cases"  s   
zRegrtest.list_casesc           
      C   s|  d| j _d| j _d| j _|  | _|   | d t| j}| j	  |D ]~}|j
}| j| |jp5g }|jp:g }dd |D }dd |D }d| j _| j j}|sU|r| j jd u r_g | j _| j j| | j j| dd| j j }	| d	| d
|	 d n	| d	| d t| j |}|| j _| j|dd t|tr nq&| jrttt| jdd t| j |   d S )NTFz'Re-running failed tests in verbose modec                 S      g | ]^}}| d d qS rU   r   r   .0test_full_namer   r9   r9   r:   
<listcomp>D      z/Regrtest.rerun_failed_tests.<locals>.<listcomp>c                 S   r   r   r   r   r9   r9   r:   r   E  r   z
matching: , zRe-running z in verbose mode ()z in verbose mode)r'   r   zfailed again:)r   r   failfastverbose3get_tests_resultr(   rc   r   r&   clearrA   r'   rC   errorsfailuresr   extendr   r   rT   rB   r   r!   rM   r   rg   r   display_result)
r8   
rerun_listrP   rQ   r   r   error_namesfailure_namesorig_match_testsmatchingr9   r9   r:   rerun_failed_tests3  sF   







zRegrtest.rerun_failed_testsc                 C   s   | j jrd S t  td|    | jrtd t| j|   }|r4t  ttt	|dd t
| | jr_| j js_t  | jsT| jsT| jsTt	| jdkrTt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  qx| jrt  ttt	| jdd t
| j | jrt  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rt  ttt	| jdd t
| j d S d S )Nz== Tests result: %s ==z(Test suite interrupted by signal SIGINT.r   zomitted:r   AllrU   )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   rh   rM   r   r)   r=   r   r>   r   rg   r   r    rf   r!   r"   
print_slowr*   sortr   r$   formatr'   r%   )r8   omittedr_   r   r9   r9   r:   r   _  sd   



zRegrtest.display_resultc                 C   s  | j jrdd l}|jddd| _tj }d}| j jr%|dt| j j 7 }| 	| d }t
| jdD ]\}}t }|}|rDd||f }| || | jrcd	}	tt }
| jj|	t |
d
 |
d }nt| j |}| | t|tru nAt|}t | }|tkrd|t|f }nt|trd }tj D ]}||vr|drt| q| j jrt|| j r nq2|rt| d S 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localsrP   z%s in %sztest.) r   r   Tracer+   rN   moduleskeystimeoutr   rc   	enumerater   r,   r-   rl   dictr   runctxr   r   rT   rB   r   strr	   r
   
startswithr   unloadr   r   rM   )r8   r   save_modulesr   previous_testri   rQ   r.   rj   cmdr   rP   r_   moduler9   r9   r:   run_tests_sequential  sP   







zRegrtest.run_tests_sequentialc                 c   s6    	 |D ]}|V  | j r d S | jjr| jr d S qqr<   )r!   r   fail_env_changedr$   )r8   r   rQ   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	 }|r-t d| t dt
dt f  d S )	Nz==T)aliasedz	%s-endianz== cwd:z== CPU count:z== encodings: locale=%s, FS=%sF)rM   platformpython_implementationrN   versionr   	byteorderrz   getcwd	cpu_countlocalegetpreferredencodinggetfilesystemencoding)r8   r   r9   r9   r:   display_header  s   

zRegrtest.display_headerc                 C   s   g }| j r|d n | jjr| jr|d nt| j| j | j| j| jfs+|d | jr3|d |s:|d d	|}| j
rId| j
|f }|S )NFAILUREzENV CHANGEDzNO TEST RUNINTERRUPTEDSUCCESSr   z
%s then %s)r!   rC   r   r   r$   anyr    r"   r)   r   r(   )r8   rP   r9   r9   r:   r     s"   



zRegrtest.get_tests_resultc              
   C   s  | j js| j js| j js| j js| js| j js|   | j jr4| j j\}}}|dk r4d}t	|t
jdd | j jr?t	d| j j | j jrS| t| j| _d| _d| _nt| j| _dt| j| _t| jd | _| j jrd	d
lm} t
jdkr| jd u rd	dlm} z| | _W n ty } zt	d|  W Y d }~nd }~ww z||  W | jd ur| j   d | _d S d S | jd ur| j   d | _w | !  d S )N   zhWARNING: Running tests with --huntrleaks/-R and less than 3 warmup repetitions can give false positives!Trn   zUsing random seedr   z/{}r   r   )run_tests_multiprocesswin32)WindowsLoadTrackerz%Failed to create WindowsLoadTracker: )"r   headerrh   rf   r   r   ru   r   rv   rM   rN   stdoutr   r   foreverr   r   r   r/   r0   iterr   rg   use_mprt   r  r   r6   test.libregrtest.win_utilsr  r4   PermissionErrorcloser   )r8   r~   r   r   r   r  r  errorr9   r9   r:   	run_tests  sZ   




zRegrtest.run_testsc                 C   s   | j r+| jr%t| j d}|| jd  W d    n1 sw   Y  nt| j  | jr=| j }|jdd| j	j
d t  t | j }tdt|  td|    | j	jrftdt   d S d S )Nw
T)show_missingsummarycoverdirzTotal duration: %szTests result: %szleaks %d)r2   r1   r   writerz   unlinkr+   resultswrite_resultsr   r  rM   r,   r-   r.   r   r   runleakssystemgetpid)r8   r   r   durationr9   r9   r:   finalize-  s&   
zRegrtest.finalizec           
   
   C   s  | j js	| js	d S dd lm  m} |d}dddd}| jD ]%}|| |D ]}z||  t|	|d7  < W q) t
yD   Y q)w q | D ]\}}||t| qJtjtj| j j}t|d}||D ]}	||	 qlW d    d S 1 sw   Y  d S )Nr   
testsuites)r   r   r   wb)r   rq   r3   rH   rI   rJ   ElementrC   r[   getrE   itemsr=   r   rz   r{   r   r   r   r   tostringlistr  )
r8   rR   roottotalsr   kvrq   fsr9   r9   r:   save_xml_resultB  s,   


 "zRegrtest.save_xml_resultc                 C   st   | j jr	| j j| _| js0t r+td| _| jd u r!td| _tj| jd| _nt	
 | _tj| j| _d S )Nabs_builddirsrcdirbuild)r   ry   r5   	sysconfigis_python_buildget_config_varrz   r{   r   tempfile
gettempdirabspathr7   r9   r9   r:   set_temp_dir[  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_{})
rz   makedirsr5   r  r6   r   r   FS_NONASCIIr{   r   )r8   pidtest_cwdr9   r9   r:   create_temp_dirq  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	| qtd|  t
| qd S )Nr   ztest_python_*zCleanup %s directoryzRemove directory: %szRemove file: %s)globrz   r{   r   escaper5   rM   isdirr   rmtreer  )r8   r<  r{   rA   r9   r9   r:   cleanup  s   zRegrtest.cleanupNc              
   K   s   |  | |   | jjr|   td |  }z&tj|dd || j_	| 
|| W d    W d S 1 s9w   Y  W d S  ty` } ztjtdd t|j W Y d }~d S d }~ww )Nr   T)rf   )rx   )r   r5  r   r@  rN   rx   r;  r   temp_cwdry   _main
SystemExitfaulthandlerdump_traceback_laterEXIT_TIMEOUTcode)r8   r   r}   r:  excr9   r9   r:   main  s    

&zRegrtest.mainc                 C   s.   | j d ur
| j  S ttdrt d S d S )NrY   r   )r4   rY   hasattrrz   r7   r9   r9   r:   rY     s
   


zRegrtest.getloadavgc                 C   s  | j d urddlm} || j| j  | jjrtd | jjt_| jj	t_
t| j | | | jjr;|   td | jjrH|   td |   |   | jjr[| jr[|   |   |   | jrktd | jrstd | jjr| jrtd td d S )Nr   )run_tests_workerzPress any key to continue...ro      r  )r6   rt   rK  r   waitinputrh   r   PGOpgo_extendedPGO_EXTENDEDr   r   r   rN   rx   r   r  r   verbose2r!   r   r  r+  r)   r   r$   )r8   r   r}   rK  r9   r9   r:   rB    s8   









zRegrtest._main)F)r   r<   )__name__
__module____qualname____doc__r;   r>   rT   rc   rl   r   r   r   r   r   r   r   r   r   r   r   r  r  r+  r5  r;  r@  rI  rY   rB  r9   r9   r9   r:   r      s6    +

'I
,;7	1
	r   c                 K   s   t  jdd| i| dS )zRun the Python suite.r   Nr9   )r   rI  )r   r}   r9   r9   r:   rI    s   rI  r<   )-rD  r   rz   r   r   r   rN   r/  r2  r,   r   test.libregrtest.cmdliner   test.libregrtest.runtestr   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   test.libregrtest.setupr   test.libregrtest.pgor   test.libregrtest.utilsr   r   r   r   r   r   test.supportr   rF  r   rI  r9   r9   r9   r:   <module>   s4    D     D