
    Rhf                     X   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m	Z	m
Z
 ddlmZmZ ddlmZmZmZ ddlmZ ddlmZ ddlmZ dd	lmZmZ dd
lmZmZ ddlmZmZ ddl m!Z!m"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/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9  G d d      Z:ddZ;y)    N)	os_helper
MS_WINDOWSflush_std_streams   )_parse_args	Namespace)	findtestssplit_test_packages
list_cases)Logger)setup_pgo_tests)State)TestResultsEXITCODE_INTERRUPTED)RunTestsHuntRefleak)setup_processsetup_test_dir)run_single_testPROGRESS_MIN_TIME)setup_tsan_tests)StrPathStrJSONTestNameTestList	TestTuple
TestFilterstrip_py_suffixcountformat_duration	printlistget_temp_dirget_work_direxit_timeoutdisplay_headercleanup_temp_dirprint_warningis_cross_compiledget_host_runnerprocess_cpu_countEXIT_TIMEOUTc                   \   e Zd ZdZd%dedefdZd&dZd'dedz  de	e
edz  f   fd	Zede
fd
       ZdefdZdefdZdedededefdZdeddfdZd ZdedefdZd Zd ZdededdfdZd Zd Zde
fdZde
dedz  defdZde
dedz  defdZd Z d  Z!d! Z"d" Z#d# Z$d'dedz  fd$Z%y)(Regrtestaa  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, 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.
    ns_add_python_optsc                    t        |j                        | _        |j                  | _        |j                  | _        |j                  | _        |j
                  | _        t               | _        d | _        t        | j                  | j                  | j                        | _
        |j                  | _        |j                  | _        |j                  | _        |j"                  | _        |j&                  | _        |j*                  | _        |j.                  | _        |j2                  | _        |j6                  xs |j8                  | _        |xr |j<                  | _        |j@                  | _         |jB                  | _!        |jD                  | _"        |jF                  | _$        |jJ                  | _&        |jN                  d}n|jN                  dk  rd}n|jN                  }|| _(        |jR                  | _)        |jT                  | _+        |jX                  | _,        |jZ                  | _-        |j\                  | _.        |j^                  | _0        |jb                  | _1        |jd                  rB|jd                  \  }}}tf        jh                  jk                  |      }tm        |||      | _7        nd | _7        |jp                  | _9        |jt                  | _;        |jx                  | _=        |j|                  | _?        t        |j                        | _A        |j                  rt        |j                        | _C        nd | _C        |j                  | _E        |j                  | _G        |j                  | _I        |j                  | _J        dtf        j                  v r3tf        j                  d   r d| _J        tf        j                  d   | _L        n8|j                  t        j                  d      | _L        n|j                  | _L        d | _O        |j                  | _Q        t        j                         | _T        |j                  | _V        d | _W        d | _X        y )Nr   SOURCE_DATE_EPOCHF    )Yintverbosequietpgopgo_extendedtsanr   resultsfirst_stater   loggerheaderwant_header
list_testswant_list_testsr   want_list_caseswait	want_waitcleanupwant_cleanuprerun
want_rerunrunleakswant_run_leaksbisectwant_bisectfast_cislow_cici_moder/   want_add_python_optsmatch_testsexcludefromfilestartstarting_testargscmdline_argsuse_mpnum_workersworker_jsonfailfast	fail_fastfail_env_changed
fail_rerunforeververbose3output_on_failuretimeout
huntrleaksospathabspathr   hunt_refleaktestdirtest_dirxmlpathjunit_filenamememlimitmemory_limit	thresholdgc_thresholdtupleuse_resourcespython
python_cmdtracecoveragecoverdircoverage_dirtempdirtmp_dir	randomizeenvironrandom_seedrandomgetrandbitsfirst_runtests
print_slowprint_slowesttimeperf_counter
start_timesinglesingle_test_runnext_single_testnext_single_filename)selfr.   r/   rX   warmupsrunsfilenames          ,/usr/lib/python3.12/test/libregrtest/main.py__init__zRegrtest.__init__5   s5   

O88
"$//''	 %0M'+ T\\4::txx@ "$%']]%']]!ww"$** "$&KK!# jj6BJJ+; ,C/1/B/B 	! (*~~ZZ(*.0hh&(gg 99KYY!^K))K ++->>  "{{&(&9&9 "ZZ'){{%'ZZ==&(mm#GT8wwx0H4?x4XD $D(*

