a
    5ñ»jO  ã                   @   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Zd dlmZ d dlmZ d dlmZ zd dlmZ dZ[W n ey¼   dZY n0 d	d
„ ZdZdZdd„ Zdd„ Zdd„ Zdd„ Z dd„ Z!dd„ Z"e#e$dœ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/G d0d1„ d1ƒZ0G d2d3„ d3ƒZ1G d4d5„ d5e1ƒZ2G d6d7„ d7e2ƒZ3G d8d9„ d9e2ƒZ4i Z5d:d;„ Z6ej7dd<�d=d>„ ƒZ8d?d@„ Z9dAdB„ Z:dCdD„ Z;dEdF„ Z<ej7ddG�dHdI„ ƒZ=dS )Jé    N)Úsuppress)Úfail)Úruntestprotocol)Úparse)Úpytest_handlecrashitemTFc                  C   s@   zt j d¡} W n t jjy(   Y dS 0 t| jƒtdƒkS dS )a[  Return compatibility with installed pytest-xdist version.

    When running tests in parallel using pytest-xdist < 1.20.0, the first
    report that is logged will finish and terminate the current node rather
    rerunning the test. Thus we must skip logging of intermediate results under
    these circumstances, otherwise no test is rerun.

    zpytest-xdistNz1.20)Ú	importlibÚmetadataÚdistributionÚPackageNotFoundErrorÚparse_versionÚversion)Úd© r   ú:E:\soccer\ml_env\Lib\site-packages\pytest_rerunfailures.pyÚworks_with_current_xdist   s
    	r   z6number of times to re-run failed tests. defaults to 0.ú(add time (seconds) delay between reruns.c                 C   sš   |   dd¡}|jdddtd dd� |jdd	d
ttd� |jdd	dtdd� |jdddtd dd� |jddddd� d}| jd
t|d� | jdt|d� d S )NZrerunfailuresz0re-run failing tests to eliminate flaky failuresz--only-rerunÚappendÚ
only_rerunz€If passed, only rerun errors matching the regex provided. Pass this flag multiple times to accumulate a list of regexes to match)ÚactionÚdestÚtypeÚdefaultÚhelpz--rerunsÚstoreÚreruns)r   r   r   r   z--reruns-delayÚreruns_delayr   z--rerun-exceptÚrerun_exceptz‹If passed, only rerun errors other than matching the regex provided. Pass this flag multiple times to accumulate a list of regexes to matchz--fail-on-flakyÚ
store_trueÚfail_on_flakyzEFail the test run with exit code 7 if a flaky test passes on a rerun.)r   r   r   Ústring)r   )ZgetgroupZ
_addoptionÚstrÚintÚRERUNS_DESCÚfloatZaddiniÚRERUNS_DELAY_DESC)ÚparserÚgroupZarg_typer   r   r   Úpytest_addoption2   sT    ÿú
ûûú
ür'   c                 C   s0   | j }|dƒs,| jjdkr,| jjr,t d¡‚d S )NZcollectonlyr   z --reruns incompatible with --pdb)ÚgetvalueÚoptionr   ZusepdbÚpytestZ
UsageError)ÚconfigÚvalr   r   r   Úcheck_optionsf   s
    r-   c                 C   s
   |   d¡S )NZflaky)Zget_closest_marker)Úitemr   r   r   Ú_get_markern   s    r/   c                 C   sš   t | ƒ}|d ur@d|jv r$|jd S t|jƒdkr<|jd S dS | jj d¡}|d urZ|S ttt	ƒ�" t
| jj d¡ƒ}W d   ƒ n1 sŒ0    Y  |S )Nr   r   é   )r/   ÚkwargsÚlenÚargsÚsessionr+   r(   r   Ú	TypeErrorÚ
ValueErrorr!   Úgetini)r.   Úrerun_markerr   r   r   r   Úget_reruns_countr   s    


