
    ܘjJ                        d Z ddlZddlmc m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Z ej        e                     Z ej        e                     ZdZ ee	d          ZerdZndZd	                                Zd
                                Zd                                Zd                                Zd                                Zh da  edg d          Z!ej"        d             Z# G d d          Z$d Z%ej"        d             Z& ej"        d          d             Z' ej"        d          d             Z( ej"        d          d             Z) ej"        d          d             Z*ej"        d             Z+d-dZ,da- ej"        d           d!             Z. ej"        d           d"             Z/d# Z0d-d$Z1d% Z2d& Z3d' Z4d( Z5d) Z6d* Z7d+ Z8d, Z9dS ).a!  Timeout for tests to stop hanging testruns.

This plugin will dump the stack and terminate the test.  This can be
useful when running tests on a continuous integration server.

If the platform supports SIGALRM this is used to raise an exception in
the test, otherwise os._exit(1) is used.
    N)
namedtuple)is_debuggingSettingsz#Timeout (>%ss) from pytest-timeout.SIGALRMsignalthreadzU
Timeout in seconds before dumping the stacks.  Default is 0 which
means no timeout.
z
Timeout mechanism to use.  'signal' uses SIGALRM, 'thread' uses a timer
thread.  If unspecified 'signal' is used on platforms which support
SIGALRM, otherwise 'thread' is used.
z
When set to True, defers the timeout evaluation to only the test
function body, ignoring the time it takes when evaluating any fixtures
used in the test.
zv
When specified, disables debugger detection. breakpoint(), pdb.set_trace(), etc.
will be interrupted by the timeout.
z
Timeout in seconds for entire session.  Default is None which
means no timeout. Timeout is checked between tests, and will not interrupt a test
in progress.
>   bdbpydevdpydevd_frame_evaluatorr   )timeoutmethod	func_onlydisable_debugger_detectionc           	         |                      dd          }|                    dt          t                     |                    ddddgd	
           |                    dddddgt                     |                    dddt
                     |                    ddddt          dt                     |                     dt                     |                     dt                     |                     dt          dd           |                     dt
          dd           |                     dt                     dS )z*Add options to control the timeout plugin.r   zHInterrupt test run and dump stacks of all threads after a test times outz	--timeout)typehelpz--timeout_methodstorer   r   z Deprecated, use --timeout-method)actionchoicesr   z--timeout-methodtimeout_method)destr   r   r   z$--timeout-disable-debugger-detection"timeout_disable_debugger_detection
store_true)r   r   r   z--session-timeoutsession_timeoutNSECONDS)r   r   defaultr   metavarr   timeout_func_onlyboolF)r   r   )	getgroup	addoptionfloatTIMEOUT_DESCMETHOD_DESCDISABLE_DEBUGGER_DETECTION_DESCSESSION_TIMEOUT_DESCaddiniFUNC_ONLY_DESC)parsergroups     U/home/ubuntu/.hermes/hermes-agent/venv/lib/python3.11/site-packages/pytest_timeout.pypytest_addoptionr,   ?   sz    OOR E 
OOKe,O???	OO8$/	     
OO8$     
OO.1,	     
OO!     MM)\***
MM"K000
MM%~FEMRRR
MM,'	     MM#%9:::::    c                   z    e Zd ZdZ ej        d          d             Z ej        d          d             ZdS )TimeoutHookszTimeout specific hooks.T)firstresultc                     dS )zCalled at timeout setup.

        'item' is a pytest node to setup timeout for.

        Can be overridden by plugins for alternative timeout implementation strategies.

        N itemsettingss     r+   pytest_timeout_set_timerz%TimeoutHooks.pytest_timeout_set_timerr         r-   c                     dS )zCalled at timeout teardown.

        'item' is a pytest node which was used for timeout setup.

        Can be overridden by plugins for alternative timeout implementation strategies.

        Nr2   r4   s    r+   pytest_timeout_cancel_timerz(TimeoutHooks.pytest_timeout_cancel_timer|   r7   r-   N)__name__
