B
    u9a                 @   s  d dl m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G dd dZ	G dd dZ
dd	 Zd
d Zdddge_dd Zdddddge_dd Zdddddge_dd Zddddddddddg
e_dd Zdddddge_d d! Zd"d# Zddd$d%d&dge_d'd( Zd)d* Zdddd$d%d+d&d,dddge_d-d. Zd/d0 Zdge_d1d2 Zd3d4 Zd,dddge_d5d6 Zddddddd7ge_d8d9 Zdddddddddd:dd;d<ge_d=d> Zddddddddd?ddd7ge_d@dA ZdBdC ZdddDdEdFdGdHdIdJdKg
ddgdL  ddg e_G dMdN dNZG dOdP dPej Z!G dQdR dRe!Z"ej#G dSdT dTe!Z$G dUdV dVej Z%G dWdX dXZ&dYdZ Z'd[d\ Z(G d]d^ d^ej Z)e*d_kre+  dS )`    )supportN)wrapsc               @   s(   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   s   || _ || _d S )N)outputvalue)selfr   r    r   '/usr/lib/python3.7/test_sys_settrace.py__init__   s    ztracecontext.__init__c             C   s   | j | j d S )N)r   appendr   )r   r   r   r	   	__enter__   s    ztracecontext.__enter__c             G   s   | j | j  d S )N)r   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      s   r   c               @   s(   e Zd ZdZdd Zdd Zdd ZdS )	asynctracecontextz>Asynchronous context manager that traces its aenter and aexit.c             C   s   || _ || _d S )N)r   r   )r   r   r   r   r   r	   r
      s    zasynctracecontext.__init__c                s   | j | j d S )N)r   r   r   )r   r   r   r	   
__aenter__   s    zasynctracecontext.__aenter__c                s   | j | j  d S )N)r   r   r   )r   r   r   r   r	   	__aexit__!   s    zasynctracecontext.__aexit__N)r   r   r   r   r
   r   r   r   r   r   r	   r      s   r   c             C  s   x| D ]
}|V  qW 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+   s    r   )r   call)r   line)r   returnc              C   s   d} ~ d} d S )Nr   r   )r   r   r   r	   arigo_example;   s    r   )   r   )   r   )r    r   c              C   s   d} ~ d} d S )Nr   r   )r   r   r   r	   one_instr_lineI   s    r!   )   r   )r"   r   c              C   s(   d} xt dD ]}|rd} qd} qW d S )Nr   r   )range)r   ar   r   r	   no_pop_topsT   s
    r%   )   r   )   r   )r   r   c              C   s   d} x| st  qW d}d S )Nr   )Zbla)yr   r   r   r	   no_pop_blocksg   s    r)   )r'   r   c              C   s   d} d S )Nr   r   )r   r   r   r	   calleds   s    r*   c               C   s
   t   d S )N)r*   r   r   r   r	   r   v   s    r   )r   )r   )r,   r   c               C   s   t d S )N)	Exceptionr   r   r   r	   raises   s    r.   c           
   C   s6   y
t   W n& tk
r0 }  zd}W d d } ~ X Y nX d S )Nr   )r.   r-   )excr   r   r   r	   
test_raise   s    
r0   )r,   	exception)r   r1   c             C   s   t |  | t  j_d S )N)syssettrace	_getframef_backf_trace)	tracefuncr   r   r	   _settrace_and_return   s    
r8   c             C   s   t |  d S )N)r8   )r7   r   r   r	   settrace_and_return   s    r9   c             C   s   t |  | t  j_td S )N)r2   r3   r4   r5   r6   RuntimeError)r7   r   r   r	   _settrace_and_raise   s    
r;   c          
   C   s4   yt |  W n" tk
r. } zW d d }~X Y nX d S )N)r;   r:   )r7   r/   r   r   r	   settrace_and_raise   s    r<   c              C   s   d} d}| |kr| d }n d S )Nr    r   r   )r$   br   r   r	   ireturn_example   s
    
r>   )r&   r   c              C   sB   t dd} yd}x| | }|d7 }qW W n tk
r<   Y nX d S )Nr   r"   r   )r#   
IndexError)itemsir=   r   r   r	   tightloop_example   s    
 rB   )r    r1   )   r   )rC   r   c              C   s:   t dd} yd}x| | }qW W n tk
r4   Y nX d S )Nr   r'   r   )r#   r?   )r@   rA   r   r   r	   tighterloop_example   s    
 rD   )r'   r1   c               c   s   z
dV  W d X d S )NTr   r   r   r   r	   generator_function   s    rE   c              C   s$   t t } xtdD ]} | }qW d S )N
   )anyrE   r#   )r   r(   r   r   r	   generator_example   s    
rH   )ir   )r   )r   )rJ   r   )rJ   r   )rJ   r1   )r   )rK   r   rF   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 )N)trace_line_eventstrace_opcode_eventsevents)r   rM   rN   r   r   r	   r
     s    zTracer.__init__c             C   s(   | j d k	r| j |_| jd k	r$| j|_d S )N)rM   f_trace_linesrN   f_trace_opcodes)r   framer   r   r	   _reconfigure_frame  s    