0r9   c              	   C   s¨   t | ƒ}|d urFd|jv r&|jd }qŽt|jƒdkr@|jd }qŽd}nH| jj d¡}|d u rŽzt| jj d¡ƒ}W n t	t
fyŒ   d}Y n0 |dk r¤d}t d¡ |S )Nr   r0   r   z@Delay time between re-runs cannot be < 0. Using default value: 0)r/   r1   r2   r3   r4   r+   r(   r#   r7   r5   r6   ÚwarningsÚwarn)r.   r8   Údelayr   r   r   Úget_reruns_delay‰   s&    

ÿr=   c                 C   s4   t | ƒ}d}|d ur0d|jv r0t| ||jd ƒ}|S )NTÚ	condition)r/   r1   Úevaluate_condition)r.   r8   r>   r   r   r   Úget_reruns_condition¥   s    ÿr@   )r>   Úreturnc           	   
   C   s€  d}t |tƒ�rttt| jdœ}t| dƒr8| | jj	¡ z(d|j
› d�}t||dƒ}t||ƒ}W n¶ ty¾ } zFd|j
 d| dd	|jpŠd
  d dg}td |¡dd� W Y d }~nbd }~0  t�y } z@d|j
 d| gt t|ƒ|¡¢}td |¡dd� W Y d }~n
d }~0 0 nbzt|ƒ}W nT t�yz } z:d|j
 gt t|ƒ|¡¢}td |¡dd� W Y d }~n
d }~0 0 |S )NF)ÚosÚsysÚplatformr+   Úobjú<z condition>ÚevalzError evaluating %r conditionz    ú r   ú^zSyntaxError: invalid syntaxÚ
)Zpytracez*Error evaluating %r condition as a boolean)Ú
isinstancer    rB   rC   rD   r+   ÚhasattrÚupdaterE   Ú__globals__ÚnameÚcompilerG   ÚSyntaxErrorÚoffsetr   ÚjoinÚ	ExceptionÚ	tracebackÚformat_exception_onlyr   Úbool)	r.   Úmarkr>   ÚresultÚglobals_ÚfilenameZcondition_codeÚexcZmsglinesr   r   r   r?   ±   sH    ü
ü&þý*ÿþ(r?   c           	      C   sj   d}t | ddƒ}t |ddƒD ]H}|j| }|D ]4}t ||dƒdur.t ||ƒ\}}}|r.t||dƒ q.qdS )z<Note: remove all cached_result attribute from every fixture.Úcached_resultZ_fixtureinfoNÚname2fixturedefsr   )Úgetattrr^   Úsetattr)	r.   r]   Zfixture_infoZfixture_def_strZfixture_defsZfixture_defrY   Ú_Úerrr   r   r   Ú+_remove_cached_results_from_failed_fixturesà   s    
rc   c                 C   s   | j j}i |_dS )zˆ
    Clean up setup state.

    Note: remove all failures from every node in _setupstate stack
          and clean the stack itself
    N)r4   Ú_setupstateÚstack)r.   Zsetup_stater   r   r   Ú'_remove_failed_setup_state_from_sessioní   s    rf   c                 C   sJ   t | ƒ}|d ur6||jv r6|j| }t|tƒrF|g}nt| jjj|ƒ}|S ©N)r/   r1   rK   r    r_   r4   r+   r)   )r.   Z
regex_namer8   Úregexr   r   r   Ú_get_rerun_filter_regexø   s    

ri   c                 C   s
   t | |ƒS rg   ©Ú_try_match_error)Úrerun_errorsÚexcinfor   r   r   Ú_matches_any_rerun_error  s    rn   c                 C   s
   t | |ƒS rg   rj   )Úrerun_except_errorsrm   r   r   r   Ú_matches_any_rerun_except_error	  s    rp   c                 C   s8   |r4|j j› d|j› �}| D ]}t ||¡r dS qdS )Nz: TF)r   Ú__name__ÚvalueÚreÚsearch)rl   rm   rb   Zrerun_regexr   r   r   rk     s    rk   c                 C   sv   |j dkrdS t| dƒ}t| dƒ}|s.|s.dS |rB|sBt||ƒ S |sT|rTt||ƒS t||ƒ}t||ƒ}| pp|S d S )NÚfailedFr   r   )Úoutcomeri   rn   rp   )r.   Úreportrm   rl   ro   Zmatches_rerun_onlyZmatches_rerun_exceptr   r   r   Ú_should_hard_fail_on_error  s    




