3
^d]V                 @   sp  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j) r@ej*da+t+dkr0ej*da+ej,j-t+d	a+ne	j. a+ej,j/t+a+G d
d dZ0dddZ1d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)supportZabs_builddirZsrcdirZ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j | _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 r3   +/usr/lib/python3.6/test/libregrtest/main.py__init__C   s*    
zRegrtest.__init__c             C   s2  |\}}}|t tfkr&| jj||f |tkr<| jj| n|tt fkrV| jj| nx|tkrl| j	j| nb|t
kr| jj| nL|tkr| jj| | jj| n*|tkr| jj| n|tkrtd| |r.dd ljj}xL|D ]D}y| jj|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ElementTreer1   Z
fromstringZ
ParseErrorprintsys
__stderr__)r2   testresultok	test_timeZxml_dataETer3   r3   r4   accumulate_resultk   s4    

zRegrtest.accumulate_resultc             C   s   | j jrd S || j | j }t| jt| j }|rP| j j rP| d| }d| d| }tt	drt	j
 d }d|dd| }tj | j }tjt|d	}| d| }t|d
d d S )N/[z] 
getloadavgr   z
load avg: z.2f )ZsecondsT)flush)r   quietr.   r-   lenr   r"   pgohasattrosrH   r*   r+   r,   datetimeZ	timedeltaintr<   )r2   
test_indexr?   lineZfailsZload_avg_1minrB   r3   r3   r4   display_progress   s    
zRegrtest.display_progressc             C   s   t tjdd  f|}|jr<ttd r<tdtjd d |_|jd k	rbt	d krbtdtjd d |_|j
rt	d k	rrntdtjd d|_
|jrg  t_| _t|j |S )Nr   Zdump_traceback_laterzFWarning: The timeout option requires faulthandler.dump_traceback_later)r6   z$No GC available, ignore --threshold.z&No GC available, disabling --findleaksF)r   r=   argvZtimeoutrN   faulthandlerr<   stderrZ	thresholdgc	findleaksxmlpathr   Zjunit_xml_listr1   r   args)r2   kwargsr   r3   r3   r4   
parse_args   s$    


zRegrtest.parse_argsc          '   C   s  || _ | jjrdtjjtd| _y0t| jd}|j	 j
 }|g| _ W d Q R X W n tk
rb   Y nX | jjrg | _ tjd}ttjjtj| jjN}xF|D ]>}|jddd }|j
 }|j|}|d k	r| j j|j  qW W d Q R X t| j  td d  }tj }| jjrJx0| jjD ]$}	|	|kr2|j|	 |j|	 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"| j!d }|
|d  | _#W n t$k
r   Y nX | jj%rNy| j!d | j!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,j-d	| j_+t,j.| jj+ t,j/| j! d S )
NZ
pynexttestrz\btest_[a-zA-Z0-9_]+\b#r   r   z1Couldn't find starting test (%s), using all tests)r6   i )0r   r   singlerO   pathjoinTEMPDIRr0   openreadstripOSErrorZfromfilerecompiler   SAVEDCWDsplitsearchr7   groupr   r   r   copyZexcluder[   removeaddZtestdirr   listsetr   indexr/   
IndexErrorstartr8   r<   r=   rW   	randomizerandom_seedrandomZ	randrangeZseedZshuffle)r2   r   fpZ	next_testZregexrS   matchZstdtestsZnottestsargZalltestsposr3   r3   r4   
find_tests   sh    











zRegrtest.find_testsc             C   s   x| j D ]}t| qW d S )N)r   r<   )r2   namer3   r3   r4   
list_tests  s    zRegrtest.list_testsc             C   s\   xV|D ]N}t |tjjrqt |tjr2| j| qt |tjrtj|rt	|j
  qW d S )N)
isinstanceunittestloaderZ_FailedTestZ	TestSuite_list_casesZTestCaser   Z
match_testr<   id)r2   suiter?   r3   r3   r4   r     s    

