B
    18\V                 @   sx  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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"m#Z#m$Z$m%Z% d dl&m'Z' yd dl(Z(W n e)k
r   dZ(Y nX e	* rHe	+da,t,dkr8e	+da,ej-.t,d	a,ne
/ a,ej-0t,a,G d
d dZ1dddZ2d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PROGRESS_MIN_TIMEformat_test_result)setup_tests)removepycountformat_duration	printlist)supportabs_builddirsrcdirbuildc               @   s   e Zd Z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%d&Zd'd( Zd$S )*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 | _g | _t | _d| _d| _d | _d | _d | _d S )NF    )nstestsselectedgoodbadskippedresource_deniedsenvironment_changedrerunrun_no_testsfirst_resultinterrupted
test_timestracerfound_garbagetime	monotonic
start_time
test_counttest_count_widthnext_single_testnext_single_filenametestsuite_xml)self r6   Y/oak/stanford/groups/akundaje/marinovg/programs/Python-3.7.3/Lib/test/libregrtest/main.py__init__D   s*    
zRegrtest.__init__c          	   C   s8  |\}}}|t tfkr&| j||f |tkr<| j| n|tt fkrV| j| nx|tkrl| j	| nb|t
kr| j| nL|tkr| j| | j| n*|tkr| j| n|tkrtd| |r4dd lm  m} xL|D ]D}y| j|| W q |jk
r.   t|tjd  Y qX qW d S )Nzinvalid test result: %rr   )file)r   r   r*   appendr   r!   r	   r"   r
   r%   r   r#   r   r$   r   r'   
ValueErrorxml.etree.ElementTreeetreeElementTreer4   
fromstring
ParseErrorprintsys
__stderr__)r5   testresultok	test_timeZxml_dataETer6   r6   r7   accumulate_resultl   s4    

zRegrtest.accumulate_resultc             C   s   | j jrd S || j | j }t| jt| j }|rN| j jsN| d| }d| d| }tt	drt	
 d }d|dd| }t | j }tjt|d	}| d| }t|d
d d S )N/[z] 
getloadavgr   z
load avg: z.2f )secondsT)flush)r   quietr1   r0   lenr"   r%   pgohasattrosrM   r-   r.   r/   datetime	timedeltaintrA   )r5   
test_indexrD   lineZfailsZload_avg_1minrG   r6   r6   r7   display_progress   s    
zRegrtest.display_progressc             C   s   t tjdd  f|}|jr:ttds:tdtjd d |_|jd k	r`t	d kr`tdtjd d |_|j
rt	d k	rpntdtjd d|_
|jrg  t_| _t|j |S )Nr   dump_traceback_laterzFWarning: The timeout option requires faulthandler.dump_traceback_later)r9   z$No GC available, ignore --threshold.z&No GC available, disabling --findleaksF)r   rB   argvtimeoutrT   faulthandlerrA   stderrZ	thresholdgc	findleaksxmlpathr   junit_xml_listr4   r   args)r5   kwargsr   r6   r6   r7   
parse_args   s$    


zRegrtest.parse_argsc          	   C   s  || _ | jjrdtjtd| _y0t| jd}|	 
 }|g| _ W d Q R X W n tk
rb   Y nX | jjrg | _ td}ttjtj| jjN}xF|D ]>}|ddd }|
 }||}|d k	r| j |  qW W d Q R X t| j  td d  }t }| jjrJx0| jjD ]$}	|	|kr2||	 ||	 qW g | j_| jjrjt| jjt t  }