__module____qualname____doc__pytesthookspecr6   r:   r2   r-   r+   r/   r/   o   sn        !!V_&&&  '& V_&&&  '&  r-   r/   c                 :    |                      t                     dS )z Register timeout-specific hooks.N)add_hookspecsr/   )pluginmanagers    r+   pytest_addhooksrD      s    -----r-   c                    |                      dd           t          |           }|j        | _        |j        | _        |j        | _        |j        | _	        | 
                    d          }|:|                     d          }|r#t          |                     d          d          }|t          j                    |z   }nd}d}|| j        t          <   || j        t           <   dS )z7Register the marker so it shows up in --markers output.markersaw  timeout(timeout, method=None, func_only=False, disable_debugger_detection=False): Set a timeout, timeout method and func_only evaluation on just one test item.  The first argument, *timeout*, is the timeout in seconds while the keyword, *method*, takes the same values as the --timeout-method option. The *func_only* keyword, when set to True, defers the timeout evaluation to only the test function body, ignoring the time it takes when evaluating any fixtures used in the test. The *disable_debugger_detection* keyword, when set to True, disables debugger detection, allowing breakpoint(), pdb.set_trace(), etc. to be interruptedr   Nconfig filer   )addinivalue_lineget_env_settingsr   _env_timeoutr   _env_timeout_methodr   _env_timeout_func_onlyr   '_env_timeout_disable_debugger_detection	getoptiongetini_validate_timeouttimestashSESSION_TIMEOUT_KEYSESSION_EXPIRE_KEY)configr5   r   iniexpire_times        r+   pytest_configurerX      s     
	    ''H"*F!)F$,$6F!5=5XF2011Gmm-.. 	Y'6G(H(H-XXGikkG+(/FL$%'2FL#$$$r-   T)hookwrapperc              #     K   | j         j        j        }t          |           }|j        duo
|j        dk    }|r |j        du r|                    | |           dV  |r|j        du r|                    |            | j        j         j	        t                   }|rE|t          j                    k     r0| j        j         j	        t                   }d| d| j        _        dS dS dS )zHook in timeouts to the runtest protocol.

    If the timeout is set on the entire test, including setup and
    teardown, then this hook installs the timeout.  Otherwise
    pytest_runtest_call is used.
    Nr   Fr3   r9   zsession-timeout: z sec exceeded)rU   rC   hook_get_item_settingsr   r   r6   r:   sessionrR   rT   rQ   rS   
