Format: application/x-python-code
⬇ Download File (13.6 KB)
   1  o
   2  
   3  ��h�;�@sndZddlZddlZddlZddlZddlmZddlmZddl	m
   4  Z
   5  ddlmZddl
   6  mZddlmZddlZGd	d
   7  �d
   8  e�ZGdd�de�Zd
   9  d�Zdd�Zdd�Zdd�Zdd�Zedd��ZdZdd�Zejjdd�edd���Zejjdd�ejj e�!�d kd!d�ed"d#����Z"ejj#ej$j%d$vd%d&d'�ejj e�!�d kd!d�ed(d)����Z&ed*d+��Z'dS),zTests for debugging machinery.
  10  �N)�NamedTemporaryFile��dedent)�patch)�debugger)�IPYTHON_TESTING_TIMEOUT_SCALE)�
  11  skip_win32c@s eZdZdZdd�Zdd�ZdS)�
  12  _FakeInputa
  13      A fake input stream for pdb's interactive debugger.  Whenever a
  14      line is read, print it (to simulate the user typing it), and then
  15      return it.  The set of lines to return is specified in the
  16      constructor; they should not have trailing newlines.
  17      cCst|�|_dS�N)�iter�lines)�selfr�r�Vc:\Users\shubh\Videos\agent\nova\Lib\site-packages\IPython/core/tests/test_debugger.py�__init__ sz_FakeInput.__init__cCst|j�}t|�|dS)N�
  18  )�nextr�print)r
  19  �linerrr�readline#s
  20  z_FakeInput.readlineN)�__name__�
  21  __module__�__qualname__�__doc__rrrrrrr	sr	c@s(eZdZdZdd�Zdd�Zdd�ZdS)	�PdbTestInputz:Context manager that makes testing Pdb in doctests easier.cCs
  22  ||_dSr
  23  )�input)r
  24  rrrrr+s
  25  zPdbTestInput.__init__cCstj|_t|j�t_dSr
  26  )�sys�stdin�
  27  real_stdinr	r)r
  28  rrr�	__enter__.szPdbTestInput.__enter__cGs|jt_dSr
  29  )rrr)r
  30  �excrrr�__exit__2szPdbTestInput.__exit__N)rrrrrrr!rrrrr(s
  31  rcC�dS)a}
  32  Test calling some IPython magics from ipdb.
  33  
  34      First, set up some test functions and classes which we can inspect.
  35  
  36      In [1]: class ExampleClass(object):
  37         ...:    """Docstring for ExampleClass."""
  38         ...:    def __init__(self):
  39         ...:        """Docstring for ExampleClass.__init__"""
  40         ...:        pass
  41         ...:    def __str__(self):
  42         ...:        return "ExampleClass()"
  43  
  44      In [2]: def example_function(x, y, z="hello"):
  45         ...:     """Docstring for example_function."""
  46         ...:     pass
  47  
  48      In [3]: old_trace = sys.gettrace()
  49  
  50      Create a function which triggers ipdb.
  51  
  52      In [4]: def trigger_ipdb():
  53         ...:    a = ExampleClass()
  54         ...:    debugger.Pdb().set_trace()
  55  
  56      Run ipdb with faked input & check output. Because of a difference between
  57      Python 3.13 & older versions, the first bit of the output is inconsistent.
  58      We need to use ... to accommodate that, so the examples have to use IPython
  59      prompts so that ... is distinct from the Python PS2 prompt.
  60  
  61      In [5]: with PdbTestInput([
  62         ...:    'pdef example_function',
  63         ...:    'pdoc ExampleClass',
  64         ...:    'up',
  65         ...:    'down',
  66         ...:    'list',
  67         ...:    'pinfo a',
  68         ...:    'll',
  69         ...:    'continue',
  70         ...: ]):
  71         ...:     trigger_ipdb()
  72      ...> <doctest ...>(3)trigger_ipdb()
  73            1 def trigger_ipdb():
  74            2    a = ExampleClass()
  75      ----> 3    debugger.Pdb().set_trace()
  76      <BLANKLINE>
  77      ipdb> pdef example_function
  78       example_function(x, y, z='hello')
  79       ipdb> pdoc ExampleClass
  80      Class docstring:
  81          Docstring for ExampleClass.
  82      Init docstring:
  83          Docstring for ExampleClass.__init__
  84      ipdb> up
  85      > <doctest ...>(11)<module>()
  86            7    'pinfo a',
  87            8    'll',
  88            9    'continue',
  89           10 ]):
  90      ---> 11     trigger_ipdb()
  91      <BLANKLINE>
  92      ipdb> down...
  93      > <doctest ...>(3)trigger_ipdb()
  94            1 def trigger_ipdb():
  95            2    a = ExampleClass()
  96      ----> 3    debugger.Pdb().set_trace()
  97      <BLANKLINE>
  98      ipdb> list
  99            1 def trigger_ipdb():
 100            2    a = ExampleClass()
 101      ----> 3    debugger.Pdb().set_trace()
 102      <BLANKLINE>
 103      ipdb> pinfo a
 104      Type:           ExampleClass
 105      String form:    ExampleClass()
 106      Namespace:      Local...
 107      Docstring:      Docstring for ExampleClass.
 108      Init docstring: Docstring for ExampleClass.__init__
 109      ipdb> ll
 110            1 def trigger_ipdb():
 111            2    a = ExampleClass()
 112      ----> 3    debugger.Pdb().set_trace()
 113      <BLANKLINE>
 114      ipdb> continue
 115  
 116      Restore previous trace function, e.g. for coverage.py
 117  
 118      In [6]: sys.settrace(old_trace)
 119      Nrrrrr�test_ipdb_magics9�r#cCr")a�Test ipdb with a very short function.
 120      
 121      >>> old_trace = sys.gettrace()
 122  
 123      >>> def bar():
 124      ...     pass
 125  
 126      Run ipdb.
 127  
 128      >>> with PdbTestInput([
 129      ...    'continue',
 130      ... ]):
 131      ...     debugger.Pdb().runcall(bar)
 132      > <doctest ...>(2)bar()
 133            1 def bar():
 134      ----> 2    pass
 135      <BLANKLINE>
 136      ipdb> continue
 137      
 138      Restore previous trace function, e.g. for coverage.py    
 139      
 140      >>> sys.settrace(old_trace)
 141      Nrrrrr�test_ipdb_magics2�r$r%cCr")a�Test that quit work in ipydb
 142  
 143      >>> old_trace = sys.gettrace()
 144  
 145      >>> def bar():
 146      ...     pass
 147  
 148      >>> with PdbTestInput([
 149      ...    'quit',
 150      ... ]):
 151      ...     debugger.Pdb().runcall(bar)
 152      > <doctest ...>(2)bar()
 153              1 def bar():
 154      ----> 2    pass
 155      <BLANKLINE>
 156      ipdb> quit
 157  
 158      Restore previous trace function, e.g. for coverage.py
 159  
 160      >>> sys.settrace(old_trace)
 161      Nrrrrr�can_quit�r$r&cCr")a�Test that quit work in ipydb
 162  
 163      >>> old_trace = sys.gettrace()
 164  
 165      >>> def bar():
 166      ...     pass
 167  
 168      >>> with PdbTestInput([
 169      ...    'exit',
 170      ... ]):
 171      ...     debugger.Pdb().runcall(bar)
 172      > <doctest ...>(2)bar()
 173              1 def bar():
 174      ----> 2    pass
 175      <BLANKLINE>
 176      ipdb> exit
 177  
 178      Restore previous trace function, e.g. for coverage.py
 179  
 180      >>> sys.settrace(old_trace)
 181      Nrrrrr�can_exit�r$r'c	Cs�ddgfdd�}t��}z-t�td|��t����Wd�n1s%wYWt�|�dSWt�|�dSt�|�w)z�The debugger can be interrupted.
 182  
 183      The presumption is there is some mechanism that causes a KeyboardInterrupt
 184      (this is implemented in ipykernel).  We want to ensure the
 185      KeyboardInterrupt cause debugging to cease.
 186      �rcSs*|dd7<|ddksJd��t��)Nr�z#input() should only be called once!)�KeyboardInterrupt)�msg�calledrrr�
 187  raising_input�sz7test_interruptible_core_debugger.<locals>.raising_inputrN)	r�gettracer�object�builtinsrZInterruptiblePdb�	set_trace�settrace)r-Ztracer_origrrr� test_interruptible_core_debugger�s�	�	r3cCsddl}tj��}d|d<|jtjgd�|d�}dt|_|�	d�|�	d	�|�
 188  d
 189  �td�}|��D]}|�
 190  |�|�
 191  |�q4|�
 192  d�td
 193  �}|��D]}|�
 194  |�|�
 195  |�qN|�	d�|�
 196  d�|�	d�|�	d�|�
 197  d�|�
 198  d�|�	d�|�	d�|��dS)zRthat xmode skip frames
 199  
 200      Not as a doctest as pytest does not run doctests.
 201      rN�1�IPY_TEST_SIMPLE_PROMPT�z-m�IPythonz--colors=nocolor��env�r7r�In [1]zo
 202      def f():
 203          __tracebackhide__ = True
 204          g()
 205  
 206      def g():
 207          raise ValueError
 208  
 209      f()
 210      Zskippingz�
 211      def f():
 212          __tracebackhide__ = True
 213          g()
 214  
 215      def g():
 216          from IPython.core.debugger import set_trace
 217          set_trace()
 218  
 219      f()
 220      �ipdb>�wZhiddenzskip_hidden falseZ	__traceba��pexpect�os�environ�copy�spawnr�
 221  executabler�timeout�expect�expect_exactr�
 222  splitlines�sendline�close�r?r9�child�blockrrrr�test_xmode_skip�s@
 223  �
 224  
 225  
 226  
 227  �
 228  
 229  �
 230  
 231  
 232  
 233  
 234  
 235  
 236  
 237  
 238  rN)	zU
 239      def helpers_helper():
 240          pass # should not stop here except breakpoint
 241      zI
 242      def helper_1():
 243          helpers_helper() # should not stop here
 244      z=
 245      def helper_2():
 246          pass # should not stop here
 247      aV
 248      def pdb_skipped_decorator2(function):
 249          def wrapped_fn(*args, **kwargs):
 250              __debuggerskip__ = True
 251              helper_2()
 252              __debuggerskip__ = False
 253              result = function(*args, **kwargs)
 254              __debuggerskip__ = True
 255              helper_2()
 256              return result
 257          return wrapped_fn
 258      aU
 259      def pdb_skipped_decorator(function):
 260          def wrapped_fn(*args, **kwargs):
 261              __debuggerskip__ = True
 262              helper_1()
 263              __debuggerskip__ = False
 264              result = function(*args, **kwargs)
 265              __debuggerskip__ = True
 266              helper_2()
 267              return result
 268          return wrapped_fn
 269      zd
 270      @pdb_skipped_decorator
 271      @pdb_skipped_decorator2
 272      def bar(x, y):
 273          return x * y
 274      z(import IPython.terminal.debugger as ipdbz=
 275      def f():
 276          ipdb.set_trace()
 277          bar(3, 4)
 278      z
 279      f()
 280      cCs�ddl}tj��}d|d<d|d<|jtjgd�|d�}dt|_|�	d�|�	d	�d
 281  t|_d|_
 282  dd
 283  �tD�}d}|D]%}|�d|�d��|d7}|�
 284  �D]}|�|�|�|�qQ|�d�q>|S)Nrr4r5�PROMPT_TOOLKIT_NO_CPRr6r8r:r7r���cSsg|]}t|����qSr)r�strip)�.0�brrr�
 285  <listcomp>�sz)_decorator_skip_setup.<locals>.<listcomp>r)�In [�]:r()r?r@rArBrCrrDrrErF�str_last_chars�skip_decorators_blocksrGrHrI)r?r9rL�dedented_blocks�in_prompt_number�cblockrrrr�_decorator_skip_setupps,
 286  �
 287  
 288  
 289  
 290  
 291  r]z&recently fail for unknown reason on CI)�reasoncCs�t�}|�d�|�d�|�d�|�d�|�d�|�d�|�d�|�d�|�d�|�d�|�d	�|��d
 292  S)z*test that decorator frames can be skipped.zipython-input-8�3     bar(3, 4)r<�stepz--Call--zipython-input-6z1 @pdb_skipped_decorator�szreturn x * yN�r]rGrFrIrJ)rLrrr�test_decorator_skip�s
 293  
 294  
 295  
 296  
 297  
 298  
 299  
 300  
 301  
 302  
 303  rc�PyPyzissues on PyPycCsRt�}|�d�dD]\}}|�d�|�|�|�|�|�|�q
 304  |��dS)�2test that decorator frame skipping can be disabledr_)	)z"skip_predicates debuggerskip Falser()Zskip_predicateszdebuggerskip : False)r`z---> 2     def wrapped_fn)r`z ----> 3         __debuggerskip__)r`z----> 4         helper_1())r`z---> 1 def helper_1():)rz----> 2     helpers_helper())rz
 305  --Return--)rz(----> 5         __debuggerskip__ = Falser<Nrb)rL�input_�expectedrrr�test_decorator_skip_disabled�s
 306  
 307  
 308  
 309  rh)�final�	candidatezfails on 3.13.devT)r^�strictc
 310  	Cs�ddl}tj��}d|d<d|d<|jtjgd�|d�}dt|_d	|_	|�
 311  d
 312  �|�
 313  d�dt|_td
 314  ddd���}|jdd��
 315  d�d}|�d�dd�tdd�D�����|��d|�d�}|dg}d}|D]%}|�d|�d��|d7}|��D]}	|�|	�|�|	�q|�d�qltjdkr�|�d�dg}
 316  n|�d �g}
 317  d!|�d"�dfg|
 318  gd#�D]\}}|�
 319  d$�|�|�|�|�|�|�q�Wd�n1s�wY|��dS)%rerNr4r5rOr6r8r:rQr7rrPz.py�.T)�suffix�dir�delete������/�����cSsg|]}t|��qSrr)rS�xrrrrU�sz7test_decorator_skip_with_breakpoint.<locals>.<listcomp>zfrom z	 import fzf()r)rVrWr()��
 320  z--> 46     ipdb.set_trace())r`�--> 47     bar(3, 4)rvzb z.py:3))r`z.1---> 3     pass # should not stop here except)r`z---> 38 @pdb_skipped_decorator)�continuer(r<)r?r@rArBrCrrDrrErXrFr�name�split�write�joinrY�encode�flushrGrHrI�version_inforJ)
 321  r?r9rL�tfrxZ	codeblockrZr[r\rZ
 322  extra_steprfrgrrr�#test_decorator_skip_with_breakpoint�s^
 323  
 324  �
 325  
 326  
 327  
 328  &�
 329  
 330  
 331  
 332  ���
 333  �
 334  
 335  
 336  ��-r�cCs8ddl}tj��}d|d<|jtjgd�|d�}dt|_|�	d�|�	d	�|�
 337  d
 338  �td�}|��D]}|�
 339  |�|�
 340  |�q4|�
 341  d�|�
 342  d
 343  �|�
 344  d�|�	d�|�
 345  d�|�	d�|�	d�|�
 346  d�|�
 347  d�|�
 348  d�|�
 349  d�|�	d�|�
 350  d�|�	d�|�
 351  d�|�
 352  d�|�	d�|��dS)z<Test that `where` does not access f_locals and erase values.rNr4r5r6r8r:r7rr;zs
 353      def simple_f():
 354           myvar = 1
 355           print(myvar)
 356           1/0
 357           print(myvar)
 358      simple_f()    �ZeroDivisionErrorzIn [2]:z%debugr<Zmyvarz	myvar = 2�2�wherer>rKrrr�test_where_erase_valuesD
 359  �
 360  
 361  
 362  
 363  �
 364  
 365  
 366  
 367  
 368  
 369  
 370  
 371  
 372  
 373  
 374  
 375  
 376  
 377  
 378  
 379  
 380  
 381  
 382  r�)(rr0r@r�platform�tempfiler�textwraprZ
 383  unittest.mockrZIPython.corerZIPython.testingrZIPython.testing.decoratorsrZpytestr/r	rr#r%r&r'r3rNrYr]�mark�skiprcZskipif�python_implementationrhZxfailr~�releaselevelr�r�rrrr�<module>sPZ
 384  B7
 385  �E