zTracer._reconfigure_framec             C   s"   |  | | j|j|f | jS )N)rS   rO   r   f_linenotrace)r   rR   eventargr   r   r	   rU     s    
zTracer.tracec             C   s0   |  | dd dD  | j|j|f | jS )Nc             s   s   | ]
}|V  qd S )Nr   ).0or   r   r	   	<genexpr>   s    z)Tracer.traceWithGenexp.<locals>.<genexpr>)r   )rS   rO   r   rT   rU   )r   rR   rV   rW   r   r   r	   traceWithGenexp  s    
zTracer.traceWithGenexp)NN)r   r   r   r
   rS   rU   r[   r   r   r   r	   rL     s   
rL   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d5S )6TraceTestCasec             C   s(   t  | _t   | tjt  d S )N)gcZ	isenabledusing_gcZdisable
addCleanupr2   r3   gettrace)r   r   r   r	   setUp)  s    
zTraceTestCase.setUpc             C   s   | j rt  d S )N)r^   r]   Zenable)r   r   r   r	   tearDown.  s    zTraceTestCase.tearDownc               C   s   t  S )z@Helper to allow test subclasses to configure tracers differently)rL   r   r   r   r	   make_tracer2  s    zTraceTestCase.make_tracerc          
      sN    fdd|D }||krJ|  ddtdd |D dd |D   d S )Nc                s   g | ]\}}|  |fqS r   r   )rX   le)line_offsetr   r	   
<listcomp>8  s    z0TraceTestCase.compare_events.<locals>.<listcomp>z"events did not match expectation:

c             S   s   g | ]}t |qS r   )str)rX   r   r   r   r	   rg   <  s    c             S   s   g | ]}t |qS r   )ri   )rX   r   r   r   r	   rg   =  s    )failjoindifflibZndiff)r   rf   rO   expected_eventsr   )rf   r	   compare_events7  s    zTraceTestCase.compare_eventsc             C   s<   |   }t|j |  td  | |jj|j| d S )N)rc   r2   r3   rU   rn   __code__co_firstlinenorO   )r   funcrO   tracerr   r   r	   run_and_compare?  s    

zTraceTestCase.run_and_comparec             C   s   |  ||j d S )N)rs   rO   )r   rq   r   r   r	   run_testG  s    zTraceTestCase.run_testc             C   s6   |   }||j td  | |jj|j|j d S )N)rc   rU   r2   r3   rn   ro   rp   rO   )r   rq   rr   r   r   r	   	run_test2J  s
    


zTraceTestCase.run_test2c             C   s   t d  t  d kstd S )N)r2   r3   r`   AssertionError)r   r   r   r	   test_set_and_retrieve_noneQ  s    
z(TraceTestCase.test_set_and_retrieve_nonec          	   C   s8   dd }t | zt  |ks$tW d t d  X d S )Nc              W   s   d S )Nr   )argsr   r   r	   fnV  s    z4TraceTestCase.test_set_and_retrieve_func.<locals>.fn)r2   r3   r`   rv   )r   ry   r   r   r	   test_set_and_retrieve_funcU  s
    
z(TraceTestCase.test_set_and_retrieve_funcc             C   s   |  t d S )N)rt   r   )r   r   r   r	   test_01_basic_  s    zTraceTestCase.test_01_basicc             C   s   |  t d S )N)rt   r   )r   r   r   r	   test_02_arigoa  s    zTraceTestCase.test_02_arigoc             C   s   |  t d S )N)rt   r!   )r   r   r   r	   test_03_one_instrc  s    zTraceTestCase.test_03_one_instrc             C   s   |  t d S )N)rt   r)   )r   r   r   r	   test_04_no_pop_blockse  s    z#TraceTestCase.test_04_no_pop_blocksc             C   s   |  t d S )N)rt   r%   )r   r   r   r	   test_05_no_pop_topsg  s    z!TraceTestCase.test_05_no_pop_topsc             C   s   |  t d S )N)rt   r   )r   r   r   r	   test_06_calli  s    zTraceTestCase.test_06_callc             C   s   |  t d S )N)rt   r0   )r   r   r   r	   test_07_raisek  s    zTraceTestCase.test_07_raisec             C   s   |  t d S )N)ru   r9   )r   r   r   r	   test_08_settrace_and_returnn  s    z)TraceTestCase.test_08_settrace_and_returnc             C   s   |  t d S )N)ru   r<   )r   r   r   r	   test_09_settrace_and_raisep  s    z(TraceTestCase.test_09_settrace_and_raisec             C   s   |  t d S )N)rt   r>   )r   r   r   r	   test_10_ireturnr  s    zTraceTestCase.test_10_ireturnc             C   s   |  t d S )N)rt   rB   )r   r   r   r	   test_11_tightloopt  s    zTraceTestCase.test_11_tightloopc             C   s   |  t d S )N)rt   rD   )r   r   r   r	   test_12_tighterloopv  s    z!TraceTestCase.test_12_tighterloopc             C   sH   |  t |  }t|j t  td  | tjj|j	tj	 d S )N)
rt   rH   rc   r2   r3   r[   rn   ro   rp   rO   )r   rr   r   r   r	   test_13_genexpy  s    