zRegrtest._list_casesc             C   s   dt _t j| jj xX| jD ]N}t| j|}ytjj	|}| j
| W q tjk
rh   | j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)r6   r?   zskipped:)r   verboseZset_match_testsr   Zmatch_testsr   r   r   ZdefaultTestLoaderZloadTestsFromNamer   ZSkipTestr    r7   r<   r=   rW   r   rL   r   )r2   r?   Zabstestr   r3   r3   r4   
list_cases  s    zRegrtest.list_casesc             C   s   d| j _d| j _d| j _| 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j| qHW | jrttt| jdd t| j | j  d S )	NTFz'Re-running failed tests in verbose modez"Re-running test %r in verbose mode)rJ   r   r?   zfailed again:)r   r   ZfailfastZverbose3get_tests_resultr%   r<   r   r#   r   KeyboardInterruptr&   r   r
   r   r   ro   r   rL   r   display_result)r2   r?   rA   r3   r3   r4   rerun_failed_tests"  s,    

zRegrtest.rerun_failed_testsc             C   s0  | j jrd S t  td| j   | 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 rt  | j r| j r| j rt| jdkrtddd tt
t| jdd	 | j jr4| jjd
d t  td x0| jd d D ]\}}td|t|f  qW | jrbt  tt
t| jdd t| j | jrt  tdjt
t| jd t| j | jr| j j rt  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AllrI   )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   rM   r<   r   r&   rr   r   r   r    r   r   rL   r   rK   Z
print_slowr'   sortr   r"   formatr#   r$   )r2   ZexecutedZomittedr*   r?   r3   r3   r4   r   @  s\    




zRegrtest.display_resultc             C   s  | j jr dd l}|jddd| _tjj }td d }xt| j	dD ]|\}}t
j }|}|rld||f }| j|| | jrd}tt }	| jj|t |	d	 |	d
 }
nLyt| j |}
W n. tk
r   d| _| j|td d f P Y nX | j||
 t||
d }t
j | }|tkr,d|t|f }n|
d tkr>d }| j jrtj  tjrtdttjdd td | jj tj tjd d = x4tjj D ]&}||kr|j!drt"j#| 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localsr@   z%s in %szWarning: test createdrI   )r   zuncollectable object(s).ztest.)$r   r   ZTracer(   r=   moduleskeysr<   	enumerater   r*   r+   rT   dictr   Zrunctxr   r   r   r&   rE   r   r   r   r   r   rY   rX   ZcollectZgarbagerL   r)   extend
startswithr   Zunload)r2   r   Zsave_modulesZprevious_testrR   r?   r,   textcmdr   r@   rB   moduler3   r3   r4   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"   )r2   r   r?   r3   r3   r4   _test_forever  s    
zRegrtest._test_foreverc             C   sv   t dtj ftjj   t dtjdddtj  t dtj  tj	 }|rXt d| t dt
jdtj f  d S )	Nz==T)Zaliasedz	%s-endianz== cwd:z== CPU count:z== encodings: locale=%s, FS=%sF)r<   platformZpython_implementationr=   versionrk   	byteorderrO   getcwd	cpu_countlocalegetpreferredencodinggetfilesystemencoding)r2   r   r3   r3   r4   display_header  s    
zRegrtest.display_headerc             C   s   g }| j r|jd n@| jjr0| jr0|jd n&t| j| j | j| j| jfsV|jd | jrf|jd |st|jd dj	|}| j
rd| j
|f }|S )NFAILUREzENV CHANGEDzNO TEST RUNr   SUCCESSz, z
%s then %s)r   r7   r   r   r"   anyr   r    r&   rb   r%   )r2   r@   r3   r3   r4   r     s     