nt| jj||}
| jjs| j p| jjp|
| _!n| j | _!| jjr| j!d d | _!y"|
"| j!d }|
|d  | _#W n t$k
r   Y nX | jj%rNy| j!d | j!"| jj%= W n, t&k
rL   t'd| jj% t(j)d Y nX | jj*r| jj+d krtt,-d	| j_+t,.| jj+ t,/| j! d S )
NZ
pynexttestrz\btest_[a-zA-Z0-9_]+\b#r   r   z1Couldn't find starting test (%s), using all tests)r9   i )0r   r   singlerU   pathjoinTEMPDIRr3   openreadstripOSErrorfromfilerecompiler   SAVEDCWDsplitsearchr:   groupr   r   r   copyexcludere   removeaddtestdirr   listsetr    indexr2   
IndexErrorstartr;   rA   rB   r`   	randomizerandom_seedrandom	randrangeseedshuffle)r5   r   fpZ	next_testregexrZ   matchZstdtestsZnottestsargZalltestsposr6   r6   r7   
find_tests   sh    











zRegrtest.find_testsc             C   s   x| j D ]}t| qW d S )N)r    rA   )r5   namer6   r6   r7   
list_tests  s    zRegrtest.list_testsc             C   s\   xV|D ]N}t |tjjrqt |tjr2| | qt |tjrt|rt	|
  qW d S )N)
isinstanceunittestloader_FailedTest	TestSuite_list_casesTestCaser   
match_testrA   id)r5   suiterD   r6   r6   r7   r     s    

zRegrtest._list_casesc          	   C   s   dt _t | jj xX| jD ]N}t| j|}ytj	|}| 
| W q tjk
rh   | j| Y qX qW | jrttjd ttt| jddtjd t| jtjd d S )NF)r9   rD   zskipped:)r   verboseset_match_testsr   match_testsr    r   r   defaultTestLoaderloadTestsFromNamer   SkipTestr#   r:   rA   rB   r`   r   rR   r   )r5   rD   Zabstestr   r6   r6   r7   
list_cases  s    zRegrtest.list_casesc          	   C   s   d| j _d| j _d| j _|  | _t  td | jd d  | _x| jD ]p}td| dd yd| j _t	| j |}W n" t
k
r   d| _t  P Y qHX |d tttthkrH| j| qHW | jrttt| jdd t| j |   d S )	NTFz'Re-running failed tests in verbose modez"Re-running test %r in verbose mode)rP   r   rD   zfailed again:)r   r   failfastverbose3get_tests_resultr(   rA   r"   r&   r   KeyboardInterruptr)   r   r
   r   r   r{   r   rR   r   display_result)r5   rD   rF   r6   r6   r7   rerun_failed_tests#  s,    

zRegrtest.rerun_failed_testsc             C   s&  | j jrd S t  td|    | jr~t  td t| jt| jB t| jB }t| j	| }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 x0| jd d D ]\}}td|t|f  q
W | jrZt  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r"t  tt
t| jdd t| j d S )Nz== Tests result: %s ==z(Test suite interrupted by signal SIGINT.rD   zomitted:r   ZAllrN   )endzOK.T)reversez10 slowest tests:
   z- %s: %szfailed:z%{} altered the execution environment:zskipped:z%s:zre-run testzrun no tests:)r   rS   rA   r   r)   r   r!   r"   r#   r    r   rR   r   rQ   
print_slowr*   sortr   r%   formatr&   r'   )r5   ZexecutedZomittedr-   rD   r6   r6   r7   r   A  s\    




zRegrtest.display_resultc          
   C   s  | j jr dd l}|jddd| _tj }td d }xt| j	dD ]|\}}t
 }|}|rld||f }| || | jrd}tt }	| jj|t |	d	 |	d
 }
nLyt| j |}
W n. tk
r   d| _| |td d f P Y nX | ||
 t||
d }t
 | }|tkr,d|t|f }n|
d tkr>d }| j jrt  tjrtdttjdd td | j tj tjd d = x4tj D ]&}||kr|!drt"#| qW qFW |rt| d S )Nr   FT)tracer   zRun tests sequentiallyr   z%s -- %szEresult = runtest(self.ns, test); self.accumulate_result(test, result))globalslocalsrE   z%s in %szWarning: test createdrN   )r   zuncollectable object(s).ztest.)$r   r   ZTracer+   rB   moduleskeysrA   	enumerater   r-   r.   r[   dictr   Zrunctxr   r   r   r)   rJ   r   r   r   r   r   rb   ra   collectgarbagerR   r,   extend
startswithr   unload)r5   r   Zsave_modulesZprevious_testrY   rD   r/   textcmdr   rE   rG   moduler6   r6   r7   run_tests_sequential|  sT    