zTraceTestCase.test_13_genexpc             C   s    dd }|  |ddddg d S )Nc              S   s   d} dS )NFr   r   )r   r   r   r	   onliners  s    z2TraceTestCase.test_14_onliner_if.<locals>.onliners)r   r   )r   r   )r"   r   )r"   r   )rs   )r   r   r   r   r	   test_14_onliner_if  s    z TraceTestCase.test_14_onliner_ifc             C   sJ   dd }|  |dddddddg dd }|  |ddd	d
d	d
d	dg d S )Nc              S   s   xt dD ]} q
W d S )Nr   )r#   )r   r   r   r	   for_example  s    z0TraceTestCase.test_15_loops.<locals>.for_example)r   r   )r   r   )r   r   )r   r   c              S   s   d} x| dkr| d8 } qW d S )Nr   r   r   r   )r   r   r   r	   while_example  s    
z2TraceTestCase.test_15_loops.<locals>.while_example)r"   r   )r'   r   )r"   r   )rs   )r   r   r   r   r   r	   test_15_loops  s*    zTraceTestCase.test_15_loopsc             C   s(   i }t d| | |d dddg d S )Na  def f():
































































































































































































































































    passf)r   r   )i  r   )i  r   )execrs   )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 )Nr   )r2   r4   r6   )linenor   r   r	   rq     s    
z0TraceTestCase.test_17_none_f_trace.<locals>.func)r   r   )r   r   )rs   )r   rq   r   r   r	   test_17_none_f_trace  s    z"TraceTestCase.test_17_none_f_traceN)r   r   r   ra   rb   staticmethodrc   rn   rs   rt   ru   rw   rz   r{   r|   r}   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r	   r\   %  s4   
	r\   c                   s,   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   s   g | ]}|d  dkr|qS )r   r   r   )rX   re   r   r   r	   rg     s    z>SkipLineEventsTraceTestCase.compare_events.<locals>.<listcomp>)superrn   )r   rf   rO   rm   Zskip_line_events)	__class__r   r	   rn     s    z*SkipLineEventsTraceTestCase.compare_eventsc               C   s
   t ddS )NF)rM   )rL   r   r   r   r	   rc     s    z'SkipLineEventsTraceTestCase.make_tracer)r   r   r   r   rn   r   rc   __classcell__r   r   )r   r	   r     s   r   c                   s,   e Zd ZdZ fddZedd Z  ZS )TraceOpcodesTestCasez;Repeat the trace tests, but with per-opcodes events enabledc                sF   dd |D }t |dkr2| jt |t |dd t ||| d S )Nc             S   s   g | ]}|d  dkr|qS )r   Zopcoder   )rX   re   r   r   r	   rg     s    z7TraceOpcodesTestCase.compare_events.<locals>.<listcomp>r   z)No 'opcode' events received by the tracer)msg)lenZ
assertLessr   rn   )r   rf   rO   rm   Zskip_opcode_events)r   r   r	   rn     s
    z#TraceOpcodesTestCase.compare_eventsc               C   s
   t ddS )NT)rN   )rL   r   r   r   r	   rc     s    z TraceOpcodesTestCase.make_tracer)r   r   r   r   rn   r   rc   r   r   r   )r   r	   r     s   r   c               @   s\   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S )RaisingTraceFuncTestCasec             C   s   |  tjt  d S )N)r_   r2   r3   r`   )r   r   r   r	   ra     s    zRaisingTraceFuncTestCase.setUpc             C   s   || j krtn| jS dS )zXA trace function that raises an exception in response to a
        specific trace event.N)raiseOnEvent
ValueErrorrU   )r   rR   rV   rW   r   r   r	   rU     s    
zRaisingTraceFuncTestCase.tracec             C   s    | j dkrd}d| }n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.r1   r   r   N)r   )r   r   r(   r   r   r	   r     s    

zRaisingTraceFuncTestCase.fc          	   C   s   || _ yVxPtt d D ]<}t| j y|   W n tk
rJ   Y qX | d qW W n t	k
rz   | d Y nX dS )zTTests that an exception raised in response to the given event is
        handled OK.r   zexception not raised!zrecursion counter not resetN)
r   r#   r2   getrecursionlimitr3   rU   r   r   rj   r:   )r   rV   rA   r   r   r	   run_test_for_event  s    z+RaisingTraceFuncTestCase.run_test_for_eventc             C   s   |  d d S )Nr   )r   )r   r   r   r	   	test_call  s    z"RaisingTraceFuncTestCase.test_callc             C   s   |  d d S )Nr   )r   )r   r   r   r	   	test_line  s    z"RaisingTraceFuncTestCase.test_linec             C   s   |  d d S )Nr   )r   )r   r   r   r	   test_return
  s    z$RaisingTraceFuncTestCase.test_returnc             C   s   |  d d S )Nr1   )r   )r   r   r   r	   test_exception  s    z'RaisingTraceFuncTestCase.test_exceptionc                s^   dd   fddt  y
   W n$ tk
rN   dd l}|  Y nX | d d S )Nc              S   s   xt dD ]} t|  q
W d S )Nr    )r#   print)rA   r   r   r	   r     s    z4RaisingTraceFuncTestCase.test_trash_stack.<locals>.fc                s&   |dkr"| j  jjd kr"tdS )Nr   r   zi am crashing)rT   ro   rp   r:   )rR   ZwhyZextra)r   gr   r	   r     s    z4RaisingTraceFuncTestCase.test_trash_stack.<locals>.gr   zexception not propagated)r2   r3   r:   r]   Zcollectrj   )r   r]   r   )r   r   r	   test_trash_stack  s    

z)RaisingTraceFuncTestCase.test_trash_stackc          	      s^   dd } fdd t  }z.t   y
|  W n tk
rH   Y nX W d t | X d S )Nc              S   s   d} | j  d S )Nr   )Zno_such_attr)r   r   r   r	   r   &  s    z<RaisingTraceFuncTestCase.test_exception_arguments.<locals>.fc                s"   |dkr|\}}} |t  S )Nr1   )ZassertIsInstancer-   )rR   rV   rW   typer1   rU   )r   r   r   r	   r   *  s    
z<RaisingTraceFuncTestCase.test_exception_arguments.<locals>.g)r2   r`   r3   AttributeError)r   r   Zexistingr   )r   r   r	   test_exception_arguments%  s    