zRegrtest.get_tests_resultc             C   s   | j js0| j jp,| j jp,| j jp,| jp,| j j r8| j  | j jrj| j j\}}}|dk rjd}t	|t
jdd | j jrt	d| j j | j jr| jt| j| _d| _d| _n.t| j| _djt| j| _t| jd | _| j jrd	d
lm} ||  n| j  d S )N   zhWARNING: Running tests with --huntrleaks/-R and less than 3 warmup repetitions can give false positives!T)r6   rJ   zUsing random seedr   z/{}r   r   )run_tests_multiprocess)r   headerrM   rK   r`   r   r[   r   
huntrleaksr<   r=   stdoutrv   rw   Zforeverr   rq   r   r-   r.   iterr   rL   Zuse_mptest.libregrtest.runtest_mpr   r   )r2   warmuprepetitions_msgr   r3   r3   r4   	run_tests  s,    
zRegrtest.run_testsc             C   s   | j rB| jr6t| j d}|j| jd  W d Q R X ntj| j  | jrf| jj }|jdd| j	j
d t  tj | j }tdt|  td| j   | j	jrtjdtj   d S )Nw
T)Zshow_missingZsummarycoverdirzTotal duration: %szTests result: %szleaks %d)r0   r/   rd   writerO   unlinkr(   ZresultsZwrite_resultsr   r   r<   r*   r+   r,   r   r   Zrunleakssystemgetpid)r2   ry   r^   Zdurationr3   r3   r4   finalize  s    
zRegrtest.finalizec       
      C   s  | j j r| j rd S dd ljj}|jd}dddd}x\| jD ]R}|j| xB|D ]:}y ||  t|j	|d7  < W qT t
k
r   Y qTX qTW q@W x$|j D ]\}}|j|t| qW tjjtj| j j}t|d$}x|j|D ]}	|j|	 qW W d Q R X d S )Nr   Z
testsuites)r   errorsZfailureswb)r   rZ   r1   r9   r:   r;   ZElementr7   rQ   getr8   itemsrr   strrO   ra   rb   r   rj   rd   Ztostringlistr   )
r2   rC   rootZtotalsr   kvrZ   fsr3   r3   r4   save_xml_result  s$    


 zRegrtest.save_xml_resultNc             K   sr   t j r,ytjt W n tk
r*   Y nX djtj }tjj	t|}t
j|dd | j|| W d Q R X d S )Nztest_python_{}T)rK   )	sysconfigis_python_buildrO   mkdirrc   FileExistsErrorr   r   ra   rb   r   Ztemp_cwd_main)r2   r   r\   Ztest_cwdr3   r3   r4   main2  s    zRegrtest.mainc             C   sZ  | j || _| jjrP| jj\}}}|dk s2|dk rPd}t|tjdd tjd | jjd k	rtddlm	} || jj | jj
rtd | jjt_t| j | j| | jjr| j  tjd | jjr| j  tjd | j  | j  | jjr| jr| j  | j  | j  | jrtjd | jr0tjd	 | jjrL| jrLtjd
 tj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)r6   rJ      r   )run_tests_workerzPress any key to continue...   r   )r]   r   r   r<   r=   rW   exitZworker_argsr   r   waitinputrM   r   ZPGOr   r}   r   r   r   r   Zverbose2r   r   r   r   r&   r   r"   )r2   r   r\   r   r   r   r   r   r3   r3   r4   r   H  sD    








zRegrtest._main)N)__name__
__module____qualname____doc__r5   rE   rT   r]   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r3   r3   r3   r4   r   ,   s(   ( E
;>	 
r   c             K   s   t  jf d| i| dS )zRun the Python suite.r   N)r   r   )r   r\   r3   r3   r4   r   |  s    r   )N)2rP   rV   r   rO   r   rx   rh   r=   r   Ztempfiler*   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.utilsr   r   r   r   r?   r   rX   ImportErrorr   Zget_config_varrc   ra   rb   Z
gettempdirabspathr   r   r3   r3   r3   r4   <module>   sB   D




    T