.0jj(*(*.3B4D4D.E996;BII6FDO"DO hh,.KK')zz  "||2::-

./"DN +-**5H*ID^^#%11"5D!~~D 04 $&== ++- &(YY1548!    c                 :    | j                   j                  |       y N)r<   log)r   lines     r   r   zRegrtest.log   s    r   Ntestsreturnc                    | j                   rpt        j                  j                  | j                  d      | _        	 t        | j
                  d      5 }|j                         j                         }|g}d d d        | j                  rg }t        j                  d      }t        t        j                  j                  t        j                  | j                              5 }|D ]Z  }|j                  dd      d   }|j                         }|j!                  |      }|<|j#                  |j%                                \ 	 d d d        t'        |       | j(                  r t+        | j,                  | j.                         | j0                  rt3        | j,                         t5               }| j6                  r)| j,                  D ]  }|j9                  |        g | _        t;        | j<                  |      }	| j                  s!|xs | j,                  }
|
rt?        |
      }
n|	}
n|}
| j                   r'|
d d }
	 |	jA                  |
d         }|	|dz      | _!        | jF                  r	 |
d |
jA                  | jF                        = tQ        jR                  | jT                         | jV                  rtQ        jX                  |
       t[        |
      |fS # 1 sw Y   _xY w# t        $ r Y nw xY w# 1 sw Y   xY w# tD        $ r Y w xY w# tH        $ r0 tK        d| jF                          tM        jN                  d       Y w xY w)	N
pynexttestrz\btest_[a-zA-Z0-9_]+\b#r   r   )rg   rQ   zCannot find starting test: ).r   rc   rd   joinrx   r   openreadstripOSErrorrR   recompiler   SAVEDCWDsplitsearchappendgroupr   r7   r   rV   r8   r9   r   setrQ   addr	   rh   r
   indexr   
IndexErrorrT   
ValueErrorprintsysexitr|   seedr{   ry   shufflero   )r   r   fp	next_testregexr   matchexclude_testsargalltestsselectedposs               r   
find_testszRegrtest.find_tests   s   (*T\\<(PD%$33S9 (R "	 1I&KE( ==E JJ89Ebggll9#5#5t}}EF 4" 4D::c1-a0D::<D!LL.E(U[[]344 	88D--t/@/@A99T../<<(( '!!#&' "DT]]%24 }}1 1 1H.x8#H|HnnXa[1(0q(9%
 @hnnT-?-?@@A
 	D$$%>>NN8$h''E( (  4 4P    3D4F4F3GHIs_   K "K5K !>K/ !K/9!K< 'L KK 	K,+K,/K9<	LL6MMc                 (    | D ]  }t        |        y r   )r   )r   names     r   r?   zRegrtest.list_tests   s     	D$K	r   runtestsc           	      F   | j                   dk(  rd| _         | j                  j                         \  }}| j                  dt	        |       d       |j                  |dddd|d      }| j                  j                  |       | j                  || j                          |S )Nr   r   zRe-running z- failed tests in verbose mode in subprocessesTF)r   rF   r5   r^   r[   match_tests_dictr`   )	rX   r:   prepare_rerunr   lencopyr<   	set_tests_run_tests_mp)r   r   r   r   s       r   _rerun_failed_testszRegrtest._rerun_failed_tests   s    q 
  !D"&,,"<"<"> 	;s5zl*WXY==-# ! % 	h'8T%5%56r   c                    | j                   r| j                  d       y | j                         | _        t	                | j                  |      }| j                  j                  rRt	        t        t        | j                  j                        d      d       t        | j                  j                         | j                  |       y )NzJRe-running failed tests is not supported with --python host runner option.testzfailed again:)rr   r   	get_stater;   r   r   r:   badr   r   r!   display_result)r   r   rerun_runtestss      r   rerun_failed_testszRegrtest.rerun_failed_tests  s    ??HH& >>+11(;<<%DLL,,-v6Hdll&&'N+r   r   progressc                 p   t                d| }|r| d| d}t        |       t        dt        |      z         t                |j                         }|j                  g d       |j                  |j	                                |j                  |       t        dt        j                  |      d       t                d	d l	}|j                  ||j                  
      }|j                  }| d| }t        |       t        dt        |      z         t        d       |rt        d|        yy)NzBisect z ()r   )-uz-mztest.bisect_cmdz
--max-iter25z-v+Tflushr   ra   z: exit code zBisect failed with exit code F)r   r   create_python_cmdextendbisect_cmd_argsr   shlexr   r   
subprocessrunra   
returncode)	r   r   r   r   titlecmdr   procexitcodes	            r   _run_bisectzRegrtest._run_bisect  s   $ gRz+EecCJ((*

 
 	 	

8++-.

4c5::c?$/~~c8+;+;~<??'hZ0ecCJD1(<=r   c                     | j                   j                  d      \  }}t        |d      D ];  \  }}t        |      dkD  r| dt        |       }nd}| j	                  |||      r; y  y )NF)clearr   / )r:   r   	enumerater   r   )r   r   r   _r   r   r   s          r   
run_bisectzRegrtest.run_bisect>  sp    <<--E-:q$UA. 	KE45zA~#WAc%j\2##HdH=	r   c                     |j                   ry | j                         }t                t        d| d       | j                  j	                  |j
                  | j                  | j                         y )Nz== Tests result: z ==)r7   r   r   r:   r   r   r6   r   )r   r   states      r   r   zRegrtest.display_resultI  sW    << !%,-##HNN$(JJ0B0B	Dr   	test_namec                     |7d}t        t                     }|j                  |t               |       |d   }nt	        ||      }| j
                  j                  ||       |S )Nz-result = run_single_test(test_name, runtests))globalslocalsresult)dictr   runctxr   r   r:   accumulate_result)r   r   r   tracerr   	namespacer   s          r   run_testzRegrtest.run_testU  s^     CCVXIMM#wyMCx(F$Y9F&&vx8r   c                    | j                   rdd l}|j                  dd      }nd }t        t        j
                        }|j                         }|t        |d      }nd}d| d}|j                  r|d	t        |j                        z  z  }| j                  |       d }|j                         }	t        |	d
      D ]f  \  }
}t        j                         }|}|r|d|}| j                  j!                  |
|       | j#                  |||      }t        j
                  D cg c]  }||vr|j%                  d      r| }}|D ]U  }t        j
                  j'                  |d        |j)                  d      \  }}}	 t+        t        j
                  |   |       W |j1                  | j2                  | j4                        r nat7        |      }t        j                         |z
  }|t8        k\  r|dt        |      }F|j:                  t<        j>                  k(  sed }i |rtA        |       |S c c}w # t,        t.        f$ r Y w xY w)Nr   FT)rs   r   r   r   zRun z sequentiallyz (timeout: %s)r   z -- )ztest.test_.z in )!rt   rs   Tracer   r   modulesget_jobsr   ra   r    r   
iter_testsr   r   r   r<   display_progressr   
startswithpop
rpartitiondelattrKeyErrorAttributeError	must_stopr[   r\   strr   r   r   PASSEDr   )r   r   rs   r   save_modulesjobsr   msgprevious_test
tests_iter
test_indexr   r   textr   modulenew_modulesparentr   r   	test_times                        r   run_tests_sequentiallyzRegrtest.run_tests_sequentiallyd  s/   ==[[uD[9FF3;;'  "$'EEUG=)#oh6F6F&GGGC((*
%.z1%=  	%!J	**,JD%)=9KK((T:]]9h?F 14 Gf$L8 & 1 12D E " GK G & -"("3"3C"84CKK/6 0E0EFKM))+j8I--.;_Y=W X- $A 	%D - 5G !.1 s   $H6?H;;IIc                     | j                   j                  | j                        }| j                  r| j                   d| }|S )Nz then )r:   r   r\   r;   )r   r   s     r   r   zRegrtest.get_state  sA    &&t'<'<=''(ug6Er   rX   c                 j    ddl m}  |||| j                  | j                        j	                          y )Nr   )
RunWorkers)run_workersr  r<   r:   r   )r   r   rX   r  s       r   r   zRegrtest._run_tests_mp  s$    +;$++t||DHHJr   c                 6   | j                   ri| j                  r>t        | j                   d      5 }|j                  | j                  dz          d d d        nt	        j
                  | j                          |.|j                         }|j                  dd| j                         | j                  r*t	        j                  dt	        j                         z         | j                  r&| j                  j                  | j                         y y # 1 sw Y   xY w)Nw
T)show_missingsummaryru   zleaks %d)r   r   r   writerc   unlinkr:   write_resultsrv   rI   systemgetpidrj   write_junit)r   r   r   r:   s       r   finalize_testszRegrtest.finalize_tests  s    $$$$$33S9 ;RHHT22T9:; ; 		$334nn&G!!tT+/+<+< " > IIj299;./LL$$T%8%89 ; ;s   DDc                 N   t        j                         | j                  j                  z
  }t	        | j
                        }t                t        dt        |      z         | j                  j                  | j                  |       | j                         }t        d|        y )NzTotal duration: %szResult: )r   r   r<   r   boolrP   r   r    r:   display_summaryr~   r   )r   durationfilteredr   s       r   r#  zRegrtest.display_summary  s    $$&)?)??(() 	"_X%>>?$$T%8%8(C   !r   c                    t        |fi d| j                  d| j                  d| j                  dd ddd| j                  d| j
                  d	| j                  d
| j                  d| j                  d| j                  d| j                  d| j                  d| j                  d| j                  d ud| j                  d| j                  d| j                   d| j"                  d| j$                  d| j&                  S )Nr[   r\   rP   r   rF   Fr^   r7   r8   r`   ra   r5   r6   rf   rh   	use_junitrl   rn   rp   rr   ry   r{   )r   r[   r\   rP   r^   r7   r8   r`   ra   r5   r6   rf   rh   rj   rl   rn   rp   rr   ry   r{   )r   r   s     r   create_run_testszRegrtest.create_run_tests  sG   
nn
 "22
 ((	

 "
 
 LL
 
 **
 #44
 LL
 LL
 **
 **
 ]]
  **$6!
" **#
$ **%
& ,,'
( )
* nn+
, ((-
 	
r   r   c                 F   | j                   r7| j                   j                  dk  rd}t        |t        j                  d       | j
                  dk  rt               xs ddz   | _        | j                  s2| j                  sF| j                  s:| j                  s.|s,| j                  s t        | j                  | j                         t        d| j                         | j!                  |      }|| _        | j$                  j'                  |       t)                |j                   | j
                  sd	}nt*        }|r| j$                  j-                          	 | j
                  r| j/                  || j
                         d }n| j1                  |      }| j3                  |       | j4                  r+| j6                  j9                         r| j;                  |       | j<                  r+| j6                  j9                         r| j?                  |       |r| j$                  jA                          	 | jC                          | jE                  |       | j6                  jG                  | jH                  | jJ                        S # |r| j$                  jA                          w w xY w)
N   zhWARNING: Running tests with --huntrleaks/-R and less than 3 warmup repetitions can give false positives!T)filer   r   r      zUsing random seed:F)&rf   r   r   r   stdoutrX   r*   r>   r7   r6   r   rV   r%   rp   rr   r{   r(  r~   r<   r   r   r   start_load_trackerr   r  r   rG   r:   
need_rerunr   rK   r   stop_load_trackerr#  r   get_exitcoder\   r]   )r   r   r   r  r   use_load_trackerr   s          r   
_run_testszRegrtest._run_tests  s   !2!2!:!:Q!>NC#CJJd3a !2 3 8qA=D 88tzzT-A-At004--t?"D$4$45((2&h'!!-8H8H$  *KK**,	0""8T-=-=>44X>)4<<#:#:#<''1DLL$;$;$=)--/F#||(()>)>)-: 	:  --/  s   B;J J c                 @   t        j                  | j                  d       t        | j                        }t	               5  t        j                  |d      5  | j                  ||      cd d d        cd d d        S # 1 sw Y   nxY w	 d d d        y # 1 sw Y   y xY w)NT)exist_ok)r6   )rc   makedirsrx   r#   r$   r   temp_cwdr3  )r   r   r   work_dirs       r   	run_testszRegrtest.run_tests  s    
DLL40- ^ 
	8
 ##HD9 8
 x78 8
	8 
	8
8 8 8
	8 
	8 
	8s$   BA>+	B>B	BBc                    t        | j                        }d }t               }t               }|rch d}t        j
                  }t        j
                  j                         D 	ci c]  \  }}	|j                  d      r||v r||	 }
}}	|
|k7  r|
}d}|ra|r_| j                  dk(  r|j                  ddg       | j                  s1t        j                  d      }| d| }|j                  d	|g       d}||fS c c}	}w )
N>   
PYTHONPATH_PYTHON_PROJECT_BASE_PYTHON_HOST_PLATFORM_PYTHON_SYSCONFIGDATA_NAME)PYTHON_PYTHONTr   z-j2BUILDPYTHON z--python)r"  rr   r(   r)   rc   rz   itemsr   rX   r   	sysconfigget_config_var)r   regrtest_optskeep_environrz   cross_compile
hostrunnerkeepold_environr   valuenew_environbuildpythonrr   s                r   _add_cross_compile_optsz Regrtest._add_cross_compile_opts0  s    DOO, *+ %&
D **K/1zz/?/?/A +e'<= eK 
 k)%LZ1$ $$dC[1 ??'66}E *|1[M:
$$j*%=>#&&/s   #"C8c                 Z   t         j                  j                  s|j                  d       dt         j                  vr|j                  d       t         j                  j                  dk  r|j                  d       |s-t         j                  j                  s|j                  d       y y y )Nr   default)z-WrR  r,  z-bbz-E)	r   r-  write_throughr   warnoptionsr   flagsbytes_warningignore_environment)r   python_optsrH  s      r   _add_ci_python_optszRegrtest._add_ci_python_opts_  s    
 zz''t$ COO+01 99""Q&u%99//""4( 0 r   c                    t         j                  j                          t         j                  j                          t	        j
                  |      }	 t        d| d       t        t        d      r t        st        j                  |d   |       y dd l}|j                  ||      5 }	 |j                          d d d        t        j$                  j(                         y # t        $ ri 	 |j                  t               nN# |j                   $ r< |j#                          |j                          t        j$                  t&               Y nw xY wY w xY w# 1 sw Y   xY w# t*        $ r}t-        d|d	|        Y d }~y d }~ww xY w)
Nz+ Tr   execvr   )envr   z!Failed to change Python options: z

Command: )r   r-  r   stderrr   r   r   hasattrrc   r   r[  r   PopenrB   KeyboardInterruptr+   TimeoutExpiredkillr   r   r   	Exceptionr'   )r   r   rz   cmd_textr   r   excs          r   _execute_pythonzRegrtest._execute_pythont  s>   



::c?	2Bxj/.r7#JQ% "%%cw%7 ;4
;		; ) - ;; IIlI;)88 ; IIK IIKHH%9:;;; ;  	2=cW E&&.Z1 2 2	2sy   ?E% E% *E,C$<'E% $	E.DEAEEEEEEEE"E% %	F	.FF	c                 *   g }g }| j                  |      \  }}| j                  r| j                  ||       |s|s|y t        t        j
                        }|r||dd |r|j                  |       |j                  d       | j                  ||       y )Nr   z--dont-add-python-opts)	rP  rN   rY  listr   	orig_argvr   r   rf  )r   rX  rG  rz   rH  r   s         r   r/   zRegrtest._add_python_opts  s     $ < <] K<<$$[,?m'/ 3==!"C!HJJ}%

+,S'*r   c                 h   t         j                  j                  d       | j                  rWt        j
                  j                  | j                        s.t        j
                  j                  | j                        | _        t        | j                         t        | j                        | _        y )Nbackslashreplace)errors)r   r-  reconfigurerj   rc   rd   isabsre   r   rV   r"   rx   )r   s    r   _initzRegrtest._init  sq     	

&89rww}}T5H5H'I"$''//$2E2E"FD))*#DLL1r   c                 4   | j                   r| j                          | j                          | j                  r*t	        | j
                         t        j                  d       | j                  rt        d       t        | j                         | j                  |      \  }}d}| j                  r| j                  |       nA| j                  r#t!        || j"                  | j                         n| j%                  ||      }t        j                  |       y )Nr   zPress any key to continue...)rP   rh   )rO   r/   ro  rE   r&   rx   r   r   rC   inputr   rh   r   r@   r?   rA   r   rP   r9  )r   r   r   r   s       r   mainzRegrtest.main  s    $$!!#

T\\*HHQK>>01t}}%//%0%OOH%!!x#'#3#3 $/ ~~h6Hr   )F)r   r   )&__name__
__module____qualname____doc__r   r"  r   r   r   ro   r   r   staticmethodr?   r   r   r   r  r   r   r   r   r   r  r   r4   r   r   r#  r(  r3  r9  rP  rY  rf  r/   ro  rr   r   r   r-   r-      su   ,d99 d9 d9LF(4 F(5HW[OA[;\ F(P )  H 2,8 ,($H $C $3 $4 $L	8 	 	
D( h :xKh KS KT K:&"
i 
48:9 8:X_ 8: 8:t8) 8HtO 8 8"-'^)*2D+,2(T/ r   r-   c                 x    t        t        j                  dd fi |}t        ||      j	                  |        y)zRun the Python suite.r   N)r/   )r   )r   r   argvr-   rr  )r   r/   kwargsr.   s       r   rr  rr    s4    	SXXab\	,V	,BR"2388u8Er   )NF)<rc   r|   r   r   r   rE  r   test.supportr   r   r   cmdliner   r   r	   r
   r   r<   r   r7   r   r   r   r:   r   r   r   r   r   setupr   r   r   r   r   r9   r   utilsr   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r-   rr  rx  r   r   <module>r     sr    	  	  
   A A + A A     6 + 0 6 "     u
 u
pFr   