B
    u9aפ                 @   s  d Z ddlZddlZddlZddlZddlZddlZddlZddl	m
Z
 ddlmZmZ ddlZG dd deZG dd deZG d	d
 d
eZG dd deZdZdd Zdd ZG dd dejZG dd deZG dd dZd5ddZe
dd ZeddfddZdZed Z d d! Z!d"d# Z"d$d% Z#d&d' Z$G d(d) d)ej%Z&G d*d+ d+e&Z'G d,d- d-e&Z(G d.d/ d/e&Z)G d0d1 d1e&Z*d2d3 Z+e,d4kre+  dS )6a   Test the bdb module.

    A test defines a list of tuples that may be seen as paired tuples, each
    pair being defined by 'expect_tuple, set_tuple' as follows:

        ([event, [lineno[, co_name[, eargs]]]]), (set_type, [sargs])

    * 'expect_tuple' describes the expected current state of the Bdb instance.
      It may be the empty tuple and no check is done in that case.
    * 'set_tuple' defines the set_*() method to be invoked when the Bdb
      instance reaches this state.

    Example of an 'expect_tuple, set_tuple' pair:

        ('line', 2, 'tfunc_main'), ('step', )

    Definitions of the members of the 'expect_tuple':
        event:
            Name of the trace event. The set methods that do not give back
            control to the tracer [1] do not trigger a tracer event and in
            that case the next 'event' may be 'None' by convention, its value
            is not checked.
            [1] Methods that trigger a trace event are set_step(), set_next(),
            set_return(), set_until() and set_continue().
        lineno:
            Line number. Line numbers are relative to the start of the
            function when tracing a function in the test_bdb module (i.e. this
            module).
        co_name:
            Name of the function being currently traced.
        eargs:
            A tuple:
            * On an 'exception' event the tuple holds a class object, the
              current exception must be an instance of this class.
            * On a 'line' event, the tuple holds a dictionary and a list. The
              dictionary maps each breakpoint number that has been hit on this
              line to its hits count. The list holds the list of breakpoint
              number temporaries that are being deleted.

    Definitions of the members of the 'set_tuple':
        set_type:
            The type of the set method to be invoked. This may
            be the type of one of the Bdb set methods: 'step', 'next',
            'until', 'return', 'continue', 'break', 'quit', or the type of one
            of the set methods added by test_bdb.Bdb: 'ignore', 'enable',
            'disable', 'clear', 'up', 'down'.
        sargs:
            The arguments of the set method if any, packed in a tuple.
    N)contextmanager)islicerepeatc               @   s   e Zd ZdS )BdbExceptionN)__name__
__module____qualname__ r	   r	   /usr/lib/python3.7/test_bdb.pyr   >   s    r   c               @   s   e Zd ZdZdS )BdbErrorz!Error raised by the Bdb instance.N)r   r   r   __doc__r	   r	   r	   r
   r   ?   s    r   c               @   s   e Zd ZdZdS )BdbSyntaxErrorzSyntax error in the test case.N)r   r   r   r   r	   r	   r	   r
   r   @   s    r   c               @   s   e Zd ZdZdS )BdbNotExpectedErrorzUnexpected result.N)r   r   r   r   r	   r	   r	   r
   r   A   s    r   c               C   s   dt j_i t j_d gt j_d S )N   )_bdb
BreakpointnextZbplist
bpbynumberr	   r	   r	   r
   reset_BreakpointI   s    r   c              C   s   dd t jjD } | sdS d}x~| D ]v}|s4d}d}|jr>dnd}|jrLd	nd
}|d|j|||j|jtj	
|j|jf 7 }|jr|d|jf 7 }|d7 }q$W |S )Nc             S   s   g | ]}|r|qS r	   r	   ).0bpr	   r	   r
   
<listcomp>O   s    z$info_breakpoints.<locals>.<listcomp> Fz!BpNum Temp Enb Hits Ignore Where
Tzyes zno  Zyeszno z%-5d %s %s %-4d %-6d at %s:%dz
	stop only if %s
)r   r   r   	temporaryenabledZnumberhitsignoreospathbasenamefilelinecond)bp_listZheader_addedr   infoZdispZenabr	   r	   r
   info_breakpointsN   s"    