z1RaisingTraceFuncTestCase.test_exception_argumentsN)r   r   r   ra   rU   r   r   r   r   r   r   r   r   r   r   r   r	   r     s   	r   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)ro   codejumpFromjumpTorV   rp   	firstLinedone)r   Zfunctionr   r   rV   	decoratedr   r   r	   r
   C  s    zJumpTracer.__init__c             C   s   | j r
d S | jd kr4|j| jkr4|dkr4|jd | _|| jkr| jr|j| j| j kr|}x|d k	rx|j| jkrx|j}q\W |d k	ry| j| j |_W n t	k
r   | j|_Y nX d| _ | j
S )Nr   r   T)r   r   f_coder   rT   rV   r   r5   r   	TypeErrorrU   )r   rR   rV   rW   r   r   r   r	   rU   L  s"    
zJumpTracer.traceN)r   F)r   r   r   r   r
   rU   r   r   r   r	   r   @  s    
r   c          
   C   sH   y|  d W n4 tk
rB } z|  dt|k W d d }~X Y nX d S )Nr   Zinteger)r   r   ri   )r   re   r   r   r	   no_jump_to_non_integersf  s    r   c           
   C   sT   yt  j} | j| _W n0 tk
rF } zdt|kr6 W d d }~X Y n
X tdd S )NrU   z'Trace-function-less jump failed to fail)r2   r4   r5   rT   r   ri   rv   )Zprevious_framere   r   r   r	   no_jump_without_trace_functionn  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dddgdd Z
eddddgdd Zeddddddddddgdd Zeddddgdd  Zeddddddddgd!d" Zedddgd#d$ Zedddddgd%d& Zedd'dd'gd(d) Zeddddged*fd+d, Zedd-dd-ged*fd.d/ Zeddddgd0d1 Zeddddgd2d3 Zeddddgd4d5 Zedddddddd6gd7d8 Zedddddddd6gd9d: Zedddgd;d< Zeddddddgd=d> Zedddgd?d@ ZedddddgdAdB ZedddddCgdDdE ZedddddgdFdG ZeddddddHgdIdJ Z eddddddHgdKdL Z!eddddddgdMdN Z"eddddddgdOdP Z#eddddddd6dgdQdR Z$eddddddd6dgdSdT Z%eddddddgdUdV Z&eddddddgdWdX Z'edCd-dddd-dYgdZd[ Z(edddddgd\d] Z)edddddgd^d_ Z*edddddCd'gd`da Z+eddCdddCdbd-dcgddde Z,eddddCgdfdg Z-eddddCgdhdi Z.edddge/djfdkdl Z0edd6dge/dmfdndo Z1edddge/dpfdqdr Z2edddge/dpfdsdt Z3edddge/dpfdudv Z4edddge/dpfdwdx Z5eddg e/dyfdzd{ Z6eddg e/dyfd|d} Z7eddddge/dyfd~d Z8eddddge/dyfdd Z9eddg e/dyfdd Z:eddddge/dyfdd Z;eddg e/dyfdd Z<eddg e/dyfdd Z=edddddge/dyfdd Z>edddddge/dyfdd Z?eddg e/dyfdd Z@eddddge/dyfdd ZAeddg e/dyfdd ZBedddge/dyfdd ZCedddge/dyfdd ZDedddddge/dfdd ZEeddg e/dfdd ZFeddddge/dfdd ZGeddddd6ge/dyfdd ZHeddddd6ge/dyfdd ZIeddddge/dyfdd ZJeddddge/dyfdd ZKdd ZLdd ZMdd ZNdd ZOedddgde/dfddd ZPedddgde/dfddd ZQedddgde/dfddd ZRedddgde/dfddd ZSdS )JumpTestCasec             C   s    |  tjt  td  d S )N)r_   r2   r3   r`   )r   r   r   r	   ra   }  s    zJumpTestCase.setUpc             C   s.   ||kr*|  dt| d d t|  d S )NzOutputs don't match:
Expected: rh   z
Received: )rj   repr)r   expectedZreceivedr   r   r	   compare_jump_output  s    z JumpTestCase.compare_jump_outputNr   Fc       
   	   C   sj   t |||||}t|j g }	|d kr2||	 n| j|  ||	 W d Q R X td  | ||	 d S )N)r   r2   r3   rU   assertRaisesRegexr   )
r   rq   r   r   r   errorrV   r   rr   r   r   r   r	   rt     s    

zJumpTestCase.run_testc       
   	   C   sv   t |||||}t|j g }	|d kr8t||	 n$| j|  t||	 W d Q R X td  | ||	 d S )N)r   r2   r3   rU   asynciorunr   r   )
r   rq   r   r   r   r   rV   r   rr   r   r   r   r	   run_async_test  s    
zJumpTestCase.run_async_testc                s    fdd}|S )zqDecorator that creates a test that makes a jump
        from one place to another in the following code.
        c                s"   t   fdd}|S )Nc          	      s   | j  dd d S )NT)r   rV   r   )rt   )r   )r   rV   r   rq   r   r   r   r	   test  s    z7JumpTestCase.jump_test.<locals>.decorator.<locals>.test)r   )rq   r   )r   rV   r   r   r   )rq   r	   	decorator  s    z)JumpTestCase.jump_test.<locals>.decoratorr   )r   r   r   r   rV   r   r   )r   rV   r   r   r   r	   	jump_test  s    zJumpTestCase.jump_testc                s    fdd}|S )z~Decorator that creates a test that makes a jump
        from one place to another in the following asynchronous code.
        c                s"   t   fdd}|S )Nc          	      s   | j  dd d S )NT)r   rV   r   )r   )r   )r   rV   r   rq   r   r   r   r	   r     s    z=JumpTestCase.async_jump_test.<locals>.decorator.<locals>.test)r   )rq   r   )r   rV   r   r   r   )rq   r	   r     s    z/JumpTestCase.async_jump_test.<locals>.decoratorr   )r   r   r   r   rV   r   r   )r   rV   r   r   r   r	   async_jump_test  s    zJumpTestCase.async_jump_testr   r"   c             C   s"   |  d |  d |  d d S )Nr   r   r"   )r   )r   r   r   r	   test_jump_simple_forwards  s    

z&JumpTestCase.test_jump_simple_forwardsr   c             C   s   |  d |  d d S )Nr   r   )r   )r   r   r   r	   test_jump_simple_backwards  s    
z'JumpTestCase.test_jump_simple_backwardsr    c             C   s>   x.dD ]&}|  d xdD ]}|  d qW qW |  d d S )N)r   r   r   )r"   r'   r    )r   )r   rA   jr   r   r	   test_jump_out_of_block_forwards  s
    


z,JumpTestCase.test_jump_out_of_block_forwardsr&   rC   c             C   sR   |  d x8dD ]0}|  d xdD ]}|  d q$W |  d qW |  d d S )Nr   )r   r"   )r   r    r&   rC   )r   )r   rA   r   r   r   r	    test_jump_out_of_block_backwards  s    



z-JumpTestCase.test_jump_out_of_block_backwardsr'   c          
      sd   x^dD ]V}xFt ddg2 y3 d H }W n
 tk
sFX | d | d q   Y  W | d qW d S )N)r   r   r   r"   r'   r    )r   StopAsyncIterationr   )r   rA   r   r   r	   )test_jump_out_of_async_for_block_forwards  s
    
&
z6JumpTestCase.test_jump_out_of_async_for_block_forwardsc          
      sl   xfdD ]^}|  d xDtdg2 y3 d H }W n
 tk
sNX |  d |  d q    Y  W |  d qW d S )N)r   r   r   r'   r    r&   )r   r   r   )r   rA   r   r   r	   *test_jump_out_of_async_for_block_backwards  s    

$
z7JumpTestCase.test_jump_out_of_async_for_block_backwardsc             C   s   |  d |  d d S )Nr   r"   )r   )r   r   r   r	   test_jump_to_codeless_line  s    
z'JumpTestCase.test_jump_to_codeless_linec             C   s"   |  d |  d |  d d S )Nr   r   r"   )r   )r   r   r   r	   test_jump_to_same_line  s    

z#JumpTestCase.test_jump_to_same_line	   c             C   sF   z|  d W d |  d z|  d W d |  d X |  d X d S )Nr   r'   r&      r   )r   )r   r   r   r	   test_jump_in_nested_finally  s    
z(JumpTestCase.test_jump_in_nested_finally c          	   C   s<   z|  d dd  d S |  d |  d X |  d d S )Nr   r   r   r&   rC   r   )r   )r   r   r   r	   test_jump_in_nested_finally_2  s    

z*JumpTestCase.test_jump_in_nested_finally_2   c             C   sX   z|  d dd  d S |  d z|  d W d |  d X |  d X |  d d S )	Nr   r   r   r&   r   rF   r      )r   )r   r   r   r	   test_jump_in_nested_finally_3  s    

z*JumpTestCase.test_jump_in_nested_finally_3c             C   s(   |  d x|  d qW |  d d S )Nr   r"   r'   )r   )r   r   r   r	   test_jump_infinite_while_loop  s    
z*JumpTestCase.test_jump_infinite_while_loopc          	   C   s.   t | d | d W d Q R X | d d S )Nr   r   r"   )r   r   )r   r   r   r	   $test_jump_forwards_out_of_with_block#  s    z1JumpTestCase.test_jump_forwards_out_of_with_blockc          
      s<   t | d4 I d H  | d W d Q I d H R X | d d S )Nr   r   r"   )r   r   )r   r   r   r	   *test_jump_forwards_out_of_async_with_block)  s    z7JumpTestCase.test_jump_forwards_out_of_async_with_blockr,   c          	   C   s.   |  d t| d |  d W d Q R X d S )Nr   r   r"   )r   r   )r   r   r   r	   %test_jump_backwards_out_of_with_block/  s    