zRegrtest.run_tests_sequentialc             c   s:   x4x.|D ]&}|V  | j rd S | jjr| jrd S qW qW d S )N)r"   r   fail_env_changedr%   )r5   r   rD   r6   r6   r7   _test_forever  s    
zRegrtest._test_foreverc             C   sv   t dt ftj   t dtjdddtj  t dt  t	 }|rXt d| t dt
dt f  d S )	Nz==T)aliasedz	%s-endianz== cwd:z== CPU count:z== encodings: locale=%s, FS=%sF)rA   platformpython_implementationrB   versionrv   	byteorderrU   getcwd	cpu_countlocalegetpreferredencodinggetfilesystemencoding)r5   r   r6   r6   r7   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"   r:   r   r   r%   anyr!   r#   r)   rl   r(   )r5   rE   r6   r6   r7   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!T)r9   rP   zUsing random seedr   z/{}r   r   )run_tests_multiprocess)r   headerrS   rQ   rj   r   re   r   
huntrleaksrA   rB   stdoutr   r   foreverr   r~   r    r0   r1   iterr   rR   use_mptest.libregrtest.runtest_mpr   r   )r5   warmuprepetitions_msgr   r6   r6   r7   	run_tests  s,    
zRegrtest.run_testsc          	   C   s   | j rB| jr6t| j d}|| jd  W d Q R X nt| j  | jrf| 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)r3   r2   rn   writerU   unlinkr+   resultsZwrite_resultsr   r   rA   r-   r.   r/   r   r   runleakssystemgetpid)r5   r   rh   Zdurationr6   r6   r7   finalize  s    
zRegrtest.finalizec       
   
   C   s
  | j js| jsd S dd lm  m} |d}dddd}x\| jD ]R}|| xB|D ]:}y ||  t|	|d7  < W qV t
k
r   Y qVX qVW qBW x$| D ]\}}||t| qW tjtj| j j}t|d$}x||D ]}	||	 qW W d Q R X d S )Nr   Z
testsuites)r   errorsfailureswb)r   rc   r4   r<   r=   r>   Elementr:   rX   getr;   itemsr   strrU   rk   rl   r   ru   rn   tostringlistr   )
r5   rH   rootZtotalsr   kvrc   fsr6   r6   r7   save_xml_result  s$    


 zRegrtest.save_xml_resultNc          	   K   s   |  || _| jjr| jjan(| jjrFt| jj\}}|dpDtatj	tdd d
t }tjt|}tj|dd | || W d Q R X d S )NtempdirT)exist_okztest_python_{})rQ   )rg   r   r   rm   worker_argsjsonloadsr   rU   makedirsr   r   rk   rl   r   temp_cwd_main)r5   r   rf   Zns_dictr   Ztest_cwdr6   r6   r7   main3  s    
zRegrtest.mainc             C   sN  | j jrD| j j\}}}|dk s&|dk rDd}t|tjdd td | j jd k	rhddlm} || j j | j j	rxt
d | j jt_t| j  | | | j jr|   td | j jr|   td |   |   | j jr| jr|   |   |   | jrtd | jr$td	 | j jr@| jr@td
 td d S )Nr   zwInvalid values for the --huntrleaks/-R parameters. The number of warmups and repetitions must be at least 1 each (1:1).T)r9   rP      r   )run_tests_workerzPress any key to continue...   r   )r   r   rA   rB   r`   exitr   r   r   waitinputrS   r   PGOr   r   r   r   r   r   verbose2r"   r   r   r   r)   r   r%   )r5   r   rf   r   r   r   r   r   r6   r6   r7   r   L  sB    








zRegrtest._main)N)__name__
__module____qualname____doc__r8   rJ   r[   rg   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r6   r6   r6   r7   r   -   s(   ( E
;>	 
r   c             K   s   t  jf d| i| dS )zRun the Python suite.r   N)r   r   )r   rf   r6   r6   r7   r   ~  s    r   )N)3rV   r_   r   r   rU   r   r   rs   rB   	sysconfigtempfiler-   r   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.utilsr   r   r   r   rD   r   ra   ImportErroris_python_buildget_config_varrm   rk   rl   
gettempdirabspathr   r   r6   r6   r6   r7   <module>   sD   D




    U