shouldfail)r4   hooksr5   
is_timeoutrW   r   s         r+   pytest_runtest_protocolra      s      K%*E!$''H!-F(2BQ2FJ Eh(E11&&D8&DDD	EEE 5h(E11))t)444 ,%+,>?K Mdikk11,%+,?@"Lg"L"L"LM M11r-   c              #     K   | j         j        j        }t          |           }|j        duo
|j        dk    }|r |j        du r|                    | |           dV  |r!|j        du r|                    |            dS dS dS )zHook in timeouts to the test function call only.

    If the timeout is set on only the test function this hook installs
    the timeout, otherwise pytest_runtest_protocol is used.
    Nr   Tr3   r9   )rU   rC   r[   r\   r   r   r6   r:   )r4   r_   r5   r`   s       r+   pytest_runtest_callrc      s       K%*E!$''H!-F(2BQ2FJ Eh(D00&&D8&DDD	EEE 5h(D00))t)444445 500r-   )tryfirstc                     g }| j         r-|                    d| j         d| j        d| j                   |                     d          }|r|                    d|z             |r|S dS )z$Add timeout config to pytest header.z	timeout: zs
timeout method: z
timeout func_only: r   zsession timeout: %ssN)rJ   appendrK   rL   rN   )rU   timeout_headerr   s      r+   pytest_report_headerrh      s     N 
 ###***--		
 	
 	
 &&'899O H4FGGG  r-   c                 T    | j         j        j        }|                    |            dS )z<Stop the timeout when pytest enters pdb in post-mortem mode.r9   N)rU   rC   r[   r:   )noder_   s     r+   pytest_exception_interactrk      s.     K%*E	%%4%00000r-   c                  
    da dS )zStop the timeouts when we entered pdb.

    This stops timeouts from triggering when pytest's builting pdb
    support notices we entered pdb.
    TN)SUPPRESS_TIMEOUTr2   r-   r+   pytest_enter_pdbrn      s     r-   c                    t           rdS | t          j                    } d}| r-t          j        |           pt          j        | j                  }|rB|j                            d          }t          D ] t          fd|D                       r dS !t          t          d          r2t          j                            t          j        j                  dk    S dS )a(  Detect if a debugging session is in progress.

    This looks at both pytest's builtin pdb support as well as
    externally installed debuggers using some heuristics.

     This is done by checking if either of the following conditions is
     true:

     1. Examines the trace function to see if the module it originates
        from is in KNOWN_DEBUGGING_MODULES.
     2. Check is SUPPRESS_TIMEOUT is set to True.

    :param trace_func: the current trace function, if not given will use
        sys.gettrace(). Used to unit-test this function.
    TN.c              3   B   K   | ]}|                               V  d S N)
startswith).0partnames     r+   	<genexpr>zis_debugging.<locals>.<genexpr>#  s/      ;;T4??4((;;;;;;r-   
monitoringF)rm   sysgettraceinspect	getmodule	__class__r;   splitKNOWN_DEBUGGING_MODULESanyhasattrrx   get_toolDEBUGGER_ID)
trace_functrace_modulepartsrv   s      @r+   r   r     s    "  t\^^
L 
(44 
8I 9
 9
  %++C00+ 	 	D;;;;U;;;;; tt sL!! K~&&s~'ABBdJJ5r-   F)trylastc                     j         }|dk    r(t          j                    t          j                    urd}|dk    rT fd}d }| _        t          j        t
          j        |           t          j        t
          j        j	                   n^|dk    rXt          j
        j	        t           f          t          d j        _        fd}| _                                         dS )z'Setup up a timeout trigger and handler.r   r   c                 ,    d}t                     d S )NT)timeout_sigalrm)signumframe__tracebackhide__r4   r5   s      r+   handlerz)pytest_timeout_set_timer.<locals>.handler;  s     $D(+++++r-   c                      t          j        t           j        d           t          j         t           j        t           j                   d S )Nr   )r   	setitimerITIMER_REALr   SIG_DFLr2   r-   r+   cancelz(pytest_timeout_set_timer.<locals>.cancel?  s4    V/333M&.&.99999r-    c                  X                                                                         d S rr   )r   join)timers   r+   r   z(pytest_timeout_set_timer.<locals>.cancelJ  s!    LLNNNJJLLLLLr-   T)r   	threadingcurrent_threadmain_threadcancel_timeoutr   r   r   r   r   Timertimeout_timerr;   nodeidrv   start)r4   r5   r   r   r   r   s   ``   @r+   r6   r6   /  s    _N(""$&&i.C.E.EEE!!!	, 	, 	, 	, 	, 	,	: 	: 	: %fng...+X-=>>>>	8	#	# 0-$AQRR ($++6
	 	 	 	 	 %4r-   c                 @    t          | dd          }|r
 |             dS )z)Cancel the timeout trigger if it was set.r   NT)getattr)r4   r   s     r+   r:   r:   S  s.     T+T22F 4r-   c                 4   |                      d          }|-t          t          j                            d          d          }|'|                     d          }|rt          |d          }|                      d          }|'|                     d          }|rt          |d          }|t          }|                     d          }|                      d          }|'|                     d          }|rt          |d          }t          ||||          S )	zpReturn the configured timeout settings.

    This looks up the settings in the environment and config file.
    r   NPYTEST_TIMEOUTz#PYTEST_TIMEOUT environment variablerG   r   r   r   )
getvaluerP   osenvirongetrO   _validate_methodDEFAULT_METHOD$_validate_disable_debugger_detectionr   )rU   r   rV   r   r   r   s         r+   rI   rI   `  s(   
 ooi((G#JNN+,,.S
 
 mmI&& 	<'];;G__-..F~mm,-- 	:%c=99F~122I!'1U!V!V!)mm@AA 	)M]* *& GVY0JKKKr-   c                    dx}x}x}}|s|                      d          }|wt          |                      d                    }t          |j        d          }t	          |j        d          }t          |j        d          }t          |j	        d          }|| j
        j        }|| j
        j        }|| j
        j        }|| j
        j        }t          ||||          S )z%Return (timeout, method) for an item.Nr   )rv   marker)get_closest_marker_parse_markerrP   r   r   r   _validate_func_onlyr   r   r   rU   rJ   rK   rL   rM   r   )r4   r   r   r   r   r   r5   s          r+   r\   r\     s    @DDGDfDy#= 4((33 !8!8i!8!H!HII#H$4h??!(/8<<'(:HEE	%I/&
 &
" +*~0K6	!)%)[%X"GVY0JKKKr-   c                    | j         s| j        st          d          t                      x}x}x}}| j                                        D ]1\  }}|dk    r|}|dk    r|}|dk    r|} t          d|z            t          | j                   dk    r||urt          d          t          | j                   dk    r| j         d         }t          | j                   d	k    r||urt          d
          t          | j                   d	k    r| j         d         }t          | j                   d	k    rt          d          ||u rd}||u rd}||u rd}t          |||d          S )zReturn (timeout, method) tuple from marker.

    Either could be None.  The values are not interpreted, so
    could still be bogus and even the wrong type.
    z.Timeout marker must have at least one argumentr   r   r   z/Invalid keyword argument for timeout marker: %s   z6Multiple values for timeout argument of timeout markerr      z5Multiple values for method argument of timeout markerz%Too many arguments for timeout markerN)argskwargs	TypeErrorobjectitemslenr   )r   r   r   r   NOTSETkwvals          r+   r   r     s    ; Jv} JHIII,2HH4G4f4y6=&&(( T TC??GG8^^FF;IIMPRRSSS
6;1!6!6PQQQ	V[		Q		+a.
6;1v!5!5OPPP	V[		Q		Q
6;!?@@@&F	GVY555r-   c                 p    | d S 	 t          |           S # t          $ r t          d| d|          w xY w)NzInvalid timeout  from )r"   
ValueError)r   wheres     r+   rP   rP     sX    tJW~~ J J Jj%%HIIIJs     5c                 @    | d S | dvrt          d| d|          | S )N)r   r   zInvalid method r   )r   )r   r   s     r+   r   r     s8    ~t)))jFGGGMr-   c                 b    | d S t          | t                    st          d| d|          | S )NzInvalid func_only value r   
isinstancer   r   )r   r   s     r+   r   r     sA    ti&& TjEERSSSr-   c                 b    | d S t          | t                    st          d| d|          | S )Nz)Invalid disable_debugger_detection value r   r   )r   r   s     r+   r   r     sN    !)t0$77 
j)))552
 
 	
 &%r-   c                 ~   |j         st                      rdS d}t          t          j                              }| j                                        }|dk    r|                    dd           t          |           |dk    r|                    dd           t          j
        t          |j        z             dS )zDump stack of threads and raise an exception.

    This will output the stacks of any threads other then the
    current to stderr and then raise an AssertionError, thus
    terminating the test.
    NTr   +Timeouttitle)r   r   r   r   	enumeraterU   get_terminal_writersepdump_stacksr?   failPYTEST_FAILURE_MESSAGEr   )r4   r5   r   nthreadsterminals        r+   r   r     s     . <>> 9&(())H{..00H!||S	***!||S	***
K&)99:::::r-   c                    |j         st                      rdS | j                                        }	 | j        j                            d          }|r-|                    |            |                                \  }}nd\  }}|                    dd           | j        j                            d          }|r\t          | d          rL| j
        j                                        }|r,|                    d	d
           |                    |           |r,|                    d	d           |                    |           |r,|                    d	d           |                    |           t          |           |                    dd           n## t          $ r t!          j                     Y nw xY w|                                 t&          j                                         t&          j                                         t-          j        d           dS # |                                 t&          j                                         t&          j                                         t-          j        d           w xY w)zDump stack of threads and call os._exit().

    This disables the capturemanager and dumps stdout and stderr.
    Then the stacks are dumped and os._exit(1) is called.
    Ncapturemanager)NNr   r   r   _capturelogcapturelog_handler~zCaptured logzCaptured stdoutzCaptured stderrr   )r   r   rU   r   rC   	getpluginsuspend_global_captureread_global_capturer   r   r   streamr   writer   	Exception	traceback	print_excflushry   stdoutstderrr   _exit)r4   r5   r   capmanr   r   caploglogs           r+   r   r     sl    . <>> {..00H*445EFF 	())$///#7799NFFF'NFFS	****44]CC 	$gd$899 	$)099;;C $S777s### 	#LL$5L666NN6""" 	#LL$5L666NN6"""HS	****    	


 	


s+   E)F H% F<9H% ;F<<H% %A&Jc           	         t          j                    j        }t          j                                                    D ]\  }}||k    rt          j                    D ]}|j        |k    r	|j        } nd}|                     dd|d|d           | 	                    d
                    t          j        |                               dS )	z9Dump the stacks of all threads except the current thread.z	<unknown>r   z	Stack of z ()r    N)r   r   identry   _current_framesr   r   rv   r   r   r   r   format_stack)r   current_identthread_identr   tthread_names         r+   r   r   !  s    ,..4M"244::<< 
? 
?e=(($&& 	& 	&Aw,&&f ' &KSSkkk<<< PQQQrwwy5e<<==>>>>
? 
?r-   rr   ):r>   builtins@py_builtins_pytest.assertion.rewrite	assertionrewrite
@pytest_arr{   r   r   ry   r   rQ   r   collectionsr   r?   __all__StashKeyr"   rS   rT   r   r   HAVE_SIGALRMr   stripr#   r$   r(   r%   r&   r   r   hookimplr,   r/   rD   rX   ra   rc   rh   rk   rn   r   rm   r6   r:   rI   r\   r   rP   r   r   r   r   r   r   r2   r-   r+   <module>r      s                 				  



          " " " " " "  ',foe,.. +V_U+-- > wvy)) NNN 
EGG  
EGG	 
 
EGG	 
# 
EGG   
EGG	  FEE :PPP 
 ,; ,; ,;^       0. . .
 "3 "3 "3J T"""M M #"M. T"""5 5 #"5  $   * $1 1  1 
 
 
# # # #L        F 	 	 	!L !L !LHL L L L0"6 "6 "6JJ J J    & & &; ; ;(% % %P? ? ? ? ?r-   