o
    HXh                     @   sl  d dl mZ d dlZd dlmZ d dlZd dlZd dlZd dlm	Z	 d dl
Z
G dd dZG dd dZd	d
 Zdd Zg de_dd Zg de_dd Zg de_dd Zg de_dd Zg de_dd Zg de_dd Zg de_dd  Zd!d" Zg d#e_d$d% Zd&d' Zg d(e_d)d* Zd+d, Zd-ge_d.d/ Zd0d1 Zg d2e_d3d4 Zg d5e_d6d7 Zg d8e_d9d: Z g d;e _d<d= Z!d>d? Z"g d@dAdBgdC  dAdDg e"_G dEdF dFZ#G dGdH dHej$Z%G dIdJ dJe%Z&ej'G dKdL dLe%Z(G dMdN dNej$Z)G dOdP dPZ*dQdR Z+dSdT Z,G dUdV dVej$Z-G dWdX dXej$Z.e/dYkr4e0  dS dS )Z    )supportN)	MagicMockwrapsc                   @   (   e Zd ZdZdd Zdd Zdd ZdS )	tracecontextz/Context manager that traces its enter and exit.c                 C      || _ || _d S Noutputvalueselfr   r    r   @/opt/python-3.10.19/usr/lib/python3.10/test/test_sys_settrace.py__init__      
ztracecontext.__init__c                 C   s   | j | j d S r	   r   appendr   r   r   r   r   	__enter__      ztracecontext.__enter__c                 G   s   | j | j  d S r	   r   r   exc_infor   r   r   __exit__   s   ztracecontext.__exit__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r      
    r   c                   @   r   )	asynctracecontextz>Asynchronous context manager that traces its aenter and aexit.c                 C   r   r	   r
   r   r   r   r   r      r   zasynctracecontext.__init__c                    s   | j | j d S r	   r   r   r   r   r   
__aenter__   s   zasynctracecontext.__aenter__c                    s   | j | j  d S r	   r   r   r   r   r   	__aexit__"   s   zasynctracecontext.__aexit__N)r   r   r   r   r   r!   r"   r   r   r   r   r       r   r    c                 C  s   | D ]}|V  qdS )z0Convert an iterable to an asynchronous iterator.Nr   )iterablexr   r   r   	asynciter%   s   r%   c                   C   s   dS N   r   r   r   r   r   basic,      r(   )r   callr'   liner'   returnc                  C      d} ~ 	 d} d S r&   r   r$   r   r   r   arigo_example0;      r2   )r*   r,      r-      r-      r-   r9   r/   c                  C   r0   r&   r   r1   r   r   r   arigo_example1I   r3   r;   c                  C   r0   r&   r   r1   r   r   r   arigo_example2W   s
   r<   )r*   r,   r4   r6      r-      r-   r@   r/   c                  C   s   d} ~ d} d S r&   r   r1   r   r   r   one_instr_linej   s   rB   r*   r,   r4   r6   r7   r/   c                  C   s$   d} t dD ]	}|rd} qd} qd S Nr'   r5   range)r$   ar   r   r   no_pop_topsu   s   rI   )
r*   r,   r4   r6      r-   r4   r6   r=   r4   r5   r/   c                  C   s   d} | st  | rd}d S r&   )Zbla)yr$   r   r   r   no_pop_blocks   s
   rN   r*   r,   r4   r=   r>   r/   c                  C   s   d} d S r&   r   r1   r   r   r   called   s   rQ   c                   C   
   t   d S r	   )rQ   r   r   r   r   r+      s   
r+   )r*   r,   r+   r-   rV   r/   r.   c                   C   s   t r	   	Exceptionr   r   r   r   raises   r)   rZ   c                   C   s"   zt   W d S  ty   Y d S w r	   )rZ   rY   r   r   r   r   
test_raise   s
   r[   )r*   r,   r4   rS   rU   rV   	exceptionrW   r5   r]   r6   r=   rP   c                 C   s   t |  | t  j_d S r	   )syssettrace	_getframef_backf_traceZ	tracefuncr   r   r   _settrace_and_return   s   
re   c                 C   s   t |  d S r	   )re   rd   r   r   r   settrace_and_return   s   rf   r.   c                 C   s   t |  | t  j_tr	   )r_   r`   ra   rb   rc   RuntimeErrorrd   r   r   r   _settrace_and_raise   s   
rh   c                 C   s$   zt |  W d S  ty   Y d S w r	   )rh   rg   rd   r   r   r   settrace_and_raise   s
   ri   )r^   r6   r=   rP   c                  C   s"   d} d}| |kr| d }d S 	 d S )Nr9   r'   r   )rH   br   r   r   ireturn_example   s
   rk   r*   r,   r4   r6   r=   rP   c                  C   s8   t dd} zd}	 | | }|d7 }q	 ty   Y d S w )Nr   r7   r'   rG   
IndexError)itemsirj   r   r   r   tightloop_example   s   
rq   )r*   r,   r4   r6   r=   r8   r=   r8   r=   r8   r=   r8   )r9   r]   rJ   r?   rA   c                  C   s.   t dd} zd}| | }q ty   Y d S w )Nr'   r>   r   rm   )ro   rp   r   r   r   tighterloop_example   s   

rr   )r*   r,   r4   r6   r=   r=   r=   r=   )r>   r]   r8   rJ   rK   r/   c                   c   s    zdV  W d S w )NTr   r   r   r   r   generator_function  s
   rt   c                  C   s    t t } tdD ]} | }q	d S )N
   )anyrt   rG   )r$   rM   r   r   r   generator_example  s   
rw   )
r*   r4   r+   r-   r-   r}   r/   )r}   r+   r}   r]   r-   r   r/   r8   rJ   ru   r:   c                   @   s.   e Zd Zd
ddZdd Zdd Zdd	 ZdS )TracerNc                 C   s   || _ || _g | _d S r	   )trace_line_eventstrace_opcode_eventsevents)r   r   r   r   r   r   r   1  s   
zTracer.__init__c                 C   s,   | j d ur	| j |_| jd ur| j|_d S d S r	   )r   f_trace_linesr   f_trace_opcodes)r   framer   r   r   _reconfigure_frame6  s
   

zTracer._reconfigure_framec                 C   s"   |  | | j|j|f | jS r	   r   r   r   f_linenotracer   r   eventargr   r   r   r   <  s   
zTracer.tracec                 C   s0   |  | dd dD  | j|j|f | jS )Nc                 s   s    | ]}|V  qd S r	   r   ).0or   r   r   	<genexpr>C  s    z)Tracer.traceWithGenexp.<locals>.<genexpr>r'   r   r   r   r   r   traceWithGenexpA  s   
zTracer.traceWithGenexp)NN)r   r   r   r   r   r   r   r   r   r   r   r   0  s
    
r   c                   @   s  e Zd Zdd Zdd Ze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( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Z d;d< Z!d=d> Z"d?d@ Z#dAdB Z$dCdD Z%dEdF Z&dGdH Z'dIdJ Z(dKdL Z)dMdN Z*dOdP Z+dQdR Z,dSdT Z-dUdV Z.dWdX Z/dYdZ Z0d[d\ Z1d]d^ Z2d_d` Z3dadb Z4dcdd Z5dedf Z6dgdh Z7didj Z8dkdl Z9dmdn Z:doS )pTraceTestCasec                 C   s(   t  | _t   | tjt  d S r	   )gc	isenabledusing_gcdisable
addCleanupr_   r`   gettracer   r   r   r   setUpL  s   
zTraceTestCase.setUpc                 C   s   | j r	t  d S d S r	   )r   r   enabler   r   r   r   tearDownQ  s   zTraceTestCase.tearDownc                   C   s   t  S )z@Helper to allow test subclasses to configure tracers differentlyr   r   r   r   r   make_tracerU  s   zTraceTestCase.make_tracerc              
      sR    fdd|D }||kr'|  ddtdd |D dd |D   d S d S )Nc                    s   g | ]
\}}|  |fqS r   r   )r   leline_offsetr   r   
<listcomp>[      z0TraceTestCase.compare_events.<locals>.<listcomp>z"events did not match expectation:

c                 S      g | ]}t |qS r   strr   r$   r   r   r   r   _      c                 S   r   r   r   r   r   r   r   r   `  r   )failjoindifflibZndiff)r   r   r   expected_eventsr   r   r   compare_eventsZ  s   zTraceTestCase.compare_eventsc                 C   s<   |   }t|j |  td  | |jj|j| d S r	   )r   r_   r`   r   r   __code__co_firstlinenor   )r   funcr   tracerr   r   r   run_and_compareb  s   

zTraceTestCase.run_and_comparec                 C   s   |  ||j d S r	   )r   r   r   r   r   r   r   run_testj  r   zTraceTestCase.run_testc                 C   s6   |   }||j td  | |jj|j|j d S r	   )r   r   r_   r`   r   r   r   r   )r   r   r   r   r   r   	run_test2m  s   


zTraceTestCase.run_test2c                 C   s   t d  t  d u sJ d S r	   r_   r`   r   r   r   r   r   test_set_and_retrieve_nonet  s   
z(TraceTestCase.test_set_and_retrieve_nonec              	   C   s@   dd }t | zt  |u sJ W t d  d S t d  w )Nc                  W      d S r	   r   argsr   r   r   fny  r)   z4TraceTestCase.test_set_and_retrieve_func.<locals>.fnr   )r   r   r   r   r   test_set_and_retrieve_funcx  s
   
z(TraceTestCase.test_set_and_retrieve_funcc                 C      |  t d S r	   )r   r(   r   r   r   r   test_01_basic     zTraceTestCase.test_01_basicc                 C   r   r	   )r   r2   r   r   r   r   test_02_arigo0  r   zTraceTestCase.test_02_arigo0c                 C   r   r	   )r   r;   r   r   r   r   test_02_arigo1  r   zTraceTestCase.test_02_arigo1c                 C   r   r	   )r   r<   r   r   r   r   test_02_arigo2  r   zTraceTestCase.test_02_arigo2c                 C   r   r	   )r   rB   r   r   r   r   test_03_one_instr  r   zTraceTestCase.test_03_one_instrc                 C   r   r	   )r   rN   r   r   r   r   test_04_no_pop_blocks  r   z#TraceTestCase.test_04_no_pop_blocksc                 C   r   r	   )r   rI   r   r   r   r   test_05_no_pop_tops  r   z!TraceTestCase.test_05_no_pop_topsc                 C   r   r	   )r   r+   r   r   r   r   test_06_call  r   zTraceTestCase.test_06_callc                 C   r   r	   )r   r[   r   r   r   r   test_07_raise  r   zTraceTestCase.test_07_raisec                 C   r   r	   )r   rf   r   r   r   r   test_08_settrace_and_return  r   z)TraceTestCase.test_08_settrace_and_returnc                 C   r   r	   )r   ri   r   r   r   r   test_09_settrace_and_raise  r   z(TraceTestCase.test_09_settrace_and_raisec                 C   r   r	   )r   rk   r   r   r   r   test_10_ireturn  r   zTraceTestCase.test_10_ireturnc                 C   r   r	   )r   rq   r   r   r   r   test_11_tightloop  r   zTraceTestCase.test_11_tightloopc                 C   r   r	   )r   rr   r   r   r   r   test_12_tighterloop  r   z!TraceTestCase.test_12_tighterloopc                 C   sH   |  t |  }t|j t  td  | tjj|j	tj	 d S r	   )
r   rw   r   r_   r`   r   r   r   r   r   )r   r   r   r   r   test_13_genexp  s   


zTraceTestCase.test_13_genexpc                 C      dd }|  |g d d S )Nc                  S   s   d} dS )NTFr   r   r1   r   r   r   onliners  s   z2TraceTestCase.test_14_onliner_if.<locals>.onliners)r*   r,   r6   rD   r   )r   r   r   r   r   test_14_onliner_if  s
   z TraceTestCase.test_14_onliner_ifc                 C   s4   dd }|  |g d dd }|  |g d d S )Nc                  S   s   t dD ]} qd S Nr5   rF   r1   r   r   r   for_example  s   z0TraceTestCase.test_15_loops.<locals>.for_example)r*   r,   r4   r,   r4   r,   r.   c                  S   s$   d} | dkr| d8 } | dksd S d S )Nr5   r   r'   r   r1   r   r   r   while_example  s   z2TraceTestCase.test_15_loops.<locals>.while_example)r*   r4   r6   r=   r6   r=   r6   rD   r   )r   r   r   r   r   r   test_15_loops  s   
zTraceTestCase.test_15_loopsc                 C   s&   i }t d| | |d g d d S )Na  def f():
































































































































































































































































    passf)r*   )  r-   )r   r/   )execr   )r   	namespacer   r   r   test_16_blank_lines  s   
z!TraceTestCase.test_16_blank_linesc                 C   s   dd }|  |ddg d S )Nc                  S   s   d t  _d} d S r   )r_   ra   rc   )linenor   r   r   r     s   
z0TraceTestCase.test_17_none_f_trace.<locals>.funcr*   r,   r   r   r   r   r   test_17_none_f_trace  s   z"TraceTestCase.test_17_none_f_tracec                 C   r   )Nc               
   S   s6   zzt  t y }  z d } ~ ww  t y   Y d S w r	   rX   )r   r$   rM   r   r   r   r     s   z4TraceTestCase.test_18_except_with_name.<locals>.func)
r*   r,   r4   r6   r7   r]   r=   r8      r-   )	   r-   )r   r/   r   r   r   r   r   test_18_except_with_name  s   z&TraceTestCase.test_18_except_with_namec                 C   r   )Nc                  S   s&   zzt d} w  t y   d}Y d S w )NZ	Something   rX   )rM   rj   r   r   r   r     s   
z7TraceTestCase.test_19_except_with_finally.<locals>.func)	r*   r,   r4   r6   r   r8   rJ   r?   rA   r   r   r   r   r   test_19_except_with_finally     	z)TraceTestCase.test_19_except_with_finallyc                    sp   G dd d  fddfdd}|   }g d}z||j W n	 ty+   Y nw | jj|j| d S )Nc                   @   $   e Zd Zdd Zdd Zdd ZdS )zBTraceTestCase.test_20_async_for_loop.<locals>.AsyncIteratorWrapperc                 S   s   t || _d S r	   )iter_it)r   objr   r   r   r     r   zKTraceTestCase.test_20_async_for_loop.<locals>.AsyncIteratorWrapper.__init__c                 S      | S r	   r   r   r   r   r   	__aiter__  r)   zLTraceTestCase.test_20_async_for_loop.<locals>.AsyncIteratorWrapper.__aiter__c                    s"   zt | jW S  ty   tw r	   )nextr   StopIterationStopAsyncIterationr   r   r   r   	__anext__  s   zLTraceTestCase.test_20_async_for_loop.<locals>.AsyncIteratorWrapper.__anext__N)r   r   r   r   r   r   r   r   r   r   AsyncIteratorWrapper  s    r   c                     s&    d2 z3 d H W } | }q6 d}d S )Nabc*   r   )Zletterr$   rM   )r   r   r   
doit_async%  s
   z8TraceTestCase.test_20_async_for_loop.<locals>.doit_asyncc              	      s8     }zt |  |d  W t d  d S t d  w r	   )r_   r`   send)r   r$   )r   r   r   run*  s
   
z1TraceTestCase.test_20_async_for_loop.<locals>.run)(r*   r,   )ir+   )r-   )r   r/   ir+   r-   r   r/   rx   rz   r|   r~   r'   r]   r4   r,   rx   rz   r|   r~   r   r4   r,   rx   rz   r|   r~   r   r4   r,   rx   rz   r|   r   rT   r-   rU   r\   rW   r   r6   rD   )r   r   rY   r   r   r   r   )r   r   r   r   r   )r   r   r   test_20_async_for_loop  s   )
z$TraceTestCase.test_20_async_for_loopc              	      s   dd   fdd}|   }| }zzt|j |d  W n	 ty(   Y nw W td  ntd  w g d}| |jj|j	| d S )Nc                 S  s   t | D ]}|V  qd S r	   rF   )nrp   r   r   r   aranged  s   zGTraceTestCase.test_async_for_backwards_jump_has_no_line.<locals>.arangec                     s,    d2 z3 d H W } | dkr d S q6 d S )Nr7   d   r   rp   r  r   r   r   g  s   zBTraceTestCase.test_async_for_backwards_jump_has_no_line.<locals>.f)r*   r,   rS   rU   r   r   r   r4   r,   r   r+   rU   r   r   r   r4   r,   r  rU   r   r   r   r4   r,   r  rU   rW   r   r.   )
r   r_   r`   r   r   rY   r   r   r   r   )r   r   r   coror   r   r  r   )test_async_for_backwards_jump_has_no_linec  s    
z7TraceTestCase.test_async_for_backwards_jump_has_no_linec                 C   r   )Nc                   S   s   	 d S r	   r   r   r   r   r   r     s   z1TraceTestCase.test_21_repeated_pass.<locals>.funcr*   r,   r4   rL   r   r   r   r   r   test_21_repeated_pass  s   z#TraceTestCase.test_21_repeated_passc                 C   r   )Nc                  S   s   zdD ]} qW dS    Y dS )Nr   r'   r5   r   r  r   r   r   r     s
   
z3TraceTestCase.test_loop_in_try_except.<locals>.funcrC   r   r   r   r   r   test_loop_in_try_except  s   z%TraceTestCase.test_loop_in_try_exceptc                 C   r   )Nc                   S   s   zzW W d S    Y W d S w r	   r   r   r   r   r   r     s   z8TraceTestCase.test_try_except_no_exception.<locals>.func)r*   r,   r4   rJ   rs   r   r   r   r   r   test_try_except_no_exception     z*TraceTestCase.test_try_except_no_exceptionc                 C   r   )Nc                  S   s,   t dD ]} t dD ]}| | }q
q|dkS )Nr5   r'   rF   )rp   jrH   r   r   r   r     s
   
z-TraceTestCase.test_nested_loops.<locals>.func)r*   r,   r4   r6   r4   r6   r4   r,   r4   r6   r4   r6   r4   r,   r=   rP   r   r   r   r   r   test_nested_loops     zTraceTestCase.test_nested_loopsc                 C   r   )Nc                  S   s*   ddg} | r|   }|r	 |S | sd}|S )Nr'   r   c   pop)seqr  r   r   r   r     s   z)TraceTestCase.test_if_break.<locals>.func)r*   r,   r4   r6   r=   r4   r6   r=   r8   r   )r   r/   r   r   r   r   r   test_if_break     
zTraceTestCase.test_if_breakc                 C   r   )Nc                  S   st   d\} }}}zt dD ]}zd} |dkrW d} n	d} W d}qd}w W n   d}Y | dkr6|dkr6|dks8J d S )	N)r'   r'   r'   r  r7   r9   r   ru   r      r'   rF   )rH   cdrp   r   r   r   r     s    z6TraceTestCase.test_break_through_finally.<locals>.func)r*   r,   r4   r6   r=   r8   rJ   r   ru   r-   r6   r=   r8   rJ   r?   r     r-   r  r/   r   r   r   r   r   test_break_through_finally     z(TraceTestCase.test_break_through_finallyc                 C   r   )Nc                  S   sl   d\} }}}}zt dD ]}zd} |dkrW d}qd}W d}qd}w W n   d}Y | |||fdks4J d S )	N)r'   r'   r'   r'   r  r5   r9   r   ru   r   r  )r9   r   ru   r'   rF   )rH   rj   r  r  rp   r   r   r   r     s   z9TraceTestCase.test_continue_through_finally.<locals>.func)r*   r,   r4   r6   r=   r8   rJ   r   r  r6   r=   r8   rJ   r?   r  r6   r  r  r   r   r   r   r   test_continue_through_finally  r   z+TraceTestCase.test_continue_through_finallyc                 C   r   )Nc                   S   s
   zW dS w r   r   r   r   r   r   r   B  s   z7TraceTestCase.test_return_through_finally.<locals>.funcrO   r   r   r   r   r   test_return_through_finally@  r  z)TraceTestCase.test_return_through_finallyc                 C   r   )Nc                   S   s6   zzdd  W W dS  t y   Y W dS w    Y dS )Nr5   r   rK   )rn   r   r   r   r   r   Q  s   z;TraceTestCase.test_try_except_with_wrong_type.<locals>.func)r*   r,   r4   r^   r6   rJ   rs   r   r   r   r   r   test_try_except_with_wrong_typeO  r  z-TraceTestCase.test_try_except_with_wrong_typec                 C   r   )Nc                  S   s"   d} dg}|r|   | r	 qd S r&   r  TRUEr$   r   r   r   r   d  s   z3TraceTestCase.test_break_to_continue1.<locals>.func)
r*   r,   r4   r6   r=   r8   rJ   r?   r6   rD   r   r   r   r   r   test_break_to_continue1b     	z%TraceTestCase.test_break_to_continue1c                 C   r   )Nc                  S   s*   d} dg}|r|   | rnq|sd S d S r&   r  r$  r   r   r   r   {  s   z3TraceTestCase.test_break_to_continue2.<locals>.func)	r*   r,   r4   r6   r=   r8   rJ   r6   rD   r   r   r   r   r   test_break_to_continue2y  r  z%TraceTestCase.test_break_to_continue2c                 C   r   )Nc                  S   s   d} | r| r		 d S d S d S r&   r   )r%  r   r   r   r     s   z/TraceTestCase.test_break_to_break.<locals>.func)r*   r,   r4   r6   r=   r8   r:   r   r   r   r   r   test_break_to_break     z!TraceTestCase.test_break_to_breakc                 C   r   )Nc                  S   s0   d } }| dkr|dkrd}d S d}d S d}d S )Nr'   r>   rK   r   r   )rH   rj   r$   rM   zr   r   r   r     s   z+TraceTestCase.test_nested_ifs.<locals>.funcrl   r   r   r   r   r   test_nested_ifs  r  zTraceTestCase.test_nested_ifsc                    s0    fdd}d  d|  |g d d S )Nc                      s4    rrr
t rdS d S d S dS trtrdS d S d S )NFT)DEFr   ABCr   r   r     s   z4TraceTestCase.test_nested_ifs_with_and.<locals>.funcTFrC   r   r   r   r0  r   test_nested_ifs_with_and  s   z&TraceTestCase.test_nested_ifs_with_andc                 C   r   )Nc                  S   s>   d} z	dd  W d}d S  t y   | dkrt Y d}d S w )NZhellor7   r   raiser@   )ZeroDivisionError
ValueError)r$   r   r   r   r   r     s   
z.TraceTestCase.test_nested_try_if.<locals>.func	r*   r,   r4   r6   r   r=   r8   r?   rA   r   r   r   r   r   test_nested_try_if  r'  z TraceTestCase.test_nested_try_ifc                    s.   G dd d  fdd}|  |g d d S )Nc                   @      e Zd Zdd Zdd ZdS )z.TraceTestCase.test_if_false_in_with.<locals>.Cc                 S   r   r	   r   r   r   r   r   r     r)   z8TraceTestCase.test_if_false_in_with.<locals>.C.__enter__c                  W   r   r	   r   r   r   r   r   r     r)   z7TraceTestCase.test_if_false_in_with.<locals>.C.__exit__Nr   r   r   r   r   r   r   r   r   r3        r3  c                      .     
 	 W d    d S 1 sw   Y  d S r	   r   r   r3  r   r   r     s   "z1TraceTestCase.test_if_false_in_with.<locals>.func)r*   r,   r{   r+   r|   r~   r4   r,   rS   rU   rW   r.   r   r   r   r>  r   test_if_false_in_with  s
   z#TraceTestCase.test_if_false_in_withc                 C   r   )Nc                   S   s    zW d S  t y   t Y d S w r	   )rY   Xr   r   r   r   r     s
   
z7TraceTestCase.test_if_false_in_try_except.<locals>.funcr
  r   r   r   r   r   test_if_false_in_try_except  r*  z)TraceTestCase.test_if_false_in_try_exceptc                 C   r   )Nc                  S   s   G dd d} d S )Nc                   @   s    e Zd Zddk rdZdS dZdS )zDTraceTestCase.test_implicit_return_in_class.<locals>.func.<locals>.Ar7   r   r'   r5   N)r   r   r   rH   r   r   r   r   r1    s    r1  r   )r1  r   r   r   r     r   z9TraceTestCase.test_implicit_return_in_class.<locals>.func)r*   r,   )r'   r+   r,   r4   r6   rD   r.   r   r   r   r   r   test_implicit_return_in_class  r*  z+TraceTestCase.test_implicit_return_in_classc                 C   r   )Nc               
   S   sH   zzW W d S  t y }  zW Y d } ~ W d S d } ~ ww  t y#   Y d S w r	   rX   )exr   r   r   r      s   z+TraceTestCase.test_try_in_try.<locals>.funcrC   r   r   r   r   r   test_try_in_try  r   zTraceTestCase.test_try_in_tryc                 C   s   ddd}|  |g d d S )Nr   r'   c                 S   s    |r| r|rd S 	 d S d S 	 d S r	   r   )rH   pr+  r   r   r   r   1  s   z/TraceTestCase.test_if_in_if_in_if.<locals>.funcr
  )r   r'   r'   r   r   r   r   r   test_if_in_if_in_if0  s   

z!TraceTestCase.test_if_in_if_in_ifc                    sJ   G dd d  fdd} fdd}|  |g d |  |g d d S )	Nc                   @   r:  )z-TraceTestCase.test_early_exit_with.<locals>.Cc                 S   r   r	   r   r   r   r   r   r   D  r)   z7TraceTestCase.test_early_exit_with.<locals>.C.__enter__c                  W   r   r	   r   r   r   r   r   r   F  r)   z6TraceTestCase.test_early_exit_with.<locals>.C.__exit__Nr;  r   r   r   r   r3  C  r<  r3  c               	      s:   dD ]}    	 W d     d S 1 sw   Y  qd S )Nr'   r5   r   r  r>  r   r   
func_breakI  s   z6TraceTestCase.test_early_exit_with.<locals>.func_breakc                      r=  r	   r   r   r>  r   r   func_returnO  s   $z7TraceTestCase.test_early_exit_with.<locals>.func_return)r*   r,   r4   r?  r|   r~   r6   r4   rS   rU   rW   r=   rP   )r*   r,   )r   r+   )r-   )rK  r/   r4   r,   r   r   r   r.   r   )r   rI  rJ  r   r>  r   test_early_exit_withA  s   z"TraceTestCase.test_early_exit_withc                    s(   dd   fdd}|  |g d d S )Nc                 S   s0   | rz	d| d   W | S  t y   Y | S w | S r&   r6  r1   r   r   r   fooq  s   z;TraceTestCase.test_flow_converges_on_same_line.<locals>.fooc                     s   t dD ]}  |  qd S r   rF   r  rN  r   r   r   y     
z<TraceTestCase.test_flow_converges_on_same_line.<locals>.func)r*   r,   r4   r   r+   ir-   rU   rW   r,   r4   rQ  rR  )ry   r-   rz   )r{   r]   r|   r   rU   rW   r,   r.   r   r   r   rO  r    test_flow_converges_on_same_lineo  s
   z.TraceTestCase.test_flow_converges_on_same_linec                 C   r   )Nc               
   S   s@   d} zd|   W dS  t y } z
| r W Y d }~dS d }~ww )Nr   r'   donerM  )r$   errorr   r   r   r     s   

zCTraceTestCase.test_no_tracing_of_named_except_cleanup.<locals>.funcr8  r   r   r   r   r   'test_no_tracing_of_named_except_cleanup  r'  z5TraceTestCase.test_no_tracing_of_named_except_cleanupN);r   r   r   r   r   staticmethodr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r	  r  r  r  r  r  r  r!  r"  r#  r&  r(  r)  r,  r4  r9  r@  rB  rC  rE  rG  rL  rS  rV  r   r   r   r   r   H  sr    

		L4$%.$r   c                       ,   e Zd ZdZ fddZedd Z  ZS )SkipLineEventsTraceTestCasez8Repeat the trace tests, but with per-line events skippedc                    s"   dd |D }t  ||| d S )Nc                 S      g | ]
}|d  dkr|qS r,   r   r   r   r   r   r   r     r   z>SkipLineEventsTraceTestCase.compare_events.<locals>.<listcomp>)superr   )r   r   r   r   Zskip_line_events	__class__r   r   r     s   z*SkipLineEventsTraceTestCase.compare_eventsc                   C   
   t ddS )NF)r   r   r   r   r   r   r        
z'SkipLineEventsTraceTestCase.make_tracerr   r   r   r   r   rW  r   __classcell__r   r   r]  r   rY    s
    rY  c                       rX  )TraceOpcodesTestCasez;Repeat the trace tests, but with per-opcodes events enabledc                    sF   dd |D }t |dkr| jt |t |dd t ||| d S )Nc                 S   rZ  )r'   Zopcoder   r[  r   r   r   r     r   z7TraceOpcodesTestCase.compare_events.<locals>.<listcomp>r'   z)No 'opcode' events received by the tracer)msg)lenZ
assertLessr\  r   )r   r   r   r   Zskip_opcode_eventsr]  r   r   r     s   z#TraceOpcodesTestCase.compare_eventsc                   C   r_  )NT)r   r   r   r   r   r   r     r`  z TraceOpcodesTestCase.make_tracerra  r   r   r]  r   rc    s
    rc  c                   @   sd   e 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S )RaisingTraceFuncTestCasec                 C   s   |  tjt  d S r	   r   r_   r`   r   r   r   r   r   r        zRaisingTraceFuncTestCase.setUpc                 C   s   || j krt| jS )zXA trace function that raises an exception in response to a
        specific trace event.)raiseOnEventr7  r   r   r   r   r   r     s   
zRaisingTraceFuncTestCase.tracec                 C   s   | j dkrd}d| }dS dS )zThe function to trace; raises an exception if that's the case
        we're testing, so that the 'exception' trace event fires.r]   r   r'   N)ri  )r   r$   rM   r   r   r   r     s   
zRaisingTraceFuncTestCase.fc              	   C   sx   || _ z)tt d D ]}t| j z|   W n	 ty#   Y qw | d qW dS  t	y;   | d Y dS w )zTTests that an exception raised in response to the given event is
        handled OK.r'   zexception not raised!zrecursion counter not resetN)
ri  rG   r_   getrecursionlimitr`   r   r   r7  r   rg   )r   r   rp   r   r   r   run_test_for_event  s   z+RaisingTraceFuncTestCase.run_test_for_eventc                 C      |  d d S )Nr+   rk  r   r   r   r   	test_call  r   z"RaisingTraceFuncTestCase.test_callc                 C   rl  Nr-   rm  r   r   r   r   	test_line  r   z"RaisingTraceFuncTestCase.test_linec                 C   rl  )Nr/   rm  r   r   r   r   test_return  r   z$RaisingTraceFuncTestCase.test_returnc                 C   rl  Nr]   rm  r   r   r   r   test_exception  r   z'RaisingTraceFuncTestCase.test_exceptionc                    s^   dd   fddt  z   W n ty'   dd l}|  Y d S w | d d S )Nc                  S   s   t dD ]} t|  qd S )Nr9   )rG   printr  r   r   r   r     rP  z4RaisingTraceFuncTestCase.test_trash_stack.<locals>.fc                    s&   |dkr| j  jjd krtdS )Nr-   r5   zi am crashing)r   r   r   rg   )r   whyextrar   gr   r   rx    s   z4RaisingTraceFuncTestCase.test_trash_stack.<locals>.gr   zexception not propagated)r_   r`   rg   r   Zcollectr   )r   r   r   rw  r   test_trash_stack  s   

z)RaisingTraceFuncTestCase.test_trash_stackc              	      st   dd } fdd t  }z$t   z|  W n	 ty#   Y n	w W t | d S W t | d S t | w )Nc                  S   s   d} | j  d S )Nr   )Zno_such_attrr1   r   r   r   r     s   
z<RaisingTraceFuncTestCase.test_exception_arguments.<locals>.fc                    s"   |dkr|\}}} |t  S rr  )assertIsInstancerY   )r   r   r   typer]   r   rx  r   r   r   rx    s   
z<RaisingTraceFuncTestCase.test_exception_arguments.<locals>.g)r_   r   r`   AttributeError)r   r   existingr   r|  r   test_exception_arguments  s   

z1RaisingTraceFuncTestCase.test_exception_argumentsc                    sj   t d  fdddd }| t }t |  W d    n1 s'w   Y  | |j  d S )NzBOOM!c                    s   |dkr d| _ S )Nr-   T)r   )r   r   r   r]   r   r   r   r   #  s   zRRaisingTraceFuncTestCase.test_line_event_raises_before_opcode_event.<locals>.tracec                   S   r   r	   r   r   r   r   r   r   (  r)   zNRaisingTraceFuncTestCase.test_line_event_raises_before_opcode_event.<locals>.f)r7  ZassertRaisesr_   r`   ZassertIsr]   )r   r   Zcaughtr   r  r   *test_line_event_raises_before_opcode_event!  s   
zCRaisingTraceFuncTestCase.test_line_event_raises_before_opcode_eventN)r   r   r   r   r   r   rk  rn  rp  rq  rs  ry  r  r  r   r   r   r   rf    s    	rf  c                   @   s&   e Zd ZdZ		d	ddZdd ZdS )

JumpTracerz>Defines a trace function that jumps from one place to another.r-   Fc                 C   s6   |j | _|| _|| _|| _|rd n| jj| _d| _d S )NF)r   codejumpFromjumpTor   r   	firstLinerT  )r   functionr  r  r   	decoratedr   r   r   r   7  s   
zJumpTracer.__init__c                 C   s   | j rd S | jd u r|j| jkr|dkr|jd | _|| jkrd| jd urd|j| j| j krd|}|d urF|j| jkrF|j}|d urF|j| jks9|d urdz	| j| j |_W n t	y`   | j|_Y nw d| _ | j
S )Nr-   r'   T)rT  r  f_coder  r   r   r  rb   r  	TypeErrorr   )r   r   r   r   r   r   r   r   r   @  s&   zJumpTracer.traceN)r-   F)r   r   r   r   r   r   r   r   r   r   r  4  s    
	r  c              
   C   sJ   z|  d W d S  ty$ } z|  dt|v  W Y d }~d S d }~ww )Nr5   Zinteger)r   r7  r   r   r   r   r   r   no_jump_to_non_integersZ  s    r  c               
   C   sR   zt  j} | j| _W td ty( } zdt|vr W Y d }~d S d }~ww )Nr   z'Trace-function-less jump failed to fail)r_   ra   rb   r   r7  r   AssertionError)Zprevious_framer   r   r   r   no_jump_without_trace_functionb  s   

r  c                   @   s  e Zd Zdd Zdd Z		ddd	Z		dd
dZdddZdddZedddgdd Z	eddg ddd Z
eddddgdd Zeddg ddd Zeddddgd d! Zeddg d"d#d$ Zedddgd%d& Zeddg d'd(d) Zedd*dd*gd+d, Zedd-dd-ged.fd/d0 Zedd1dd1ged.fd2d3 Zedd1ddged4fd5d6 Zedddged4fd7d8 Zeddddgd9d: Zeddg d;d<d= Zeddddgd>d? Zeddddgd@dA Zeddg dBdCdD Zeddg dBdEdF ZedddgdGdH Zeddg dIdJdK ZedddgdLdM Z eddg dNdOdP Z!edd-g dQdRdS Z"eddg dTdUdV Z#eddg dWdXdY Z$eddg dWdZd[ Z%eddg d\d]d^ Z&eddg d\d_d` Z'eddg dadbdc Z(eddg daddde Z)eddg dfdgdh Z*eddg dfdidj Z+edkd1g dldmdn Z,eddg dodpdq Z-eddg dodrds Z.eddg dtdudv Z/eddkg dwdxdy Z0edd-d-dkgdzd{ Z1edd-d-dkgd|d} Z2edddged4fd~d Z3edddgedfdd Z4edddgedfdd Z5edddgedfdd Z6edddgedfdd Z7edddgedfdd Z8eddg edfdd Z9eddg edfdd Z:eddddgedfdd Z;eddddgedfdd Z<eddg edfdd Z=eddg edfdd Z>eddg dedfdd Z?eddg dedfdd Z@eddg edfdd ZAeddddgedfdd ZBeddg edfdd ZCedddgedfdd ZDedd-dgedfdd ZEedddgdd ZFeddg ddd ZGeddg ddd ZHeddg edfdd ZIeddg edfdd ZJeddg dedfdd ZKedddgedfdd ZLed-dg dedfdd ZMed-dg dedfdd ZNeddg dddÄ ZOeddg dddń ZPedd-ddged4fddǄ ZQed-dddgedfddɄ ZRed-dddgedfdd˄ ZSdd̈́ ZTddτ ZUddф ZVddӄ ZWedddgdedfd֍dd؄ ZXedddgdedfd֍dd܄ ZYedddgdedfd֍dd߄ ZZeddddgdddd Z[eddddgdd Z\eddg dd Z]edkdg ddd Z^eddddgdd Z_eddg dd Z`edkdg ddd ZadS )JumpTestCasec                 C       |  tjt  td  d S r	   rg  r   r   r   r   r   q     zJumpTestCase.setUpc                 C   s2   ||kr|  dt| d d t|  d S d S )NzOutputs don't match:
Expected: r   z
Received: )r   repr)r   expectedZreceivedr   r   r   compare_jump_outputu  s   
z JumpTestCase.compare_jump_outputNr-   Fc           
      C   s~   t |||||}t|j g }	|d u r||	 n| j|  ||	 W d    n1 s-w   Y  td  | ||	 d S r	   )r  r_   r`   r   assertRaisesRegexr  
r   r   r  r  r  rU  r   r  r   r   r   r   r   r   {  s   


zJumpTestCase.run_testc           
      C   s   t |||||}t|j g }	|d u rt||	 n| j|  t||	 W d    n1 s3w   Y  td  td  | ||	 d S r	   )	r  r_   r`   r   asyncior   r  Zset_event_loop_policyr  r  r   r   r   run_async_test  s   

zJumpTestCase.run_async_testc                        fdd}|S )zqDecorator that creates a test that makes a jump
        from one place to another in the following code.
        c                    "   t   fdd}|S )Nc              	         | j  dd d S NT)rU  r   r  )r   r   rU  r   r  r   r  r  r   r   test     
z7JumpTestCase.jump_test.<locals>.decorator.<locals>.testr   r   r  rU  r   r  r  r  r   r   	decorator     z)JumpTestCase.jump_test.<locals>.decoratorr   r  r  r  rU  r   r  r   r  r   	jump_test     zJumpTestCase.jump_testc                    r  )z~Decorator that creates a test that makes a jump
        from one place to another in the following asynchronous code.
        c                    r  )Nc              	      r  r  )r  r   r  r   r   r    r  z=JumpTestCase.async_jump_test.<locals>.decorator.<locals>.testr   r  r  r  r   r    r  z/JumpTestCase.async_jump_test.<locals>.decoratorr   r  r   r  r   async_jump_test  r  zJumpTestCase.async_jump_testr'   r7   c                 C   "   |  d |  d |  d d S Nr'   r5   r7   r   r   r   r   r   test_jump_simple_forwards     

z&JumpTestCase.test_jump_simple_forwardsr5   )r'   r'   r5   c                 C      |  d |  d d S rE   r  r  r   r   r   test_jump_simple_backwards     
z'JumpTestCase.test_jump_simple_backwardsr9   c                 C   s6   dD ]}|  d dD ]}|  d qq|  d d S )NrH  r5   r7   r>   r9   r  r   rp   r  r   r   r   test_jump_out_of_block_forwards  s   
z,JumpTestCase.test_jump_out_of_block_forwardsrK   )r'   r7   r9   r'   r7   r9   rK   r@   c                 C   sJ   |  d dD ]}|  d dD ]}|  d q|  d q|  d d S )Nr'   r   r7   )r5   r9   rK   r@   r  r  r   r   r    test_jump_out_of_block_backwards  s   

z-JumpTestCase.test_jump_out_of_block_backwardsr>   c                    sJ   dD ]}t ddg2 z3 d H W }| d | d q6 | d qd S )Nr   r'   r5   r7   r>   r9   r%   r   r   rp   r   r   r   )test_jump_out_of_async_for_block_forwards  s   
z6JumpTestCase.test_jump_out_of_async_for_block_forwards)r5   r>   r5   r>   r9   rK   c                    sR   dD ]#}|  d tdg2 z3 d H W }|  d |  d q6 |  d qd S )Nr   r5   r'   r>   r9   rK   r   r%   r  r   r   r   *test_jump_out_of_async_for_block_backwards  s   

z7JumpTestCase.test_jump_out_of_async_for_block_backwardsc                 C   r  )Nr'   r7   r  r  r   r   r   test_jump_to_codeless_line  s   
z'JumpTestCase.test_jump_to_codeless_line)r'   r5   r7   c                 C   r  r  r  r  r   r   r   test_jump_to_same_line  r  z#JumpTestCase.test_jump_to_same_liner   c                 C   s   z%|  d W |  d z|  d W |  d n|  d w |  d d S |  d z|  d W |  d n|  d w |  d w )Nr5   r>   rK   r   r   r  r  r   r   r   test_jump_in_nested_finally  s   

z(JumpTestCase.test_jump_in_nested_finallyr@    c              	   C   sD   z|  d dd  W |  d |  d d S |  d |  d w )Nr5   r'   r   rK   r@   r  r  r   r   r   test_jump_in_nested_finally_2  s   


z*JumpTestCase.test_jump_in_nested_finally_2   c                 C   s   z)|  d dd  W |  d z|  d W |  d n|  d w |  d d S |  d z|  d W |  d n|  d w |  d w )Nr5   r'   r   rK   r   ru   r  r  r  r   r   r   test_jump_in_nested_finally_3  s   


z*JumpTestCase.test_jump_in_nested_finally_3Zafterc              	   C   F   z|  d W |  d |  d d S |  d |  d    Y d S Nr5   r>   r9   r  r  r   r   r   5test_no_jump_over_return_try_finally_in_finally_block     



zBJumpTestCase.test_no_jump_over_return_try_finally_in_finally_blockc                 C   s   |  d 	 |  d q)Nr'   Tr7   r  r  r   r   r    test_no_jump_infinite_while_loop   s   

z-JumpTestCase.test_no_jump_infinite_while_loopc                 C   s8   d}|  d |dkr|  d |d7 }|dksd S d S )Nr'   r5   r>   r  r  r   r   r   #test_jump_forwards_into_while_block'  s   

z0JumpTestCase.test_jump_forwards_into_while_block)r7   r7   r7   r9   c                 C   s4   d}|dkr|  d |d7 }|dks|  d d S )Nr'   r5   r7   r9   r  r  r   r   r   $test_jump_backwards_into_while_block/  s   
z1JumpTestCase.test_jump_backwards_into_while_blockc                 C   B   t | d | d W d    n1 sw   Y  | d d S r  r   r   r  r   r   r   $test_jump_forwards_out_of_with_block7     z1JumpTestCase.test_jump_forwards_out_of_with_blockc              	      X   t | d4 I d H  | d W d   I d H  n1 I d H s w   Y  | d d S r  r    r   r  r   r   r   *test_jump_forwards_out_of_async_with_block=  
   (z7JumpTestCase.test_jump_forwards_out_of_async_with_block)r'   r5   r'   r5   r7   rV   c                 C   D   |  d t| d |  d W d    d S 1 sw   Y  d S r  r   r   r  r   r   r   %test_jump_backwards_out_of_with_blockC     
"z2JumpTestCase.test_jump_backwards_out_of_with_blockc              	      Z   |  d t| d4 I d H  |  d W d   I d H  d S 1 I d H s&w   Y  d S r  r   r    r  r   r   r   +test_jump_backwards_out_of_async_with_blockI  
   
.z8JumpTestCase.test_jump_backwards_out_of_async_with_blockc              	   C   4   z|  d W |  d n|  d w |  d d S r  r  r  r   r   r   +test_jump_forwards_out_of_try_finally_blockO     z8JumpTestCase.test_jump_forwards_out_of_try_finally_block)r'   r'   r7   r9   c              	   C   2   |  d z|  d W |  d d S |  d w Nr'   r7   r9   r  r  r   r   r   ,test_jump_backwards_out_of_try_finally_blockW     
z9JumpTestCase.test_jump_backwards_out_of_try_finally_blockc                 C   0   z|  d W n	   |  d  |  d d S Nr5   r>   rK   r  r  r   r   r   *test_jump_forwards_out_of_try_except_block_     
z7JumpTestCase.test_jump_forwards_out_of_try_except_block)r'   r'   r7   c                 C   .   |  d z|  d W d S    |  d  r  r  r  r   r   r   +test_jump_backwards_out_of_try_except_blockh     

z8JumpTestCase.test_jump_backwards_out_of_try_except_block)r>   r@   r   c                 C   s\   zdd  W n  t y   | d | d Y n ty&   | d Y nw | d d S Nr'   r   r>   r9   r@   r   r6  r   FloatingPointErrorr  r   r   r   test_jump_between_except_blocksq  s   
z,JumpTestCase.test_jump_between_except_blocks)r>   rK   r@   c                 C   sB   zdd  W n   |  d |  d |  d Y |  d d S )Nr'   r   r>   r9   rK   r@   r  r  r   r   r   test_jump_within_except_block|  s   

z*JumpTestCase.test_jump_within_except_block)r'   r>   r9   r}   c                 C   x   |  d t| d |  d W d    n1 sw   Y  t| d |  d W d    d S 1 s5w   Y  d S Nr'   r5   r7   r>   r9   r  r  r   r   r   test_jump_across_with     
"z"JumpTestCase.test_jump_across_withc              	         |  d t| d4 I d H  |  d W d   I d H  n1 I d H s%w   Y  t| d4 I d H  |  d W d   I d H  d S 1 I d H sJw   Y  d S r  r  r  r   r   r   test_jump_across_async_with     
(.z(JumpTestCase.test_jump_across_async_with)r'   r7   r9   rK   c              	   C   s`   |  d dD ]!}t| d |  d W d    n1 sw   Y  |  d q|  d d S Nr'   r   r7   r>   r9   rK   r  r  r   r   r   ,test_jump_out_of_with_block_within_for_block  s   
z9JumpTestCase.test_jump_out_of_with_block_within_for_blockc              
      sv   |  d dD ]+}t| d4 I d H  |  d W d   I d H  n1 I d H s)w   Y  |  d q|  d d S r  r  r  r   r   r   2test_jump_out_of_async_with_block_within_for_block  s   
(z?JumpTestCase.test_jump_out_of_async_with_block_within_for_block)r'   r5   r7   r9   rV   rK   c              	   C   s   |  d t| d' t| d |  d W d    n1 s w   Y  |  d W d    n1 s4w   Y  |  d d S Nr'   r5   r7   r>   r9   rK   r  r  r   r   r   -test_jump_out_of_with_block_within_with_block  s   
z:JumpTestCase.test_jump_out_of_with_block_within_with_blockc              
      s   |  d t| d1 t| d4 I d H  |  d W d   I d H  n1 I d H s+w   Y  |  d W d    n1 s?w   Y  |  d d S r  )r   r   r    r  r   r   r   3test_jump_out_of_async_with_block_within_with_block  s   
(z@JumpTestCase.test_jump_out_of_async_with_block_within_with_block)r5   r>   rK   r@   c                 C   s   z&|  d W t| d |  d W d    n1 sw   Y  |  d n t| d |  d W d    n1 s<w   Y  |  d w |  d d S Nr5   r>   r9   rK   r@   r  r  r   r   r   0test_jump_out_of_with_block_within_finally_block  s   z=JumpTestCase.test_jump_out_of_with_block_within_finally_blockc                    s   z0|  d W t| d4 I d H  |  d W d   I d H  n1 I d H s'w   Y  |  d n*t| d4 I d H  |  d W d   I d H  n1 I d H sQw   Y  |  d w |  d d S r  r  r  r   r   r   6test_jump_out_of_async_with_block_within_finally_block  s   ((zCJumpTestCase.test_jump_out_of_async_with_block_within_finally_blockr   )r'   r7   r9   r  r  c                 C   sx   |  d dD ]-}|  d dD ]}|  d zdD ]}|  d qW |  d q|  d w |  d q|  d	 d S )
Nr'   r   r7   rH  r9   r   ru   r  r  r  )r   rp   r  kr   r   r   &test_jump_out_of_complex_nested_blocks  s   


z3JumpTestCase.test_jump_out_of_complex_nested_blocks)r'   r5   r9   c                 C   sL   |  d t| d}|  d W d    n1 sw   Y  |  d d S Nr'   r5   r>   r9   r  r   r$   r   r   r    test_jump_out_of_with_assignment  s   

z-JumpTestCase.test_jump_out_of_with_assignmentc              	      sb   |  d t| d4 I d H }|  d W d   I d H  n1 I d H s%w   Y  |  d d S r  r  r  r   r   r   &test_jump_out_of_async_with_assignment  s   
(z3JumpTestCase.test_jump_out_of_async_with_assignment)r'   rK   r   r   c              	   C   s\   |  d z|  d | sW |  d d S |  d W |  d n|  d w |  d d S )Nr'   r7   r   rK   r   r  r  r   r   r   *test_jump_over_return_in_try_finally_block  s   

z7JumpTestCase.test_jump_over_return_in_try_finally_block)r'   r7   r   ru   r  r  c              	   C   sr   |  d 	 |  d z|  d | sW |  d n|  d W |  d n|  d w |  d 	 |  d d S )	Nr'   Tr7   r9   ru   r   r  r  r  r  r   r   r   )test_jump_over_break_in_try_finally_block  s   



z6JumpTestCase.test_jump_over_break_in_try_finally_blockc                 C   F   |  d | sdD ]}|  d q	n
|  d |  d |  d d S Nr'   r  r>   rK   r@   r   r  r  r   r   r   $test_jump_over_for_block_before_else	     


z1JumpTestCase.test_jump_over_for_block_before_elsec                    X   |  d | stdg2 z3 d H W }|  d q6 n
|  d |  d |  d d S Nr'   r7   r>   rK   r@   r   r  r  r   r   r   *test_jump_over_async_for_block_before_else     


z7JumpTestCase.test_jump_over_async_for_block_before_elsec                 C   r  rE   r  r  r   r   r   test_no_jump_too_far_forwards!  r  z*JumpTestCase.test_no_jump_too_far_forwardsrV   beforec                 C   r  rE   r  r  r   r   r   test_no_jump_too_far_backwards&  r  z+JumpTestCase.test_no_jump_too_far_backwardsexceptc                 C   s$   z|  d W d S    |  d  Nr5   r>   r  r  r   r   r   test_no_jump_to_except_1,  s
   
z%JumpTestCase.test_no_jump_to_except_1c                 C   s,   z|  d W d S  ty   |  d  w r  r   r7  r  r   r   r   test_no_jump_to_except_24  s   
z%JumpTestCase.test_no_jump_to_except_2c              
   C   s8   z|  d W d S  ty } z|  d |d }~ww r  r  r  r   r   r   test_no_jump_to_except_3<  s   
z%JumpTestCase.test_no_jump_to_except_3c              
   C   s<   z|  d W d S  ttfy } z|  d |d }~ww r  )r   r7  rg   r  r   r   r   test_no_jump_to_except_4D  s   
z%JumpTestCase.test_no_jump_to_except_4Zintoc                 C   s"   |  d dD ]}|  d qd S )Nr'   rH  r7   r  r  r   r   r   $test_no_jump_forwards_into_for_blockL  s   
z1JumpTestCase.test_no_jump_forwards_into_for_blockc                    s6   |  d tddg2 z3 d H W }|  d q6 d S r  r  r  r   r   r   *test_no_jump_forwards_into_async_for_blockR  s   
z7JumpTestCase.test_no_jump_forwards_into_async_for_blockc                 C   s"   dD ]}|  d q|  d d S )NrH  r5   r7   r  r  r   r   r   %test_no_jump_backwards_into_for_blockY  s   z2JumpTestCase.test_no_jump_backwards_into_for_blockc                    s6   t ddg2 z3 d H W }| d q6 | d d S r  r  r  r   r   r   +test_no_jump_backwards_into_async_for_block_  s
   z8JumpTestCase.test_no_jump_backwards_into_async_for_blockc                 C   r  r  r  r  r   r   r   %test_no_jump_forwards_into_with_blocke  r  z2JumpTestCase.test_no_jump_forwards_into_with_blockc              	      r  r  r  r  r   r   r   +test_no_jump_forwards_into_async_with_blockk  r  z8JumpTestCase.test_no_jump_forwards_into_async_with_block)r'   r5   r   c                 C   r  r  r  r  r   r   r   &test_no_jump_backwards_into_with_blockq  r  z3JumpTestCase.test_no_jump_backwards_into_with_blockc              	      r  r  r  r  r   r   r   ,test_no_jump_backwards_into_async_with_blockw  r  z9JumpTestCase.test_no_jump_backwards_into_async_with_blockc              	   C   r  r  r  r  r   r   r   ,test_no_jump_forwards_into_try_finally_block}  r  z9JumpTestCase.test_no_jump_forwards_into_try_finally_blockc              	   C   r  r  r  r  r   r   r   -test_no_jump_backwards_into_try_finally_block  r  z:JumpTestCase.test_no_jump_backwards_into_try_finally_blockc                 C   r  r  r  r  r   r   r   +test_no_jump_forwards_into_try_except_block  r  z8JumpTestCase.test_no_jump_forwards_into_try_except_blockc                 C   r  r  r  r  r   r   r   ,test_no_jump_backwards_into_try_except_block  r  z9JumpTestCase.test_no_jump_backwards_into_try_except_blockc              
   C   sn   zdd  W n) t y   | d | d Y n ty/ } z| d W Y d }~nd }~ww | d d S r  r  r  r   r   r   $test_no_jump_between_except_blocks_2  s   
z1JumpTestCase.test_no_jump_between_except_blocks_2c              	   C   r  r  r  r  r   r   r   test_jump_into_finally_block  r  z)JumpTestCase.test_jump_into_finally_block)r5   rK   r@   c              	   C   sR   z|  d |  d W |  d |  d n|  d |  d w |  d d S )Nr5   r7   r9   rK   r@   r  r  r   r   r   +test_jump_into_finally_block_from_try_block  s   


z8JumpTestCase.test_jump_into_finally_block_from_try_block)r'   r7   r'   r7   r9   c              	   C   r  r  r  r  r   r   r   test_jump_out_of_finally_block  r  z+JumpTestCase.test_jump_out_of_finally_blockzinto an 'except'c                 C   s2   |  d z|  d W d S    |  d Y d S r  r  r  r   r   r   #test_no_jump_into_bare_except_block  s
   
z0JumpTestCase.test_no_jump_into_bare_except_blockc                 C   s:   |  d z|  d W d S  ty   |  d Y d S w r  r   rY   r  r   r   r   (test_no_jump_into_qualified_except_block  s   
z5JumpTestCase.test_no_jump_into_qualified_except_block)r5   r9   rK   c                 C   sD   z|  d |  d W n   |  d |  d  |  d d S Nr5   r7   r9   rK   r   r  r  r   r   r   2test_no_jump_into_bare_except_block_from_try_block  s   


z?JumpTestCase.test_no_jump_into_bare_except_block_from_try_blockc                 C   sL   z|  d |  d W n ty   |  d |  d  w |  d d S r)  )r   r6  r  r   r   r   7test_no_jump_into_qualified_except_block_from_try_block  s   


zDJumpTestCase.test_no_jump_into_qualified_except_block_from_try_block)r'   r7   rK   zout of an 'except'c                 C   sD   |  d z|  d dd  W d S    |  d |  d Y d S Nr'   r7   r   rK   r@   r  r  r   r   r   %test_no_jump_out_of_bare_except_block  s   


z2JumpTestCase.test_no_jump_out_of_bare_except_blockc                 C   sL   |  d z|  d dd  W d S  ty%   |  d |  d Y d S w r,  r'  r  r   r   r   *test_no_jump_out_of_qualified_except_block  s   


z7JumpTestCase.test_no_jump_out_of_qualified_except_block)r'   r5   r9   rV   c                 C   r  r  r  r  r   r   r   test_jump_between_with_blocks  r  z*JumpTestCase.test_jump_between_with_blocksc              	      r  r  r  r  r   r   r   #test_jump_between_async_with_blocks  r  z0JumpTestCase.test_jump_between_async_with_blocksc              	   C   r  r  r  r  r   r   r   -test_no_jump_over_return_out_of_finally_block  r  z:JumpTestCase.test_no_jump_over_return_out_of_finally_blockc                 C   r  r  r  r  r   r   r   'test_no_jump_into_for_block_before_else  r  z4JumpTestCase.test_no_jump_into_for_block_before_elsec                    r  r	  r  r  r   r   r   -test_no_jump_into_async_for_block_before_else$  r  z:JumpTestCase.test_no_jump_into_async_for_block_before_elsec                 C   s   |  tdddg d S )Nr5   ZSpamT)r   r  r   r   r   r   test_no_jump_to_non_integers/  rh  z)JumpTestCase.test_no_jump_to_non_integersc                 C   rR   r	   )r  r   r   r   r   #test_no_jump_without_trace_function2  s   
z0JumpTestCase.test_no_jump_without_trace_functionc                 C   s0   i }t dd | |d }| |dddg d S )Na  def f(output):        # line 0
            x = 0                     # line 1
            y = 1                     # line 2
            '''                       # line 3
            %s                        # lines 4-1004
            '''                       # line 1005
            x += 1                    # line 1006
            output.append(x)          # line 1007
            return)a  







































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































r   r5   i  r   )r   r   )r   r  r   r   r   r   test_large_function7  s   	z JumpTestCase.test_large_functionc                    sj   t ddd G  fddd}t|dd}t|j dg i}t | td  | g d	|d  d S )
Nzd
# Comments don't count.
output.append(2)  # firstlineno is here.
output.append(3)
output.append(4)
z<fake module>r   c                       s   e Zd Z ZdS )z<JumpTestCase.test_jump_to_firstlineno.<locals>.fake_functionN)r   r   r   r   r   r  r   r   fake_functionO  s    r8  r>   r'   r   )r5   r7   r5   r7   r>   )compiler  r_   r`   r   r   r  )r   r8  r   r   r   r7  r   test_jump_to_firstlinenoE  s   

z%JumpTestCase.test_jump_to_firstlinenor+   z5can't jump from the 'call' trace event of a new frame)r   rU  c                    s*     d  fdd}|    d d S )Nr'   c                      s     d d S )Nr7   r  r   r  r   r   nested\  r   z3JumpTestCase.test_no_jump_from_call.<locals>.nestedr9   r  )r   r;  r   r  r   test_no_jump_from_callX  s   
z#JumpTestCase.test_no_jump_from_callr/   z'can only jump from a 'line' trace eventc                 C   rl  r&   r  r  r   r   r   test_no_jump_from_return_eventa  s   
z+JumpTestCase.test_no_jump_from_return_eventr]   c                 C   s   |  d dd  d S )Nr'   r   r  r  r   r   r   !test_no_jump_from_exception_eventg  s   
z.JumpTestCase.test_no_jump_from_exception_event)r   c                    s$    fdd}t |   d d S )Nc                   3   s      d dV  d S )Nr5   r7   r  r   r  r   r   geno  s   

z.JumpTestCase.test_jump_from_yield.<locals>.genr9   )r   r   )r   r?  r   r  r   test_jump_from_yieldm  s   
z!JumpTestCase.test_jump_from_yieldc                 C   s*   |  d dd tdD }|  d d S )Nr'   c                 S      g | ]}|qS r   r   r   rp   r   r   r   r   x      z@JumpTestCase.test_jump_forward_over_listcomp.<locals>.<listcomp>ru   r7   r   rG   r  r   r   r   test_jump_forward_over_listcompu  s   
z,JumpTestCase.test_jump_forward_over_listcompc                 C   s   d}dd t dD }d}d S )Nr'   c                 S   rA  r   r   rB  r   r   r   r     rC  zAJumpTestCase.test_jump_backward_over_listcomp.<locals>.<listcomp>ru   r7   rF   r   rH   r$   r  r   r   r    test_jump_backward_over_listcomp}  s   z-JumpTestCase.test_jump_backward_over_listcomp)r5   r@   r5   c                 C   sD   d}|  d |rd S dd tdD }d}|  d |  d d S )	NFr5   c                 S   rA  r   r   rB  r   r   r   r     rC  zDJumpTestCase.test_jump_backward_over_listcomp_v2.<locals>.<listcomp>r9   rK   r@   r   rD  r   flagr$   r   r   r   #test_jump_backward_over_listcomp_v2  s   

z0JumpTestCase.test_jump_backward_over_listcomp_v2c                    s6   |  d dd ttd2 I d H }|  d d S )Nr'   c                       g | z3 d H W }|q6 S r	   r   rB  r   r   r   r         zFJumpTestCase.test_jump_forward_over_async_listcomp.<locals>.<listcomp>ru   r7   r   r%   rG   r  r   r   r   %test_jump_forward_over_async_listcomp  s   
z2JumpTestCase.test_jump_forward_over_async_listcompc                    s*   d}dd t td2 I d H }d}d S )Nr'   c                    rK  r	   r   rB  r   r   r   r     rL  zGJumpTestCase.test_jump_backward_over_async_listcomp.<locals>.<listcomp>ru   r7   )r%   rG   rF  r   r   r   &test_jump_backward_over_async_listcomp  s   z3JumpTestCase.test_jump_backward_over_async_listcompc                    sP   d}|  d |rd S dd ttd2 I d H }d}|  d |  d d S )	NFr5   c                    rK  r	   r   rB  r   r   r   r     rL  zJJumpTestCase.test_jump_backward_over_async_listcomp_v2.<locals>.<listcomp>r9   rK   r@   r   rM  rH  r   r   r   )test_jump_backward_over_async_listcomp_v2  s   

z6JumpTestCase.test_jump_backward_over_async_listcomp_v2)Nr-   Fro  )br   r   r   r   r  r   r  r  r  r  r  r  r  r  r  r  r  r  r6  r  r  r7  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  r
  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r!  r"  r#  r$  r%  r&  r(  r*  r+  r-  r.  r/  r0  r1  r2  r3  r4  r5  r6  r:  r<  r=  r>  r@  rE  rG  rJ  rN  rO  rP  r   r   r   r   r  p  sr   







	






















	






	
	

























	



	







	
	


	















r  c                   @   r   )TestEdgeCasesc                 C   r  r	   rg  r   r   r   r   r     r  zTestEdgeCases.setUpc                    s   dd }dd  G  fddd}t |  t }t | | |jj|j | |jj	t
 W d    n1 s<w   Y  | t  | d S )Nc                  W   r   r	   r   r   r   r   r   rN    r)   z*TestEdgeCases.test_reentrancy.<locals>.fooc                  W   r   r	   r   r   r   r   r   bar  r)   z*TestEdgeCases.test_reentrancy.<locals>.barc                       s    e Zd Zdd Z fddZdS )z(TestEdgeCases.test_reentrancy.<locals>.Ac                 W   r   r	   r   )r   r   r   r   r   __call__  r)   z1TestEdgeCases.test_reentrancy.<locals>.A.__call__c                    s   t   d S r	   )r_   r`   r   rR  r   r   __del__  r   z0TestEdgeCases.test_reentrancy.<locals>.A.__del__N)r   r   r   rS  rU  r   rT  r   r   r1    s    r1  )r_   r`   r   Zcatch_unraisable_exceptionZassertEqualZ
unraisableobjectrU  rz  	exc_valuerg   r   )r   rN  r1  cmr   rT  r   test_reentrancy  s   

zTestEdgeCases.test_reentrancyc                 C   s&   dd }t | ~t t   d S )Nc                  W   r   r	   r   r   r   r   r   rN    r)   z+TestEdgeCases.test_same_object.<locals>.foor   )r   rN  r   r   r   test_same_object  s   
zTestEdgeCases.test_same_objectN)r   r   r   r   rY  rZ  r   r   r   r   rQ    s    rQ  __main__)1r  r   ZunittestZunittest.mockr   r_   r   r   	functoolsr   r  r   r    r%   r(   r   r2   r;   r<   rB   rI   rN   rQ   r+   rZ   r[   re   rf   rh   ri   rk   rq   rr   rt   rw   r   ZTestCaser   rY  Zcpython_onlyrc  rf  r  r  r  r  rQ  r   mainr   r   r   r   <module>   s   



	








	


      hn&      <
&