r&   c                   sj   e Zd ZdZ fddZd fdd	Z fdd	Zd
d Zdd Zdd Z	dd Z
dd Zdd Z  ZS )Bdbz$Extend Bdb to enhance test coverage.c                s   d | _ t |||S )N)	currentbpsupertrace_dispatch)selfframeeventarg)	__class__r	   r
   r*   f   s    zBdb.trace_dispatchFNc                s   t |trT|tkrt }nt|d d }|j}t||}|j}	|	j	}|	j
}|	j}t j|||||d}
t |
tr|t|
|
S )N)r   r#   funcname)
isinstancestr__file__globals	importlibimport_module__dict__eval__code__co_filenameco_firstlinenoco_namer)   	set_breakr   )r+   filenamelinenor   r#   r1   globals_modulefunccoderes)r/   r	   r
   r>   j   s    



zBdb.set_breakc                s4   t  ||\| _| _| j| j d | _| j| jfS )Nr   )r)   	get_stackstackindexr,   )r+   ft)r/   r	   r
   rF   ~   s    zBdb.get_stackc             C   s   |  |}| jd7  _dS )z8Increment the ignore count of Breakpoint number 'bpnum'.r   N)get_bpbynumberr   )r+   bpnumr   r	   r	   r
   
set_ignore   s    
zBdb.set_ignorec             C   s   |  |}d|_d S )NT)rK   r   )r+   rL   r   r	   r	   r
   
set_enable   s    
zBdb.set_enablec             C   s   |  |}d|_d S )NF)rK   r   )r+   rL   r   r	   r	   r
   set_disable   s    
zBdb.set_disablec             C   s   |  ||}|rt|d S )N)Zclear_breakr   )r+   fnamer@   errr	   r	   r
   	set_clear   s    zBdb.set_clearc             C   s2   | j std|  j d8  _ | j| j  d | _dS )zMove up in the frame stack.zOldest framer   r   N)rH   r   rG   r,   )r+   r	   r	   r
   set_up   s    z
Bdb.set_upc             C   s@   | j d t| jkrtd|  j d7  _ | j| j  d | _dS )zMove down in the frame stack.r   zNewest framer   N)rH   lenrG   r   r,   )r+   r	   r	   r
   set_down   s    zBdb.set_down)FNN)r   r   r   r   r*   r>   rF   rM   rN   rO   rR   rS   rU   __classcell__r	   r	   )r/   r
   r'   c   s    r'   c                   s   e Zd ZdZd, fdd	Zdd Z fdd	Zd
d Zdd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd-d&d'Zd(d) Zd*d+ Z  ZS ).Tracerz$A tracer for testing the bdb module.NFc                s<   t  j|d || _|| _|d k	r*d| nd | _|   d S )N)skipzDry-run results for %s:)r)   __init__
expect_setdry_runheader	init_test)r+   rZ   rX   r[   	test_case)r/   r	   r
   rY      s
    zTracer.__init__c             C   sB   d | _ d| _d | _tt| jdd d| _tt| jdd d| _d S )Nr      r   )
cur_exceptexpect_set_nobreakpoint_hitslistr   rZ   expected_listset_list)r+   r	   r	   r
   r]      s
    zTracer.init_testc          
      sn   | j d k	r| j |dkrZyt |||}|S  tk