z2JumpTestCase.test_jump_backwards_out_of_with_blockc          
      s<   |  d t| d4 I d H  |  d W d Q I d H R X d S )Nr   r   r"   )r   r   )r   r   r   r	   +test_jump_backwards_out_of_async_with_block5  s    
z8JumpTestCase.test_jump_backwards_out_of_async_with_blockc          	   C   s*   z|  d W d |  d X |  d d S )Nr   r'   r    )r   )r   r   r   r	   +test_jump_forwards_out_of_try_finally_block;  s    z8JumpTestCase.test_jump_forwards_out_of_try_finally_blockc          	   C   s*   |  d z|  d W d |  d X d S )Nr   r"   r    )r   )r   r   r   r	   ,test_jump_backwards_out_of_try_finally_blockC  s    
z9JumpTestCase.test_jump_backwards_out_of_try_finally_blockc             C   s6   y|  d W n   |  d  Y nX |  d d S )Nr   r'   r&   )r   )r   r   r   r	   *test_jump_forwards_out_of_try_except_blockK  s    
z7JumpTestCase.test_jump_forwards_out_of_try_except_blockc             C   s6   |  d y|  d W n   |  d  Y nX d S )Nr   r"   r    )r   )r   r   r   r	   +test_jump_backwards_out_of_try_except_blockT  s    

z8JumpTestCase.test_jump_backwards_out_of_try_except_blockr   c             C   s`   ydd  W nD t k
r4   | d | d Y n tk
rP   | d Y nX | d d S )Nr   r   r'   r    rC   r   )ZeroDivisionErrorr   FloatingPointError)r   r   r   r	   test_jump_between_except_blocks]  s    
z,JumpTestCase.test_jump_between_except_blocksc             C   sF   ydd  W n*   |  d |  d |  d Y nX |  d d S )Nr   r   r'   r    r&   rC   )r   )r   r   r   r	   test_jump_within_except_blockh  s    

z*JumpTestCase.test_jump_within_except_blockrJ   c          	   C   sN   |  d t| d |  d W d Q R X t| d |  d W d Q R X d S )Nr   r   r"   r'   r    )r   r   )r   r   r   r	   test_jump_across_withr  s
    
z"JumpTestCase.test_jump_across_withc          
      sj   |  d t| d4 I d H  |  d W d Q I d H R X t| d4 I d H  |  d W d Q I d H R X d S )Nr   r   r"   r'   r    )r   r   )r   r   r   r	   test_jump_across_async_withz  s
    
z(JumpTestCase.test_jump_across_async_withc          
   C   sP   |  d x6dD ].}t| d |  d W d Q R X |  d qW |  d d S )Nr   )r   r"   r'   r    r&   )r   r   )r   rA   r   r   r	   ,test_jump_out_of_with_block_within_for_block  s    

z9JumpTestCase.test_jump_out_of_with_block_within_for_blockc                s^   |  d xDdD ]<}t| d4 I d H  |  d W d Q I d H R X |  d qW |  d d S )Nr   )r   r"   r'   r    r&   )r   r   )r   rA   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_blockc          
   C   sX   |  d t| d0 t| d |  d W d Q R X |  d W d Q R X |  d d S )Nr   r   r"   r'   r    r&   )r   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                sf   |  d t| d> t| d4 I d H  |  d W d Q I d H R X |  d W d Q R X |  d d S )Nr   r   r"   r'   r    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_blockc             C   sJ   z|  d W d t| d |  d W d Q R X |  d X |  d d S )Nr   r'   r    r&   rC   )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                sX   z|  d W d t| d4 I d H  |  d W d Q I d H R X |  d X |  d d S )Nr   r'   r    r&   rC   )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   c             C   s|   |  d xbdD ]Z}|  d x@dD ]8}|  d zxdD ]}|  d q:W W d |  d X q$W |  d qW |  d	 d S )
Nr   )r   r"   )r   r   r    r   rF   r   r   )r   )r   rA   r   kr   r   r	   &test_jump_out_of_complex_nested_blocks  s    





z3JumpTestCase.test_jump_out_of_complex_nested_blocksc          	   C   s8   |  d t| d}|  d W d Q R X |  d d S )Nr   r   r'   r    )r   r   )r   r   r   r   r	    test_jump_out_of_with_assignment  s
    

z-JumpTestCase.test_jump_out_of_with_assignmentc          
      sF   |  d t| d4 I d H }|  d W d Q I d H R X |  d d S )Nr   r   r'   r    )r   r   )r   r   r   r   r	   &test_jump_out_of_async_with_assignment  s
    
z3JumpTestCase.test_jump_out_of_async_with_assignmentc          	   C   sF   |  d z |  d | sd S |  d W d |  d X |  d d S )Nr   r"   r&   r   r   )r   )r   r   r   r	   *test_jump_over_return_in_try_finally_block  s    

z7JumpTestCase.test_jump_over_return_in_try_finally_blockrF      c          	   C   s`   |  d xF|  d z|  d | s(P |  d W d |  d X |  d P qW |  d d S )Nr   r"   r    r   rF   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   sJ   |  d | s(x,dD ]}|  d qW n|  d |  d |  d d S )Nr   )r"   r'   r&   rC   r   )r   )r   rA   r   r   r	   $test_jump_over_for_block_before_else  s    



