| 1 |
"""Tests for debugging machinery.""" |
| 2 |
|
| 3 |
# Copyright (c) IPython Development Team. |
| 4 |
# Distributed under the terms of the Modified BSD License. |
| 5 |
|
| 6 |
import builtins |
| 7 |
import os |
| 8 |
import sys |
| 9 |
import platform |
| 10 |
from pathlib import Path |
| 11 |
|
| 12 |
from tempfile import NamedTemporaryFile, TemporaryDirectory |
| 13 |
from textwrap import dedent |
| 14 |
from unittest.mock import patch |
| 15 |
|
| 16 |
from IPython.core import debugger |
| 17 |
from IPython.testing import IPYTHON_TESTING_TIMEOUT_SCALE |
| 18 |
from IPython.testing.decorators import skip_win32 |
| 19 |
import pytest |
| 20 |
|
| 21 |
# ----------------------------------------------------------------------------- |
| 22 |
# Helper classes, from CPython's Pdb test suite |
| 23 |
# ----------------------------------------------------------------------------- |
| 24 |
|
| 25 |
|
| 26 |
class _FakeInput(object): |
| 27 |
""" |
| 28 |
A fake input stream for pdb's interactive debugger. Whenever a |
| 29 |
line is read, print it (to simulate the user typing it), and then |
| 30 |
return it. The set of lines to return is specified in the |
| 31 |
constructor; they should not have trailing newlines. |
| 32 |
""" |
| 33 |
|
| 34 |
def __init__(self, lines): |
| 35 |
self.lines = iter(lines) |
| 36 |
|
| 37 |
def readline(self): |
| 38 |
line = next(self.lines) |
| 39 |
print(line) |
| 40 |
return line + "\n" |
| 41 |
|
| 42 |
|
| 43 |
class PdbTestInput(object): |
| 44 |
"""Context manager that makes testing Pdb in doctests easier.""" |
| 45 |
|
| 46 |
def __init__(self, input): |
| 47 |
self.input = input |
| 48 |
|
| 49 |
def __enter__(self): |
| 50 |
self.real_stdin = sys.stdin |
| 51 |
sys.stdin = _FakeInput(self.input) |
| 52 |
|
| 53 |
def __exit__(self, *exc): |
| 54 |
sys.stdin = self.real_stdin |
| 55 |
|
| 56 |
|
| 57 |
# ----------------------------------------------------------------------------- |
| 58 |
# Tests |
| 59 |
# ----------------------------------------------------------------------------- |
| 60 |
|
| 61 |
|
| 62 |
def test_ipdb_magics(): |
| 63 |
'''Test calling some IPython magics from ipdb. |
| 64 |
|
| 65 |
First, set up some test functions and classes which we can inspect. |
| 66 |
|
| 67 |
In [1]: class ExampleClass(object): |
| 68 |
...: """Docstring for ExampleClass.""" |
| 69 |
...: def __init__(self): |
| 70 |
...: """Docstring for ExampleClass.__init__""" |
| 71 |
...: pass |
| 72 |
...: def __str__(self): |
| 73 |
...: return "ExampleClass()" |
| 74 |
|
| 75 |
In [2]: def example_function(x, y, z="hello"): |
| 76 |
...: """Docstring for example_function.""" |
| 77 |
...: pass |
| 78 |
|
| 79 |
In [3]: old_trace = sys.gettrace() |
| 80 |
|
| 81 |
Create a function which triggers ipdb. |
| 82 |
|
| 83 |
In [4]: def trigger_ipdb(): |
| 84 |
...: a = ExampleClass() |
| 85 |
...: debugger.Pdb().set_trace() |
| 86 |
|
| 87 |
Run ipdb with faked input & check output. Because of a difference between |
| 88 |
Python 3.13 & older versions, the first bit of the output is inconsistent. |
| 89 |
We need to use ... to accommodate that, so the examples have to use IPython |
| 90 |
prompts so that ... is distinct from the Python PS2 prompt. |
| 91 |
|
| 92 |
In [5]: with PdbTestInput([ |
| 93 |
...: 'pdef example_function', |
| 94 |
...: 'pdoc ExampleClass', |
| 95 |
...: 'up', |
| 96 |
...: 'down', |
| 97 |
...: 'list', |
| 98 |
...: 'pinfo a', |
| 99 |
...: 'll', |
| 100 |
...: 'continue', |
| 101 |
...: ]): |
| 102 |
...: trigger_ipdb() |
| 103 |
...> <doctest ...>(3)trigger_ipdb() |
| 104 |
1 def trigger_ipdb(): |
| 105 |
2 a = ExampleClass() |
| 106 |
----> 3 debugger.Pdb().set_trace() |
| 107 |
<BLANKLINE> |
| 108 |
ipdb> pdef example_function |
| 109 |
example_function(x, y, z='hello') |
| 110 |
ipdb> pdoc ExampleClass |
| 111 |
Class docstring: |
| 112 |
Docstring for ExampleClass. |
| 113 |
Init docstring: |
| 114 |
Docstring for ExampleClass.__init__ |
| 115 |
ipdb> up |
| 116 |
> <doctest ...>(11)<module>() |
| 117 |
7 'pinfo a', |
| 118 |
8 'll', |
| 119 |
9 'continue', |
| 120 |
10 ]): |
| 121 |
---> 11 trigger_ipdb() |
| 122 |
<BLANKLINE> |
| 123 |
ipdb> down... |
| 124 |
> <doctest ...>(3)trigger_ipdb() |
| 125 |
1 def trigger_ipdb(): |
| 126 |
2 a = ExampleClass() |
| 127 |
----> 3 debugger.Pdb().set_trace() |
| 128 |
<BLANKLINE> |
| 129 |
ipdb> list |
| 130 |
1 def trigger_ipdb(): |
| 131 |
2 a = ExampleClass() |
| 132 |
----> 3 debugger.Pdb().set_trace() |
| 133 |
<BLANKLINE> |
| 134 |
ipdb> pinfo a |
| 135 |
Type: ExampleClass |
| 136 |
String form: ExampleClass() |
| 137 |
Namespace: Local... |
| 138 |
Docstring: Docstring for ExampleClass. |
| 139 |
Init docstring: Docstring for ExampleClass.__init__ |
| 140 |
ipdb> ll |
| 141 |
1 def trigger_ipdb(): |
| 142 |
2 a = ExampleClass() |
| 143 |
----> 3 debugger.Pdb().set_trace() |
| 144 |
<BLANKLINE> |
| 145 |
ipdb> continue |
| 146 |
|
| 147 |
Restore previous trace function, e.g. for coverage.py |
| 148 |
|
| 149 |
In [6]: sys.settrace(old_trace) |
| 150 |
''' |
| 151 |
|
| 152 |
|
| 153 |
def test_ipdb_closure(): |
| 154 |
"""Test evaluation of expressions which depend on closure. |
| 155 |
|
| 156 |
In [1]: old_trace = sys.gettrace() |
| 157 |
|
| 158 |
Create a function which triggers ipdb. |
| 159 |
|
| 160 |
In [2]: def trigger_ipdb(): |
| 161 |
...: debugger.Pdb().set_trace() |
| 162 |
|
| 163 |
In [3]: with PdbTestInput([ |
| 164 |
...: 'x = 1; sum(x * i for i in range(5))', |
| 165 |
...: 'continue', |
| 166 |
...: ]): |
| 167 |
...: trigger_ipdb() |
| 168 |
...> <doctest ...>(2)trigger_ipdb() |
| 169 |
1 def trigger_ipdb(): |
| 170 |
----> 2 debugger.Pdb().set_trace() |
| 171 |
<BLANKLINE> |
| 172 |
ipdb> x = 1; sum(x * i for i in range(5)) |
| 173 |
ipdb> continue |
| 174 |
|
| 175 |
Restore previous trace function, e.g. for coverage.py |
| 176 |
|
| 177 |
In [4]: sys.settrace(old_trace) |
| 178 |
""" |
| 179 |
|
| 180 |
|
| 181 |
def test_ipdb_magics2(): |
| 182 |
"""Test ipdb with a very short function. |
| 183 |
|
| 184 |
>>> old_trace = sys.gettrace() |
| 185 |
|
| 186 |
>>> def bar(): |
| 187 |
... pass |
| 188 |
|
| 189 |
Run ipdb. |
| 190 |
|
| 191 |
>>> with PdbTestInput([ |
| 192 |
... 'continue', |
| 193 |
... ]): |
| 194 |
... debugger.Pdb().runcall(bar) |
| 195 |
> <doctest ...>(2)bar() |
| 196 |
1 def bar(): |
| 197 |
----> 2 pass |
| 198 |
<BLANKLINE> |
| 199 |
ipdb> continue |
| 200 |
|
| 201 |
Restore previous trace function, e.g. for coverage.py |
| 202 |
|
| 203 |
>>> sys.settrace(old_trace) |
| 204 |
""" |
| 205 |
|
| 206 |
|
| 207 |
def can_quit(): |
| 208 |
"""Test that quit work in ipydb |
| 209 |
|
| 210 |
>>> old_trace = sys.gettrace() |
| 211 |
|
| 212 |
>>> def bar(): |
| 213 |
... pass |
| 214 |
|
| 215 |
>>> with PdbTestInput([ |
| 216 |
... 'quit', |
| 217 |
... ]): |
| 218 |
... debugger.Pdb().runcall(bar) |
| 219 |
> <doctest ...>(2)bar() |
| 220 |
1 def bar(): |
| 221 |
----> 2 pass |
| 222 |
<BLANKLINE> |
| 223 |
ipdb> quit |
| 224 |
|
| 225 |
Restore previous trace function, e.g. for coverage.py |
| 226 |
|
| 227 |
>>> sys.settrace(old_trace) |
| 228 |
""" |
| 229 |
|
| 230 |
|
| 231 |
def can_exit(): |
| 232 |
"""Test that quit work in ipydb |
| 233 |
|
| 234 |
>>> old_trace = sys.gettrace() |
| 235 |
|
| 236 |
>>> def bar(): |
| 237 |
... pass |
| 238 |
|
| 239 |
>>> with PdbTestInput([ |
| 240 |
... 'exit', |
| 241 |
... ]): |
| 242 |
... debugger.Pdb().runcall(bar) |
| 243 |
> <doctest ...>(2)bar() |
| 244 |
1 def bar(): |
| 245 |
----> 2 pass |
| 246 |
<BLANKLINE> |
| 247 |
ipdb> exit |
| 248 |
|
| 249 |
Restore previous trace function, e.g. for coverage.py |
| 250 |
|
| 251 |
>>> sys.settrace(old_trace) |
| 252 |
""" |
| 253 |
|
| 254 |
|
| 255 |
def test_interruptible_core_debugger(): |
| 256 |
"""The debugger can be interrupted. |
| 257 |
|
| 258 |
The presumption is there is some mechanism that causes a KeyboardInterrupt |
| 259 |
(this is implemented in ipykernel). We want to ensure the |
| 260 |
KeyboardInterrupt cause debugging to cease. |
| 261 |
""" |
| 262 |
|
| 263 |
def raising_input(msg="", called=[0]): |
| 264 |
called[0] += 1 |
| 265 |
assert called[0] == 1, "input() should only be called once!" |
| 266 |
raise KeyboardInterrupt() |
| 267 |
|
| 268 |
tracer_orig = sys.gettrace() |
| 269 |
try: |
| 270 |
with patch.object(builtins, "input", raising_input): |
| 271 |
debugger.InterruptiblePdb().set_trace() |
| 272 |
# The way this test will fail is by set_trace() never exiting, |
| 273 |
# resulting in a timeout by the test runner. The alternative |
| 274 |
# implementation would involve a subprocess, but that adds issues |
| 275 |
# with interrupting subprocesses that are rather complex, so it's |
| 276 |
# simpler just to do it this way. |
| 277 |
finally: |
| 278 |
# restore the original trace function |
| 279 |
sys.settrace(tracer_orig) |
| 280 |
|
| 281 |
|
| 282 |
@skip_win32 |
| 283 |
def test_xmode_skip(): |
| 284 |
"""that xmode skip frames |
| 285 |
|
| 286 |
Not as a doctest as pytest does not run doctests. |
| 287 |
""" |
| 288 |
import pexpect |
| 289 |
|
| 290 |
env = os.environ.copy() |
| 291 |
env["IPY_TEST_SIMPLE_PROMPT"] = "1" |
| 292 |
|
| 293 |
child = pexpect.spawn( |
| 294 |
sys.executable, ["-m", "IPython", "--colors=nocolor"], env=env |
| 295 |
) |
| 296 |
child.timeout = 15 * IPYTHON_TESTING_TIMEOUT_SCALE |
| 297 |
|
| 298 |
child.expect("IPython") |
| 299 |
child.expect("\n") |
| 300 |
child.expect_exact("In [1]") |
| 301 |
|
| 302 |
block = dedent( |
| 303 |
""" |
| 304 |
def f(): |
| 305 |
__tracebackhide__ = True |
| 306 |
g() |
| 307 |
|
| 308 |
def g(): |
| 309 |
raise ValueError |
| 310 |
|
| 311 |
f() |
| 312 |
""" |
| 313 |
) |
| 314 |
|
| 315 |
for line in block.splitlines(): |
| 316 |
child.sendline(line) |
| 317 |
child.expect_exact(line) |
| 318 |
child.expect_exact("skipping") |
| 319 |
|
| 320 |
block = dedent( |
| 321 |
""" |
| 322 |
def f(): |
| 323 |
__tracebackhide__ = True |
| 324 |
g() |
| 325 |
|
| 326 |
def g(): |
| 327 |
from IPython.core.debugger import set_trace |
| 328 |
set_trace() |
| 329 |
|
| 330 |
f() |
| 331 |
""" |
| 332 |
) |
| 333 |
|
| 334 |
for line in block.splitlines(): |
| 335 |
child.sendline(line) |
| 336 |
child.expect_exact(line) |
| 337 |
|
| 338 |
child.expect("ipdb>") |
| 339 |
child.sendline("w") |
| 340 |
child.expect("hidden") |
| 341 |
child.expect("ipdb>") |
| 342 |
child.sendline("skip_hidden false") |
| 343 |
child.sendline("w") |
| 344 |
child.expect("__traceba") |
| 345 |
child.expect("ipdb>") |
| 346 |
|
| 347 |
child.close() |
| 348 |
|
| 349 |
|
| 350 |
skip_decorators_blocks = ( |
| 351 |
""" |
| 352 |
def helpers_helper(): |
| 353 |
pass # should not stop here except breakpoint |
| 354 |
""", |
| 355 |
""" |
| 356 |
def helper_1(): |
| 357 |
helpers_helper() # should not stop here |
| 358 |
""", |
| 359 |
""" |
| 360 |
def helper_2(): |
| 361 |
pass # should not stop here |
| 362 |
""", |
| 363 |
""" |
| 364 |
def pdb_skipped_decorator2(function): |
| 365 |
def wrapped_fn(*args, **kwargs): |
| 366 |
__debuggerskip__ = True |
| 367 |
helper_2() |
| 368 |
__debuggerskip__ = False |
| 369 |
result = function(*args, **kwargs) |
| 370 |
__debuggerskip__ = True |
| 371 |
helper_2() |
| 372 |
return result |
| 373 |
return wrapped_fn |
| 374 |
""", |
| 375 |
""" |
| 376 |
def pdb_skipped_decorator(function): |
| 377 |
def wrapped_fn(*args, **kwargs): |
| 378 |
__debuggerskip__ = True |
| 379 |
helper_1() |
| 380 |
__debuggerskip__ = False |
| 381 |
result = function(*args, **kwargs) |
| 382 |
__debuggerskip__ = True |
| 383 |
helper_2() |
| 384 |
return result |
| 385 |
return wrapped_fn |
| 386 |
""", |
| 387 |
""" |
| 388 |
@pdb_skipped_decorator |
| 389 |
@pdb_skipped_decorator2 |
| 390 |
def bar(x, y): |
| 391 |
return x * y |
| 392 |
""", |
| 393 |
"""import IPython.terminal.debugger as ipdb""", |
| 394 |
""" |
| 395 |
def f(): |
| 396 |
ipdb.set_trace() |
| 397 |
bar(3, 4) |
| 398 |
""", |
| 399 |
""" |
| 400 |
f() |
| 401 |
""", |
| 402 |
) |
| 403 |
|
| 404 |
|
| 405 |
def _decorator_skip_setup(): |
| 406 |
import pexpect |
| 407 |
|
| 408 |
env = os.environ.copy() |
| 409 |
env["IPY_TEST_SIMPLE_PROMPT"] = "1" |
| 410 |
env["PROMPT_TOOLKIT_NO_CPR"] = "1" |
| 411 |
|
| 412 |
child = pexpect.spawn( |
| 413 |
sys.executable, ["-m", "IPython", "--colors=nocolor"], env=env |
| 414 |
) |
| 415 |
child.timeout = 15 * IPYTHON_TESTING_TIMEOUT_SCALE |
| 416 |
|
| 417 |
child.expect("IPython") |
| 418 |
child.expect("\n") |
| 419 |
|
| 420 |
child.timeout = 5 * IPYTHON_TESTING_TIMEOUT_SCALE |
| 421 |
child.str_last_chars = 500 |
| 422 |
|
| 423 |
dedented_blocks = [dedent(b).strip() for b in skip_decorators_blocks] |
| 424 |
in_prompt_number = 1 |
| 425 |
for cblock in dedented_blocks: |
| 426 |
child.expect_exact(f"In [{in_prompt_number}]:") |
| 427 |
in_prompt_number += 1 |
| 428 |
for line in cblock.splitlines(): |
| 429 |
child.sendline(line) |
| 430 |
child.expect_exact(line) |
| 431 |
child.sendline("") |
| 432 |
return child |
| 433 |
|
| 434 |
|
| 435 |
@pytest.mark.skip(reason="recently fail for unknown reason on CI") |
| 436 |
@skip_win32 |
| 437 |
def test_decorator_skip(): |
| 438 |
"""test that decorator frames can be skipped.""" |
| 439 |
|
| 440 |
child = _decorator_skip_setup() |
| 441 |
|
| 442 |
child.expect_exact("ipython-input-8") |
| 443 |
child.expect_exact("3 bar(3, 4)") |
| 444 |
child.expect("ipdb>") |
| 445 |
|
| 446 |
child.expect("ipdb>") |
| 447 |
child.sendline("step") |
| 448 |
child.expect_exact("step") |
| 449 |
child.expect_exact("--Call--") |
| 450 |
child.expect_exact("ipython-input-6") |
| 451 |
|
| 452 |
child.expect_exact("1 @pdb_skipped_decorator") |
| 453 |
|
| 454 |
child.sendline("s") |
| 455 |
child.expect_exact("return x * y") |
| 456 |
|
| 457 |
child.close() |
| 458 |
|
| 459 |
|
| 460 |
@pytest.mark.skip(reason="recently fail for unknown reason on CI") |
| 461 |
@pytest.mark.skipif(platform.python_implementation() == "PyPy", reason="issues on PyPy") |
| 462 |
@skip_win32 |
| 463 |
def test_decorator_skip_disabled(): |
| 464 |
"""test that decorator frame skipping can be disabled""" |
| 465 |
|
| 466 |
child = _decorator_skip_setup() |
| 467 |
|
| 468 |
child.expect_exact("3 bar(3, 4)") |
| 469 |
|
| 470 |
for input_, expected in [ |
| 471 |
("skip_predicates debuggerskip False", ""), |
| 472 |
("skip_predicates", "debuggerskip : False"), |
| 473 |
("step", "---> 2 def wrapped_fn"), |
| 474 |
("step", "----> 3 __debuggerskip__"), |
| 475 |
("step", "----> 4 helper_1()"), |
| 476 |
("step", "---> 1 def helper_1():"), |
| 477 |
("next", "----> 2 helpers_helper()"), |
| 478 |
("next", "--Return--"), |
| 479 |
("next", "----> 5 __debuggerskip__ = False"), |
| 480 |
]: |
| 481 |
child.expect("ipdb>") |
| 482 |
child.sendline(input_) |
| 483 |
child.expect_exact(input_) |
| 484 |
child.expect_exact(expected) |
| 485 |
|
| 486 |
child.close() |
| 487 |
|
| 488 |
|
| 489 |
@pytest.mark.skip(reason="recently fail for unknown reason on CI") |
| 490 |
@pytest.mark.skipif(platform.python_implementation() == "PyPy", reason="issues on PyPy") |
| 491 |
@skip_win32 |
| 492 |
def test_decorator_skip_with_breakpoint(): |
| 493 |
"""test that decorator frame skipping can be disabled""" |
| 494 |
|
| 495 |
import pexpect |
| 496 |
|
| 497 |
env = os.environ.copy() |
| 498 |
env["IPY_TEST_SIMPLE_PROMPT"] = "1" |
| 499 |
env["PROMPT_TOOLKIT_NO_CPR"] = "1" |
| 500 |
|
| 501 |
child = pexpect.spawn( |
| 502 |
sys.executable, ["-m", "IPython", "--colors=nocolor"], env=env |
| 503 |
) |
| 504 |
child.timeout = 15 * IPYTHON_TESTING_TIMEOUT_SCALE |
| 505 |
child.str_last_chars = 500 |
| 506 |
|
| 507 |
child.expect("IPython") |
| 508 |
child.expect("\n") |
| 509 |
|
| 510 |
child.timeout = 5 * IPYTHON_TESTING_TIMEOUT_SCALE |
| 511 |
|
| 512 |
### we need a filename, so we need to exec the full block with a filename |
| 513 |
with NamedTemporaryFile(suffix=".py", dir=".", delete=True) as tf: |
| 514 |
name = tf.name[:-3].split("/")[-1] |
| 515 |
tf.write("\n".join([dedent(x) for x in skip_decorators_blocks[:-1]]).encode()) |
| 516 |
tf.flush() |
| 517 |
codeblock = f"from {name} import f" |
| 518 |
|
| 519 |
dedented_blocks = [ |
| 520 |
codeblock, |
| 521 |
"f()", |
| 522 |
] |
| 523 |
|
| 524 |
in_prompt_number = 1 |
| 525 |
for cblock in dedented_blocks: |
| 526 |
child.expect_exact(f"In [{in_prompt_number}]:") |
| 527 |
in_prompt_number += 1 |
| 528 |
for line in cblock.splitlines(): |
| 529 |
child.sendline(line) |
| 530 |
child.expect_exact(line) |
| 531 |
child.sendline("") |
| 532 |
|
| 533 |
# From 3.13, set_trace()/breakpoint() stop on the line where they're |
| 534 |
# called, instead of the next line. |
| 535 |
if sys.version_info >= (3, 14): |
| 536 |
child.expect_exact(" 46 ipdb.set_trace()") |
| 537 |
extra_step = [("step", "--> 47 bar(3, 4)")] |
| 538 |
elif sys.version_info >= (3, 13): |
| 539 |
child.expect_exact("--> 46 ipdb.set_trace()") |
| 540 |
extra_step = [("step", "--> 47 bar(3, 4)")] |
| 541 |
else: |
| 542 |
child.expect_exact("--> 47 bar(3, 4)") |
| 543 |
extra_step = [] |
| 544 |
|
| 545 |
for input_, expected in ( |
| 546 |
[ |
| 547 |
(f"b {name}.py:3", ""), |
| 548 |
] |
| 549 |
+ extra_step |
| 550 |
+ [ |
| 551 |
("step", "1---> 3 pass # should not stop here except"), |
| 552 |
("step", "---> 38 @pdb_skipped_decorator"), |
| 553 |
("continue", ""), |
| 554 |
] |
| 555 |
): |
| 556 |
child.expect("ipdb>") |
| 557 |
child.sendline(input_) |
| 558 |
child.expect_exact(input_) |
| 559 |
child.expect_exact(expected) |
| 560 |
|
| 561 |
child.close() |
| 562 |
|
| 563 |
|
| 564 |
@skip_win32 |
| 565 |
def test_where_erase_value(): |
| 566 |
"""Test that `where` does not access f_locals and erase values.""" |
| 567 |
import pexpect |
| 568 |
|
| 569 |
env = os.environ.copy() |
| 570 |
env["IPY_TEST_SIMPLE_PROMPT"] = "1" |
| 571 |
|
| 572 |
child = pexpect.spawn( |
| 573 |
sys.executable, ["-m", "IPython", "--colors=nocolor"], env=env |
| 574 |
) |
| 575 |
child.timeout = 15 * IPYTHON_TESTING_TIMEOUT_SCALE |
| 576 |
|
| 577 |
child.expect("IPython") |
| 578 |
child.expect("\n") |
| 579 |
child.expect_exact("In [1]") |
| 580 |
|
| 581 |
block = dedent( |
| 582 |
""" |
| 583 |
def simple_f(): |
| 584 |
myvar = 1 |
| 585 |
print(myvar) |
| 586 |
1/0 |
| 587 |
print(myvar) |
| 588 |
simple_f() """ |
| 589 |
) |
| 590 |
|
| 591 |
for line in block.splitlines(): |
| 592 |
child.sendline(line) |
| 593 |
child.expect_exact(line) |
| 594 |
child.expect_exact("ZeroDivisionError") |
| 595 |
child.expect_exact("In [2]:") |
| 596 |
|
| 597 |
child.sendline("%debug") |
| 598 |
|
| 599 |
## |
| 600 |
child.expect("ipdb>") |
| 601 |
|
| 602 |
child.sendline("myvar") |
| 603 |
child.expect("1") |
| 604 |
|
| 605 |
## |
| 606 |
child.expect("ipdb>") |
| 607 |
|
| 608 |
child.sendline("myvar = 2") |
| 609 |
|
| 610 |
## |
| 611 |
child.expect_exact("ipdb>") |
| 612 |
|
| 613 |
child.sendline("myvar") |
| 614 |
|
| 615 |
child.expect_exact("2") |
| 616 |
|
| 617 |
## |
| 618 |
child.expect("ipdb>") |
| 619 |
child.sendline("where") |
| 620 |
|
| 621 |
## |
| 622 |
child.expect("ipdb>") |
| 623 |
child.sendline("myvar") |
| 624 |
|
| 625 |
child.expect_exact("2") |
| 626 |
child.expect("ipdb>") |
| 627 |
|
| 628 |
child.close() |
| 629 |
|
| 630 |
|
| 631 |
@skip_win32 |
| 632 |
def test_ignore_module_basic_functionality(): |
| 633 |
"""Test basic ignore/unignore functionality and error handling.""" |
| 634 |
import pexpect |
| 635 |
|
| 636 |
env = os.environ.copy() |
| 637 |
env["IPY_TEST_SIMPLE_PROMPT"] = "1" |
| 638 |
|
| 639 |
with TemporaryDirectory() as temp_dir: |
| 640 |
main_path = create_test_modules(temp_dir) |
| 641 |
|
| 642 |
child = pexpect.spawn(sys.executable, [main_path], env=env, cwd=temp_dir) |
| 643 |
child.timeout = 15 * IPYTHON_TESTING_TIMEOUT_SCALE |
| 644 |
child.expect("ipdb>") |
| 645 |
|
| 646 |
# Test listing modules when none are ignored |
| 647 |
child.sendline("ignore_module") |
| 648 |
child.expect_exact("No modules are currently ignored.") |
| 649 |
child.expect("ipdb>") |
| 650 |
|
| 651 |
# Test ignoring a module |
| 652 |
child.sendline("ignore_module level2_module") |
| 653 |
child.expect("ipdb>") |
| 654 |
|
| 655 |
# Test listing ignored modules |
| 656 |
child.sendline("ignore_module") |
| 657 |
child.expect_exact("Currently ignored modules: ['level2_module']") |
| 658 |
child.expect("ipdb>") |
| 659 |
|
| 660 |
# Test wildcard pattern |
| 661 |
child.sendline("ignore_module testpkg.*") |
| 662 |
child.expect("ipdb>") |
| 663 |
|
| 664 |
child.sendline("ignore_module") |
| 665 |
child.expect_exact("Currently ignored modules: ['level2_module', 'testpkg.*']") |
| 666 |
child.expect("ipdb>") |
| 667 |
|
| 668 |
# Test error handling - removing non-existent module |
| 669 |
child.sendline("unignore_module nonexistent") |
| 670 |
child.expect_exact("Module nonexistent is not currently ignored") |
| 671 |
child.expect("ipdb>") |
| 672 |
|
| 673 |
# Test successful removal |
| 674 |
child.sendline("unignore_module level2_module") |
| 675 |
child.expect("ipdb>") |
| 676 |
|
| 677 |
child.sendline("ignore_module") |
| 678 |
child.expect_exact("Currently ignored modules: ['testpkg.*']") |
| 679 |
child.expect("ipdb>") |
| 680 |
|
| 681 |
# Test removing already removed module |
| 682 |
child.sendline("unignore_module level2_module") |
| 683 |
child.expect_exact("Module level2_module is not currently ignored") |
| 684 |
child.expect("ipdb>") |
| 685 |
|
| 686 |
# Remove wildcard pattern |
| 687 |
child.sendline("unignore_module testpkg.*") |
| 688 |
child.expect("ipdb>") |
| 689 |
|
| 690 |
child.sendline("ignore_module") |
| 691 |
child.expect_exact("No modules are currently ignored.") |
| 692 |
child.expect("ipdb>") |
| 693 |
|
| 694 |
child.sendline("continue") |
| 695 |
child.close() |
| 696 |
|
| 697 |
|
| 698 |
# Helper function for creating temporary modules |
| 699 |
def create_test_modules(temp_dir): |
| 700 |
"""Create a comprehensive module hierarchy for testing all debugger commands.""" |
| 701 |
|
| 702 |
temp_path = Path(temp_dir) |
| 703 |
|
| 704 |
# Create package structure for wildcard testing |
| 705 |
package_dir = temp_path / "testpkg" |
| 706 |
package_dir.mkdir() |
| 707 |
|
| 708 |
# Package __init__.py |
| 709 |
(package_dir / "__init__.py").write_text("# Test package") |
| 710 |
|
| 711 |
# testpkg/submod1.py |
| 712 |
(package_dir / "submod1.py").write_text( |
| 713 |
dedent( |
| 714 |
""" |
| 715 |
def submod1_func(): |
| 716 |
x = 1 |
| 717 |
y = 2 |
| 718 |
return x + y |
| 719 |
""" |
| 720 |
) |
| 721 |
) |
| 722 |
|
| 723 |
# testpkg/submod2.py |
| 724 |
(package_dir / "submod2.py").write_text( |
| 725 |
dedent( |
| 726 |
""" |
| 727 |
def submod2_func(): |
| 728 |
z = 10 |
| 729 |
return z * 2 |
| 730 |
""" |
| 731 |
) |
| 732 |
) |
| 733 |
|
| 734 |
# Level 1 (top level module) |
| 735 |
(temp_path / "level1_module.py").write_text( |
| 736 |
dedent( |
| 737 |
""" |
| 738 |
from level2_module import level2_func |
| 739 |
|
| 740 |
def level1_func(): |
| 741 |
return level2_func() |
| 742 |
""" |
| 743 |
) |
| 744 |
) |
| 745 |
|
| 746 |
# Level 2 (middle level module) |
| 747 |
(temp_path / "level2_module.py").write_text( |
| 748 |
dedent( |
| 749 |
""" |
| 750 |
from level3_module import level3_func |
| 751 |
from testpkg.submod1 import submod1_func |
| 752 |
from testpkg.submod2 import submod2_func |
| 753 |
|
| 754 |
def level2_func(): |
| 755 |
# Call package functions for step/next testing |
| 756 |
result1 = submod1_func() |
| 757 |
result2 = submod2_func() |
| 758 |
return level3_func() + result1 + result2 |
| 759 |
""" |
| 760 |
) |
| 761 |
) |
| 762 |
|
| 763 |
# Level 3 (bottom level with debugger) |
| 764 |
(temp_path / "level3_module.py").write_text( |
| 765 |
dedent( |
| 766 |
""" |
| 767 |
from level4_module import level4_func |
| 768 |
|
| 769 |
from IPython.core.debugger import set_trace |
| 770 |
|
| 771 |
def level3_func(): |
| 772 |
set_trace() |
| 773 |
pass |
| 774 |
result = level4_func() |
| 775 |
return result |
| 776 |
""" |
| 777 |
) |
| 778 |
) |
| 779 |
|
| 780 |
# Level 4 (bottom level with debugger) |
| 781 |
(temp_path / "level4_module.py").write_text( |
| 782 |
dedent( |
| 783 |
""" |
| 784 |
def level4_func(): |
| 785 |
a = 70 |
| 786 |
b = 30 |
| 787 |
return a + b |
| 788 |
""" |
| 789 |
) |
| 790 |
) |
| 791 |
|
| 792 |
# Main runner |
| 793 |
main_path = temp_path / "main_runner.py" |
| 794 |
main_path.write_text( |
| 795 |
dedent( |
| 796 |
""" |
| 797 |
import sys |
| 798 |
sys.path.insert(0, '.') |
| 799 |
from level1_module import level1_func |
| 800 |
|
| 801 |
if __name__ == "__main__": |
| 802 |
result = level1_func() |
| 803 |
print(f"Final result: {result}") |
| 804 |
""" |
| 805 |
) |
| 806 |
) |
| 807 |
|
| 808 |
return str(main_path) |
| 809 |
|
| 810 |
|
| 811 |
@skip_win32 |
| 812 |
def test_ignore_module_all_commands(): |
| 813 |
"""Comprehensive test for all debugger commands (up/down/step/next) with ignore functionality.""" |
| 814 |
import pexpect |
| 815 |
|
| 816 |
env = os.environ.copy() |
| 817 |
env["IPY_TEST_SIMPLE_PROMPT"] = "1" |
| 818 |
|
| 819 |
with TemporaryDirectory() as temp_dir: |
| 820 |
main_path = create_test_modules(temp_dir) |
| 821 |
|
| 822 |
# Test UP and DOWN commands |
| 823 |
child = pexpect.spawn(sys.executable, [main_path], env=env, cwd=temp_dir) |
| 824 |
child.timeout = 15 * IPYTHON_TESTING_TIMEOUT_SCALE |
| 825 |
child.expect("ipdb>") |
| 826 |
|
| 827 |
# Test up without ignores (baseline) |
| 828 |
child.sendline("up") |
| 829 |
child.expect("ipdb>") |
| 830 |
child.sendline("__name__") |
| 831 |
child.expect_exact("level2_module") |
| 832 |
child.expect("ipdb>") |
| 833 |
|
| 834 |
# Reset position |
| 835 |
child.sendline("down") |
| 836 |
child.expect("ipdb>") |
| 837 |
|
| 838 |
# Test up with single module ignore |
| 839 |
child.sendline("ignore_module level2_module") |
| 840 |
child.expect("ipdb>") |
| 841 |
child.sendline("up") |
| 842 |
child.expect_exact( |
| 843 |
"[... skipped 1 frame(s): 0 hidden frames + 1 ignored modules]" |
| 844 |
) |
| 845 |
child.expect("ipdb>") |
| 846 |
child.sendline("__name__") |
| 847 |
child.expect_exact("level1_module") |
| 848 |
child.expect("ipdb>") |
| 849 |
|
| 850 |
# Test up with wildcard ignore |
| 851 |
child.sendline("down") |
| 852 |
child.expect_exact( |
| 853 |
"[... skipped 1 frame(s): 0 hidden frames + 1 ignored modules]" |
| 854 |
) |
| 855 |
child.expect("ipdb>") |
| 856 |
child.sendline("unignore_module level2_module") |
| 857 |
child.expect("ipdb>") |
| 858 |
child.sendline("ignore_module level*") |
| 859 |
child.expect("ipdb>") |
| 860 |
child.sendline("up") |
| 861 |
child.expect_exact( |
| 862 |
"[... skipped 2 frame(s): 0 hidden frames + 2 ignored modules]" |
| 863 |
) |
| 864 |
child.expect("ipdb>") |
| 865 |
child.sendline("__name__") |
| 866 |
child.expect_exact("__main__") |
| 867 |
child.expect("ipdb>") |
| 868 |
|
| 869 |
child.sendline("continue") |
| 870 |
child.close() |
| 871 |
|
| 872 |
# Test STEP command |
| 873 |
child = pexpect.spawn(sys.executable, [main_path], env=env, cwd=temp_dir) |
| 874 |
child.timeout = 15 * IPYTHON_TESTING_TIMEOUT_SCALE |
| 875 |
child.expect("ipdb>") |
| 876 |
|
| 877 |
# Test step without ignores (should step into module) |
| 878 |
child.sendline("until 9") |
| 879 |
child.expect("ipdb>") |
| 880 |
child.sendline("step") |
| 881 |
child.expect("ipdb>") |
| 882 |
child.sendline("__name__") |
| 883 |
child.expect_exact("level4_module") |
| 884 |
child.expect("ipdb>") |
| 885 |
|
| 886 |
child.sendline("continue") |
| 887 |
child.close() |
| 888 |
|
| 889 |
# Test step with single module ignore |
| 890 |
child = pexpect.spawn(sys.executable, [main_path], env=env, cwd=temp_dir) |
| 891 |
child.timeout = 15 * IPYTHON_TESTING_TIMEOUT_SCALE |
| 892 |
child.expect("ipdb>") |
| 893 |
|
| 894 |
child.sendline("ignore_module level4_module") |
| 895 |
child.expect("ipdb>") |
| 896 |
child.sendline("until 9") |
| 897 |
child.expect("ipdb>") |
| 898 |
child.sendline("step") |
| 899 |
child.expect_exact("[... skipped 1 ignored module(s)]") |
| 900 |
child.expect("ipdb>") |
| 901 |
child.sendline("__name__") |
| 902 |
child.expect_exact("level3_module") |
| 903 |
child.expect("ipdb>") |
| 904 |
|
| 905 |
child.sendline("continue") |
| 906 |
child.close() |
| 907 |
|
| 908 |
# Test NEXT command |
| 909 |
child = pexpect.spawn(sys.executable, [main_path], env=env, cwd=temp_dir) |
| 910 |
child.timeout = 15 * IPYTHON_TESTING_TIMEOUT_SCALE |
| 911 |
child.expect("ipdb>") |
| 912 |
|
| 913 |
# Test next without ignores |
| 914 |
child.sendline("until 9") |
| 915 |
child.expect("ipdb>") |
| 916 |
child.sendline("next") |
| 917 |
child.expect("ipdb>") |
| 918 |
child.sendline("__name__") |
| 919 |
child.expect_exact("level3_module") |
| 920 |
child.expect("ipdb>") |
| 921 |
|
| 922 |
child.sendline("continue") |
| 923 |
child.close() |
| 924 |
|
| 925 |
# Test next with module ignore |
| 926 |
child = pexpect.spawn(sys.executable, [main_path], env=env, cwd=temp_dir) |
| 927 |
child.timeout = 15 * IPYTHON_TESTING_TIMEOUT_SCALE |
| 928 |
child.expect("ipdb>") |
| 929 |
|
| 930 |
child.sendline("ignore_module level2_module") |
| 931 |
child.expect("ipdb>") |
| 932 |
child.sendline("return") |
| 933 |
child.expect("ipdb>") |
| 934 |
child.sendline("next") |
| 935 |
child.expect_exact("[... skipped 1 ignored module(s)]") |
| 936 |
child.expect("ipdb>") |
| 937 |
|
| 938 |
child.sendline("continue") |
| 939 |
child.close() |