rV } z|| _ | jS d }~X Y qjX nt |||S d S )N	exception)r`   r)   r*   r   )r+   r,   r-   r.   rE   e)r/   r	   r
   r*      s    
zTracer.trace_dispatchc             C   s(   |  |sd S | d|| |   d S )Ncall)Z	stop_hereprocess_eventnext_set_method)r+   r,   Zargument_listr	   r	   r
   	user_call   s    
zTracer.user_callc             C   s^   |  d| | jrD| jrDt d}x|dD ]}td|  q0W |   d | _|   d S )Nr"   r   z  )	ri   r[   rb   r&   stripsplitprintdelete_temporariesrj   )r+   r,   r%   r"   r	   r	   r
   	user_line   s    zTracer.user_linec             C   s   |  d|| |   d S )Nreturn)ri   rj   )r+   r,   Zreturn_valuer	   r	   r
   user_return   s    zTracer.user_returnc             C   s   || _ | d| |   d S )Nrf   )exc_infori   rj   )r+   r,   rs   r	   r	   r
   user_exception   s    zTracer.user_exceptionc             C   s   | j g}||f| _d S )N)r(   rb   )r+   r.   r$   r	   r	   r
   do_clear   s    zTracer.do_clearc             C   s(   | j r$x| j d D ]}| | qW d S )Nr   )rb   Zclear_bpbynumber)r+   nr	   r	   r
   ro      s    zTracer.delete_temporariesc             C   sV   |  j d7  _ y| jd| _W n" tk
rB   td| j  Y nX | jd| _d S )Nr   r   z-expect_set list exhausted, cannot pop item %d)ra   rd   popexpect
IndexErrorr   re   	set_tuple)r+   r	   r	   r
   pop_next   s    zTracer.pop_nextc                s  d }|dkrj d }|| jd k	rDjsDjg}|g f_|_  jrhj d S j	r
j	d |d   |dkrd ntj	dkr~|dkrFj	d \ }t  }jsdj  
|jd d	 
 fd
d|D fddjd D d 
t|jd d n8|dkr~tj d j	d s~djj f  d S )Nrf   r_   r   zWrong event type)rh   rq      r"   z(No breakpoints hit at expect_set item %dzBreakpoint numbers do not matchc                s   g | ]} | qS r	   r	   )r   rv   )bpsr	   r
   r     s    z(Tracer.process_event.<locals>.<listcomp>c                s   g | ]}  |jqS r	   )rK   r   )r   rv   )r+   r	   r
   r      s   zWrong breakpoint hit countr   zWrong temporary breakpointsz/Wrong exception at expect_set item %d, got '%s')rs   rF   r(   rb   r-   r{   r[   print_stater\   rx   check_equalcheck_lno_namecheck_expect_max_sizerT   sortedkeysraise_not_expectedra   r2   )r+   r-   r,   argstbr$   ZtemporariesZbpnumsr	   )r}   r+   r
   ri      sL    





zTracer.process_eventc             C   s&   ||krd S |  d|| j|f  d S )Nz"%s at expect_set item %d, got '%s')r   ra   )r+   Zexpectedresultmsgr	   r	   r
   r   ,  s    zTracer.check_equalc             C   sT   t | j}|dkr.|  }| | jd |d |dkrP| | jd | jjjd dS )z+Check the line number and function co_name.r   zWrong line numberr_   zWrong function nameN)rT   rx   lno_abs2relr   r,   f_coder=   )r+   sr@   r	   r	   r
   r   2  s    
zTracer.check_lno_namec             C   s&   t | j|kr"td| j| jf d S )Nz'Invalid size of the %s expect tuple: %s)rT   rx   r   r-   )r+   sizer	   r	   r
   r   <  s    zTracer.check_expect_max_sizec             C   s<   |  | jjj}| jj}||  tkr8|| jjj d S |S )Nr   )canonicr,   r   r;   f_linenor4   r<   )r+   rP   r@   r	   r	   r
   r   A  s    zTracer.lno_abs2relc             C   s.   |r*|  ||  tkr*| jjj| d S |S )Nr   )r   r4   r,   r   r<   )r+   rP   r@   r	   r	   r
   lno_rel2absG  s    *zTracer.lno_rel2absc             C   s   |   }| jjj}d| j||f }| jrd}x:| jd D ],}|dkrL|d7 }|d|| |jf 7 }q8W |d7 }d| d t| jd  d	 }|d| 7 }n| jd
kr|d| j	d j
 7 }|d7 }|dt| j d S )Nz('%s', %d, '%s'{r   z, z%s: %s}(r   )rf   z),     ,)r   r,   r   r=   r-   rb   rK   r   r3   rs   r   ljustrz   )r+   r@   r=   stater}   rv   r	   r	   r
   	get_stateL  s     

zTracer.get_statec             C   s:   |d k	r | j dkr t  t| td| j |  f  d S )Nr   z%d: %s)ra   rn   r   )r+   r\   r	   r	   r
   r~   ^  s    zTracer.print_statec             C   s6   |d7 }|dt | j 7 }|d|   7 }t|d S )Nr   z  Expected: %s
z  Got:      )r3   rx   r   r   )r+   r   r	   r	   r
   r   d  s    zTracer.raise_not_expectedc             C   sZ  | j d }t| j dkr"| j d nd }t| d| }|dkrF|  d S |dkr\|| j d S |dkrd }|r| | jjj|d }|| j| d S |r|dks|d	krH|d
kr|^}}}| ||}||g}|| ||  n"|dkr||  n|d	kr|  d | _| 	  | j
r$|   n| jr4|   | d |   ntd| j  d S )Nr   r_   r   Zset_)stepcontinuequit)r   rq   until)breakclearr   enabledisable)updown)r   r   )r   r   r   r|   z"%s" is an invalid set_tuple)rz   rT   getattrr,   r   r   r;   extendr-   r{   r[   r~   rx   r   r   rj   r   )r+   set_typer   Z
set_methodr@   rP   Zremainr	   r	   r
   rj   j  sL    











zTracer.next_set_method)NFN)N)r   r   r   r   rY   r]   r*   rk   rp   rr   rt   ru   ro   r{   ri   r   r   r   r   r   r   r~   r   rj   rV   r	   r	   )r/   r
   rW      s*   	
2

rW   c               @   s,   e Zd ZdZd	ddZdd Zd
ddZdS )	TracerRunzAProvide a context for running a Tracer instance with a test case.Nc             C   s2   || _ |j| _t|j|| j| d| _d | _d S )N)rX   r[   r^   )r^   r[   rW   rZ   idtracer_original_tracer)r+   r^   rX   r	   r	   r
   rY     s
    zTracerRun.__init__c             C   s   t   t | _| jS )N)r   sysgettracer   r   )r+   r	   r	   r
   	__enter__  s    
zTracerRun.__enter__c             C   s   t   t| j d}| jjr6|d7 }|d| jj 7 }|d k	rtt|rt	|t
r|jr|jd }|rr|d| 7 }| jrt| dS | j| n |r| jrt| n| j| d S )Nr   z+All paired tuples have not been processed, zthe last one was number %dr   r   T)r   r   settracer   r   re   ra   
issubclassr   r2   BaseExceptionr   r[   rn   r^   fail)r+   Ztype_value	tracebackZ	not_emptyZerr_msgr	   r	   r
   __exit__  s(    

zTracerRun.__exit__)N)NNN)r   r   r   r   rY   r   r   r	   r	   r	   r
   r     s   
r   c          
   C   s   dd }ddg}| | t }d|_dd |_t|tdt||_t| ( t	||d	}|
t W d
Q R X W d
Q R X d
S )a{  Run a test and print the dry-run results.

    'modules':  A dictionary mapping module names to their source code as a
                string. The dictionary MUST include one module named
                'test_module' with a main() function.
    'set_list': A list of set_type tuples to be run on the module.

    For example, running the following script outputs the following results:

    *****************************   SCRIPT   ********************************

    from test.test_bdb import run_test, break_in_func

    code = '''
        def func():
            lno = 3

        def main():
            func()
            lno = 7
    '''

    set_list = [
                break_in_func('func', 'test_module.py'),
                ('continue', ),
                ('step', ),
                ('step', ),
                ('step', ),
                ('quit', ),
            ]

    modules = { 'test_module': code }
    run_test(modules, set_list)

    ****************************   results   ********************************

    1: ('line', 2, 'tfunc_import'),    ('next',),
    2: ('line', 3, 'tfunc_import'),    ('step',),
    3: ('call', 5, 'main'),            ('break', ('test_module.py', None, False, None, 'func')),
    4: ('None', 5, 'main'),            ('continue',),
    5: ('line', 3, 'func', ({1: 1}, [])), ('step',),
      BpNum Temp Enb Hits Ignore Where
      1     no   yes 1    0      at test_module.py:2
    6: ('return', 3, 'func'),          ('step',),
    7: ('line', 7, 'main'),            ('step',),
    8: ('return', 7, 'main'),          ('quit',),

    *************************************************************************

    c             s   s@   y&x t | }t |}|V  |V  qW W n tk
r:   d S X d S )N)r   StopIteration)abxyr	   r	   r
   gen  s    zrun_test.<locals>.gen)r   )r   Tc               S   s   d S )Nr	   r	   r	   r	   r
   <lambda>      zrun_test.<locals>.<lambda>r	   )rX   N)r   BaseTestCaser[   r   rc   r   iterrZ   create_modulesr   runcalltfunc_import)modulesre   rX   r   sltestr   r	   r	   r
   run_test  s    3


r   c             c   s   t j  tjt  z\xH| D ]@}|d }t|d}|	t
| |  W d Q R X t| q$W t  d V  W d x| D ]}t j| qW tj  X W d Q R X d S )Nz.pyw)r   supportZtemp_cwdr   r   appendr   getcwdopenwritetextwrapdedent	linecache
checkcacher6   invalidate_cachesZforgetrw   )r   mrP   rI   r	   r	   r
   r     s    


r   Fc             C   s   d|d ||| ffS )Nr   r	   )r1   rP   r   r#   r	   r	   r
   break_in_func%  s    r   test_module_for_bdbz.pyc              C   s   dd l } |   d S )Nr   )r   main)r   r	   r	   r
   r   *  s    r   c              C   s    d} t   t  d} d} d} d S )Nr_            )tfunc_firsttfunc_second)lnor	   r	   r
   
tfunc_main.  s    r   c              C   s   d} d} d} d S )Nr_   r|      r	   )r   r	   r	   r
   r   6  s    r   c              C   s   d} d S )Nr_   r	   )r   r	   r	   r
   r   ;  s    r   c               @   s   e Zd ZdZeZdddZdS )r   zBase class for all tests.Nc             C   s   |  |d d S )N)ZfailureException)r+   r   r	   r	   r
   r   C  s    zBaseTestCase.fail)N)r   r   r   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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 )StateTestCasez;Test the step, next, return, until and quit 'set_' methods.c          	   C   s8   ddddddddg| _ t| }|t W d Q R X d S )N)r"   r_   r   )r   )r"   r|   r   )rh   r   r   )r"   r_   r   )r   )rZ   r   r   r   )r+   r   r	   r	   r
   	test_stepK  s    

zStateTestCase.test_stepc             C   s   xzdD ]r}| j |d\ ddddddtdffd	d
dddddig ff|fddg| _t| }|t W d Q R X W d Q R X qW d S )N)r   r   )r   )r"   r_   r   )r   )r"   r|   r   )rh   r   r   r   r|   )Noner   r   )r   r"   r   r   )r"   r   r   )r   )subTestr4   rZ   r   r   r   )r+   r   r   r	   r	   r
    test_step_next_on_last_statementU  s    


z.StateTestCase.test_step_next_on_last_statementc          
   C   s<   ddddddddddg
| _ t| }|t W d Q R X d S )	N)r"   r_   r   )r   )r"   r|   r   )r   )r"   r   r   )rh   r   r   )r"   r_   r   )r   )rZ   r   r   r   )r+   r   r	   r	   r
   	test_nextc  s    

zStateTestCase.test_nextc          
   C   sP   d}t |i}t|2 ddddg| _t| }|t W d Q R X W d Q R X d S )Nz9
            def main():
                lno = 3
        )r"   r_   r   )r   )r"   r|   r   )r   )TEST_MODULEr   rZ   r   r   r   )r+   rD   r   r   r	   r	   r
   test_next_over_importn  s    


z#StateTestCase.test_next_over_importc          	   C   s8   ddddddddg| _ t| }|t W d Q R X d S )N)r"   r_   r   )r   )r"   r|   r   )rh   r   r   )r   )r"   r_   r   )r   )rZ   r   r   r   )r+   r   r	   r	   r
   test_next_on_plain_statement|  s    

z*StateTestCase.test_next_on_plain_statementc          
   C   s<   dddddddddd	g
| _ t| }|t W d Q R X d S )
N)r"   r_   r   )r   )r"   r|   r   )rh   r   r   )r   )r   r|   r   )r   )r"   r   r   )r   )rZ   r   r   r   )r+   r   r	   r	   r
   test_next_in_caller_frame  s    

z'StateTestCase.test_next_in_caller_framec             C   s@   dddddddddddd	g| _ t| }|t W d Q R X d S )
N)r"   r_   r   )r   )r"   r|   r   )rh   r   r   )r"   r_   r   )rq   )rq   r   r   )r"   r   r   )r   )rZ   r   r   r   )r+   r   r	   r	   r
   test_return  s    

zStateTestCase.test_returnc          
   C   s<   dddddddddd	g
| _ t| }|t W d Q R X d S )
N)r"   r_   r   )r   )r"   r|   r   )rh   r   r   )r   )r   r|   r   )rq   )rq   r   r   )r   )rZ   r   r   r   )r+   r   r	   r	   r
   test_return_in_caller_frame  s    

z)StateTestCase.test_return_in_caller_framec          
   C   s<   ddddddddddg
| _ t| }|t W d Q R X d S )	N)r"   r_   r   )r   )r"   r|   r   )rh   r   r   )r"   r_   r   )r   )r   )r"   r   r   )r   )rZ   r   r   r   )r+   r   r	   r	   r
   
test_until  s    

zStateTestCase.test_untilc          	   C   sL   dt ddddddddig ffdd	d
g| _t| }|t W d Q R X d S )N)r"   r_   r   r   )r   r_   r   )r   r"   r_   r   )r   )i'  )rq   r   r   )r   )r   rZ   r   r   r   )r+   r   r	   r	   r
   test_until_with_too_large_count  s    

z-StateTestCase.test_until_with_too_large_countc          
   C   s<   dddddddddd	g
| _ t| }|t W d Q R X d S )
N)r"   r_   r   )r   )r"   r|   r   )rh   r   r   )r   )r   r|   r   )r   )r   )r"   r   r   )r   )rZ   r   r   r   )r+   r   r	   r	   r
   test_until_in_caller_frame  s    

z(StateTestCase.test_until_in_caller_framec          
   C   s\   d}t |i}t|> ddddg| _dt f}t| |d}|t W d Q R X W d Q R X d S )Nz9
            def main():
                lno = 3
        )r"   r_   r   )r   )r"   r|   r   )r   z
importlib*)rX   )r   r   rZ   r   r   r   )r+   rD   r   rX   r   r	   r	   r
   	test_skip  s    

zStateTestCase.test_skipc          	   C   s2   ddg| _ t| }| t|jt W d Q R X d S )N)r"   r_   r   )r   )rZ   r   assertRaisesr   r   r   )r+   r   r	   r	   r
   	test_down  s    

zStateTestCase.test_downc          	   C   s8   ddddddddg| _ t| }|t W d Q R X d S )N)r"   r_   r   )r   )r"   r|   r   )rh   r   r   )r   )r   r|   r   )r   )rZ   r   r   r   )r+   r   r	   r	   r
   test_up  s    

zStateTestCase.test_upN)r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r	   r	   r	   r
   r   H  s   

r   c               @   s`   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd ZdS )BreakpointTestCasezTest the breakpoint set method.c          	   C   s2   ddg| _ t| }| t|jt W d Q R X d S )N)r"   r_   r   )r   )z/non/existent/module.pyr   )rZ   r   r   r   r   r   )r+   r   r	   r	   r
   test_bp_on_non_existent_module  s    

z1BreakpointTestCase.test_bp_on_non_existent_modulec          
   C   sZ   d}t |i}t|< ddtdffg| _t| }| t|jt W d Q R X W d Q R X d S )Nz9
            def main():
                lno = 3
        )r"   r_   r   r   r   )	r   r   TEST_MODULE_FNAMErZ   r   r   r   r   r   )r+   rD   r   r   r	   r	   r
   test_bp_after_last_statement  s    

z/BreakpointTestCase.test_bp_after_last_statementc             C   s   d}t |i}t|r dtdtddtdtddddddd	d	id	gffddddd
d	id
gffdg
| _t| }|t W d Q R X W d Q R X d S )Nz
            def func():
                lno = 3

            def main():
                for i in range(2):
                    func()
        )r"   r_   r   rC   T)r   r_   r   )r   r"   r|   r   r_   )r   )r   r   r   r   rZ   r   r   r   )r+   rD   r   r   r	   r	   r
   test_temporary_bp  s    



z$BreakpointTestCase.test_temporary_bpc             C   s   d}t |i}t| dtdtdtdtddddddd	dd
d
ig ffddddddd	ddd
idgffddddd	dd
dig ffdg| _t| }|t W d Q R X W d Q R X d S )Nz
            def func():
                lno = 3

            def main():
                for i in range(3):
                    func()
        )r"   r_   r   rC   )r   r_   r   T)r   )r_   )r   r"   r|   r   )r   )r_   )r   r|   rC   )r   )r   r_   )r   )r   )r   )r   r   r   r   rZ   r   r   r   )r+   rD   r   r   r	   r	   r
   test_disabled_temporary_bp  s"    


z-BreakpointTestCase.test_disabled_temporary_bpc          
   C   sn   d}t |i}t|P dtdtdddddd	dd
d	ig ffdg| _t| }|t W d Q R X W d Q R X d S )Nz
            def func(a):
                lno = 3

            def main():
                for i in range(3):
                    func(i)
        )r"   r_   r   rC   Fza == 2)r   r_   r   )r   r"   r|   r   )r   )r   r   r   r   rZ   r   r   r   )r+   rD   r   r   r	   r	   r
   test_bp_condition9  s    

z$BreakpointTestCase.test_bp_conditionc          
   C   sn   d}t |i}t|P dtdtdddddd	dd
d
ig ffdg| _t| }|t W d Q R X W d Q R X d S )Nzk
            def func(a):
                lno = 3

            def main():
                func(0)
        )r"   r_   r   rC   Fz1 / 0)r   r_   r   )r   r"   r|   r   )r   )r   r   r   r   rZ   r   r   r   )r+   rD   r   r   r	   r	   r
   )test_bp_exception_on_condition_evaluationM  s    

z<BreakpointTestCase.test_bp_exception_on_condition_evaluationc             C   sn   d}t |i}t|P dtdtdddddddd	d
ig ffdg| _t| }|t W d Q R X W d Q R X d S )Nz
            def func():
                lno = 3

            def main():
                for i in range(2):
                    func()
        )r"   r_   r   rC   )r   r_   r   )r   )r   )r   r"   r|   r   r_   )r   )r   r   r   r   rZ   r   r   r   )r+   rD   r   r   r	   r	   r
   test_bp_ignore_count`  s    

z'BreakpointTestCase.test_bp_ignore_countc             C   s   d}t |i}t| dtdtdtdtdddddddd	dd
dig ffddddd	dd
d
ig ffddd	ddd
ig ffdg| _t| }|t W d Q R X W d Q R X d S )Nz
            def func():
                lno = 3

            def main():
                for i in range(3):
                    func()
        )r"   r_   r   rC   )r   r_   r   )r   )r   )r   )r   )r   r"   r|   r_   r   )r   )r   )r   r|   rC   )r   )r   r   r   r   rZ   r   r   r   )r+   rD   r   r   r	   r	   r
    test_ignore_count_on_disabled_bpu  s     

z3BreakpointTestCase.test_ignore_count_on_disabled_bpc             C   s   d}t |i}t| ddtdffddtdffddtdffddddd	d
d
ig ffdddd	dd
ig ffdtdffddddd	ddig ffdg| _t| }|t W d Q R X W d Q R X d S )Nz
            def func():
                lno = 3
                lno = 4

            def main():
                for i in range(3):
                    func()
        )r"   r_   r   r   r|   )r   r_   r   r   )r   r"   rC   r   r   )r   r   rC   r_   )r   )r   r   r   rZ   r   r   r   )r+   rD   r   r   r	   r	   r
   test_clear_two_bp_on_same_line  s    	

z1BreakpointTestCase.test_clear_two_bp_on_same_linec          	   C   s:   ddt dffg| _t| }| t|jt W d Q R X d S )N)r"   r_   r   r   r   )r4   rZ   r   r   r   r   r   )r+   r   r	   r	   r
   test_clear_at_no_bp  s    
z&BreakpointTestCase.test_clear_at_no_bpN)r   r   r   r   r   r   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 Zdd ZdS )RunTestCasez Test run, runeval and set_trace.c          	   C   sB   d}ddddg| _ t| }|tt|dd W d Q R X d S )Nz
            lno = 2
        )r"   r_   z<module>)r   )rq   r_   z<module>)r   z<string>exec)rZ   r   runcompiler   r   )r+   rD   r   r	   r	   r
   test_run_step  s
    

zRunTestCase.test_run_stepc             C   sl   d}t |i}t|N ddddddddddg
| _d	d l}t| }|d
t t  W d Q R X W d Q R X d S )Nz9
            def main():
                lno = 3
        )r"   r   z<module>)r   )rh   r_   r   )r"   r|   r   )rq   r|   r   )rq   r   z<module>)r   r   ztest_module_for_bdb.main())r   r   rZ   r   r   Zrunevalr5   locals)r+   rD   r   r   r   r	   r	   r
   test_runeval_step  s    


zRunTestCase.test_runeval_stepN)r   r   r   r   r   r  r	   r	   r	   r
   r     s   r   c               @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )IssuesTestCasezTest fixed bdb issues.c             C   sz   d}d}t |d|i}t|T dtddddd	d
dddig ffdddddg
| _t| }|t W d Q R X W d Q R X d S )Nz
            from test_module_for_bdb_2 import func
            def main():
                func()
                lno = 5
        z9
            def func():
                lno = 3
        Ztest_module_for_bdb_2)r"   r_   r   rC   ztest_module_for_bdb_2.py)r   r_   r   )r   r"   r|   r   )r   )rq   r|   rC   )r"   r   r   )r   )r   r   r   rZ   r   r   r   )r+   Zcode_1Zcode_2r   r   r	   r	   r
   +test_step_at_return_with_no_trace_in_caller  s    	


z:IssuesTestCase.test_step_at_return_with_no_trace_in_callerc             C   s   d}t |i}xdD ]}| j|d t| dtdtdddd	dd
d
ig ff|fg| _|dkr~| jdddtfdddg n| jddg t| }|	t
 W d Q R X W d Q R X W d Q R X qW d S )Nz
            def test_gen():
                yield 0
                lno = 4
                return 123

            def main():
                it = test_gen()
                next(it)
                next(it)
                lno = 11
        )r   r   rq   )r   )r"   r_   r   test_gen)r   r_   r   )r   r"   r|   r   rq   rf   
   r   )r   )rq   r  r   )r   )r"   r   r  )r   r   r   r   r   rZ   r   r   r   r   r   )r+   rD   r   r   r   r	   r	   r
   #test_next_until_return_in_generator  s"    



z2IssuesTestCase.test_next_until_return_in_generatorc             C   s   d}t |i}t|h dtdtdddddddig ffd	d
d	dd	dd	dddtfdddg| _t| }|t W d Q R X W d Q R X d S )Nz
            def test_gen():
                yield 0
                lno = 4
                yield 1
                return 123

            def main():
                for i in test_gen():
                    lno = 10
                lno = 11
        )r"   r_   r   r  )r   r_   r   )r   r"   r|   r   )r   )r"   r   r  )r"   r   r  )r"   r   r  rf   	   r   )r   )r"      r   )r   )	r   r   r   r   r   rZ   r   r   r   )r+   rD   r   r   r	   r	   r
   'test_next_command_in_generator_for_loop  s    


z6IssuesTestCase.test_next_command_in_generator_for_loopc             C   s~   d}t |i}t|` dtdtdddddddig ffd	d
d	dddtfdddg| _t| }|t W d Q R X W d Q R X d S )Na4  
            def test_subgen():
                yield 0
                return 123

            def test_gen():
                x = yield from test_subgen()
                return 456

            def main():
                for i in test_gen():
                    lno = 12
                lno = 13
        )r"   r_   r   r  )r   r_   r   )r   r"   r   r   )r   )r"      r  rf   r  r   )r   )r"      r   )r   )	r   r   r   r   r   rZ   r   r   r   )r+   rD   r   r   r	   r	   r
   /test_next_command_in_generator_with_subiterator;  s    


z>IssuesTestCase.test_next_command_in_generator_with_subiteratorc             C   s   d}t |i}t|h dtdtdddddddig ffd	d
ddtfd	d
ddtfdddg| _t| }|t W d Q R X W d Q R X d S )Na4  
            def test_subgen():
                yield 0
                return 123

            def test_gen():
                x = yield from test_subgen()
                return 456

            def main():
                for i in test_gen():
                    lno = 12
                lno = 13
        )r"   r_   r   Ztest_subgen)r   r_   r   )r   r"   r|   r   )rq   rf   r   r  r  r   )r   )r"   r  r   )r   )	r   r   r   r   r   rZ   r   r   r   )r+   rD   r   r   r	   r	   r
   1test_return_command_in_generator_with_subiteratorZ  s    


z@IssuesTestCase.test_return_command_in_generator_with_subiteratorN)	r   r   r   r   r  r  r	  r  r  r	   r	   r	   r
   r    s   )r  c               C   s   t jtttt d S )N)r   r   Zrun_unittestr   r   r   r  r	   r	   r	   r
   	test_mainy  s
    r  __main__)N)-r   Zbdbr   r   r   Zunittestr   r6   r   
contextlibr   	itertoolsr   r   Ztest.supportr   	Exceptionr   r   r   r   r[   r   r&   r'   rW   r   r   r   r4   r   r   r   r   r   r   r   ZTestCaser   r   r   r   r  r  r   r	   r	   r	   r
   <module>1   sR   @ z.
J
 ( B" (