ÿrx   c                 C   s>   t |dƒ}| j|j }t| ƒ}| j|kp<|j p<|p<|p<| S )NZwasxfail)rL   Ú_terminal_errorsÚwhenr@   Úexecution_countru   )r.   rw   r   ZxfailZis_terminal_errorr>   r   r   r   Ú_should_not_rerun2  s    

ÿþýûr|   c                 C   s   t | dƒpt | dƒ S )NÚworkerinputZ
slaveinput)rL   ©r+   r   r   r   Ú	is_master?  s    r   c                 C   sZ   |   dd¡ | j d¡rNtrN| j tƒ ¡ t| ƒr<tƒ | _qVt	| j
d ƒ| _ntƒ | _d S )NÚmarkersz�flaky(reruns=1, reruns_delay=0): mark test to re-run up to 'reruns' times. Add a delay of 'reruns_delay' seconds between re-runs.ZxdistÚ	sock_port)Zaddinivalue_lineZpluginmanagerZ	haspluginÚHAS_PYTEST_HANDLECRASHITEMÚregisterÚ
XDistHooksr   ÚServerStatusDBÚfailures_dbÚClientStatusDBr}   ÚStatusDBr~   r   r   r   Úpytest_configureC  s    þ
r‰   c                   @   s   e Zd Zdd„ Zdd„ ZdS )r„   c                 C   s   |j jj|jd< dS )z(Configure xdist hook for node sock_port.r�   N)r+   r†   r�   r}   )ÚselfÚnoder   r   r   Úpytest_configure_nodeW  s    z XDistHooks.pytest_configure_nodec                 C   s>   |j j}| |¡}| |¡|k r0| |¡ d|_| |¡ dS )z1Return the crashitem from pending and collection.ÚrerunN)r+   r†   Úget_test_rerunsÚget_test_failuresZmark_test_pendingrv   Úadd_test_failure)rŠ   Ú	crashitemrw   ZschedÚdbr   r   r   r   r   [  s    

z!XDistHooks.pytest_handlecrashitemN)rq   Ú
__module__Ú__qualname__rŒ   r   r   r   r   r   r„   V  s   r„   c                   @   sh   e Zd Zdd„ Zeedœdd„Zdd„ Zdd	„ Zd
d„ Zdd„ Z	eee
dœdd„Zeee
dœdd„ZdS )rˆ   c                 C   s   d| _ i | _d S )Nó   
)ÚdelimÚhmap©rŠ   r   r   r   Ú__init__k  s    zStatusDB.__init__)r‘   rA   c                 C   s4   || j vr*t | ¡ ¡ ¡ d d… | j |< | j | S )Né
   )r—   ÚhashlibÚsha1ÚencodeÚ	hexdigest)rŠ   r‘   r   r   r   Ú_hasho  s    
 zStatusDB._hashc                 C   s0   |   |¡}|  |d¡}|d7 }|  |d|¡ d S )NÚfr0   )rŸ   Ú_getÚ_set)rŠ   r‘   ÚhashZfailuresr   r   r   r�   u  s    
zStatusDB.add_test_failurec                 C   s   |   |¡}|  |d¡S )Nr    ©rŸ   r¡   ©rŠ   r‘   r£   r   r   r   r�   {  s    
zStatusDB.get_test_failuresc                 C   s   |   |¡}|  |d|¡ d S ©NÚr)rŸ   r¢   )rŠ   r‘   r   r£   r   r   r   Úset_test_reruns  s    
zStatusDB.set_test_rerunsc                 C   s   |   |¡}|  |d¡S r¦   r¤   r¥   r   r   r   rŽ   ƒ  s    
zStatusDB.get_test_reruns©ÚiÚkÚvc                 C   s   d S rg   r   ©rŠ   rª   r«   r¬   r   r   r   r¢   Š  s    zStatusDB._set©rª   r«   rA   c                 C   s   dS ©Nr   r   ©rŠ   rª   r«   r   r   r   r¡   �  s    zStatusDB._getN)rq   r“   r”   r™   r    rŸ   r�   r�   r¨   rŽ   r!   r¢   r¡   r   r   r   r   rˆ   j  s   rˆ   c                       s8   e Zd Z‡ fdd„Zedœdd„Zedœdd„Z‡  ZS )	ÚSocketDBc                    s,   t ƒ  ¡  t tjtj¡| _| j d¡ d S ©Nr0   )Úsuperr™   ÚsocketÚAF_INETÚSOCK_STREAMÚsockÚsetblockingr˜   ©Ú	__class__r   r   r™   ’  s    
zSocketDB.__init__)rA   c                 C   s,   d}|  d¡}|| jkrq$||7 }q| ¡ S )Nó    r0   )Úrecvr–   Údecode)rŠ   ÚconnÚbufÚbr   r   r   Ú
_sock_recv—  s    