z1JumpTestCase.test_jump_over_for_block_before_elsec          	      sn   |  d | sLxPtdg2 y3 d H }W n
 tk
s>X |  d q   Y  W n|  d |  d |  d d S )Nr   r"   r'   r&   rC   r   )r   r   r   )r   rA   r   r   r	   *test_jump_over_async_for_block_before_else   s    
$

z7JumpTestCase.test_jump_over_async_for_block_before_elseZafterc             C   s   |  d |  d d S )Nr   r   )r   )r   r   r   r	   test_no_jump_too_far_forwards  s    
z*JumpTestCase.test_no_jump_too_far_forwardsZbeforec             C   s   |  d |  d d S )Nr   r   )r   )r   r   r   r	   test_no_jump_too_far_backwards  s    
z+JumpTestCase.test_no_jump_too_far_backwardsexceptc             C   s,   y|  d W n   |  d  Y nX d S )Nr   r'   )r   )r   r   r   r	   test_no_jump_to_except_1  s
    
z%JumpTestCase.test_no_jump_to_except_1c             C   s4   y|  d W n  tk
r.   |  d  Y nX d S )Nr   r'   )r   r   )r   r   r   r	   test_no_jump_to_except_2   s
    
z%JumpTestCase.test_no_jump_to_except_2c          
   C   sD   y|  d W n0 tk
r> } z|  d |W d d }~X Y nX d S )Nr   r'   )r   r   )r   re   r   r   r	   test_no_jump_to_except_3(  s
    
z%JumpTestCase.test_no_jump_to_except_3c          
   C   sH   y|  d W n4 ttfk
rB } z|  d |W d d }~X Y nX d S )Nr   r'   )r   r   r:   )r   re   r   r   r	   test_no_jump_to_except_40  s
    
z%JumpTestCase.test_no_jump_to_except_4Zintoc             C   s&   |  d xdD ]}|  d qW d S )Nr   )r   r   r"   )r   )r   rA   r   r   r	   $test_no_jump_forwards_into_for_block8  s    

z1JumpTestCase.test_no_jump_forwards_into_for_blockc          	      sL   |  d x<tddg2 y3 d H }W n
 tk
s<X |  d q   Y  W d S )Nr   r   r"   )r   r   r   )r   rA   r   r   r	   *test_no_jump_forwards_into_async_for_block>  s    
&z7JumpTestCase.test_no_jump_forwards_into_async_for_blockc             C   s&   xdD ]}|  d qW |  d d S )N)r   r   r   r"   )r   )r   rA   r   r   r	   %test_no_jump_backwards_into_for_blockD  s    
z2JumpTestCase.test_no_jump_backwards_into_for_blockc          	      sL   x<t ddg2 y3 d H }W n
 tk
s2X | d q   Y  W | d d S )Nr   r   r"   )r   r   r   )r   rA   r   r   r	   +test_no_jump_backwards_into_async_for_blockJ  s    &z8JumpTestCase.test_no_jump_backwards_into_async_for_blockc             C   s2   d}|  d x|dkr,|  d |d7 }qW d S )Nr   r   r'   )r   )r   rA   r   r   r	   &test_no_jump_forwards_into_while_blockP  s
    


z3JumpTestCase.test_no_jump_forwards_into_while_blockc             C   s2   d}x|dkr"|  d |d7 }qW |  d d S )Nr   r   r"   r    )r   )r   rA   r   r   r	   'test_no_jump_backwards_into_while_blockX  s
    

z4JumpTestCase.test_no_jump_backwards_into_while_blockc          	   C   s.   |  d t| d |  d W d Q R X d S )Nr   r   r"   )r   r   )r   r   r   r	   %test_no_jump_forwards_into_with_block`  s    
z2JumpTestCase.test_no_jump_forwards_into_with_blockc          
      s<   |  d t| d4 I d H  |  d W d Q I d H R X d S )Nr   r   r"   )r   r   )r   r   r   r	   +test_no_jump_forwards_into_async_with_blockf  s    
z8JumpTestCase.test_no_jump_forwards_into_async_with_blockrK   c          	   C   s.   t | d | d W d Q R X | d d S )Nr   r   r"   )r   r   )r   r   r   r	   &test_no_jump_backwards_into_with_blockl  s    z3JumpTestCase.test_no_jump_backwards_into_with_blockc          
      s<   t | d4 I d H  | d W d Q I d H R X | d d S )Nr   r   r"   )r   r   )r   r   r   r	   ,test_no_jump_backwards_into_async_with_blockr  s    z9JumpTestCase.test_no_jump_backwards_into_async_with_blockc          	   C   s*   |  d z|  d W d |  d X d S )Nr   r"   r    )r   )r   r   r   r	   ,test_no_jump_forwards_into_try_finally_blockx  s    
z9JumpTestCase.test_no_jump_forwards_into_try_finally_blockc          	   C   s*   z|  d W d |  d X |  d d S )Nr   r'   r    )r   )r   r   r   r	   -test_no_jump_backwards_into_try_finally_block  s    z:JumpTestCase.test_no_jump_backwards_into_try_finally_blockc             C   s6   |  d y|  d W n   |  d  Y nX d S )Nr   r"   r    )r   )r   r   r   r	   +test_no_jump_forwards_into_try_except_block  s    

