o
    û›hÿ;  ã                   @   sn  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
„ d
eƒZG dd„ deƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zedd„ ƒZdZdd„ Zejjdd�edd„ ƒƒZejjdd�ejj e !¡ d kd!d�ed"d#„ ƒƒƒZ"ejj#ej$j%d$vd%d&d'�ejj e !¡ d kd!d�ed(d)„ ƒƒƒZ&ed*d+„ ƒZ'dS ),zTests for debugging machinery.
é    N)ÚNamedTemporaryFile©Údedent)Úpatch)Údebugger)ÚIPYTHON_TESTING_TIMEOUT_SCALE)Ú
skip_win32c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú
_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                 C   s   t |ƒ| _d S ©N)ÚiterÚlines)Úselfr   © r   úVc:\Users\shubh\Videos\agent\nova\Lib\site-packages\IPython/core/tests/test_debugger.pyÚ__init__    s   z_FakeInput.__init__c                 C   s   t | jƒ}t|ƒ |d S )NÚ
)Únextr   Úprint)r   Úliner   r   r   Úreadline#   s   
z_FakeInput.readlineN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__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 )	ÚPdbTestInputz:Context manager that makes testing Pdb in doctests easier.c                 C   s
   || _ d S r
   )Úinput)r   r   r   r   r   r   +   s   
zPdbTestInput.__init__c                 C   s   t j| _t| jƒt _d S r
   )ÚsysÚstdinÚ
real_stdinr	   r   )r   r   r   r   Ú	__enter__.   s   zPdbTestInput.__enter__c                 G   s   | j t_d S r
   )r   r   r   )r   Úexcr   r   r   Ú__exit__2   s   zPdbTestInput.__exit__N)r   r   r   r   r   r   r!   r   r   r   r   r   (   s
    r   c                   C   ó   dS )a}
  Test calling some IPython magics from ipdb.

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

    In [1]: class ExampleClass(object):
       ...:    """Docstring for ExampleClass."""
       ...:    def __init__(self):
       ...:        """Docstring for ExampleClass.__init__"""
       ...:        pass
       ...:    def __str__(self):
       ...:        return "ExampleClass()"

    In [2]: def example_function(x, y, z="hello"):
       ...:     """Docstring for example_function."""
       ...:     pass

    In [3]: old_trace = sys.gettrace()

    Create a function which triggers ipdb.

    In [4]: def trigger_ipdb():
       ...:    a = ExampleClass()
       ...:    debugger.Pdb().set_trace()

    Run ipdb with faked input & check output. Because of a difference between
    Python 3.13 & older versions, the first bit of the output is inconsistent.
    We need to use ... to accommodate that, so the examples have to use IPython
    prompts so that ... is distinct from the Python PS2 prompt.

    In [5]: with PdbTestInput([
       ...:    'pdef example_function',
       ...:    'pdoc ExampleClass',
       ...:    'up',
       ...:    'down',
       ...:    'list',
       ...:    'pinfo a',
       ...:    'll',
       ...:    'continue',
       ...: ]):
       ...:     trigger_ipdb()
    ...> <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...
    > <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

    In [6]: sys.settrace(old_trace)
    Nr   r   r   r   r   Útest_ipdb_magics9   ó    r#   c                   C   r"   )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   r   Útest_ipdb_magics2“   r$   r%   c                   C   r"   )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   r   Úcan_quit¬   r$   r&   c                   C   r"   )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   r   Úcan_exitÄ   r$   r'   c               	   C   s€   ddgfdd„} t  ¡ }z-t td| ¡� t ¡  ¡  W d  ƒ n1 s%w   Y  W t  |¡ dS W t  |¡ dS t  |¡ w )zäThe 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                 S   s*   |d  d7  < |d dksJ dƒ‚t ƒ ‚)Nr   é   z#input() should only be called once!)ÚKeyboardInterrupt)ÚmsgÚcalledr   r   r   Úraising_inputã   s   z7test_interruptible_core_debugger.<locals>.raising_inputr   N)	r   Úgettracer   ÚobjectÚbuiltinsr   ZInterruptiblePdbÚ	set_traceÚsettrace)r-   Ztracer_origr   r   r   Ú test_interruptible_core_debuggerÜ   s   ÿ	÷	r3   c                  C   s  ddl } tj ¡ }d|d< | jtjg d¢|d�}dt |_| 	d¡ | 	d	¡ | 
d
¡ tdƒ}| ¡ D ]}| |¡ | 
|¡ q4| 
d¡ tdƒ}| ¡ D ]}| |¡ | 
|¡ qN| 	d¡ | d¡ | 	d¡ | 	d¡ | d¡ | d¡ | 	d¡ | 	d¡ | ¡  dS )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é   r7   r   úIn [1]zo
    def f():
        __tracebackhide__ = True
        g()

    def g():
        raise ValueError

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

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

    f()
    úipdb>ÚwZhiddenzskip_hidden falseZ	__traceba©ÚpexpectÚosÚenvironÚcopyÚspawnr   Ú
executabler   ÚtimeoutÚexpectÚexpect_exactr   Ú
splitlinesÚsendlineÚclose©r?   r9   ÚchildÚblockr   r   r   r   Útest_xmode_skipö   s@   
ÿ



ÿ

ÿ








rN   )	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                  C   sÌ   dd l } tj ¡ }d|d< d|d< | jtjg d¢|d�}dt |_| 	d¡ | 	d	¡ d
t |_d|_
dd„ tD ƒ}d}|D ]%}| d|› d�¡ |d7 }| ¡ D ]}| |¡ | |¡ qQ| d¡ q>|S )Nr   r4   r5   ÚPROMPT_TOOLKIT_NO_CPRr6   r8   r:   r7   r   é   éô  c                 S   s   g | ]}t |ƒ ¡ ‘qS r   )r   Ústrip)Ú.0Úbr   r   r   Ú
<listcomp>‚  s    z)_decorator_skip_setup.<locals>.<listcomp>r)   úIn [ú]:r(   )r?   r@   rA   rB   rC   r   rD   r   rE   rF   Ústr_last_charsÚskip_decorators_blocksrG   rH   rI   )r?   r9   rL   Údedented_blocksÚin_prompt_numberÚcblockr   r   r   r   Ú_decorator_skip_setupp  s,   
ÿ




r]   z&recently fail for unknown reason on CI)Úreasonc                  C   s€   t ƒ } |  d¡ |  d¡ |  d¡ |  d¡ |  d¡ |  d¡ |  d¡ |  d¡ |  d¡ |  d¡ |  d	¡ |  ¡  d
S )z*test that decorator frames can be skipped.zipython-input-8ú3     bar(3, 4)r<   Ústepz--Call--zipython-input-6z1 @pdb_skipped_decoratorÚszreturn x * yN©r]   rG   rF   rI   rJ   )rL   r   r   r   Útest_decorator_skipŽ  s   










rc   ÚPyPyzissues on PyPyc                  C   sR   t ƒ } |  d¡ dD ]\}}|  d¡ |  |¡ |  |¡ |  |¡ q
|  ¡  dS )ú2test that decorator frame skipping can be disabledr_   )	)z"skip_predicates debuggerskip Falser(   )Zskip_predicateszdebuggerskip : False)r`   z---> 2     def wrapped_fn)r`   z ----> 3         __debuggerskip__)r`   z----> 4         helper_1())r`   z---> 1 def helper_1():)r   z----> 2     helpers_helper())r   z
--Return--)r   z(----> 5         __debuggerskip__ = Falser<   Nrb   )rL   Úinput_Úexpectedr   r   r   Útest_decorator_skip_disabled§  s   



rh   )ÚfinalÚ	candidatezfails on 3.13.devT)r^   Ústrictc               	   C   sÈ  ddl } tj ¡ }d|d< d|d< | jtjg d¢|d�}dt |_d	|_	| 
d
¡ | 
d¡ dt |_tdddd��œ}|jdd…  d¡d }| d dd„ tdd… D ƒ¡ ¡ ¡ | ¡  d|› d�}|dg}d}|D ]%}| d|› d�¡ |d7 }| ¡ D ]}	| |	¡ | |	¡ q| d¡ qltjdkr | d¡ dg}
n| d ¡ g }
d!|› d"�dfg|
 g d#¢ D ]\}}| 
d$¡ | |¡ | |¡ | |¡ q¶W d  ƒ n1 sÙw   Y  | ¡  dS )%re   r   Nr4   r5   rO   r6   r8   r:   rQ   r7   r   rP   z.pyÚ.T)ÚsuffixÚdirÚdeleteéýÿÿÿú/éÿÿÿÿc                 S   s   g | ]}t |ƒ‘qS r   r   )rS   Úxr   r   r   rU   â  s    z7test_decorator_skip_with_breakpoint.<locals>.<listcomp>zfrom z	 import fzf()r)   rV   rW   r(   )é   é   z--> 46     ipdb.set_trace())r`   ú--> 47     bar(3, 4)rv   zb z.py:3))r`   z.1---> 3     pass # should not stop here except)r`   z---> 38 @pdb_skipped_decorator)Úcontinuer(   r<   )r?   r@   rA   rB   rC   r   rD   r   rE   rX   rF   r   ÚnameÚsplitÚwriteÚjoinrY   ÚencodeÚflushrG   rH   rI   Úversion_inforJ   )r?   r9   rL   Útfrx   Z	codeblockrZ   r[   r\   r   Z
extra_steprf   rg   r   r   r   Ú#test_decorator_skip_with_breakpointÄ  s^   

ÿ



&þ



ÿýü
ÿ


òã-r€   c                  C   s8  ddl } tj ¡ }d|d< | jtjg d¢|d�}dt |_| 	d¡ | 	d	¡ | 
d
¡ tdƒ}| ¡ D ]}| |¡ | 
|¡ q4| 
d¡ | 
d¡ | d¡ | 	d¡ | d¡ | 	d¡ | 	d¡ | d¡ | 
d¡ | d¡ | 
d¡ | 	d¡ | d¡ | 	d¡ | d¡ | 
d¡ | 	d¡ | ¡  dS )z<Test that `where` does not access f_locals and erase values.r   Nr4   r5   r6   r8   r:   r7   r   r;   zs
    def simple_f():
         myvar = 1
         print(myvar)
         1/0
         print(myvar)
    simple_f()    ÚZeroDivisionErrorzIn [2]:z%debugr<   Zmyvarz	myvar = 2Ú2Úwherer>   rK   r   r   r   Útest_where_erase_value  sD   
ÿ



ÿ


















r„   )(r   r0   r@   r   ÚplatformÚtempfiler   Útextwrapr   Zunittest.mockr   ZIPython.corer   ZIPython.testingr   ZIPython.testing.decoratorsr   Zpytestr/   r	   r   r#   r%   r&   r'   r3   rN   rY   r]   ÚmarkÚskiprc   ZskipifÚpython_implementationrh   Zxfailr~   Úreleaselevelr€   r„   r   r   r   r   Ú<module>   sP    Z
B7
ýE