
    }e8                     `   d 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 ddl	m
Z
 ddlmZ ddlmZ ddlmZ ddlZ G d	 d
e      Z G d de      Zd Zd Zd Zd Zd Zed        ZdZd Zej:                  j=                  d      ed               Zej:                  j=                  d      ej:                  jA                   ejB                         dk(  d      ed                      Z"ej:                  jA                   ejB                         dk(  d      ed               Z#ed        Z$y)zTests for debugging machinery.
    N)NamedTemporaryFile)dedent)patch)debugger)IPYTHON_TESTING_TIMEOUT_SCALE)
skip_win32c                       e Zd ZdZd Zd Zy)
_FakeInputa  
    A fake input stream for pdb's interactive debugger.  Whenever a
    line is read, print it (to simulate the user typing it), and then
    return it.  The set of lines to return is specified in the
    constructor; they should not have trailing newlines.
    c                 $    t        |      | _        y N)iterlines)selfr   s     B/usr/lib/python3/dist-packages/IPython/core/tests/test_debugger.py__init__z_FakeInput.__init__    s    %[
    c                 L    t        | j                        }t        |       |dz   S )N
)nextr   print)r   lines     r   readlinez_FakeInput.readline#   s!    DJJdDyr   N)__name__
__module____qualname____doc__r   r    r   r   r
   r
      s    !r   r
   c                   "    e Zd ZdZd Zd Zd Zy)PdbTestInputz:Context manager that makes testing Pdb in doctests easier.c                     || _         y r   )input)r   r!   s     r   r   zPdbTestInput.__init__+   s	    
r   c                 j    t         j                  | _        t        | j                        t         _        y r   )sysstdin
real_stdinr
   r!   )r   s    r   	__enter__zPdbTestInput.__enter__.   s    ))tzz*	r   c                 .    | j                   t        _        y r   )r%   r#   r$   )r   excs     r   __exit__zPdbTestInput.__exit__2   s    OO	r   N)r   r   r   r   r   r&   r)   r   r   r   r   r   (   s    D+$r   r   c                       y)a	  Test calling some IPython magics from ipdb.

    First, set up some test functions and classes which we can inspect.

    >>> class ExampleClass(object):
    ...    """Docstring for ExampleClass."""
    ...    def __init__(self):
    ...        """Docstring for ExampleClass.__init__"""
    ...        pass
    ...    def __str__(self):
    ...        return "ExampleClass()"

    >>> def example_function(x, y, z="hello"):
    ...     """Docstring for example_function."""
    ...     pass

    >>> old_trace = sys.gettrace()

    Create a function which triggers ipdb.

    >>> def trigger_ipdb():
    ...    a = ExampleClass()
    ...    debugger.Pdb().set_trace()

    >>> with PdbTestInput([
    ...    'pdef example_function',
    ...    'pdoc ExampleClass',
    ...    'up',
    ...    'down',
    ...    'list',
    ...    'pinfo a',
    ...    'll',
    ...    'continue',
    ... ]):
    ...     trigger_ipdb()
    --Return--
    None
    > <doctest ...>(3)trigger_ipdb()
          1 def trigger_ipdb():
          2    a = ExampleClass()
    ----> 3    debugger.Pdb().set_trace()
    <BLANKLINE>
    ipdb> pdef example_function
     example_function(x, y, z='hello')
     ipdb> pdoc ExampleClass
    Class docstring:
        Docstring for ExampleClass.
    Init docstring:
        Docstring for ExampleClass.__init__
    ipdb> up
    > <doctest ...>(11)<module>()
          7    'pinfo a',
          8    'll',
          9    'continue',
         10 ]):
    ---> 11     trigger_ipdb()
    <BLANKLINE>
    ipdb> down
    None
    > <doctest ...>(3)trigger_ipdb()
          1 def trigger_ipdb():
          2    a = ExampleClass()
    ----> 3    debugger.Pdb().set_trace()
    <BLANKLINE>
    ipdb> list
          1 def trigger_ipdb():
          2    a = ExampleClass()
    ----> 3    debugger.Pdb().set_trace()
    <BLANKLINE>
    ipdb> pinfo a
    Type:           ExampleClass
    String form:    ExampleClass()
    Namespace:      Local...
    Docstring:      Docstring for ExampleClass.
    Init docstring: Docstring for ExampleClass.__init__
    ipdb> ll
          1 def trigger_ipdb():
          2    a = ExampleClass()
    ----> 3    debugger.Pdb().set_trace()
    <BLANKLINE>
    ipdb> continue
    
    Restore previous trace function, e.g. for coverage.py    
    
    >>> sys.settrace(old_trace)
    Nr   r   r   r   test_ipdb_magicsr+   9       r   c                       y)a  Test ipdb with a very short function.
    
    >>> old_trace = sys.gettrace()

    >>> def bar():
    ...     pass

    Run ipdb.

    >>> with PdbTestInput([
    ...    'continue',
    ... ]):
    ...     debugger.Pdb().runcall(bar)
    > <doctest ...>(2)bar()
          1 def bar():
    ----> 2    pass
    <BLANKLINE>
    ipdb> continue
    
    Restore previous trace function, e.g. for coverage.py    
    
    >>> sys.settrace(old_trace)
    Nr   r   r   r   test_ipdb_magics2r.      r,   r   c                       y)a  Test that quit work in ipydb

    >>> old_trace = sys.gettrace()

    >>> def bar():
    ...     pass

    >>> with PdbTestInput([
    ...    'quit',
    ... ]):
    ...     debugger.Pdb().runcall(bar)
    > <doctest ...>(2)bar()
            1 def bar():
    ----> 2    pass
    <BLANKLINE>
    ipdb> quit

    Restore previous trace function, e.g. for coverage.py

    >>> sys.settrace(old_trace)
    Nr   r   r   r   can_quitr0      r,   r   c                       y)a  Test that quit work in ipydb

    >>> old_trace = sys.gettrace()

    >>> def bar():
    ...     pass

    >>> with PdbTestInput([
    ...    'exit',
    ... ]):
    ...     debugger.Pdb().runcall(bar)
    > <doctest ...>(2)bar()
            1 def bar():
    ----> 2    pass
    <BLANKLINE>
    ipdb> exit

    Restore previous trace function, e.g. for coverage.py

    >>> sys.settrace(old_trace)
    Nr   r   r   r   can_exitr2      r,   r   c                  >   ddgfd} t        j                         }	 t        j                  t        d|       5  t        j                         j                          ddd       t        j                  |       y# 1 sw Y   xY w# t        j                  |       w xY w)zThe debugger can be interrupted.

    The presumption is there is some mechanism that causes a KeyboardInterrupt
    (this is implemented in ipykernel).  We want to ensure the
    KeyboardInterrupt cause debugging to cease.
     r   c                 N    |dxx   dz  cc<   |d   dk(  sJ d       t               )Nr      z#input() should only be called once!)KeyboardInterrupt)msgcalleds     r   raising_inputz7test_interruptible_core_debugger.<locals>.raising_input   s.    q	Q	ayA~DDD~!!r   r!   N)	r#   gettracer   objectbuiltinsr   InterruptiblePdb	set_tracesettrace)r:   tracer_origs     r    test_interruptible_core_debuggerrB      sy     aS "
 ,,.K
"\\(G]; 	4%%'113	4 	[!	4 	4 	[!s"   B #A9B 9B>B Bc                  v   ddl } t        j                  j                         }d|d<   | j	                  t
        j                  g d|      }dt        z  |_        |j                  d       |j                  d	       |j                  d
       t        d      }|j                         D ]$  }|j                  |       |j                  |       & |j                  d       t        d      }|j                         D ]$  }|j                  |       |j                  |       & |j                  d       |j                  d       |j                  d       |j                  d       |j                  d       |j                  d       |j                  d       |j                  d       |j                          y)zRthat xmode skip frames

    Not as a doctest as pytest does not run doctests.
    r   N1IPY_TEST_SIMPLE_PROMPTz-mIPythonz--colors=nocolorenv   rG   r   In [1]zo
    def f():
        __tracebackhide__ = True
        g()

    def g():
        raise ValueError

    f()
    skippingz
    def f():
        __tracebackhide__ = True
        g()

    def g():
        from IPython.core.debugger import set_trace
        set_trace()

    f()
    ipdb>whiddenzskip_hidden false	__tracebapexpectosenvironcopyspawnr#   
executabler   timeoutexpectexpect_exactr   
splitlinessendlinecloserR   rI   childblockr   s        r   test_xmode_skipra      sx    
**//
C$'C !MM=3  E 66EM	LL	LL	x 		E   " !t4 ! 
z"
	E   " !t4 ! 
LL	NN3	LL	LL	NN&'	NN3	LL	LL	KKMr   )	zU
    def helpers_helper():
        pass # should not stop here except breakpoint
    zI
    def helper_1():
        helpers_helper() # should not stop here
    z=
    def helper_2():
        pass # should not stop here
    aV  
    def pdb_skipped_decorator2(function):
        def wrapped_fn(*args, **kwargs):
            __debuggerskip__ = True
            helper_2()
            __debuggerskip__ = False
            result = function(*args, **kwargs)
            __debuggerskip__ = True
            helper_2()
            return result
        return wrapped_fn
    aU  
    def pdb_skipped_decorator(function):
        def wrapped_fn(*args, **kwargs):
            __debuggerskip__ = True
            helper_1()
            __debuggerskip__ = False
            result = function(*args, **kwargs)
            __debuggerskip__ = True
            helper_2()
            return result
        return wrapped_fn
    zd
    @pdb_skipped_decorator
    @pdb_skipped_decorator2
    def bar(x, y):
        return x * y
    z(import IPython.terminal.debugger as ipdbz=
    def f():
        ipdb.set_trace()
        bar(3, 4)
    z
    f()
    c                  d   dd l } t        j                  j                         }d|d<   d|d<   | j	                  t
        j                  g d|      }dt        z  |_        |j                  d       |j                  d	       d
t        z  |_        d|_
        t        D cg c]  }t        |      j                          }}d}|D ]d  }|j                  d| d       |dz  }|j                         D ]$  }|j!                  |       |j                  |       & |j!                  d       f |S c c}w )Nr   rD   rE   PROMPT_TOOLKIT_NO_CPRrF   rH   rJ   rG   r        r6   In []:r4   )rR   rS   rT   rU   rV   r#   rW   r   rX   rY   str_last_charsskip_decorators_blocksr   striprZ   r[   r\   )rR   rI   r_   bdedented_blocksin_prompt_numbercblockr   s           r   _decorator_skip_setupro   n  s*   
**//
C$'C !#&C MM=3  E 66EM	LL	LL55EME2HIQvay(IOI! T"2!3267A%%' 	%DNN4 t$	% 	r L Js    D-z&recently fail for unknown reason on CI)reasonc                     t               } | j                  d       | j                  d       | j                  d       | j                  d       | j                  d       | j                  d       | j                  d       | j                  d       | j                  d       | j                  d       | j                  d	       | j	                          y
)z*test that decorator frames can be skipped.zipython-input-83     bar(3, 4)rM   stepz--Call--zipython-input-6z1 @pdb_skipped_decoratorszreturn x * yNro   rZ   rY   r\   r]   )r_   s    r   test_decorator_skiprv     s    
 "#E	()	()	LL	LL	NN6	v	z"	()	12	NN3	~&	KKMr   PyPyzissues on PyPyc                      t               } | j                  d       dD ]I  \  }}| j                  d       | j                  |       | j                  |       | j                  |       K | j	                          y)2test that decorator frame skipping can be disabledrr   )	)z"skip_predicates debuggerskip Falser4   )skip_predicateszdebuggerskip : False)rs   z---> 2     def wrapped_fn)rs   z ----> 3         __debuggerskip__)rs   z----> 4         helper_1())rs   z---> 1 def helper_1():)r   z----> 2     helpers_helper())r   z
--Return--)r   z(----> 5         __debuggerskip__ = FalserM   Nru   )r_   input_expecteds      r   test_decorator_skip_disabledr}     sr     "#E	()
 % 	Wv6"8$%  
KKMr   c                  V   ddl } t        j                  j                         }d|d<   d|d<   | j	                  t
        j                  g d|      }dt        z  |_        d	|_	        |j                  d
       |j                  d       dt        z  |_        t        ddd      5 }|j                  dd j                  d      d   }|j                  dj                  t         dd D cg c]  }t#        |       c}      j%                                |j'                          d| d}|dg}d}|D ]d  }	|j)                  d| d       |dz  }|	j+                         D ]$  }
|j-                  |
       |j)                  |
       & |j-                  d       f |j)                  d       d| ddfddd fD ]I  \  }}|j                  d!       |j-                  |       |j)                  |       |j)                  |       K 	 ddd       |j/                          yc c}w # 1 sw Y   xY w)"ry   r   NrD   rE   rc   rF   rH   rJ   re   rG   r   rd   z.py.T)suffixdirdelete/zfrom z	 import fzf()r6   rf   rg   r4   z47     bar(3, 4)zb z.py:3)rs   z.1---> 3     pass # should not stop here except)rs   z---> 38 @pdb_skipped_decorator)continuer4   rM   )rR   rS   rT   rU   rV   r#   rW   r   rX   rh   rY   r   namesplitwritejoinri   r   encodeflushrZ   r[   r\   r]   )rR   rI   r_   tfr   x	codeblockrl   rm   rn   r   r{   r|   s                r   #test_decorator_skip_with_breakpointr     s.   
 
**//
C$'C !#&C MM=3  E 66EME	LL	LL55EM 
5c$	?  )2wws|!!#&r*
/Ecr/JK!F1IKLSSUV

D6+	 

 % 	F&6%7r:;!))+ )t$""4() NN2	 	-. $ur"F6	!
 		)FH LL!NN6"v&x(		)/ )D 
KKMA L )  )s    $AH'H9DHHH(c                     ddl } t        j                  j                         }d|d<   | j	                  t
        j                  g d|      }dt        z  |_        |j                  d       |j                  d	       |j                  d
       t        d      }|j                         D ]$  }|j                  |       |j                  |       & |j                  d       |j                  d       |j                  d       |j                  d       |j                  d       |j                  d       |j                  d       |j                  d       |j                  d       |j                  d       |j                  d       |j                  d       |j                  d       |j                  d       |j                  d       |j                  d       |j                  d       |j                          y)z<Test that `where` does not access f_locals and erase values.r   NrD   rE   rF   rH   rJ   rG   r   rK   zs
    def simple_f():
         myvar = 1
         print(myvar)
         1/0
         print(myvar)
    simple_f()    ZeroDivisionErrorzIn [2]:z%debugrM   myvarz	myvar = 22whererQ   r^   s        r   test_where_erase_valuer     s    
**//
C$'C !MM=3  E 66EM	LL	LL	x 	E   " !t4 ! 
*+	y!	NN8 
LL	NN7	LL 
LL	NN; 
w	NN7	s 
LL	NN7 
LL	NN7	s	LL	KKMr   )%r   r=   rS   r#   platformtempfiler   textwrapr   unittest.mockr   IPython.corer   IPython.testingr   IPython.testing.decoratorsr   pytestr<   r
   r   r+   r.   r0   r2   rB   ra   ri   ro   markskiprv   skipifpython_implementationr}   r   r   r   r   r   <module>r      sj    	 
  '   ! 9 1  $6 $"Vp200"4 ? ?D4 n< AB  C. AB2H224>GWX  Y C4 2H224>GWX7  Y7t ? ?r   