z8JumpTestCase.test_no_jump_forwards_into_try_except_blockc             C   s6   y|  d W n   |  d  Y nX |  d d S )Nr   r'   r&   )r   )r   r   r   r	   ,test_no_jump_backwards_into_try_except_block  s    
z9JumpTestCase.test_no_jump_backwards_into_try_except_blockc          
   C   sn   ydd  W nR t k
r4   | d | d Y n, tk
r^ } z| d W d d }~X Y nX | d d S )Nr   r   r'   r    rC   r   )r   r   r   )r   re   r   r   r	   $test_no_jump_between_except_blocks_2  s    
z1JumpTestCase.test_no_jump_between_except_blocks_2finallyc          	   C   s>   z|  d |  d W d |  d |  d X |  d d S )Nr   r"   r    r&   rC   )r   )r   r   r   r	   test_no_jump_into_finally_block  s    

z,JumpTestCase.test_no_jump_into_finally_blockc          	   C   s*   |  d z|  d W d |  d X d S )Nr   r"   r    )r   )r   r   r   r	   !test_no_jump_into_finally_block_2  s    
z.JumpTestCase.test_no_jump_into_finally_block_2c          	   C   s*   |  d z|  d W d |  d X d S )Nr   r"   r    )r   )r   r   r   r	   !test_no_jump_out_of_finally_block  s    
z.JumpTestCase.test_no_jump_out_of_finally_blockc          	   C   sN   |  d t| d |  d W d Q R X t| d |  d W d Q R X d S )Nr   r   r"   r'   r    )r   r   )r   r   r   r	    test_no_jump_between_with_blocks  s
    
z-JumpTestCase.test_no_jump_between_with_blocksc          
      sj   |  d t| d4 I d H  |  d W d Q I d H R X t| d4 I d H  |  d W d Q I d H R X d S )Nr   r   r"   r'   r    )r   r   )r   r   r   r	   &test_no_jump_between_async_with_blocks  s
    
z3JumpTestCase.test_no_jump_between_async_with_blocksc             C   sJ   |  d | s(x,dD ]}|  d qW n|  d |  d |  d d S )Nr   )r"   r'   r&   rC   r   )r   )r   rA   r   r   r	   'test_no_jump_into_for_block_before_else  s    



z4JumpTestCase.test_no_jump_into_for_block_before_elsec          	      sn   |  d | sLxPtdg2 y3 d H }W n
 tk
s>X |  d q   Y  W n|  d |  d |  d d S )Nr   r"   r'   r&   rC   r   )r   r   r   )r   rA   r   r   r	   -test_no_jump_into_async_for_block_before_else  s    
$

z:JumpTestCase.test_no_jump_into_async_for_block_before_elsec             C   s   |  tdddg d S )Nr   ZSpamT)rt   r   )r   r   r   r	   test_no_jump_to_non_integers  s    z)JumpTestCase.test_no_jump_to_non_integersc             C   s
   t   d S )N)r   )r   r   r   r	   #test_no_jump_without_trace_function  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   r   i  r   )r   rt   )r   dr   r   r   r	   test_large_function  s
    z JumpTestCase.test_large_functionc                sp   t ddd G  fddd}t|dd}t|j dg i}t | td  | dd	dd	d
g|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   ro   r   )r   r   r	   fake_function  s   r  r   r   r   r"   r'   )compiler   r2   r3   rU   r   r   )r   r  rr   r   r   )r   r	   test_jump_to_firstlineno  s    


z%JumpTestCase.test_jump_to_firstlinenor   z5can't jump from the 'call' trace event of a new frame)rV   r   c                s*     d  fdd}|    d d S )Nr   c                  s     d d S )Nr"   )r   r   )r   r   r	   nested  s    z3JumpTestCase.test_no_jump_from_call.<locals>.nestedr    )r   )r   r  r   )r   r	   test_no_jump_from_call  s    
z#JumpTestCase.test_no_jump_from_callr   z'can only jump from a 'line' trace eventc             C   s   |  d d S )Nr   )r   )r   r   r   r	   test_no_jump_from_return_event  s    
z+JumpTestCase.test_no_jump_from_return_eventr1   c             C   s   |  d dd  d S )Nr   r   )r   )r   r   r   r	   !test_no_jump_from_exception_event  s    
z.JumpTestCase.test_no_jump_from_exception_eventz!can't jump from a yield statementc                s$    fdd}t |   d d S )Nc               3   s     d dV  d S )Nr   r"   )r   r   )r   r   r	   gen'  s    
z1JumpTestCase.test_no_jump_from_yield.<locals>.genr    )nextr   )r   r  r   )r   r	   test_no_jump_from_yield$  s    
z$JumpTestCase.test_no_jump_from_yield)Nr   F)Nr   F)Nr   )Nr   )Tr   r   r   ra   r   rt   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  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   |  s    
 


$
 	  		
		 	 	

   	
 
   	r   __main__),r   r   Zunittestr2   rl   r]   	functoolsr   r   r   r   r   r   rO   r   r!   r%   r)   r*   r   r.   r0   r8   r9   r;   r<   r>   rB   rD   rE   rH   rL   ZTestCaser\   r   Zcpython_onlyr   r   r   r   r   r   r   mainr   r   r   r	   <module>   s  
	"  `&     7