zSocketDB._sock_recv)Úmsgc                 C   s   |  | ¡ | j ¡ d S rg   )Úsendr�   r–   )rŠ   r¾   rÂ   r   r   r   Ú
_sock_send¡  s    zSocketDB._sock_send)rq   r“   r”   r™   r    rÁ   rÄ   Ú__classcell__r   r   r¹   r   r±   ‘  s   
r±   c                       s\   e Zd Z‡ fdd„Zedd„ ƒZdd„ Zdd„ Zeee	d	œd
d„Z
eee	dœdd„Z‡  ZS )r…   c                    sL   t ƒ  ¡  | j d¡ | j tjtjd¡ i | _t	j
| jdd�}| ¡  d S )N)Ú	localhostr   r0   T)ÚtargetÚdaemon)r³   r™   r·   ÚbindÚ
setsockoptr´   Ú
SOL_SOCKETÚSO_REUSEADDRÚrerunfailures_dbÚ	threadingÚThreadÚ
run_serverÚstart)rŠ   Útr¹   r   r   r™   ¦  s    
zServerStatusDB.__init__c                 C   s   | j  ¡ d S r²   )r·   Úgetsocknamer˜   r   r   r   r�   ¯  s    zServerStatusDB.sock_portc                 C   s:   | j  ¡  | j  ¡ \}}tj| j|fdd�}| ¡  q
d S )NT)rÇ   r3   rÈ   )r·   ÚlistenÚacceptrÎ   rÏ   Úrun_connectionrÑ   )rŠ   r¾   ra   rÒ   r   r   r   rÐ   ³  s    
zServerStatusDB.run_serverc              	   C   s‚   t tƒ�f |  |¡ d¡\}}}}|dkr>|  ||t|ƒ¡ q
|dkr
|  |t|  ||¡ƒ¡ q
W d   ƒ n1 st0    Y  d S )Nú|ÚsetÚget)	r   ÚConnectionErrorrÁ   Úsplitr¢   r!   rÄ   r    r¡   )rŠ   r¾   Úoprª   r«   r¬   r   r   r   rÖ   º  s    
zServerStatusDB.run_connectionr©   c                 C   s&   || j vri | j |< || j | |< d S rg   )rÍ   r­   r   r   r   r¢   Ã  s    

zServerStatusDB._setr®   c                 C   s*   z| j | | W S  ty$   Y dS 0 d S r¯   )rÍ   ÚKeyErrorr°   r   r   r   r¡   È  s    zServerStatusDB._get)rq   r“   r”   r™   Úpropertyr�   rÐ   rÖ   r    r!   r¢   r¡   rÅ   r   r   r¹   r   r…   ¥  s   	
	r…   c                       s@   e Zd Z‡ fdd„Zeeedœdd„Zeeedœdd„Z‡  ZS )	r‡   c                    s   t ƒ  ¡  | j d|f¡ d S )NrÆ   )r³   r™   r·   Úconnect)rŠ   r�   r¹   r   r   r™   Ð  s    
zClientStatusDB.__init__r©   c              
   C   s$   |   | jd d||t|ƒf¡¡ d S )Nr×   rØ   )rÄ   r·   rS   r    r­   r   r   r   r¢   Ô  s    zClientStatusDB._setr®   c              	   C   s,   |   | jd d||df¡¡ t|  | j¡ƒS )Nr×   rÙ   Ú )rÄ   r·   rS   r!   rÁ   r°   r   r   r   r¡   ×  s    zClientStatusDB._get)	rq   r“   r”   r™   r    r!   r¢   r¡   rÅ   r   r   r¹   r   r‡   Ï  s   r‡   c                 C   sÌ   t | ƒ}|d u rd S t| dƒs"d S t| di ƒ}| j|kr°t| ¡ ƒr°t| j ¡ ƒs°t| ƒ | | jj	j
v rÈt| jj	j
 ¡ ƒD ]2}|| krz|tvr | jj	j
| t|< | jj	j
|= qzn| jj	j
 t¡ t ¡  d S )Nr{   Ú_test_failed_statuses)r9   rL   r_   r{   ÚanyÚvaluesry   rc   r4   rd   re   ÚlistÚkeysÚsuspended_finalizersrM   Úclear)r.   Únextitemr   rá   Úkeyr   r   r   Úpytest_runtest_teardownß  s*    
ÿ
þýrê   )Zhookwrapperc                 c   sh   d V }|  ¡ }|jdkr0t| di ƒ t| di ƒ t| di ƒ}|j||j< || _t| ||jƒ| j|j< d S )NÚsetuprá   ry   )	Z
get_resultrz   r`   r_   ru   rá   rx   rm   ry   )r.   Úcallrv   rY   rá   r   r   r   Úpytest_runtest_makereport  s    
ÿrí   c           	      C   s0  t | ƒ}|du rdS t| jjƒ t| ƒ}t| jƒ }| jjj}| | j¡| _	| 
| j|¡ | j	|krhdS d}|�r,|  j	d7  _	| jj| j| jd� t| |dd�}|D ]j}| j	d |_t| ||ƒrÒ| jj|d� q¦d|_t |¡ |rìtƒ rú| jj|d� t| ƒ t| ƒ  �qq¦d}| jj| j| jd� qldS )	z¨
    Run the test protocol.

    Note: when teardown fails, two reports are generated for the case, one for
    the test case and the other for the teardown error.
    NTr0   )ÚnodeidÚlocationF)rè   Úlog©rw   r�   )r9   r-   r4   r+   r=   r   r†   r�   rî   r{   r¨   ZihookZpytest_runtest_logstartrï   r   r�   r|   Zpytest_runtest_logreportrv   ÚtimeÚsleepr   rc   rf   Zpytest_runtest_logfinish)	r.   rè   r   r<   Úparallelr’   Zneed_to_runZreportsrw   r   r   r   Úpytest_runtest_protocol  s<    



rõ   c                 C   s    | j dkrdddddiffS d S )Nr�   ÚRZRERUNÚyellowT)rv   rñ   r   r   r   Úpytest_report_teststatusR  s    
rø   c                 C   s\   | }|j sd S g }|j D ]}|dv rt| |ƒ q|rX|j dd¡ |D ]}|j |¡ qFd S )NZrRú=zrerun test summary info)ZreportcharsÚ
show_rerunZ_twÚsepÚline)ÚterminalreporterÚtrÚlinesÚcharrü   r   r   r   Úpytest_terminal_summaryX  s    
r  c                 C   s4   | j  d¡}|r0|D ]}|j}| d|› �¡ qd S )Nr�   zRERUN )ÚstatsrÙ   rî   r   )rý   rÿ   r�   ÚrepÚposr   r   r   rú   i  s
    rú   )Ztrylastc                 C   sF   |dkrd S | j jjrB| jD ]$}t|dƒs,q|jdkrd| _ qBqd S )Nr   r{   r0   é   )r+   r)   r   ÚitemsrL   r{   Ú
exitstatus)r4   r  r.   r   r   r   Úpytest_sessionfinishq  s    



r  )>r›   Úimportlib.metadatar   rB   rD   rs   r´   rC   rÎ   rò   rU   r:   Ú
contextlibr   r*   Z_pytest.outcomesr   Z_pytest.runnerr   Zpackaging.versionr   r   Zxdist.newhooksr   r‚   ÚImportErrorr   r"   r$   r'   r-   r/   r9   r=   r@   ÚobjectrW   r?   rc   rf   ri   rn   rp   rk   rx   r|   r   r‰   r„   rˆ   r±   r…   r‡   ræ   rê   Zhookimplrí   rõ   rø   r  rú   r  r   r   r   r   Ú<module>   sp   
4/	'*&

:
