| 1 |
# A test suite for pdb; not very comprehensive at the moment. |
| 2 |
|
| 3 |
import _colorize |
| 4 |
import doctest |
| 5 |
import gc |
| 6 |
import io |
| 7 |
import os |
| 8 |
import pdb |
| 9 |
import sys |
| 10 |
import types |
| 11 |
import codecs |
| 12 |
import unittest |
| 13 |
import subprocess |
| 14 |
import textwrap |
| 15 |
import linecache |
| 16 |
import zipapp |
| 17 |
import zipfile |
| 18 |
|
| 19 |
from asyncio.events import _set_event_loop_policy |
| 20 |
from contextlib import ExitStack, redirect_stdout |
| 21 |
from io import StringIO |
| 22 |
from test import support |
| 23 |
from test.support import has_socket_support, os_helper |
| 24 |
from test.support.import_helper import import_module |
| 25 |
from test.support.pty_helper import run_pty, FakeInput |
| 26 |
from test.support.script_helper import kill_python |
| 27 |
from unittest.mock import patch |
| 28 |
|
| 29 |
SKIP_CORO_TESTS = False |
| 30 |
|
| 31 |
|
| 32 |
class PdbTestInput(object): |
| 33 |
"""Context manager that makes testing Pdb in doctests easier.""" |
| 34 |
|
| 35 |
def __init__(self, input): |
| 36 |
self.input = input |
| 37 |
|
| 38 |
def __enter__(self): |
| 39 |
self.real_stdin = sys.stdin |
| 40 |
sys.stdin = FakeInput(self.input) |
| 41 |
self.orig_trace = sys.gettrace() if hasattr(sys, 'gettrace') else None |
| 42 |
|
| 43 |
def __exit__(self, *exc): |
| 44 |
sys.stdin = self.real_stdin |
| 45 |
if self.orig_trace: |
| 46 |
sys.settrace(self.orig_trace) |
| 47 |
|
| 48 |
|
| 49 |
def test_pdb_displayhook(): |
| 50 |
"""This tests the custom displayhook for pdb. |
| 51 |
|
| 52 |
>>> def test_function(foo, bar): |
| 53 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 54 |
|
| 55 |
>>> with PdbTestInput([ |
| 56 |
... 'foo', |
| 57 |
... 'bar', |
| 58 |
... 'for i in range(5): print(i)', |
| 59 |
... 'continue', |
| 60 |
... ]): |
| 61 |
... test_function(1, None) |
| 62 |
> <doctest test.test_pdb.test_pdb_displayhook[0]>(2)test_function() |
| 63 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 64 |
(Pdb) foo |
| 65 |
1 |
| 66 |
(Pdb) bar |
| 67 |
(Pdb) for i in range(5): print(i) |
| 68 |
0 |
| 69 |
1 |
| 70 |
2 |
| 71 |
3 |
| 72 |
4 |
| 73 |
(Pdb) continue |
| 74 |
""" |
| 75 |
|
| 76 |
|
| 77 |
def test_pdb_basic_commands(): |
| 78 |
"""Test the basic commands of pdb. |
| 79 |
|
| 80 |
>>> def test_function_2(foo, bar='default'): |
| 81 |
... print(foo) |
| 82 |
... for i in range(5): |
| 83 |
... print(i) |
| 84 |
... print(bar) |
| 85 |
... for i in range(10): |
| 86 |
... never_executed |
| 87 |
... print('after for') |
| 88 |
... print('...') |
| 89 |
... return foo.upper() |
| 90 |
|
| 91 |
>>> def test_function3(arg=None, *, kwonly=None): |
| 92 |
... pass |
| 93 |
|
| 94 |
>>> def test_function4(a, b, c, /): |
| 95 |
... pass |
| 96 |
|
| 97 |
>>> def test_function(): |
| 98 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 99 |
... ret = test_function_2('baz') |
| 100 |
... test_function3(kwonly=True) |
| 101 |
... test_function4(1, 2, 3) |
| 102 |
... print(ret) |
| 103 |
|
| 104 |
>>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
| 105 |
... 'step', # go to line ret = test_function_2('baz') |
| 106 |
... 'step', # entering the function call |
| 107 |
... 'args', # display function args |
| 108 |
... 'list', # list function source |
| 109 |
... 'bt', # display backtrace |
| 110 |
... 'up', # step up to test_function() |
| 111 |
... 'down', # step down to test_function_2() again |
| 112 |
... 'next', # stepping to print(foo) |
| 113 |
... 'next', # stepping to the for loop |
| 114 |
... 'step', # stepping into the for loop |
| 115 |
... 'until', # continuing until out of the for loop |
| 116 |
... 'next', # executing the print(bar) |
| 117 |
... 'jump 8', # jump over second for loop |
| 118 |
... 'return', # return out of function |
| 119 |
... 'retval', # display return value |
| 120 |
... 'next', # step to test_function3() |
| 121 |
... 'step', # stepping into test_function3() |
| 122 |
... 'args', # display function args |
| 123 |
... 'return', # return out of function |
| 124 |
... 'next', # step to test_function4() |
| 125 |
... 'step', # stepping to test_function4() |
| 126 |
... 'args', # display function args |
| 127 |
... 'continue', |
| 128 |
... ]): |
| 129 |
... test_function() |
| 130 |
> <doctest test.test_pdb.test_pdb_basic_commands[3]>(2)test_function() |
| 131 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 132 |
(Pdb) step |
| 133 |
> <doctest test.test_pdb.test_pdb_basic_commands[3]>(3)test_function() |
| 134 |
-> ret = test_function_2('baz') |
| 135 |
(Pdb) step |
| 136 |
--Call-- |
| 137 |
> <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2() |
| 138 |
-> def test_function_2(foo, bar='default'): |
| 139 |
(Pdb) args |
| 140 |
foo = 'baz' |
| 141 |
bar = 'default' |
| 142 |
(Pdb) list |
| 143 |
1 -> def test_function_2(foo, bar='default'): |
| 144 |
2 print(foo) |
| 145 |
3 for i in range(5): |
| 146 |
4 print(i) |
| 147 |
5 print(bar) |
| 148 |
6 for i in range(10): |
| 149 |
7 never_executed |
| 150 |
8 print('after for') |
| 151 |
9 print('...') |
| 152 |
10 return foo.upper() |
| 153 |
[EOF] |
| 154 |
(Pdb) bt |
| 155 |
... |
| 156 |
<doctest test.test_pdb.test_pdb_basic_commands[4]>(26)<module>() |
| 157 |
-> test_function() |
| 158 |
<doctest test.test_pdb.test_pdb_basic_commands[3]>(3)test_function() |
| 159 |
-> ret = test_function_2('baz') |
| 160 |
> <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2() |
| 161 |
-> def test_function_2(foo, bar='default'): |
| 162 |
(Pdb) up |
| 163 |
> <doctest test.test_pdb.test_pdb_basic_commands[3]>(3)test_function() |
| 164 |
-> ret = test_function_2('baz') |
| 165 |
(Pdb) down |
| 166 |
> <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2() |
| 167 |
-> def test_function_2(foo, bar='default'): |
| 168 |
(Pdb) next |
| 169 |
> <doctest test.test_pdb.test_pdb_basic_commands[0]>(2)test_function_2() |
| 170 |
-> print(foo) |
| 171 |
(Pdb) next |
| 172 |
baz |
| 173 |
> <doctest test.test_pdb.test_pdb_basic_commands[0]>(3)test_function_2() |
| 174 |
-> for i in range(5): |
| 175 |
(Pdb) step |
| 176 |
> <doctest test.test_pdb.test_pdb_basic_commands[0]>(4)test_function_2() |
| 177 |
-> print(i) |
| 178 |
(Pdb) until |
| 179 |
0 |
| 180 |
1 |
| 181 |
2 |
| 182 |
3 |
| 183 |
4 |
| 184 |
> <doctest test.test_pdb.test_pdb_basic_commands[0]>(5)test_function_2() |
| 185 |
-> print(bar) |
| 186 |
(Pdb) next |
| 187 |
default |
| 188 |
> <doctest test.test_pdb.test_pdb_basic_commands[0]>(6)test_function_2() |
| 189 |
-> for i in range(10): |
| 190 |
(Pdb) jump 8 |
| 191 |
> <doctest test.test_pdb.test_pdb_basic_commands[0]>(8)test_function_2() |
| 192 |
-> print('after for') |
| 193 |
(Pdb) return |
| 194 |
after for |
| 195 |
... |
| 196 |
--Return-- |
| 197 |
> <doctest test.test_pdb.test_pdb_basic_commands[0]>(10)test_function_2()->'BAZ' |
| 198 |
-> return foo.upper() |
| 199 |
(Pdb) retval |
| 200 |
'BAZ' |
| 201 |
(Pdb) next |
| 202 |
> <doctest test.test_pdb.test_pdb_basic_commands[3]>(4)test_function() |
| 203 |
-> test_function3(kwonly=True) |
| 204 |
(Pdb) step |
| 205 |
--Call-- |
| 206 |
> <doctest test.test_pdb.test_pdb_basic_commands[1]>(1)test_function3() |
| 207 |
-> def test_function3(arg=None, *, kwonly=None): |
| 208 |
(Pdb) args |
| 209 |
arg = None |
| 210 |
kwonly = True |
| 211 |
(Pdb) return |
| 212 |
--Return-- |
| 213 |
> <doctest test.test_pdb.test_pdb_basic_commands[1]>(2)test_function3()->None |
| 214 |
-> pass |
| 215 |
(Pdb) next |
| 216 |
> <doctest test.test_pdb.test_pdb_basic_commands[3]>(5)test_function() |
| 217 |
-> test_function4(1, 2, 3) |
| 218 |
(Pdb) step |
| 219 |
--Call-- |
| 220 |
> <doctest test.test_pdb.test_pdb_basic_commands[2]>(1)test_function4() |
| 221 |
-> def test_function4(a, b, c, /): |
| 222 |
(Pdb) args |
| 223 |
a = 1 |
| 224 |
b = 2 |
| 225 |
c = 3 |
| 226 |
(Pdb) continue |
| 227 |
BAZ |
| 228 |
""" |
| 229 |
|
| 230 |
def test_pdb_breakpoint_commands(): |
| 231 |
"""Test basic commands related to breakpoints. |
| 232 |
|
| 233 |
>>> def test_function(): |
| 234 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 235 |
... print(1) |
| 236 |
... print(2) |
| 237 |
... print(3) |
| 238 |
... print(4) |
| 239 |
|
| 240 |
Now test the breakpoint commands. NORMALIZE_WHITESPACE is needed because |
| 241 |
the breakpoint list outputs a tab for the "stop only" and "ignore next" |
| 242 |
lines, which we don't want to put in here. |
| 243 |
|
| 244 |
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE |
| 245 |
... 'break 3', |
| 246 |
... 'break 4, +', |
| 247 |
... 'disable 1', |
| 248 |
... 'ignore 1 10', |
| 249 |
... 'condition 1 1 < 2', |
| 250 |
... 'condition 1 1 <', |
| 251 |
... 'break 4', |
| 252 |
... 'break 4', |
| 253 |
... 'break', |
| 254 |
... 'clear 3', |
| 255 |
... 'break', |
| 256 |
... 'condition 1', |
| 257 |
... 'commands 1', |
| 258 |
... 'EOF', # Simulate Ctrl-D/Ctrl-Z from user, should end input |
| 259 |
... 'enable 1', |
| 260 |
... 'clear 1', |
| 261 |
... 'commands 2', |
| 262 |
... 'p "42"', |
| 263 |
... 'print("42", 7*6)', # Issue 18764 (not about breakpoints) |
| 264 |
... 'end', |
| 265 |
... 'continue', # will stop at breakpoint 2 (line 4) |
| 266 |
... 'clear', # clear all! |
| 267 |
... 'y', |
| 268 |
... 'tbreak 5', |
| 269 |
... 'continue', # will stop at temporary breakpoint |
| 270 |
... 'break', # make sure breakpoint is gone |
| 271 |
... 'commands 10', # out of range |
| 272 |
... 'commands a', # display help |
| 273 |
... 'commands 4', # already deleted |
| 274 |
... 'break 6, undefined', # condition causing `NameError` during evaluation |
| 275 |
... 'continue', # will stop, ignoring runtime error |
| 276 |
... 'continue', |
| 277 |
... ]): |
| 278 |
... test_function() |
| 279 |
> <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(2)test_function() |
| 280 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 281 |
(Pdb) break 3 |
| 282 |
Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 |
| 283 |
(Pdb) break 4, + |
| 284 |
*** Invalid condition +: SyntaxError: invalid syntax |
| 285 |
(Pdb) disable 1 |
| 286 |
Disabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 |
| 287 |
(Pdb) ignore 1 10 |
| 288 |
Will ignore next 10 crossings of breakpoint 1. |
| 289 |
(Pdb) condition 1 1 < 2 |
| 290 |
New condition set for breakpoint 1. |
| 291 |
(Pdb) condition 1 1 < |
| 292 |
*** Invalid condition 1 <: SyntaxError: invalid syntax |
| 293 |
(Pdb) break 4 |
| 294 |
Breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 |
| 295 |
(Pdb) break 4 |
| 296 |
Breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 |
| 297 |
(Pdb) break |
| 298 |
Num Type Disp Enb Where |
| 299 |
1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 |
| 300 |
stop only if 1 < 2 |
| 301 |
ignore next 10 hits |
| 302 |
2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 |
| 303 |
3 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 |
| 304 |
(Pdb) clear 3 |
| 305 |
Deleted breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 |
| 306 |
(Pdb) break |
| 307 |
Num Type Disp Enb Where |
| 308 |
1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 |
| 309 |
stop only if 1 < 2 |
| 310 |
ignore next 10 hits |
| 311 |
2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 |
| 312 |
(Pdb) condition 1 |
| 313 |
Breakpoint 1 is now unconditional. |
| 314 |
(Pdb) commands 1 |
| 315 |
(com) EOF |
| 316 |
<BLANKLINE> |
| 317 |
(Pdb) enable 1 |
| 318 |
Enabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 |
| 319 |
(Pdb) clear 1 |
| 320 |
Deleted breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 |
| 321 |
(Pdb) commands 2 |
| 322 |
(com) p "42" |
| 323 |
(com) print("42", 7*6) |
| 324 |
(com) end |
| 325 |
(Pdb) continue |
| 326 |
1 |
| 327 |
'42' |
| 328 |
42 42 |
| 329 |
> <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(4)test_function() |
| 330 |
-> print(2) |
| 331 |
(Pdb) clear |
| 332 |
Clear all breaks? y |
| 333 |
Deleted breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 |
| 334 |
(Pdb) tbreak 5 |
| 335 |
Breakpoint 4 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5 |
| 336 |
(Pdb) continue |
| 337 |
2 |
| 338 |
Deleted breakpoint 4 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5 |
| 339 |
> <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(5)test_function() |
| 340 |
-> print(3) |
| 341 |
(Pdb) break |
| 342 |
(Pdb) commands 10 |
| 343 |
*** cannot set commands: Breakpoint number 10 out of range |
| 344 |
(Pdb) commands a |
| 345 |
*** Invalid argument: a |
| 346 |
Usage: (Pdb) commands [bpnumber] |
| 347 |
(com) ... |
| 348 |
(com) end |
| 349 |
(Pdb) |
| 350 |
(Pdb) commands 4 |
| 351 |
*** cannot set commands: Breakpoint 4 already deleted |
| 352 |
(Pdb) break 6, undefined |
| 353 |
Breakpoint 5 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:6 |
| 354 |
(Pdb) continue |
| 355 |
3 |
| 356 |
> <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(6)test_function() |
| 357 |
-> print(4) |
| 358 |
(Pdb) continue |
| 359 |
4 |
| 360 |
""" |
| 361 |
|
| 362 |
def test_pdb_breakpoint_ignore_and_condition(): |
| 363 |
""" |
| 364 |
>>> def test_function(): |
| 365 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 366 |
... for i in range(5): |
| 367 |
... print(i) |
| 368 |
|
| 369 |
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE |
| 370 |
... 'break 4', |
| 371 |
... 'ignore 1 2', # ignore once |
| 372 |
... 'continue', |
| 373 |
... 'condition 1 i == 4', |
| 374 |
... 'continue', |
| 375 |
... 'clear 1', |
| 376 |
... 'continue', |
| 377 |
... ]): |
| 378 |
... test_function() |
| 379 |
> <doctest test.test_pdb.test_pdb_breakpoint_ignore_and_condition[0]>(2)test_function() |
| 380 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 381 |
(Pdb) break 4 |
| 382 |
Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_ignore_and_condition[0]>:4 |
| 383 |
(Pdb) ignore 1 2 |
| 384 |
Will ignore next 2 crossings of breakpoint 1. |
| 385 |
(Pdb) continue |
| 386 |
0 |
| 387 |
1 |
| 388 |
> <doctest test.test_pdb.test_pdb_breakpoint_ignore_and_condition[0]>(4)test_function() |
| 389 |
-> print(i) |
| 390 |
(Pdb) condition 1 i == 4 |
| 391 |
New condition set for breakpoint 1. |
| 392 |
(Pdb) continue |
| 393 |
2 |
| 394 |
3 |
| 395 |
> <doctest test.test_pdb.test_pdb_breakpoint_ignore_and_condition[0]>(4)test_function() |
| 396 |
-> print(i) |
| 397 |
(Pdb) clear 1 |
| 398 |
Deleted breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_ignore_and_condition[0]>:4 |
| 399 |
(Pdb) continue |
| 400 |
4 |
| 401 |
""" |
| 402 |
|
| 403 |
def test_pdb_breakpoint_on_annotated_function_def(): |
| 404 |
"""Test breakpoints on function definitions with annotation. |
| 405 |
|
| 406 |
>>> def foo[T](): |
| 407 |
... return 0 |
| 408 |
|
| 409 |
>>> def bar() -> int: |
| 410 |
... return 0 |
| 411 |
|
| 412 |
>>> def foobar[T]() -> int: |
| 413 |
... return 0 |
| 414 |
|
| 415 |
>>> def test_function(): |
| 416 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 417 |
... pass |
| 418 |
|
| 419 |
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE |
| 420 |
... 'break foo', |
| 421 |
... 'break bar', |
| 422 |
... 'break foobar', |
| 423 |
... 'continue', |
| 424 |
... ]): |
| 425 |
... test_function() |
| 426 |
> <doctest test.test_pdb.test_pdb_breakpoint_on_annotated_function_def[3]>(2)test_function() |
| 427 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 428 |
(Pdb) break foo |
| 429 |
Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_on_annotated_function_def[0]>:2 |
| 430 |
(Pdb) break bar |
| 431 |
Breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_on_annotated_function_def[1]>:2 |
| 432 |
(Pdb) break foobar |
| 433 |
Breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_on_annotated_function_def[2]>:2 |
| 434 |
(Pdb) continue |
| 435 |
""" |
| 436 |
|
| 437 |
def test_pdb_commands(): |
| 438 |
"""Test the commands command of pdb. |
| 439 |
|
| 440 |
>>> def test_function(): |
| 441 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 442 |
... print(1) |
| 443 |
... print(2) |
| 444 |
... print(3) |
| 445 |
|
| 446 |
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE |
| 447 |
... 'b 3', |
| 448 |
... 'commands', |
| 449 |
... 'silent', # suppress the frame status output |
| 450 |
... 'p "hello"', |
| 451 |
... 'end', |
| 452 |
... 'b 4', |
| 453 |
... 'commands', |
| 454 |
... 'until 5', # no output, should stop at line 5 |
| 455 |
... 'continue', # hit breakpoint at line 3 |
| 456 |
... '', # repeat continue, hit breakpoint at line 4 then `until` to line 5 |
| 457 |
... '', |
| 458 |
... ]): |
| 459 |
... test_function() |
| 460 |
> <doctest test.test_pdb.test_pdb_commands[0]>(2)test_function() |
| 461 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 462 |
(Pdb) b 3 |
| 463 |
Breakpoint 1 at <doctest test.test_pdb.test_pdb_commands[0]>:3 |
| 464 |
(Pdb) commands |
| 465 |
(com) silent |
| 466 |
(com) p "hello" |
| 467 |
(com) end |
| 468 |
(Pdb) b 4 |
| 469 |
Breakpoint 2 at <doctest test.test_pdb.test_pdb_commands[0]>:4 |
| 470 |
(Pdb) commands |
| 471 |
(com) until 5 |
| 472 |
(Pdb) continue |
| 473 |
'hello' |
| 474 |
(Pdb) |
| 475 |
1 |
| 476 |
2 |
| 477 |
> <doctest test.test_pdb.test_pdb_commands[0]>(5)test_function() |
| 478 |
-> print(3) |
| 479 |
(Pdb) |
| 480 |
3 |
| 481 |
""" |
| 482 |
|
| 483 |
def test_pdb_breakpoint_with_filename(): |
| 484 |
"""Breakpoints with filename:lineno |
| 485 |
|
| 486 |
>>> def test_function(): |
| 487 |
... # inspect_fodder2 is a great module as the line number is stable |
| 488 |
... from test.test_inspect import inspect_fodder2 as mod2 |
| 489 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 490 |
... mod2.func88() |
| 491 |
... mod2.func114() |
| 492 |
|
| 493 |
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS |
| 494 |
... 'break test.test_inspect.inspect_fodder2:90', |
| 495 |
... 'continue', # will stop at func88 |
| 496 |
... 'break test/test_inspect/inspect_fodder2.py:115', |
| 497 |
... 'continue', # will stop at func114 |
| 498 |
... 'continue', |
| 499 |
... ]): |
| 500 |
... test_function() |
| 501 |
> <doctest test.test_pdb.test_pdb_breakpoint_with_filename[0]>(4)test_function() |
| 502 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 503 |
(Pdb) break test.test_inspect.inspect_fodder2:90 |
| 504 |
Breakpoint 1 at ...inspect_fodder2.py:90 |
| 505 |
(Pdb) continue |
| 506 |
> ...inspect_fodder2.py(90)func88() |
| 507 |
-> return 90 |
| 508 |
(Pdb) break test/test_inspect/inspect_fodder2.py:115 |
| 509 |
Breakpoint 2 at ...inspect_fodder2.py:115 |
| 510 |
(Pdb) continue |
| 511 |
> ...inspect_fodder2.py(115)func114() |
| 512 |
-> return 115 |
| 513 |
(Pdb) continue |
| 514 |
""" |
| 515 |
|
| 516 |
def test_pdb_breakpoint_on_disabled_line(): |
| 517 |
"""New breakpoint on once disabled line should work |
| 518 |
|
| 519 |
>>> def test_function(): |
| 520 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 521 |
... for i in range(3): |
| 522 |
... j = i * 2 |
| 523 |
... print(j) |
| 524 |
|
| 525 |
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE |
| 526 |
... 'break 5', |
| 527 |
... 'c', |
| 528 |
... 'clear 1', |
| 529 |
... 'break 4', |
| 530 |
... 'c', |
| 531 |
... 'clear 2', |
| 532 |
... 'c' |
| 533 |
... ]): |
| 534 |
... test_function() |
| 535 |
> <doctest test.test_pdb.test_pdb_breakpoint_on_disabled_line[0]>(2)test_function() |
| 536 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 537 |
(Pdb) break 5 |
| 538 |
Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_on_disabled_line[0]>:5 |
| 539 |
(Pdb) c |
| 540 |
> <doctest test.test_pdb.test_pdb_breakpoint_on_disabled_line[0]>(5)test_function() |
| 541 |
-> print(j) |
| 542 |
(Pdb) clear 1 |
| 543 |
Deleted breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_on_disabled_line[0]>:5 |
| 544 |
(Pdb) break 4 |
| 545 |
Breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_on_disabled_line[0]>:4 |
| 546 |
(Pdb) c |
| 547 |
0 |
| 548 |
> <doctest test.test_pdb.test_pdb_breakpoint_on_disabled_line[0]>(4)test_function() |
| 549 |
-> j = i * 2 |
| 550 |
(Pdb) clear 2 |
| 551 |
Deleted breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_on_disabled_line[0]>:4 |
| 552 |
(Pdb) c |
| 553 |
2 |
| 554 |
4 |
| 555 |
""" |
| 556 |
|
| 557 |
def test_pdb_breakpoints_preserved_across_interactive_sessions(): |
| 558 |
"""Breakpoints are remembered between interactive sessions |
| 559 |
|
| 560 |
>>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
| 561 |
... 'import test.test_pdb', |
| 562 |
... 'break test.test_pdb.do_something', |
| 563 |
... 'break test.test_pdb.do_nothing', |
| 564 |
... 'break', |
| 565 |
... 'continue', |
| 566 |
... ]): |
| 567 |
... pdb.run('print()') |
| 568 |
> <string>(1)<module>()... |
| 569 |
(Pdb) import test.test_pdb |
| 570 |
(Pdb) break test.test_pdb.do_something |
| 571 |
Breakpoint 1 at ...test_pdb.py:... |
| 572 |
(Pdb) break test.test_pdb.do_nothing |
| 573 |
Breakpoint 2 at ...test_pdb.py:... |
| 574 |
(Pdb) break |
| 575 |
Num Type Disp Enb Where |
| 576 |
1 breakpoint keep yes at ...test_pdb.py:... |
| 577 |
2 breakpoint keep yes at ...test_pdb.py:... |
| 578 |
(Pdb) continue |
| 579 |
|
| 580 |
>>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
| 581 |
... 'break', |
| 582 |
... 'break pdb.find_function', |
| 583 |
... 'break', |
| 584 |
... 'clear 1', |
| 585 |
... 'continue', |
| 586 |
... ]): |
| 587 |
... pdb.run('print()') |
| 588 |
> <string>(1)<module>()... |
| 589 |
(Pdb) break |
| 590 |
Num Type Disp Enb Where |
| 591 |
1 breakpoint keep yes at ...test_pdb.py:... |
| 592 |
2 breakpoint keep yes at ...test_pdb.py:... |
| 593 |
(Pdb) break pdb.find_function |
| 594 |
Breakpoint 3 at ...pdb.py:... |
| 595 |
(Pdb) break |
| 596 |
Num Type Disp Enb Where |
| 597 |
1 breakpoint keep yes at ...test_pdb.py:... |
| 598 |
2 breakpoint keep yes at ...test_pdb.py:... |
| 599 |
3 breakpoint keep yes at ...pdb.py:... |
| 600 |
(Pdb) clear 1 |
| 601 |
Deleted breakpoint 1 at ...test_pdb.py:... |
| 602 |
(Pdb) continue |
| 603 |
|
| 604 |
>>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
| 605 |
... 'break', |
| 606 |
... 'clear 2', |
| 607 |
... 'clear 3', |
| 608 |
... 'continue', |
| 609 |
... ]): |
| 610 |
... pdb.run('print()') |
| 611 |
> <string>(1)<module>()... |
| 612 |
(Pdb) break |
| 613 |
Num Type Disp Enb Where |
| 614 |
2 breakpoint keep yes at ...test_pdb.py:... |
| 615 |
3 breakpoint keep yes at ...pdb.py:... |
| 616 |
(Pdb) clear 2 |
| 617 |
Deleted breakpoint 2 at ...test_pdb.py:... |
| 618 |
(Pdb) clear 3 |
| 619 |
Deleted breakpoint 3 at ...pdb.py:... |
| 620 |
(Pdb) continue |
| 621 |
""" |
| 622 |
|
| 623 |
def test_pdb_break_anywhere(): |
| 624 |
"""Test break_anywhere() method of Pdb. |
| 625 |
|
| 626 |
>>> def outer(): |
| 627 |
... def inner(): |
| 628 |
... import pdb |
| 629 |
... import sys |
| 630 |
... p = pdb.Pdb(nosigint=True, readrc=False) |
| 631 |
... p.set_trace() |
| 632 |
... frame = sys._getframe() |
| 633 |
... print(p.break_anywhere(frame)) # inner |
| 634 |
... print(p.break_anywhere(frame.f_back)) # outer |
| 635 |
... print(p.break_anywhere(frame.f_back.f_back)) # caller |
| 636 |
... inner() |
| 637 |
|
| 638 |
>>> def caller(): |
| 639 |
... outer() |
| 640 |
|
| 641 |
>>> def test_function(): |
| 642 |
... caller() |
| 643 |
|
| 644 |
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE |
| 645 |
... 'b 3', |
| 646 |
... 'c', |
| 647 |
... ]): |
| 648 |
... test_function() |
| 649 |
> <doctest test.test_pdb.test_pdb_break_anywhere[0]>(6)inner() |
| 650 |
-> p.set_trace() |
| 651 |
(Pdb) b 3 |
| 652 |
Breakpoint 1 at <doctest test.test_pdb.test_pdb_break_anywhere[0]>:3 |
| 653 |
(Pdb) c |
| 654 |
True |
| 655 |
False |
| 656 |
False |
| 657 |
""" |
| 658 |
|
| 659 |
def test_pdb_pp_repr_exc(): |
| 660 |
"""Test that do_p/do_pp do not swallow exceptions. |
| 661 |
|
| 662 |
>>> class BadRepr: |
| 663 |
... def __repr__(self): |
| 664 |
... raise Exception('repr_exc') |
| 665 |
>>> obj = BadRepr() |
| 666 |
|
| 667 |
>>> def test_function(): |
| 668 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 669 |
|
| 670 |
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE |
| 671 |
... 'p obj', |
| 672 |
... 'pp obj', |
| 673 |
... 'continue', |
| 674 |
... ]): |
| 675 |
... test_function() |
| 676 |
> <doctest test.test_pdb.test_pdb_pp_repr_exc[2]>(2)test_function() |
| 677 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 678 |
(Pdb) p obj |
| 679 |
*** Exception: repr_exc |
| 680 |
(Pdb) pp obj |
| 681 |
*** Exception: repr_exc |
| 682 |
(Pdb) continue |
| 683 |
""" |
| 684 |
|
| 685 |
def test_pdb_empty_line(): |
| 686 |
"""Test that empty line repeats the last command. |
| 687 |
|
| 688 |
>>> def test_function(): |
| 689 |
... x = 1 |
| 690 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 691 |
... y = 2 |
| 692 |
|
| 693 |
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE |
| 694 |
... 'p x', |
| 695 |
... '', # Should repeat p x |
| 696 |
... 'n ;; p 0 ;; p x', # Fill cmdqueue with multiple commands |
| 697 |
... '', # Should still repeat p x |
| 698 |
... 'continue', |
| 699 |
... ]): |
| 700 |
... test_function() |
| 701 |
> <doctest test.test_pdb.test_pdb_empty_line[0]>(3)test_function() |
| 702 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 703 |
(Pdb) p x |
| 704 |
1 |
| 705 |
(Pdb) |
| 706 |
1 |
| 707 |
(Pdb) n ;; p 0 ;; p x |
| 708 |
0 |
| 709 |
1 |
| 710 |
> <doctest test.test_pdb.test_pdb_empty_line[0]>(4)test_function() |
| 711 |
-> y = 2 |
| 712 |
(Pdb) |
| 713 |
1 |
| 714 |
(Pdb) continue |
| 715 |
""" |
| 716 |
|
| 717 |
def do_nothing(): |
| 718 |
pass |
| 719 |
|
| 720 |
def do_something(): |
| 721 |
print(42) |
| 722 |
|
| 723 |
def test_list_commands(): |
| 724 |
"""Test the list and source commands of pdb. |
| 725 |
|
| 726 |
>>> def test_function_2(foo): |
| 727 |
... import test.test_pdb |
| 728 |
... test.test_pdb.do_nothing() |
| 729 |
... 'some...' |
| 730 |
... 'more...' |
| 731 |
... 'code...' |
| 732 |
... 'to...' |
| 733 |
... 'make...' |
| 734 |
... 'a...' |
| 735 |
... 'long...' |
| 736 |
... 'listing...' |
| 737 |
... 'useful...' |
| 738 |
... '...' |
| 739 |
... '...' |
| 740 |
... return foo |
| 741 |
|
| 742 |
>>> def test_function(): |
| 743 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 744 |
... ret = test_function_2('baz') |
| 745 |
|
| 746 |
>>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
| 747 |
... 'step', # go to the test function line |
| 748 |
... 'list', # list first function |
| 749 |
... 'step', # step into second function |
| 750 |
... 'list', # list second function |
| 751 |
... 'list', # continue listing to EOF |
| 752 |
... 'list 1,3', # list specific lines |
| 753 |
... 'list x', # invalid argument |
| 754 |
... 'next', # step to import |
| 755 |
... 'next', # step over import |
| 756 |
... 'step', # step into do_nothing |
| 757 |
... 'longlist', # list all lines |
| 758 |
... 'source do_something', # list all lines of function |
| 759 |
... 'source fooxxx', # something that doesn't exit |
| 760 |
... 'continue', |
| 761 |
... ]): |
| 762 |
... test_function() |
| 763 |
> <doctest test.test_pdb.test_list_commands[1]>(2)test_function() |
| 764 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 765 |
(Pdb) step |
| 766 |
> <doctest test.test_pdb.test_list_commands[1]>(3)test_function() |
| 767 |
-> ret = test_function_2('baz') |
| 768 |
(Pdb) list |
| 769 |
1 def test_function(): |
| 770 |
2 import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 771 |
3 -> ret = test_function_2('baz') |
| 772 |
[EOF] |
| 773 |
(Pdb) step |
| 774 |
--Call-- |
| 775 |
> <doctest test.test_pdb.test_list_commands[0]>(1)test_function_2() |
| 776 |
-> def test_function_2(foo): |
| 777 |
(Pdb) list |
| 778 |
1 -> def test_function_2(foo): |
| 779 |
2 import test.test_pdb |
| 780 |
3 test.test_pdb.do_nothing() |
| 781 |
4 'some...' |
| 782 |
5 'more...' |
| 783 |
6 'code...' |
| 784 |
7 'to...' |
| 785 |
8 'make...' |
| 786 |
9 'a...' |
| 787 |
10 'long...' |
| 788 |
11 'listing...' |
| 789 |
(Pdb) list |
| 790 |
12 'useful...' |
| 791 |
13 '...' |
| 792 |
14 '...' |
| 793 |
15 return foo |
| 794 |
[EOF] |
| 795 |
(Pdb) list 1,3 |
| 796 |
1 -> def test_function_2(foo): |
| 797 |
2 import test.test_pdb |
| 798 |
3 test.test_pdb.do_nothing() |
| 799 |
(Pdb) list x |
| 800 |
*** ... |
| 801 |
(Pdb) next |
| 802 |
> <doctest test.test_pdb.test_list_commands[0]>(2)test_function_2() |
| 803 |
-> import test.test_pdb |
| 804 |
(Pdb) next |
| 805 |
> <doctest test.test_pdb.test_list_commands[0]>(3)test_function_2() |
| 806 |
-> test.test_pdb.do_nothing() |
| 807 |
(Pdb) step |
| 808 |
--Call-- |
| 809 |
> ...test_pdb.py(...)do_nothing() |
| 810 |
-> def do_nothing(): |
| 811 |
(Pdb) longlist |
| 812 |
... -> def do_nothing(): |
| 813 |
... pass |
| 814 |
(Pdb) source do_something |
| 815 |
... def do_something(): |
| 816 |
... print(42) |
| 817 |
(Pdb) source fooxxx |
| 818 |
*** ... |
| 819 |
(Pdb) continue |
| 820 |
""" |
| 821 |
|
| 822 |
def test_pdb_whatis_command(): |
| 823 |
"""Test the whatis command |
| 824 |
|
| 825 |
>>> myvar = (1,2) |
| 826 |
>>> def myfunc(): |
| 827 |
... pass |
| 828 |
|
| 829 |
>>> class MyClass: |
| 830 |
... def mymethod(self): |
| 831 |
... pass |
| 832 |
|
| 833 |
>>> def test_function(): |
| 834 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 835 |
|
| 836 |
>>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
| 837 |
... 'whatis myvar', |
| 838 |
... 'whatis myfunc', |
| 839 |
... 'whatis MyClass', |
| 840 |
... 'whatis MyClass()', |
| 841 |
... 'whatis MyClass.mymethod', |
| 842 |
... 'whatis MyClass().mymethod', |
| 843 |
... 'continue', |
| 844 |
... ]): |
| 845 |
... test_function() |
| 846 |
> <doctest test.test_pdb.test_pdb_whatis_command[3]>(2)test_function() |
| 847 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 848 |
(Pdb) whatis myvar |
| 849 |
<class 'tuple'> |
| 850 |
(Pdb) whatis myfunc |
| 851 |
Function myfunc |
| 852 |
(Pdb) whatis MyClass |
| 853 |
Class test.test_pdb.MyClass |
| 854 |
(Pdb) whatis MyClass() |
| 855 |
<class 'test.test_pdb.MyClass'> |
| 856 |
(Pdb) whatis MyClass.mymethod |
| 857 |
Function mymethod |
| 858 |
(Pdb) whatis MyClass().mymethod |
| 859 |
Method mymethod |
| 860 |
(Pdb) continue |
| 861 |
""" |
| 862 |
|
| 863 |
def test_pdb_display_command(): |
| 864 |
"""Test display command |
| 865 |
|
| 866 |
>>> def test_function(): |
| 867 |
... a = 0 |
| 868 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 869 |
... a = 1 |
| 870 |
... a = 2 |
| 871 |
... a = 3 |
| 872 |
... a = 4 |
| 873 |
|
| 874 |
>>> with PdbTestInput([ # doctest: +ELLIPSIS |
| 875 |
... 's', |
| 876 |
... 'display +', |
| 877 |
... 'display', |
| 878 |
... 'display a', |
| 879 |
... 'n', |
| 880 |
... 'display', |
| 881 |
... 'undisplay a', |
| 882 |
... 'n', |
| 883 |
... 'display a', |
| 884 |
... 'undisplay', |
| 885 |
... 'display a < 1', |
| 886 |
... 'n', |
| 887 |
... 'display undefined', |
| 888 |
... 'continue', |
| 889 |
... ]): |
| 890 |
... test_function() |
| 891 |
> <doctest test.test_pdb.test_pdb_display_command[0]>(3)test_function() |
| 892 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 893 |
(Pdb) s |
| 894 |
> <doctest test.test_pdb.test_pdb_display_command[0]>(4)test_function() |
| 895 |
-> a = 1 |
| 896 |
(Pdb) display + |
| 897 |
*** Unable to display +: SyntaxError: invalid syntax |
| 898 |
(Pdb) display |
| 899 |
No expression is being displayed |
| 900 |
(Pdb) display a |
| 901 |
display a: 0 |
| 902 |
(Pdb) n |
| 903 |
> <doctest test.test_pdb.test_pdb_display_command[0]>(5)test_function() |
| 904 |
-> a = 2 |
| 905 |
display a: 1 [old: 0] |
| 906 |
(Pdb) display |
| 907 |
Currently displaying: |
| 908 |
a: 1 |
| 909 |
(Pdb) undisplay a |
| 910 |
(Pdb) n |
| 911 |
> <doctest test.test_pdb.test_pdb_display_command[0]>(6)test_function() |
| 912 |
-> a = 3 |
| 913 |
(Pdb) display a |
| 914 |
display a: 2 |
| 915 |
(Pdb) undisplay |
| 916 |
(Pdb) display a < 1 |
| 917 |
display a < 1: False |
| 918 |
(Pdb) n |
| 919 |
> <doctest test.test_pdb.test_pdb_display_command[0]>(7)test_function() |
| 920 |
-> a = 4 |
| 921 |
(Pdb) display undefined |
| 922 |
display undefined: ** raised NameError: name 'undefined' is not defined ** |
| 923 |
(Pdb) continue |
| 924 |
""" |
| 925 |
|
| 926 |
def test_pdb_alias_command(): |
| 927 |
"""Test alias command |
| 928 |
|
| 929 |
>>> class A: |
| 930 |
... def __init__(self): |
| 931 |
... self.attr1 = 10 |
| 932 |
... self.attr2 = 'str' |
| 933 |
... def method(self): |
| 934 |
... pass |
| 935 |
|
| 936 |
>>> def test_function(): |
| 937 |
... o = A() |
| 938 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 939 |
... o.method() |
| 940 |
|
| 941 |
>>> with PdbTestInput([ # doctest: +ELLIPSIS |
| 942 |
... 's', |
| 943 |
... 'alias pi', |
| 944 |
... 'alias pi for k in %1.__dict__.keys(): print(f"%1.{k} = {%1.__dict__[k]}")', |
| 945 |
... 'alias ps pi self', |
| 946 |
... 'alias ps', |
| 947 |
... 'pi o', |
| 948 |
... 's', |
| 949 |
... 'ps', |
| 950 |
... 'alias myp p %2', |
| 951 |
... 'alias myp', |
| 952 |
... 'alias myp p %1', |
| 953 |
... 'myp', |
| 954 |
... 'myp 1', |
| 955 |
... 'myp 1 2', |
| 956 |
... 'alias repeat_second_arg p "%* %2"', |
| 957 |
... 'repeat_second_arg 1 2 3', |
| 958 |
... 'continue', |
| 959 |
... ]): |
| 960 |
... test_function() |
| 961 |
> <doctest test.test_pdb.test_pdb_alias_command[1]>(3)test_function() |
| 962 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 963 |
(Pdb) s |
| 964 |
> <doctest test.test_pdb.test_pdb_alias_command[1]>(4)test_function() |
| 965 |
-> o.method() |
| 966 |
(Pdb) alias pi |
| 967 |
*** Unknown alias 'pi' |
| 968 |
(Pdb) alias pi for k in %1.__dict__.keys(): print(f"%1.{k} = {%1.__dict__[k]}") |
| 969 |
(Pdb) alias ps pi self |
| 970 |
(Pdb) alias ps |
| 971 |
ps = pi self |
| 972 |
(Pdb) pi o |
| 973 |
o.attr1 = 10 |
| 974 |
o.attr2 = str |
| 975 |
(Pdb) s |
| 976 |
--Call-- |
| 977 |
> <doctest test.test_pdb.test_pdb_alias_command[0]>(5)method() |
| 978 |
-> def method(self): |
| 979 |
(Pdb) ps |
| 980 |
self.attr1 = 10 |
| 981 |
self.attr2 = str |
| 982 |
(Pdb) alias myp p %2 |
| 983 |
*** Replaceable parameters must be consecutive |
| 984 |
(Pdb) alias myp |
| 985 |
*** Unknown alias 'myp' |
| 986 |
(Pdb) alias myp p %1 |
| 987 |
(Pdb) myp |
| 988 |
*** Not enough arguments for alias 'myp' |
| 989 |
(Pdb) myp 1 |
| 990 |
1 |
| 991 |
(Pdb) myp 1 2 |
| 992 |
*** Too many arguments for alias 'myp' |
| 993 |
(Pdb) alias repeat_second_arg p "%* %2" |
| 994 |
(Pdb) repeat_second_arg 1 2 3 |
| 995 |
'1 2 3 2' |
| 996 |
(Pdb) continue |
| 997 |
""" |
| 998 |
|
| 999 |
def test_pdb_where_command(): |
| 1000 |
"""Test where command |
| 1001 |
|
| 1002 |
>>> def g(): |
| 1003 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 1004 |
|
| 1005 |
>>> def f(): |
| 1006 |
... g() |
| 1007 |
|
| 1008 |
>>> def test_function(): |
| 1009 |
... f() |
| 1010 |
|
| 1011 |
>>> with PdbTestInput([ # doctest: +ELLIPSIS |
| 1012 |
... 'w', |
| 1013 |
... 'where', |
| 1014 |
... 'w 1', |
| 1015 |
... 'w invalid', |
| 1016 |
... 'u', |
| 1017 |
... 'w', |
| 1018 |
... 'w 0', |
| 1019 |
... 'w 100', |
| 1020 |
... 'w -100', |
| 1021 |
... 'continue', |
| 1022 |
... ]): |
| 1023 |
... test_function() |
| 1024 |
> <doctest test.test_pdb.test_pdb_where_command[0]>(2)g() |
| 1025 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 1026 |
(Pdb) w |
| 1027 |
... |
| 1028 |
<doctest test.test_pdb.test_pdb_where_command[3]>(13)<module>() |
| 1029 |
-> test_function() |
| 1030 |
<doctest test.test_pdb.test_pdb_where_command[2]>(2)test_function() |
| 1031 |
-> f() |
| 1032 |
<doctest test.test_pdb.test_pdb_where_command[1]>(2)f() |
| 1033 |
-> g() |
| 1034 |
> <doctest test.test_pdb.test_pdb_where_command[0]>(2)g() |
| 1035 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 1036 |
(Pdb) where |
| 1037 |
... |
| 1038 |
<doctest test.test_pdb.test_pdb_where_command[3]>(13)<module>() |
| 1039 |
-> test_function() |
| 1040 |
<doctest test.test_pdb.test_pdb_where_command[2]>(2)test_function() |
| 1041 |
-> f() |
| 1042 |
<doctest test.test_pdb.test_pdb_where_command[1]>(2)f() |
| 1043 |
-> g() |
| 1044 |
> <doctest test.test_pdb.test_pdb_where_command[0]>(2)g() |
| 1045 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 1046 |
(Pdb) w 1 |
| 1047 |
> <doctest test.test_pdb.test_pdb_where_command[0]>(2)g() |
| 1048 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 1049 |
(Pdb) w invalid |
| 1050 |
*** Invalid count (invalid) |
| 1051 |
(Pdb) u |
| 1052 |
> <doctest test.test_pdb.test_pdb_where_command[1]>(2)f() |
| 1053 |
-> g() |
| 1054 |
(Pdb) w |
| 1055 |
... |
| 1056 |
<doctest test.test_pdb.test_pdb_where_command[3]>(13)<module>() |
| 1057 |
-> test_function() |
| 1058 |
<doctest test.test_pdb.test_pdb_where_command[2]>(2)test_function() |
| 1059 |
-> f() |
| 1060 |
> <doctest test.test_pdb.test_pdb_where_command[1]>(2)f() |
| 1061 |
-> g() |
| 1062 |
<doctest test.test_pdb.test_pdb_where_command[0]>(2)g() |
| 1063 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 1064 |
(Pdb) w 0 |
| 1065 |
> <doctest test.test_pdb.test_pdb_where_command[1]>(2)f() |
| 1066 |
-> g() |
| 1067 |
(Pdb) w 100 |
| 1068 |
... |
| 1069 |
<doctest test.test_pdb.test_pdb_where_command[3]>(13)<module>() |
| 1070 |
-> test_function() |
| 1071 |
<doctest test.test_pdb.test_pdb_where_command[2]>(2)test_function() |
| 1072 |
-> f() |
| 1073 |
> <doctest test.test_pdb.test_pdb_where_command[1]>(2)f() |
| 1074 |
-> g() |
| 1075 |
<doctest test.test_pdb.test_pdb_where_command[0]>(2)g() |
| 1076 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 1077 |
(Pdb) w -100 |
| 1078 |
... |
| 1079 |
<doctest test.test_pdb.test_pdb_where_command[3]>(13)<module>() |
| 1080 |
-> test_function() |
| 1081 |
<doctest test.test_pdb.test_pdb_where_command[2]>(2)test_function() |
| 1082 |
-> f() |
| 1083 |
> <doctest test.test_pdb.test_pdb_where_command[1]>(2)f() |
| 1084 |
-> g() |
| 1085 |
<doctest test.test_pdb.test_pdb_where_command[0]>(2)g() |
| 1086 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 1087 |
(Pdb) continue |
| 1088 |
""" |
| 1089 |
|
| 1090 |
def test_pdb_restart_command(): |
| 1091 |
"""Test restart command |
| 1092 |
|
| 1093 |
>>> def test_function(): |
| 1094 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False, mode='inline').set_trace() |
| 1095 |
... x = 1 |
| 1096 |
|
| 1097 |
>>> with PdbTestInput([ # doctest: +ELLIPSIS |
| 1098 |
... 'restart', |
| 1099 |
... 'continue', |
| 1100 |
... ]): |
| 1101 |
... test_function() |
| 1102 |
> <doctest test.test_pdb.test_pdb_restart_command[0]>(2)test_function() |
| 1103 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False, mode='inline').set_trace() |
| 1104 |
(Pdb) restart |
| 1105 |
*** run/restart command is disabled when pdb is running in inline mode. |
| 1106 |
Use the command line interface to enable restarting your program |
| 1107 |
e.g. "python -m pdb myscript.py" |
| 1108 |
(Pdb) continue |
| 1109 |
""" |
| 1110 |
|
| 1111 |
def test_pdb_commands_with_set_trace(): |
| 1112 |
"""Test that commands can be passed to Pdb.set_trace() |
| 1113 |
|
| 1114 |
>>> def test_function(): |
| 1115 |
... x = 1 |
| 1116 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace(commands=['p x', 'c']) |
| 1117 |
|
| 1118 |
>>> test_function() |
| 1119 |
1 |
| 1120 |
""" |
| 1121 |
|
| 1122 |
|
| 1123 |
# skip this test if sys.flags.no_site = True; |
| 1124 |
# exit() isn't defined unless there's a site module. |
| 1125 |
if not sys.flags.no_site: |
| 1126 |
def test_pdb_interact_command(): |
| 1127 |
"""Test interact command |
| 1128 |
|
| 1129 |
>>> g = 0 |
| 1130 |
>>> dict_g = {} |
| 1131 |
|
| 1132 |
>>> def test_function(): |
| 1133 |
... x = 1 |
| 1134 |
... lst_local = [] |
| 1135 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 1136 |
|
| 1137 |
>>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
| 1138 |
... 'interact', |
| 1139 |
... 'x', |
| 1140 |
... 'g', |
| 1141 |
... 'x = 2', |
| 1142 |
... 'g = 3', |
| 1143 |
... 'dict_g["a"] = True', |
| 1144 |
... 'lst_local.append(x)', |
| 1145 |
... 'exit()', |
| 1146 |
... 'p x', |
| 1147 |
... 'p g', |
| 1148 |
... 'p dict_g', |
| 1149 |
... 'p lst_local', |
| 1150 |
... 'continue', |
| 1151 |
... ]): |
| 1152 |
... test_function() |
| 1153 |
> <doctest test.test_pdb.test_pdb_interact_command[2]>(4)test_function() |
| 1154 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 1155 |
(Pdb) interact |
| 1156 |
*pdb interact start* |
| 1157 |
... x |
| 1158 |
1 |
| 1159 |
... g |
| 1160 |
0 |
| 1161 |
... x = 2 |
| 1162 |
... g = 3 |
| 1163 |
... dict_g["a"] = True |
| 1164 |
... lst_local.append(x) |
| 1165 |
... exit() |
| 1166 |
*exit from pdb interact command* |
| 1167 |
(Pdb) p x |
| 1168 |
1 |
| 1169 |
(Pdb) p g |
| 1170 |
0 |
| 1171 |
(Pdb) p dict_g |
| 1172 |
{'a': True} |
| 1173 |
(Pdb) p lst_local |
| 1174 |
[2] |
| 1175 |
(Pdb) continue |
| 1176 |
""" |
| 1177 |
|
| 1178 |
def test_convenience_variables(): |
| 1179 |
"""Test convenience variables |
| 1180 |
|
| 1181 |
>>> def util_function(): |
| 1182 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 1183 |
... try: |
| 1184 |
... raise Exception('test') |
| 1185 |
... except Exception: |
| 1186 |
... pass |
| 1187 |
... return 1 |
| 1188 |
|
| 1189 |
>>> def test_function(): |
| 1190 |
... util_function() |
| 1191 |
|
| 1192 |
>>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
| 1193 |
... 'step', # Step to try statement |
| 1194 |
... '$_frame.f_lineno', # Check frame convenience variable |
| 1195 |
... '$ _frame', # This should be a syntax error |
| 1196 |
... '$a = 10', # Set a convenience variable |
| 1197 |
... '$a', # Print its value |
| 1198 |
... 'p "$a"', # Print the string $a |
| 1199 |
... 'p $a + 2', # Do some calculation |
| 1200 |
... 'p f"$a = {$a}"', # Make sure $ in string is not converted and f-string works |
| 1201 |
... 'u', # Switch frame |
| 1202 |
... '$_frame.f_lineno', # Make sure the frame changed |
| 1203 |
... '$a', # Make sure the value persists |
| 1204 |
... 'd', # Go back to the original frame |
| 1205 |
... 'next', |
| 1206 |
... '$a', # The value should be gone |
| 1207 |
... 'next', |
| 1208 |
... '$_exception', # Check exception convenience variable |
| 1209 |
... 'next', |
| 1210 |
... '$_exception', # Exception should be gone |
| 1211 |
... 'return', |
| 1212 |
... '$_retval', # Check return convenience variable |
| 1213 |
... 'continue', |
| 1214 |
... ]): |
| 1215 |
... test_function() |
| 1216 |
> <doctest test.test_pdb.test_convenience_variables[0]>(2)util_function() |
| 1217 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 1218 |
(Pdb) step |
| 1219 |
> <doctest test.test_pdb.test_convenience_variables[0]>(3)util_function() |
| 1220 |
-> try: |
| 1221 |
(Pdb) $_frame.f_lineno |
| 1222 |
3 |
| 1223 |
(Pdb) $ _frame |
| 1224 |
*** SyntaxError: invalid syntax |
| 1225 |
(Pdb) $a = 10 |
| 1226 |
(Pdb) $a |
| 1227 |
10 |
| 1228 |
(Pdb) p "$a" |
| 1229 |
'$a' |
| 1230 |
(Pdb) p $a + 2 |
| 1231 |
12 |
| 1232 |
(Pdb) p f"$a = {$a}" |
| 1233 |
'$a = 10' |
| 1234 |
(Pdb) u |
| 1235 |
> <doctest test.test_pdb.test_convenience_variables[1]>(2)test_function() |
| 1236 |
-> util_function() |
| 1237 |
(Pdb) $_frame.f_lineno |
| 1238 |
2 |
| 1239 |
(Pdb) $a |
| 1240 |
10 |
| 1241 |
(Pdb) d |
| 1242 |
> <doctest test.test_pdb.test_convenience_variables[0]>(3)util_function() |
| 1243 |
-> try: |
| 1244 |
(Pdb) next |
| 1245 |
> <doctest test.test_pdb.test_convenience_variables[0]>(4)util_function() |
| 1246 |
-> raise Exception('test') |
| 1247 |
(Pdb) $a |
| 1248 |
*** KeyError: 'a' |
| 1249 |
(Pdb) next |
| 1250 |
Exception: test |
| 1251 |
> <doctest test.test_pdb.test_convenience_variables[0]>(4)util_function() |
| 1252 |
-> raise Exception('test') |
| 1253 |
(Pdb) $_exception |
| 1254 |
Exception('test') |
| 1255 |
(Pdb) next |
| 1256 |
> <doctest test.test_pdb.test_convenience_variables[0]>(5)util_function() |
| 1257 |
-> except Exception: |
| 1258 |
(Pdb) $_exception |
| 1259 |
*** KeyError: '_exception' |
| 1260 |
(Pdb) return |
| 1261 |
--Return-- |
| 1262 |
> <doctest test.test_pdb.test_convenience_variables[0]>(7)util_function()->1 |
| 1263 |
-> return 1 |
| 1264 |
(Pdb) $_retval |
| 1265 |
1 |
| 1266 |
(Pdb) continue |
| 1267 |
""" |
| 1268 |
|
| 1269 |
|
| 1270 |
def test_post_mortem_chained(): |
| 1271 |
"""Test post mortem traceback debugging of chained exception |
| 1272 |
|
| 1273 |
>>> def test_function_2(): |
| 1274 |
... try: |
| 1275 |
... 1/0 |
| 1276 |
... finally: |
| 1277 |
... print('Exception!') |
| 1278 |
|
| 1279 |
>>> def test_function_reraise(): |
| 1280 |
... try: |
| 1281 |
... test_function_2() |
| 1282 |
... except ZeroDivisionError as e: |
| 1283 |
... raise ZeroDivisionError('reraised') from e |
| 1284 |
|
| 1285 |
>>> def test_function(): |
| 1286 |
... import pdb; |
| 1287 |
... instance = pdb.Pdb(nosigint=True, readrc=False) |
| 1288 |
... try: |
| 1289 |
... test_function_reraise() |
| 1290 |
... except Exception as e: |
| 1291 |
... pdb._post_mortem(e, instance) |
| 1292 |
|
| 1293 |
>>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
| 1294 |
... 'exceptions', |
| 1295 |
... 'exceptions 0', |
| 1296 |
... '$_exception', |
| 1297 |
... 'up', |
| 1298 |
... 'down', |
| 1299 |
... 'exceptions 1', |
| 1300 |
... '$_exception', |
| 1301 |
... 'up', |
| 1302 |
... 'down', |
| 1303 |
... 'exceptions -1', |
| 1304 |
... 'exceptions 3', |
| 1305 |
... 'up', |
| 1306 |
... 'exit', |
| 1307 |
... ]): |
| 1308 |
... try: |
| 1309 |
... test_function() |
| 1310 |
... except ZeroDivisionError: |
| 1311 |
... print('Correctly reraised.') |
| 1312 |
Exception! |
| 1313 |
> <doctest test.test_pdb.test_post_mortem_chained[1]>(5)test_function_reraise() |
| 1314 |
-> raise ZeroDivisionError('reraised') from e |
| 1315 |
(Pdb) exceptions |
| 1316 |
0 ZeroDivisionError('division by zero') |
| 1317 |
> 1 ZeroDivisionError('reraised') |
| 1318 |
(Pdb) exceptions 0 |
| 1319 |
> <doctest test.test_pdb.test_post_mortem_chained[0]>(3)test_function_2() |
| 1320 |
-> 1/0 |
| 1321 |
(Pdb) $_exception |
| 1322 |
ZeroDivisionError('division by zero') |
| 1323 |
(Pdb) up |
| 1324 |
> <doctest test.test_pdb.test_post_mortem_chained[1]>(3)test_function_reraise() |
| 1325 |
-> test_function_2() |
| 1326 |
(Pdb) down |
| 1327 |
> <doctest test.test_pdb.test_post_mortem_chained[0]>(3)test_function_2() |
| 1328 |
-> 1/0 |
| 1329 |
(Pdb) exceptions 1 |
| 1330 |
> <doctest test.test_pdb.test_post_mortem_chained[1]>(5)test_function_reraise() |
| 1331 |
-> raise ZeroDivisionError('reraised') from e |
| 1332 |
(Pdb) $_exception |
| 1333 |
ZeroDivisionError('reraised') |
| 1334 |
(Pdb) up |
| 1335 |
> <doctest test.test_pdb.test_post_mortem_chained[2]>(5)test_function() |
| 1336 |
-> test_function_reraise() |
| 1337 |
(Pdb) down |
| 1338 |
> <doctest test.test_pdb.test_post_mortem_chained[1]>(5)test_function_reraise() |
| 1339 |
-> raise ZeroDivisionError('reraised') from e |
| 1340 |
(Pdb) exceptions -1 |
| 1341 |
*** No exception with that number |
| 1342 |
(Pdb) exceptions 3 |
| 1343 |
*** No exception with that number |
| 1344 |
(Pdb) up |
| 1345 |
> <doctest test.test_pdb.test_post_mortem_chained[2]>(5)test_function() |
| 1346 |
-> test_function_reraise() |
| 1347 |
(Pdb) exit |
| 1348 |
""" |
| 1349 |
|
| 1350 |
|
| 1351 |
def test_post_mortem_cause_no_context(): |
| 1352 |
"""Test post mortem traceback debugging of chained exception |
| 1353 |
|
| 1354 |
>>> def make_exc_with_stack(type_, *content, from_=None): |
| 1355 |
... try: |
| 1356 |
... raise type_(*content) from from_ |
| 1357 |
... except Exception as out: |
| 1358 |
... return out |
| 1359 |
... |
| 1360 |
|
| 1361 |
>>> def main(): |
| 1362 |
... try: |
| 1363 |
... raise ValueError('Context Not Shown') |
| 1364 |
... except Exception as e1: |
| 1365 |
... raise ValueError("With Cause") from make_exc_with_stack(TypeError,'The Cause') |
| 1366 |
|
| 1367 |
>>> def test_function(): |
| 1368 |
... import pdb; |
| 1369 |
... instance = pdb.Pdb(nosigint=True, readrc=False) |
| 1370 |
... try: |
| 1371 |
... main() |
| 1372 |
... except Exception as e: |
| 1373 |
... pdb._post_mortem(e, instance) |
| 1374 |
|
| 1375 |
>>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
| 1376 |
... 'exceptions', |
| 1377 |
... 'exceptions 0', |
| 1378 |
... 'exceptions 1', |
| 1379 |
... 'up', |
| 1380 |
... 'down', |
| 1381 |
... 'exit', |
| 1382 |
... ]): |
| 1383 |
... try: |
| 1384 |
... test_function() |
| 1385 |
... except ValueError: |
| 1386 |
... print('Ok.') |
| 1387 |
> <doctest test.test_pdb.test_post_mortem_cause_no_context[1]>(5)main() |
| 1388 |
-> raise ValueError("With Cause") from make_exc_with_stack(TypeError,'The Cause') |
| 1389 |
(Pdb) exceptions |
| 1390 |
0 TypeError('The Cause') |
| 1391 |
> 1 ValueError('With Cause') |
| 1392 |
(Pdb) exceptions 0 |
| 1393 |
> <doctest test.test_pdb.test_post_mortem_cause_no_context[0]>(3)make_exc_with_stack() |
| 1394 |
-> raise type_(*content) from from_ |
| 1395 |
(Pdb) exceptions 1 |
| 1396 |
> <doctest test.test_pdb.test_post_mortem_cause_no_context[1]>(5)main() |
| 1397 |
-> raise ValueError("With Cause") from make_exc_with_stack(TypeError,'The Cause') |
| 1398 |
(Pdb) up |
| 1399 |
> <doctest test.test_pdb.test_post_mortem_cause_no_context[2]>(5)test_function() |
| 1400 |
-> main() |
| 1401 |
(Pdb) down |
| 1402 |
> <doctest test.test_pdb.test_post_mortem_cause_no_context[1]>(5)main() |
| 1403 |
-> raise ValueError("With Cause") from make_exc_with_stack(TypeError,'The Cause') |
| 1404 |
(Pdb) exit""" |
| 1405 |
|
| 1406 |
|
| 1407 |
def test_post_mortem_context_of_the_cause(): |
| 1408 |
"""Test post mortem traceback debugging of chained exception |
| 1409 |
|
| 1410 |
|
| 1411 |
>>> def main(): |
| 1412 |
... try: |
| 1413 |
... raise TypeError('Context of the cause') |
| 1414 |
... except Exception as e1: |
| 1415 |
... try: |
| 1416 |
... raise ValueError('Root Cause') |
| 1417 |
... except Exception as e2: |
| 1418 |
... ex = e2 |
| 1419 |
... raise ValueError("With Cause, and cause has context") from ex |
| 1420 |
|
| 1421 |
>>> def test_function(): |
| 1422 |
... import pdb; |
| 1423 |
... instance = pdb.Pdb(nosigint=True, readrc=False) |
| 1424 |
... try: |
| 1425 |
... main() |
| 1426 |
... except Exception as e: |
| 1427 |
... pdb._post_mortem(e, instance) |
| 1428 |
|
| 1429 |
>>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
| 1430 |
... 'exceptions', |
| 1431 |
... 'exceptions 2', |
| 1432 |
... 'up', |
| 1433 |
... 'down', |
| 1434 |
... 'exceptions 3', |
| 1435 |
... 'up', |
| 1436 |
... 'down', |
| 1437 |
... 'exceptions 4', |
| 1438 |
... 'up', |
| 1439 |
... 'down', |
| 1440 |
... 'exit', |
| 1441 |
... ]): |
| 1442 |
... try: |
| 1443 |
... test_function() |
| 1444 |
... except ValueError: |
| 1445 |
... print('Correctly reraised.') |
| 1446 |
> <doctest test.test_pdb.test_post_mortem_context_of_the_cause[0]>(9)main() |
| 1447 |
-> raise ValueError("With Cause, and cause has context") from ex |
| 1448 |
(Pdb) exceptions |
| 1449 |
0 TypeError('Context of the cause') |
| 1450 |
1 ValueError('Root Cause') |
| 1451 |
> 2 ValueError('With Cause, and cause has context') |
| 1452 |
(Pdb) exceptions 2 |
| 1453 |
> <doctest test.test_pdb.test_post_mortem_context_of_the_cause[0]>(9)main() |
| 1454 |
-> raise ValueError("With Cause, and cause has context") from ex |
| 1455 |
(Pdb) up |
| 1456 |
> <doctest test.test_pdb.test_post_mortem_context_of_the_cause[1]>(5)test_function() |
| 1457 |
-> main() |
| 1458 |
(Pdb) down |
| 1459 |
> <doctest test.test_pdb.test_post_mortem_context_of_the_cause[0]>(9)main() |
| 1460 |
-> raise ValueError("With Cause, and cause has context") from ex |
| 1461 |
(Pdb) exceptions 3 |
| 1462 |
*** No exception with that number |
| 1463 |
(Pdb) up |
| 1464 |
> <doctest test.test_pdb.test_post_mortem_context_of_the_cause[1]>(5)test_function() |
| 1465 |
-> main() |
| 1466 |
(Pdb) down |
| 1467 |
> <doctest test.test_pdb.test_post_mortem_context_of_the_cause[0]>(9)main() |
| 1468 |
-> raise ValueError("With Cause, and cause has context") from ex |
| 1469 |
(Pdb) exceptions 4 |
| 1470 |
*** No exception with that number |
| 1471 |
(Pdb) up |
| 1472 |
> <doctest test.test_pdb.test_post_mortem_context_of_the_cause[1]>(5)test_function() |
| 1473 |
-> main() |
| 1474 |
(Pdb) down |
| 1475 |
> <doctest test.test_pdb.test_post_mortem_context_of_the_cause[0]>(9)main() |
| 1476 |
-> raise ValueError("With Cause, and cause has context") from ex |
| 1477 |
(Pdb) exit |
| 1478 |
""" |
| 1479 |
|
| 1480 |
|
| 1481 |
def test_post_mortem_from_none(): |
| 1482 |
"""Test post mortem traceback debugging of chained exception |
| 1483 |
|
| 1484 |
In particular that cause from None (which sets __suppress_context__ to True) |
| 1485 |
does not show context. |
| 1486 |
|
| 1487 |
|
| 1488 |
>>> def main(): |
| 1489 |
... try: |
| 1490 |
... raise TypeError('Context of the cause') |
| 1491 |
... except Exception as e1: |
| 1492 |
... raise ValueError("With Cause, and cause has context") from None |
| 1493 |
|
| 1494 |
>>> def test_function(): |
| 1495 |
... import pdb; |
| 1496 |
... instance = pdb.Pdb(nosigint=True, readrc=False) |
| 1497 |
... try: |
| 1498 |
... main() |
| 1499 |
... except Exception as e: |
| 1500 |
... pdb._post_mortem(e, instance) |
| 1501 |
|
| 1502 |
>>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
| 1503 |
... 'exceptions', |
| 1504 |
... 'exit', |
| 1505 |
... ]): |
| 1506 |
... try: |
| 1507 |
... test_function() |
| 1508 |
... except ValueError: |
| 1509 |
... print('Correctly reraised.') |
| 1510 |
> <doctest test.test_pdb.test_post_mortem_from_none[0]>(5)main() |
| 1511 |
-> raise ValueError("With Cause, and cause has context") from None |
| 1512 |
(Pdb) exceptions |
| 1513 |
> 0 ValueError('With Cause, and cause has context') |
| 1514 |
(Pdb) exit |
| 1515 |
""" |
| 1516 |
|
| 1517 |
|
| 1518 |
def test_post_mortem_from_no_stack(): |
| 1519 |
"""Test post mortem traceback debugging of chained exception |
| 1520 |
|
| 1521 |
especially when one exception has no stack. |
| 1522 |
|
| 1523 |
>>> def main(): |
| 1524 |
... raise Exception() from Exception() |
| 1525 |
|
| 1526 |
|
| 1527 |
>>> def test_function(): |
| 1528 |
... import pdb; |
| 1529 |
... instance = pdb.Pdb(nosigint=True, readrc=False) |
| 1530 |
... try: |
| 1531 |
... main() |
| 1532 |
... except Exception as e: |
| 1533 |
... pdb._post_mortem(e, instance) |
| 1534 |
|
| 1535 |
>>> with PdbTestInput( # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
| 1536 |
... ["exceptions", |
| 1537 |
... "exceptions 0", |
| 1538 |
... "exit"], |
| 1539 |
... ): |
| 1540 |
... try: |
| 1541 |
... test_function() |
| 1542 |
... except ValueError: |
| 1543 |
... print('Correctly reraised.') |
| 1544 |
> <doctest test.test_pdb.test_post_mortem_from_no_stack[0]>(2)main() |
| 1545 |
-> raise Exception() from Exception() |
| 1546 |
(Pdb) exceptions |
| 1547 |
- Exception() |
| 1548 |
> 1 Exception() |
| 1549 |
(Pdb) exceptions 0 |
| 1550 |
*** This exception does not have a traceback, cannot jump to it |
| 1551 |
(Pdb) exit |
| 1552 |
""" |
| 1553 |
|
| 1554 |
|
| 1555 |
def test_post_mortem_single_no_stack(): |
| 1556 |
"""Test post mortem called when origin exception has no stack |
| 1557 |
|
| 1558 |
|
| 1559 |
>>> def test_function(): |
| 1560 |
... import pdb; |
| 1561 |
... instance = pdb.Pdb(nosigint=True, readrc=False) |
| 1562 |
... import sys |
| 1563 |
... sys.last_exc = Exception() |
| 1564 |
... pdb._post_mortem(sys.last_exc, instance) |
| 1565 |
|
| 1566 |
>>> with PdbTestInput( # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
| 1567 |
... [] |
| 1568 |
... ): |
| 1569 |
... try: |
| 1570 |
... test_function() |
| 1571 |
... except ValueError as e: |
| 1572 |
... print(e) |
| 1573 |
A valid traceback must be passed if no exception is being handled |
| 1574 |
""" |
| 1575 |
|
| 1576 |
def test_post_mortem_complex(): |
| 1577 |
"""Test post mortem traceback debugging of chained exception |
| 1578 |
|
| 1579 |
Test with simple and complex cycles, exception groups,... |
| 1580 |
|
| 1581 |
>>> def make_ex_with_stack(type_, *content, from_=None): |
| 1582 |
... try: |
| 1583 |
... raise type_(*content) from from_ |
| 1584 |
... except Exception as out: |
| 1585 |
... return out |
| 1586 |
... |
| 1587 |
|
| 1588 |
>>> def cycle(): |
| 1589 |
... try: |
| 1590 |
... raise ValueError("Cycle Leaf") |
| 1591 |
... except Exception as e: |
| 1592 |
... raise e from e |
| 1593 |
... |
| 1594 |
|
| 1595 |
>>> def tri_cycle(): |
| 1596 |
... a = make_ex_with_stack(ValueError, "Cycle1") |
| 1597 |
... b = make_ex_with_stack(ValueError, "Cycle2") |
| 1598 |
... c = make_ex_with_stack(ValueError, "Cycle3") |
| 1599 |
... |
| 1600 |
... a.__cause__ = b |
| 1601 |
... b.__cause__ = c |
| 1602 |
... |
| 1603 |
... raise c from a |
| 1604 |
... |
| 1605 |
|
| 1606 |
>>> def cause(): |
| 1607 |
... try: |
| 1608 |
... raise ValueError("Cause Leaf") |
| 1609 |
... except Exception as e: |
| 1610 |
... raise e |
| 1611 |
... |
| 1612 |
|
| 1613 |
>>> def context(n=10): |
| 1614 |
... try: |
| 1615 |
... raise ValueError(f"Context Leaf {n}") |
| 1616 |
... except Exception as e: |
| 1617 |
... if n == 0: |
| 1618 |
... raise ValueError(f"With Context {n}") from e |
| 1619 |
... else: |
| 1620 |
... context(n - 1) |
| 1621 |
... |
| 1622 |
|
| 1623 |
>>> def main(): |
| 1624 |
... try: |
| 1625 |
... cycle() |
| 1626 |
... except Exception as e1: |
| 1627 |
... try: |
| 1628 |
... tri_cycle() |
| 1629 |
... except Exception as e2: |
| 1630 |
... ex = e2 |
| 1631 |
... raise ValueError("With Context and With Cause") from ex |
| 1632 |
|
| 1633 |
|
| 1634 |
>>> def test_function(): |
| 1635 |
... import pdb; |
| 1636 |
... instance = pdb.Pdb(nosigint=True, readrc=False) |
| 1637 |
... try: |
| 1638 |
... main() |
| 1639 |
... except Exception as e: |
| 1640 |
... pdb._post_mortem(e, instance) |
| 1641 |
|
| 1642 |
>>> with PdbTestInput( # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
| 1643 |
... ["exceptions", |
| 1644 |
... "exceptions 0", |
| 1645 |
... "exceptions 1", |
| 1646 |
... "exceptions 2", |
| 1647 |
... "exceptions 3", |
| 1648 |
... "exit"], |
| 1649 |
... ): |
| 1650 |
... try: |
| 1651 |
... test_function() |
| 1652 |
... except ValueError: |
| 1653 |
... print('Correctly reraised.') |
| 1654 |
> <doctest test.test_pdb.test_post_mortem_complex[5]>(9)main() |
| 1655 |
-> raise ValueError("With Context and With Cause") from ex |
| 1656 |
(Pdb) exceptions |
| 1657 |
0 ValueError('Cycle2') |
| 1658 |
1 ValueError('Cycle1') |
| 1659 |
2 ValueError('Cycle3') |
| 1660 |
> 3 ValueError('With Context and With Cause') |
| 1661 |
(Pdb) exceptions 0 |
| 1662 |
> <doctest test.test_pdb.test_post_mortem_complex[0]>(3)make_ex_with_stack() |
| 1663 |
-> raise type_(*content) from from_ |
| 1664 |
(Pdb) exceptions 1 |
| 1665 |
> <doctest test.test_pdb.test_post_mortem_complex[0]>(3)make_ex_with_stack() |
| 1666 |
-> raise type_(*content) from from_ |
| 1667 |
(Pdb) exceptions 2 |
| 1668 |
> <doctest test.test_pdb.test_post_mortem_complex[0]>(3)make_ex_with_stack() |
| 1669 |
-> raise type_(*content) from from_ |
| 1670 |
(Pdb) exceptions 3 |
| 1671 |
> <doctest test.test_pdb.test_post_mortem_complex[5]>(9)main() |
| 1672 |
-> raise ValueError("With Context and With Cause") from ex |
| 1673 |
(Pdb) exit |
| 1674 |
""" |
| 1675 |
|
| 1676 |
|
| 1677 |
def test_post_mortem(): |
| 1678 |
"""Test post mortem traceback debugging. |
| 1679 |
|
| 1680 |
>>> def test_function_2(): |
| 1681 |
... try: |
| 1682 |
... 1/0 |
| 1683 |
... finally: |
| 1684 |
... print('Exception!') |
| 1685 |
|
| 1686 |
>>> def test_function(): |
| 1687 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 1688 |
... test_function_2() |
| 1689 |
... print('Not reached.') |
| 1690 |
|
| 1691 |
>>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
| 1692 |
... 'step', # step to test_function_2() line |
| 1693 |
... 'next', # step over exception-raising call |
| 1694 |
... 'bt', # get a backtrace |
| 1695 |
... 'list', # list code of test_function() |
| 1696 |
... 'down', # step into test_function_2() |
| 1697 |
... 'list', # list code of test_function_2() |
| 1698 |
... 'continue', |
| 1699 |
... ]): |
| 1700 |
... try: |
| 1701 |
... test_function() |
| 1702 |
... except ZeroDivisionError: |
| 1703 |
... print('Correctly reraised.') |
| 1704 |
> <doctest test.test_pdb.test_post_mortem[1]>(2)test_function() |
| 1705 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 1706 |
(Pdb) step |
| 1707 |
> <doctest test.test_pdb.test_post_mortem[1]>(3)test_function() |
| 1708 |
-> test_function_2() |
| 1709 |
(Pdb) next |
| 1710 |
Exception! |
| 1711 |
ZeroDivisionError: division by zero |
| 1712 |
> <doctest test.test_pdb.test_post_mortem[1]>(3)test_function() |
| 1713 |
-> test_function_2() |
| 1714 |
(Pdb) bt |
| 1715 |
... |
| 1716 |
<doctest test.test_pdb.test_post_mortem[2]>(11)<module>() |
| 1717 |
-> test_function() |
| 1718 |
> <doctest test.test_pdb.test_post_mortem[1]>(3)test_function() |
| 1719 |
-> test_function_2() |
| 1720 |
<doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2() |
| 1721 |
-> 1/0 |
| 1722 |
(Pdb) list |
| 1723 |
1 def test_function(): |
| 1724 |
2 import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 1725 |
3 -> test_function_2() |
| 1726 |
4 print('Not reached.') |
| 1727 |
[EOF] |
| 1728 |
(Pdb) down |
| 1729 |
> <doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2() |
| 1730 |
-> 1/0 |
| 1731 |
(Pdb) list |
| 1732 |
1 def test_function_2(): |
| 1733 |
2 try: |
| 1734 |
3 >> 1/0 |
| 1735 |
4 finally: |
| 1736 |
5 -> print('Exception!') |
| 1737 |
[EOF] |
| 1738 |
(Pdb) continue |
| 1739 |
Correctly reraised. |
| 1740 |
""" |
| 1741 |
|
| 1742 |
|
| 1743 |
def test_pdb_return_to_different_file(): |
| 1744 |
"""When pdb returns to a different file, it should not skip if f_trace is |
| 1745 |
not already set |
| 1746 |
|
| 1747 |
>>> import pprint |
| 1748 |
|
| 1749 |
>>> class A: |
| 1750 |
... def __repr__(self): |
| 1751 |
... return 'A' |
| 1752 |
|
| 1753 |
>>> def test_function(): |
| 1754 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 1755 |
... pprint.pprint(A()) |
| 1756 |
|
| 1757 |
>>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
| 1758 |
... 'b A.__repr__', |
| 1759 |
... 'continue', |
| 1760 |
... 'return', |
| 1761 |
... 'next', |
| 1762 |
... 'return', |
| 1763 |
... 'return', |
| 1764 |
... 'continue', |
| 1765 |
... ]): |
| 1766 |
... test_function() |
| 1767 |
> <doctest test.test_pdb.test_pdb_return_to_different_file[2]>(2)test_function() |
| 1768 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 1769 |
(Pdb) b A.__repr__ |
| 1770 |
Breakpoint 1 at <doctest test.test_pdb.test_pdb_return_to_different_file[1]>:3 |
| 1771 |
(Pdb) continue |
| 1772 |
> <doctest test.test_pdb.test_pdb_return_to_different_file[1]>(3)__repr__() |
| 1773 |
-> return 'A' |
| 1774 |
(Pdb) return |
| 1775 |
--Return-- |
| 1776 |
> <doctest test.test_pdb.test_pdb_return_to_different_file[1]>(3)__repr__()->'A' |
| 1777 |
-> return 'A' |
| 1778 |
(Pdb) next |
| 1779 |
> ...pprint.py..._safe_repr() |
| 1780 |
-> return rep,... |
| 1781 |
(Pdb) return |
| 1782 |
--Return-- |
| 1783 |
> ...pprint.py..._safe_repr()->('A'...) |
| 1784 |
-> return rep,... |
| 1785 |
(Pdb) return |
| 1786 |
--Return-- |
| 1787 |
> ...pprint.py...format()->('A'...) |
| 1788 |
-> return... |
| 1789 |
(Pdb) continue |
| 1790 |
A |
| 1791 |
""" |
| 1792 |
|
| 1793 |
|
| 1794 |
def test_pdb_skip_modules(): |
| 1795 |
"""This illustrates the simple case of module skipping. |
| 1796 |
|
| 1797 |
>>> def skip_module(): |
| 1798 |
... import string |
| 1799 |
... import pdb; pdb.Pdb(skip=['stri*'], nosigint=True, readrc=False).set_trace() |
| 1800 |
... string.capwords('FOO') |
| 1801 |
|
| 1802 |
>>> with PdbTestInput([ |
| 1803 |
... 'step', |
| 1804 |
... 'step', |
| 1805 |
... 'continue', |
| 1806 |
... ]): |
| 1807 |
... skip_module() |
| 1808 |
> <doctest test.test_pdb.test_pdb_skip_modules[0]>(3)skip_module() |
| 1809 |
-> import pdb; pdb.Pdb(skip=['stri*'], nosigint=True, readrc=False).set_trace() |
| 1810 |
(Pdb) step |
| 1811 |
> <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module() |
| 1812 |
-> string.capwords('FOO') |
| 1813 |
(Pdb) step |
| 1814 |
--Return-- |
| 1815 |
> <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None |
| 1816 |
-> string.capwords('FOO') |
| 1817 |
(Pdb) continue |
| 1818 |
""" |
| 1819 |
|
| 1820 |
def test_pdb_invalid_arg(): |
| 1821 |
"""This tests pdb commands that have invalid arguments |
| 1822 |
|
| 1823 |
>>> def test_function(): |
| 1824 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 1825 |
|
| 1826 |
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE |
| 1827 |
... 'a = 3', |
| 1828 |
... 'll 4', |
| 1829 |
... 'step 1', |
| 1830 |
... 'p', |
| 1831 |
... 'enable ', |
| 1832 |
... 'continue' |
| 1833 |
... ]): |
| 1834 |
... test_function() |
| 1835 |
> <doctest test.test_pdb.test_pdb_invalid_arg[0]>(2)test_function() |
| 1836 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 1837 |
(Pdb) a = 3 |
| 1838 |
*** Invalid argument: = 3 |
| 1839 |
Usage: a(rgs) |
| 1840 |
(Pdb) ll 4 |
| 1841 |
*** Invalid argument: 4 |
| 1842 |
Usage: ll | longlist |
| 1843 |
(Pdb) step 1 |
| 1844 |
*** Invalid argument: 1 |
| 1845 |
Usage: s(tep) |
| 1846 |
(Pdb) p |
| 1847 |
*** Argument is required for this command |
| 1848 |
Usage: p expression |
| 1849 |
(Pdb) enable |
| 1850 |
*** Argument is required for this command |
| 1851 |
Usage: enable bpnumber [bpnumber ...] |
| 1852 |
(Pdb) continue |
| 1853 |
""" |
| 1854 |
|
| 1855 |
|
| 1856 |
# Module for testing skipping of module that makes a callback |
| 1857 |
mod = types.ModuleType('module_to_skip') |
| 1858 |
exec('def foo_pony(callback): x = 1; callback(); return None', mod.__dict__) |
| 1859 |
|
| 1860 |
|
| 1861 |
def test_pdb_skip_modules_with_callback(): |
| 1862 |
"""This illustrates skipping of modules that call into other code. |
| 1863 |
|
| 1864 |
>>> def skip_module(): |
| 1865 |
... def callback(): |
| 1866 |
... return None |
| 1867 |
... import pdb; pdb.Pdb(skip=['module_to_skip*'], nosigint=True, readrc=False).set_trace() |
| 1868 |
... mod.foo_pony(callback) |
| 1869 |
|
| 1870 |
>>> with PdbTestInput([ |
| 1871 |
... 'step', |
| 1872 |
... 'step', |
| 1873 |
... 'step', |
| 1874 |
... 'step', |
| 1875 |
... 'step', |
| 1876 |
... 'step', |
| 1877 |
... 'continue', |
| 1878 |
... ]): |
| 1879 |
... skip_module() |
| 1880 |
... pass # provides something to "step" to |
| 1881 |
> <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(4)skip_module() |
| 1882 |
-> import pdb; pdb.Pdb(skip=['module_to_skip*'], nosigint=True, readrc=False).set_trace() |
| 1883 |
(Pdb) step |
| 1884 |
> <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module() |
| 1885 |
-> mod.foo_pony(callback) |
| 1886 |
(Pdb) step |
| 1887 |
--Call-- |
| 1888 |
> <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback() |
| 1889 |
-> def callback(): |
| 1890 |
(Pdb) step |
| 1891 |
> <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback() |
| 1892 |
-> return None |
| 1893 |
(Pdb) step |
| 1894 |
--Return-- |
| 1895 |
> <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None |
| 1896 |
-> return None |
| 1897 |
(Pdb) step |
| 1898 |
--Return-- |
| 1899 |
> <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None |
| 1900 |
-> mod.foo_pony(callback) |
| 1901 |
(Pdb) step |
| 1902 |
> <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(11)<module>() |
| 1903 |
-> pass # provides something to "step" to |
| 1904 |
(Pdb) continue |
| 1905 |
""" |
| 1906 |
|
| 1907 |
|
| 1908 |
def test_pdb_continue_in_bottomframe(): |
| 1909 |
"""Test that "continue" and "next" work properly in bottom frame (issue #5294). |
| 1910 |
|
| 1911 |
>>> def test_function(): |
| 1912 |
... import pdb, sys; inst = pdb.Pdb(nosigint=True, readrc=False) |
| 1913 |
... inst.set_trace() |
| 1914 |
... inst.botframe = sys._getframe() # hackery to get the right botframe |
| 1915 |
... print(1) |
| 1916 |
... print(2) |
| 1917 |
... print(3) |
| 1918 |
... print(4) |
| 1919 |
|
| 1920 |
>>> with PdbTestInput([ # doctest: +ELLIPSIS |
| 1921 |
... 'step', |
| 1922 |
... 'next', |
| 1923 |
... 'break 7', |
| 1924 |
... 'continue', |
| 1925 |
... 'next', |
| 1926 |
... 'continue', |
| 1927 |
... 'continue', |
| 1928 |
... ]): |
| 1929 |
... test_function() |
| 1930 |
> <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(3)test_function() |
| 1931 |
-> inst.set_trace() |
| 1932 |
(Pdb) step |
| 1933 |
> <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(4)test_function() |
| 1934 |
-> inst.botframe = sys._getframe() # hackery to get the right botframe |
| 1935 |
(Pdb) next |
| 1936 |
> <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(5)test_function() |
| 1937 |
-> print(1) |
| 1938 |
(Pdb) break 7 |
| 1939 |
Breakpoint ... at <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>:7 |
| 1940 |
(Pdb) continue |
| 1941 |
1 |
| 1942 |
2 |
| 1943 |
> <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(7)test_function() |
| 1944 |
-> print(3) |
| 1945 |
(Pdb) next |
| 1946 |
3 |
| 1947 |
> <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(8)test_function() |
| 1948 |
-> print(4) |
| 1949 |
(Pdb) continue |
| 1950 |
4 |
| 1951 |
""" |
| 1952 |
|
| 1953 |
|
| 1954 |
def pdb_invoke(method, arg): |
| 1955 |
"""Run pdb.method(arg).""" |
| 1956 |
getattr(pdb.Pdb(nosigint=True, readrc=False), method)(arg) |
| 1957 |
|
| 1958 |
|
| 1959 |
def test_pdb_run_with_incorrect_argument(): |
| 1960 |
"""Testing run and runeval with incorrect first argument. |
| 1961 |
|
| 1962 |
>>> pti = PdbTestInput(['continue',]) |
| 1963 |
>>> with pti: |
| 1964 |
... pdb_invoke('run', lambda x: x) |
| 1965 |
Traceback (most recent call last): |
| 1966 |
TypeError: exec() arg 1 must be a string, bytes or code object |
| 1967 |
|
| 1968 |
>>> with pti: |
| 1969 |
... pdb_invoke('runeval', lambda x: x) |
| 1970 |
Traceback (most recent call last): |
| 1971 |
TypeError: eval() arg 1 must be a string, bytes or code object |
| 1972 |
""" |
| 1973 |
|
| 1974 |
|
| 1975 |
def test_pdb_run_with_code_object(): |
| 1976 |
"""Testing run and runeval with code object as a first argument. |
| 1977 |
|
| 1978 |
>>> with PdbTestInput(['step','x', 'continue']): # doctest: +ELLIPSIS |
| 1979 |
... pdb_invoke('run', compile('x=1', '<string>', 'exec')) |
| 1980 |
> <string>(1)<module>()... |
| 1981 |
(Pdb) step |
| 1982 |
--Return-- |
| 1983 |
> <string>(1)<module>()->None |
| 1984 |
(Pdb) x |
| 1985 |
1 |
| 1986 |
(Pdb) continue |
| 1987 |
|
| 1988 |
>>> with PdbTestInput(['x', 'continue']): |
| 1989 |
... x=0 |
| 1990 |
... pdb_invoke('runeval', compile('x+1', '<string>', 'eval')) |
| 1991 |
> <string>(1)<module>()->None |
| 1992 |
(Pdb) x |
| 1993 |
1 |
| 1994 |
(Pdb) continue |
| 1995 |
""" |
| 1996 |
|
| 1997 |
def test_next_until_return_at_return_event(): |
| 1998 |
"""Test that pdb stops after a next/until/return issued at a return debug event. |
| 1999 |
|
| 2000 |
>>> def test_function_2(): |
| 2001 |
... x = 1 |
| 2002 |
... x = 2 |
| 2003 |
|
| 2004 |
>>> def test_function(): |
| 2005 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 2006 |
... test_function_2() |
| 2007 |
... test_function_2() |
| 2008 |
... test_function_2() |
| 2009 |
... end = 1 |
| 2010 |
|
| 2011 |
>>> with PdbTestInput(['break test_function_2', |
| 2012 |
... 'continue', |
| 2013 |
... 'return', |
| 2014 |
... 'next', |
| 2015 |
... 'continue', |
| 2016 |
... 'return', |
| 2017 |
... 'until', |
| 2018 |
... 'continue', |
| 2019 |
... 'return', |
| 2020 |
... 'return', |
| 2021 |
... 'continue']): |
| 2022 |
... test_function() |
| 2023 |
> <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(2)test_function() |
| 2024 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 2025 |
(Pdb) break test_function_2 |
| 2026 |
Breakpoint 1 at <doctest test.test_pdb.test_next_until_return_at_return_event[0]>:2 |
| 2027 |
(Pdb) continue |
| 2028 |
> <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2() |
| 2029 |
-> x = 1 |
| 2030 |
(Pdb) return |
| 2031 |
--Return-- |
| 2032 |
> <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(3)test_function_2()->None |
| 2033 |
-> x = 2 |
| 2034 |
(Pdb) next |
| 2035 |
> <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(4)test_function() |
| 2036 |
-> test_function_2() |
| 2037 |
(Pdb) continue |
| 2038 |
> <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2() |
| 2039 |
-> x = 1 |
| 2040 |
(Pdb) return |
| 2041 |
--Return-- |
| 2042 |
> <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(3)test_function_2()->None |
| 2043 |
-> x = 2 |
| 2044 |
(Pdb) until |
| 2045 |
> <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(5)test_function() |
| 2046 |
-> test_function_2() |
| 2047 |
(Pdb) continue |
| 2048 |
> <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2() |
| 2049 |
-> x = 1 |
| 2050 |
(Pdb) return |
| 2051 |
--Return-- |
| 2052 |
> <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(3)test_function_2()->None |
| 2053 |
-> x = 2 |
| 2054 |
(Pdb) return |
| 2055 |
> <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(6)test_function() |
| 2056 |
-> end = 1 |
| 2057 |
(Pdb) continue |
| 2058 |
""" |
| 2059 |
|
| 2060 |
def test_pdb_next_command_for_generator(): |
| 2061 |
"""Testing skip unwinding stack on yield for generators for "next" command |
| 2062 |
|
| 2063 |
>>> def test_gen(): |
| 2064 |
... yield 0 |
| 2065 |
... return 1 |
| 2066 |
... yield 2 |
| 2067 |
|
| 2068 |
>>> def test_function(): |
| 2069 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 2070 |
... it = test_gen() |
| 2071 |
... try: |
| 2072 |
... if next(it) != 0: |
| 2073 |
... raise AssertionError |
| 2074 |
... next(it) |
| 2075 |
... except StopIteration as ex: |
| 2076 |
... if ex.value != 1: |
| 2077 |
... raise AssertionError |
| 2078 |
... print("finished") |
| 2079 |
|
| 2080 |
>>> with PdbTestInput(['step', |
| 2081 |
... 'step', |
| 2082 |
... 'step', |
| 2083 |
... 'step', |
| 2084 |
... 'next', |
| 2085 |
... 'next', |
| 2086 |
... 'step', |
| 2087 |
... 'step', |
| 2088 |
... 'continue']): |
| 2089 |
... test_function() |
| 2090 |
> <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(2)test_function() |
| 2091 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 2092 |
(Pdb) step |
| 2093 |
> <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(3)test_function() |
| 2094 |
-> it = test_gen() |
| 2095 |
(Pdb) step |
| 2096 |
> <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(4)test_function() |
| 2097 |
-> try: |
| 2098 |
(Pdb) step |
| 2099 |
> <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(5)test_function() |
| 2100 |
-> if next(it) != 0: |
| 2101 |
(Pdb) step |
| 2102 |
--Call-- |
| 2103 |
> <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(1)test_gen() |
| 2104 |
-> def test_gen(): |
| 2105 |
(Pdb) next |
| 2106 |
> <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(2)test_gen() |
| 2107 |
-> yield 0 |
| 2108 |
(Pdb) next |
| 2109 |
> <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(3)test_gen() |
| 2110 |
-> return 1 |
| 2111 |
(Pdb) step |
| 2112 |
--Return-- |
| 2113 |
> <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(3)test_gen()->1 |
| 2114 |
-> return 1 |
| 2115 |
(Pdb) step |
| 2116 |
StopIteration: 1 |
| 2117 |
> <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(7)test_function() |
| 2118 |
-> next(it) |
| 2119 |
(Pdb) continue |
| 2120 |
finished |
| 2121 |
""" |
| 2122 |
|
| 2123 |
if not SKIP_CORO_TESTS: |
| 2124 |
if has_socket_support: |
| 2125 |
def test_pdb_asynctask(): |
| 2126 |
"""Testing $_asynctask is accessible in async context |
| 2127 |
|
| 2128 |
>>> import asyncio |
| 2129 |
|
| 2130 |
>>> async def test(): |
| 2131 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 2132 |
|
| 2133 |
>>> def test_function(): |
| 2134 |
... asyncio.run(test(), loop_factory=asyncio.EventLoop) |
| 2135 |
|
| 2136 |
>>> with PdbTestInput([ # doctest: +ELLIPSIS |
| 2137 |
... '$_asynctask', |
| 2138 |
... 'continue', |
| 2139 |
... ]): |
| 2140 |
... test_function() |
| 2141 |
> <doctest test.test_pdb.test_pdb_asynctask[1]>(2)test() |
| 2142 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 2143 |
(Pdb) $_asynctask |
| 2144 |
<Task pending name=... coro=<test() running at <doctest test.test_pdb.test_pdb_asynctask[1]>:2> ... |
| 2145 |
(Pdb) continue |
| 2146 |
""" |
| 2147 |
|
| 2148 |
def test_pdb_await_support(): |
| 2149 |
"""Testing await support in pdb |
| 2150 |
|
| 2151 |
>>> import asyncio |
| 2152 |
|
| 2153 |
>>> async def test(): |
| 2154 |
... print("hello") |
| 2155 |
... await asyncio.sleep(0) |
| 2156 |
... print("world") |
| 2157 |
... return 42 |
| 2158 |
|
| 2159 |
>>> async def main(): |
| 2160 |
... import pdb |
| 2161 |
... task = asyncio.create_task(test()) |
| 2162 |
... await pdb.Pdb(nosigint=True, readrc=False).set_trace_async() |
| 2163 |
... pass |
| 2164 |
|
| 2165 |
>>> def test_function(): |
| 2166 |
... asyncio.run(main(), loop_factory=asyncio.EventLoop) |
| 2167 |
|
| 2168 |
>>> with PdbTestInput([ # doctest: +ELLIPSIS |
| 2169 |
... 'x = await task', |
| 2170 |
... 'p x', |
| 2171 |
... 'x = await test()', |
| 2172 |
... 'p x', |
| 2173 |
... 'new_task = asyncio.create_task(test())', |
| 2174 |
... 'await new_task', |
| 2175 |
... 'await non_exist()', |
| 2176 |
... 's', |
| 2177 |
... 'continue', |
| 2178 |
... ]): |
| 2179 |
... test_function() |
| 2180 |
> <doctest test.test_pdb.test_pdb_await_support[2]>(4)main() |
| 2181 |
-> await pdb.Pdb(nosigint=True, readrc=False).set_trace_async() |
| 2182 |
(Pdb) x = await task |
| 2183 |
hello |
| 2184 |
world |
| 2185 |
> <doctest test.test_pdb.test_pdb_await_support[2]>(4)main() |
| 2186 |
-> await pdb.Pdb(nosigint=True, readrc=False).set_trace_async() |
| 2187 |
(Pdb) p x |
| 2188 |
42 |
| 2189 |
(Pdb) x = await test() |
| 2190 |
hello |
| 2191 |
world |
| 2192 |
> <doctest test.test_pdb.test_pdb_await_support[2]>(4)main() |
| 2193 |
-> await pdb.Pdb(nosigint=True, readrc=False).set_trace_async() |
| 2194 |
(Pdb) p x |
| 2195 |
42 |
| 2196 |
(Pdb) new_task = asyncio.create_task(test()) |
| 2197 |
(Pdb) await new_task |
| 2198 |
hello |
| 2199 |
world |
| 2200 |
> <doctest test.test_pdb.test_pdb_await_support[2]>(4)main() |
| 2201 |
-> await pdb.Pdb(nosigint=True, readrc=False).set_trace_async() |
| 2202 |
(Pdb) await non_exist() |
| 2203 |
*** NameError: name 'non_exist' is not defined |
| 2204 |
> <doctest test.test_pdb.test_pdb_await_support[2]>(4)main() |
| 2205 |
-> await pdb.Pdb(nosigint=True, readrc=False).set_trace_async() |
| 2206 |
(Pdb) s |
| 2207 |
> <doctest test.test_pdb.test_pdb_await_support[2]>(5)main() |
| 2208 |
-> pass |
| 2209 |
(Pdb) continue |
| 2210 |
""" |
| 2211 |
|
| 2212 |
def test_pdb_await_with_breakpoint(): |
| 2213 |
"""Testing await support with breakpoints set in tasks |
| 2214 |
|
| 2215 |
>>> import asyncio |
| 2216 |
|
| 2217 |
>>> async def test(): |
| 2218 |
... x = 2 |
| 2219 |
... await asyncio.sleep(0) |
| 2220 |
... return 42 |
| 2221 |
|
| 2222 |
>>> async def main(): |
| 2223 |
... import pdb |
| 2224 |
... task = asyncio.create_task(test()) |
| 2225 |
... await pdb.Pdb(nosigint=True, readrc=False).set_trace_async() |
| 2226 |
|
| 2227 |
>>> def test_function(): |
| 2228 |
... asyncio.run(main(), loop_factory=asyncio.EventLoop) |
| 2229 |
|
| 2230 |
>>> with PdbTestInput([ # doctest: +ELLIPSIS |
| 2231 |
... 'b test', |
| 2232 |
... 'k = await task', |
| 2233 |
... 'n', |
| 2234 |
... 'p x', |
| 2235 |
... 'continue', |
| 2236 |
... 'p k', |
| 2237 |
... 'continue', |
| 2238 |
... ]): |
| 2239 |
... test_function() |
| 2240 |
> <doctest test.test_pdb.test_pdb_await_with_breakpoint[2]>(4)main() |
| 2241 |
-> await pdb.Pdb(nosigint=True, readrc=False).set_trace_async() |
| 2242 |
(Pdb) b test |
| 2243 |
Breakpoint 1 at <doctest test.test_pdb.test_pdb_await_with_breakpoint[1]>:2 |
| 2244 |
(Pdb) k = await task |
| 2245 |
> <doctest test.test_pdb.test_pdb_await_with_breakpoint[1]>(2)test() |
| 2246 |
-> x = 2 |
| 2247 |
(Pdb) n |
| 2248 |
> <doctest test.test_pdb.test_pdb_await_with_breakpoint[1]>(3)test() |
| 2249 |
-> await asyncio.sleep(0) |
| 2250 |
(Pdb) p x |
| 2251 |
2 |
| 2252 |
(Pdb) continue |
| 2253 |
> <doctest test.test_pdb.test_pdb_await_with_breakpoint[2]>(4)main() |
| 2254 |
-> await pdb.Pdb(nosigint=True, readrc=False).set_trace_async() |
| 2255 |
(Pdb) p k |
| 2256 |
42 |
| 2257 |
(Pdb) continue |
| 2258 |
""" |
| 2259 |
|
| 2260 |
def test_pdb_await_contextvar(): |
| 2261 |
"""Testing await support context vars |
| 2262 |
|
| 2263 |
>>> import asyncio |
| 2264 |
>>> import contextvars |
| 2265 |
|
| 2266 |
>>> var = contextvars.ContextVar('var') |
| 2267 |
|
| 2268 |
>>> async def get_var(): |
| 2269 |
... return var.get() |
| 2270 |
|
| 2271 |
>>> async def set_var(val): |
| 2272 |
... var.set(val) |
| 2273 |
... return var.get() |
| 2274 |
|
| 2275 |
>>> async def main(): |
| 2276 |
... var.set(42) |
| 2277 |
... import pdb |
| 2278 |
... await pdb.Pdb(nosigint=True, readrc=False).set_trace_async() |
| 2279 |
|
| 2280 |
>>> def test_function(): |
| 2281 |
... asyncio.run(main(), loop_factory=asyncio.EventLoop) |
| 2282 |
|
| 2283 |
>>> with PdbTestInput([ |
| 2284 |
... 'p var.get()', |
| 2285 |
... 'print(await get_var())', |
| 2286 |
... 'print(await asyncio.create_task(set_var(100)))', |
| 2287 |
... 'p var.get()', |
| 2288 |
... 'print(await set_var(99))', |
| 2289 |
... 'p var.get()', |
| 2290 |
... 'print(await get_var())', |
| 2291 |
... 'continue', |
| 2292 |
... ]): |
| 2293 |
... test_function() |
| 2294 |
> <doctest test.test_pdb.test_pdb_await_contextvar[5]>(4)main() |
| 2295 |
-> await pdb.Pdb(nosigint=True, readrc=False).set_trace_async() |
| 2296 |
(Pdb) p var.get() |
| 2297 |
42 |
| 2298 |
(Pdb) print(await get_var()) |
| 2299 |
42 |
| 2300 |
> <doctest test.test_pdb.test_pdb_await_contextvar[5]>(4)main() |
| 2301 |
-> await pdb.Pdb(nosigint=True, readrc=False).set_trace_async() |
| 2302 |
(Pdb) print(await asyncio.create_task(set_var(100))) |
| 2303 |
100 |
| 2304 |
> <doctest test.test_pdb.test_pdb_await_contextvar[5]>(4)main() |
| 2305 |
-> await pdb.Pdb(nosigint=True, readrc=False).set_trace_async() |
| 2306 |
(Pdb) p var.get() |
| 2307 |
42 |
| 2308 |
(Pdb) print(await set_var(99)) |
| 2309 |
99 |
| 2310 |
> <doctest test.test_pdb.test_pdb_await_contextvar[5]>(4)main() |
| 2311 |
-> await pdb.Pdb(nosigint=True, readrc=False).set_trace_async() |
| 2312 |
(Pdb) p var.get() |
| 2313 |
99 |
| 2314 |
(Pdb) print(await get_var()) |
| 2315 |
99 |
| 2316 |
> <doctest test.test_pdb.test_pdb_await_contextvar[5]>(4)main() |
| 2317 |
-> await pdb.Pdb(nosigint=True, readrc=False).set_trace_async() |
| 2318 |
(Pdb) continue |
| 2319 |
""" |
| 2320 |
|
| 2321 |
def test_pdb_next_command_for_coroutine(): |
| 2322 |
"""Testing skip unwinding stack on yield for coroutines for "next" command |
| 2323 |
|
| 2324 |
>>> from test.support import run_yielding_async_fn, async_yield |
| 2325 |
|
| 2326 |
>>> async def test_coro(): |
| 2327 |
... await async_yield(0) |
| 2328 |
... await async_yield(0) |
| 2329 |
... await async_yield(0) |
| 2330 |
|
| 2331 |
>>> async def test_main(): |
| 2332 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 2333 |
... await test_coro() |
| 2334 |
|
| 2335 |
>>> def test_function(): |
| 2336 |
... run_yielding_async_fn(test_main) |
| 2337 |
... print("finished") |
| 2338 |
|
| 2339 |
>>> with PdbTestInput(['step', |
| 2340 |
... 'step', |
| 2341 |
... 'step', |
| 2342 |
... 'next', |
| 2343 |
... 'next', |
| 2344 |
... 'next', |
| 2345 |
... 'step', |
| 2346 |
... 'continue']): |
| 2347 |
... test_function() |
| 2348 |
> <doctest test.test_pdb.test_pdb_next_command_for_coroutine[2]>(2)test_main() |
| 2349 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 2350 |
(Pdb) step |
| 2351 |
> <doctest test.test_pdb.test_pdb_next_command_for_coroutine[2]>(3)test_main() |
| 2352 |
-> await test_coro() |
| 2353 |
(Pdb) step |
| 2354 |
--Call-- |
| 2355 |
> <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(1)test_coro() |
| 2356 |
-> async def test_coro(): |
| 2357 |
(Pdb) step |
| 2358 |
> <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(2)test_coro() |
| 2359 |
-> await async_yield(0) |
| 2360 |
(Pdb) next |
| 2361 |
> <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(3)test_coro() |
| 2362 |
-> await async_yield(0) |
| 2363 |
(Pdb) next |
| 2364 |
> <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(4)test_coro() |
| 2365 |
-> await async_yield(0) |
| 2366 |
(Pdb) next |
| 2367 |
Internal StopIteration |
| 2368 |
> <doctest test.test_pdb.test_pdb_next_command_for_coroutine[2]>(3)test_main() |
| 2369 |
-> await test_coro() |
| 2370 |
(Pdb) step |
| 2371 |
--Return-- |
| 2372 |
> <doctest test.test_pdb.test_pdb_next_command_for_coroutine[2]>(3)test_main()->None |
| 2373 |
-> await test_coro() |
| 2374 |
(Pdb) continue |
| 2375 |
finished |
| 2376 |
""" |
| 2377 |
|
| 2378 |
def test_pdb_next_command_for_asyncgen(): |
| 2379 |
"""Testing skip unwinding stack on yield for coroutines for "next" command |
| 2380 |
|
| 2381 |
>>> from test.support import run_yielding_async_fn, async_yield |
| 2382 |
|
| 2383 |
>>> async def agen(): |
| 2384 |
... yield 1 |
| 2385 |
... await async_yield(0) |
| 2386 |
... yield 2 |
| 2387 |
|
| 2388 |
>>> async def test_coro(): |
| 2389 |
... async for x in agen(): |
| 2390 |
... print(x) |
| 2391 |
|
| 2392 |
>>> async def test_main(): |
| 2393 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 2394 |
... await test_coro() |
| 2395 |
|
| 2396 |
>>> def test_function(): |
| 2397 |
... run_yielding_async_fn(test_main) |
| 2398 |
... print("finished") |
| 2399 |
|
| 2400 |
>>> with PdbTestInput(['step', |
| 2401 |
... 'step', |
| 2402 |
... 'step', |
| 2403 |
... 'next', |
| 2404 |
... 'next', |
| 2405 |
... 'step', |
| 2406 |
... 'next', |
| 2407 |
... 'continue']): |
| 2408 |
... test_function() |
| 2409 |
> <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[3]>(2)test_main() |
| 2410 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 2411 |
(Pdb) step |
| 2412 |
> <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[3]>(3)test_main() |
| 2413 |
-> await test_coro() |
| 2414 |
(Pdb) step |
| 2415 |
--Call-- |
| 2416 |
> <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[2]>(1)test_coro() |
| 2417 |
-> async def test_coro(): |
| 2418 |
(Pdb) step |
| 2419 |
> <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[2]>(2)test_coro() |
| 2420 |
-> async for x in agen(): |
| 2421 |
(Pdb) next |
| 2422 |
> <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[2]>(3)test_coro() |
| 2423 |
-> print(x) |
| 2424 |
(Pdb) next |
| 2425 |
1 |
| 2426 |
> <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[2]>(2)test_coro() |
| 2427 |
-> async for x in agen(): |
| 2428 |
(Pdb) step |
| 2429 |
--Call-- |
| 2430 |
> <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[1]>(2)agen() |
| 2431 |
-> yield 1 |
| 2432 |
(Pdb) next |
| 2433 |
> <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[1]>(3)agen() |
| 2434 |
-> await async_yield(0) |
| 2435 |
(Pdb) continue |
| 2436 |
2 |
| 2437 |
finished |
| 2438 |
""" |
| 2439 |
|
| 2440 |
def test_pdb_return_command_for_generator(): |
| 2441 |
"""Testing no unwinding stack on yield for generators |
| 2442 |
for "return" command |
| 2443 |
|
| 2444 |
>>> def test_gen(): |
| 2445 |
... yield 0 |
| 2446 |
... return 1 |
| 2447 |
... yield 2 |
| 2448 |
|
| 2449 |
>>> def test_function(): |
| 2450 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 2451 |
... it = test_gen() |
| 2452 |
... try: |
| 2453 |
... if next(it) != 0: |
| 2454 |
... raise AssertionError |
| 2455 |
... next(it) |
| 2456 |
... except StopIteration as ex: |
| 2457 |
... if ex.value != 1: |
| 2458 |
... raise AssertionError |
| 2459 |
... print("finished") |
| 2460 |
|
| 2461 |
>>> with PdbTestInput(['step', |
| 2462 |
... 'step', |
| 2463 |
... 'step', |
| 2464 |
... 'step', |
| 2465 |
... 'return', |
| 2466 |
... 'step', |
| 2467 |
... 'step', |
| 2468 |
... 'continue']): |
| 2469 |
... test_function() |
| 2470 |
> <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(2)test_function() |
| 2471 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 2472 |
(Pdb) step |
| 2473 |
> <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(3)test_function() |
| 2474 |
-> it = test_gen() |
| 2475 |
(Pdb) step |
| 2476 |
> <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(4)test_function() |
| 2477 |
-> try: |
| 2478 |
(Pdb) step |
| 2479 |
> <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(5)test_function() |
| 2480 |
-> if next(it) != 0: |
| 2481 |
(Pdb) step |
| 2482 |
--Call-- |
| 2483 |
> <doctest test.test_pdb.test_pdb_return_command_for_generator[0]>(1)test_gen() |
| 2484 |
-> def test_gen(): |
| 2485 |
(Pdb) return |
| 2486 |
StopIteration: 1 |
| 2487 |
> <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(7)test_function() |
| 2488 |
-> next(it) |
| 2489 |
(Pdb) step |
| 2490 |
> <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(8)test_function() |
| 2491 |
-> except StopIteration as ex: |
| 2492 |
(Pdb) step |
| 2493 |
> <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(9)test_function() |
| 2494 |
-> if ex.value != 1: |
| 2495 |
(Pdb) continue |
| 2496 |
finished |
| 2497 |
""" |
| 2498 |
|
| 2499 |
if not SKIP_CORO_TESTS: |
| 2500 |
def test_pdb_return_command_for_coroutine(): |
| 2501 |
"""Testing no unwinding stack on yield for coroutines for "return" command |
| 2502 |
|
| 2503 |
>>> from test.support import run_yielding_async_fn, async_yield |
| 2504 |
|
| 2505 |
>>> async def test_coro(): |
| 2506 |
... await async_yield(0) |
| 2507 |
... await async_yield(0) |
| 2508 |
... await async_yield(0) |
| 2509 |
|
| 2510 |
>>> async def test_main(): |
| 2511 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 2512 |
... await test_coro() |
| 2513 |
|
| 2514 |
>>> def test_function(): |
| 2515 |
... run_yielding_async_fn(test_main) |
| 2516 |
... print("finished") |
| 2517 |
|
| 2518 |
>>> with PdbTestInput(['step', |
| 2519 |
... 'step', |
| 2520 |
... 'step', |
| 2521 |
... 'next', |
| 2522 |
... 'continue']): |
| 2523 |
... test_function() |
| 2524 |
> <doctest test.test_pdb.test_pdb_return_command_for_coroutine[2]>(2)test_main() |
| 2525 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 2526 |
(Pdb) step |
| 2527 |
> <doctest test.test_pdb.test_pdb_return_command_for_coroutine[2]>(3)test_main() |
| 2528 |
-> await test_coro() |
| 2529 |
(Pdb) step |
| 2530 |
--Call-- |
| 2531 |
> <doctest test.test_pdb.test_pdb_return_command_for_coroutine[1]>(1)test_coro() |
| 2532 |
-> async def test_coro(): |
| 2533 |
(Pdb) step |
| 2534 |
> <doctest test.test_pdb.test_pdb_return_command_for_coroutine[1]>(2)test_coro() |
| 2535 |
-> await async_yield(0) |
| 2536 |
(Pdb) next |
| 2537 |
> <doctest test.test_pdb.test_pdb_return_command_for_coroutine[1]>(3)test_coro() |
| 2538 |
-> await async_yield(0) |
| 2539 |
(Pdb) continue |
| 2540 |
finished |
| 2541 |
""" |
| 2542 |
|
| 2543 |
def test_pdb_until_command_for_generator(): |
| 2544 |
"""Testing no unwinding stack on yield for generators |
| 2545 |
for "until" command if target breakpoint is not reached |
| 2546 |
|
| 2547 |
>>> def test_gen(): |
| 2548 |
... yield 0 |
| 2549 |
... yield 1 |
| 2550 |
... yield 2 |
| 2551 |
|
| 2552 |
>>> def test_function(): |
| 2553 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 2554 |
... for i in test_gen(): |
| 2555 |
... print(i) |
| 2556 |
... print("finished") |
| 2557 |
|
| 2558 |
>>> with PdbTestInput(['step', |
| 2559 |
... 'step', |
| 2560 |
... 'until 4', |
| 2561 |
... 'step', |
| 2562 |
... 'step', |
| 2563 |
... 'continue']): |
| 2564 |
... test_function() |
| 2565 |
> <doctest test.test_pdb.test_pdb_until_command_for_generator[1]>(2)test_function() |
| 2566 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 2567 |
(Pdb) step |
| 2568 |
> <doctest test.test_pdb.test_pdb_until_command_for_generator[1]>(3)test_function() |
| 2569 |
-> for i in test_gen(): |
| 2570 |
(Pdb) step |
| 2571 |
--Call-- |
| 2572 |
> <doctest test.test_pdb.test_pdb_until_command_for_generator[0]>(1)test_gen() |
| 2573 |
-> def test_gen(): |
| 2574 |
(Pdb) until 4 |
| 2575 |
0 |
| 2576 |
1 |
| 2577 |
> <doctest test.test_pdb.test_pdb_until_command_for_generator[0]>(4)test_gen() |
| 2578 |
-> yield 2 |
| 2579 |
(Pdb) step |
| 2580 |
--Return-- |
| 2581 |
> <doctest test.test_pdb.test_pdb_until_command_for_generator[0]>(4)test_gen()->2 |
| 2582 |
-> yield 2 |
| 2583 |
(Pdb) step |
| 2584 |
> <doctest test.test_pdb.test_pdb_until_command_for_generator[1]>(4)test_function() |
| 2585 |
-> print(i) |
| 2586 |
(Pdb) continue |
| 2587 |
2 |
| 2588 |
finished |
| 2589 |
""" |
| 2590 |
|
| 2591 |
if not SKIP_CORO_TESTS: |
| 2592 |
def test_pdb_until_command_for_coroutine(): |
| 2593 |
"""Testing no unwinding stack for coroutines |
| 2594 |
for "until" command if target breakpoint is not reached |
| 2595 |
|
| 2596 |
>>> from test.support import run_yielding_async_fn, async_yield |
| 2597 |
|
| 2598 |
>>> async def test_coro(): |
| 2599 |
... print(0) |
| 2600 |
... await async_yield(0) |
| 2601 |
... print(1) |
| 2602 |
... await async_yield(0) |
| 2603 |
... print(2) |
| 2604 |
... await async_yield(0) |
| 2605 |
... print(3) |
| 2606 |
|
| 2607 |
>>> async def test_main(): |
| 2608 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 2609 |
... await test_coro() |
| 2610 |
|
| 2611 |
>>> def test_function(): |
| 2612 |
... run_yielding_async_fn(test_main) |
| 2613 |
... print("finished") |
| 2614 |
|
| 2615 |
>>> with PdbTestInput(['step', |
| 2616 |
... 'step', |
| 2617 |
... 'until 8', |
| 2618 |
... 'continue']): |
| 2619 |
... test_function() |
| 2620 |
> <doctest test.test_pdb.test_pdb_until_command_for_coroutine[2]>(2)test_main() |
| 2621 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 2622 |
(Pdb) step |
| 2623 |
> <doctest test.test_pdb.test_pdb_until_command_for_coroutine[2]>(3)test_main() |
| 2624 |
-> await test_coro() |
| 2625 |
(Pdb) step |
| 2626 |
--Call-- |
| 2627 |
> <doctest test.test_pdb.test_pdb_until_command_for_coroutine[1]>(1)test_coro() |
| 2628 |
-> async def test_coro(): |
| 2629 |
(Pdb) until 8 |
| 2630 |
0 |
| 2631 |
1 |
| 2632 |
2 |
| 2633 |
> <doctest test.test_pdb.test_pdb_until_command_for_coroutine[1]>(8)test_coro() |
| 2634 |
-> print(3) |
| 2635 |
(Pdb) continue |
| 2636 |
3 |
| 2637 |
finished |
| 2638 |
""" |
| 2639 |
|
| 2640 |
def test_pdb_next_command_in_generator_for_loop(): |
| 2641 |
"""The next command on returning from a generator controlled by a for loop. |
| 2642 |
|
| 2643 |
>>> def test_gen(): |
| 2644 |
... yield 0 |
| 2645 |
... return 1 |
| 2646 |
|
| 2647 |
>>> def test_function(): |
| 2648 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 2649 |
... for i in test_gen(): |
| 2650 |
... print('value', i) |
| 2651 |
... x = 123 |
| 2652 |
|
| 2653 |
>>> with PdbTestInput(['break test_gen', |
| 2654 |
... 'continue', |
| 2655 |
... 'next', |
| 2656 |
... 'next', |
| 2657 |
... 'next', |
| 2658 |
... 'continue']): |
| 2659 |
... test_function() |
| 2660 |
> <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(2)test_function() |
| 2661 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 2662 |
(Pdb) break test_gen |
| 2663 |
Breakpoint 1 at <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>:2 |
| 2664 |
(Pdb) continue |
| 2665 |
> <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>(2)test_gen() |
| 2666 |
-> yield 0 |
| 2667 |
(Pdb) next |
| 2668 |
value 0 |
| 2669 |
> <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>(3)test_gen() |
| 2670 |
-> return 1 |
| 2671 |
(Pdb) next |
| 2672 |
Internal StopIteration: 1 |
| 2673 |
> <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(3)test_function() |
| 2674 |
-> for i in test_gen(): |
| 2675 |
(Pdb) next |
| 2676 |
> <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(5)test_function() |
| 2677 |
-> x = 123 |
| 2678 |
(Pdb) continue |
| 2679 |
""" |
| 2680 |
|
| 2681 |
def test_pdb_next_command_subiterator(): |
| 2682 |
"""The next command in a generator with a subiterator. |
| 2683 |
|
| 2684 |
>>> def test_subgenerator(): |
| 2685 |
... yield 0 |
| 2686 |
... return 1 |
| 2687 |
|
| 2688 |
>>> def test_gen(): |
| 2689 |
... x = yield from test_subgenerator() |
| 2690 |
... return x |
| 2691 |
|
| 2692 |
>>> def test_function(): |
| 2693 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 2694 |
... for i in test_gen(): |
| 2695 |
... print('value', i) |
| 2696 |
... x = 123 |
| 2697 |
|
| 2698 |
>>> with PdbTestInput(['step', |
| 2699 |
... 'step', |
| 2700 |
... 'step', |
| 2701 |
... 'next', |
| 2702 |
... 'next', |
| 2703 |
... 'next', |
| 2704 |
... 'continue']): |
| 2705 |
... test_function() |
| 2706 |
> <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(2)test_function() |
| 2707 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 2708 |
(Pdb) step |
| 2709 |
> <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(3)test_function() |
| 2710 |
-> for i in test_gen(): |
| 2711 |
(Pdb) step |
| 2712 |
--Call-- |
| 2713 |
> <doctest test.test_pdb.test_pdb_next_command_subiterator[1]>(1)test_gen() |
| 2714 |
-> def test_gen(): |
| 2715 |
(Pdb) step |
| 2716 |
> <doctest test.test_pdb.test_pdb_next_command_subiterator[1]>(2)test_gen() |
| 2717 |
-> x = yield from test_subgenerator() |
| 2718 |
(Pdb) next |
| 2719 |
value 0 |
| 2720 |
> <doctest test.test_pdb.test_pdb_next_command_subiterator[1]>(3)test_gen() |
| 2721 |
-> return x |
| 2722 |
(Pdb) next |
| 2723 |
Internal StopIteration: 1 |
| 2724 |
> <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(3)test_function() |
| 2725 |
-> for i in test_gen(): |
| 2726 |
(Pdb) next |
| 2727 |
> <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(5)test_function() |
| 2728 |
-> x = 123 |
| 2729 |
(Pdb) continue |
| 2730 |
""" |
| 2731 |
|
| 2732 |
def test_pdb_breakpoint_with_throw(): |
| 2733 |
"""GH-132536: PY_THROW event should not be turned off |
| 2734 |
|
| 2735 |
>>> def gen(): |
| 2736 |
... yield 0 |
| 2737 |
|
| 2738 |
>>> def test_function(): |
| 2739 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 2740 |
... g = gen() |
| 2741 |
... try: |
| 2742 |
... g.throw(TypeError) |
| 2743 |
... except TypeError: |
| 2744 |
... pass |
| 2745 |
|
| 2746 |
>>> with PdbTestInput([ |
| 2747 |
... 'b 7', |
| 2748 |
... 'continue', |
| 2749 |
... 'clear 1', |
| 2750 |
... 'continue', |
| 2751 |
... ]): |
| 2752 |
... test_function() |
| 2753 |
> <doctest test.test_pdb.test_pdb_breakpoint_with_throw[1]>(2)test_function() |
| 2754 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 2755 |
(Pdb) b 7 |
| 2756 |
Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_with_throw[1]>:7 |
| 2757 |
(Pdb) continue |
| 2758 |
> <doctest test.test_pdb.test_pdb_breakpoint_with_throw[1]>(7)test_function() |
| 2759 |
-> pass |
| 2760 |
(Pdb) clear 1 |
| 2761 |
Deleted breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_with_throw[1]>:7 |
| 2762 |
(Pdb) continue |
| 2763 |
""" |
| 2764 |
|
| 2765 |
def test_pdb_multiline_statement(): |
| 2766 |
"""Test for multiline statement |
| 2767 |
|
| 2768 |
>>> def test_function(): |
| 2769 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 2770 |
|
| 2771 |
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE |
| 2772 |
... 'def f(x):', |
| 2773 |
... ' return x * 2', |
| 2774 |
... '', |
| 2775 |
... 'val = 2', |
| 2776 |
... 'if val > 0:', |
| 2777 |
... ' val = f(val)', |
| 2778 |
... '', |
| 2779 |
... '', # empty line should repeat the multi-line statement |
| 2780 |
... 'val', |
| 2781 |
... 'c' |
| 2782 |
... ]): |
| 2783 |
... test_function() |
| 2784 |
> <doctest test.test_pdb.test_pdb_multiline_statement[0]>(2)test_function() |
| 2785 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 2786 |
(Pdb) def f(x): |
| 2787 |
... return x * 2 |
| 2788 |
... |
| 2789 |
(Pdb) val = 2 |
| 2790 |
(Pdb) if val > 0: |
| 2791 |
... val = f(val) |
| 2792 |
... |
| 2793 |
(Pdb) |
| 2794 |
(Pdb) val |
| 2795 |
8 |
| 2796 |
(Pdb) c |
| 2797 |
""" |
| 2798 |
|
| 2799 |
def test_pdb_closure(): |
| 2800 |
"""Test for all expressions/statements that involve closure |
| 2801 |
|
| 2802 |
>>> k = 0 |
| 2803 |
>>> g = 1 |
| 2804 |
>>> def test_function(): |
| 2805 |
... x = 2 |
| 2806 |
... g = 3 |
| 2807 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 2808 |
|
| 2809 |
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE |
| 2810 |
... 'k', |
| 2811 |
... 'g', |
| 2812 |
... 'y = y', |
| 2813 |
... 'global g; g', |
| 2814 |
... 'global g; (lambda: g)()', |
| 2815 |
... '(lambda: x)()', |
| 2816 |
... '(lambda: g)()', |
| 2817 |
... 'lst = [n for n in range(10) if (n % x) == 0]', |
| 2818 |
... 'lst', |
| 2819 |
... 'sum(n for n in lst if n > x)', |
| 2820 |
... 'x = 1; raise Exception()', |
| 2821 |
... 'x', |
| 2822 |
... 'def f():', |
| 2823 |
... ' return x', |
| 2824 |
... '', |
| 2825 |
... 'f()', |
| 2826 |
... 'c' |
| 2827 |
... ]): |
| 2828 |
... test_function() |
| 2829 |
> <doctest test.test_pdb.test_pdb_closure[2]>(4)test_function() |
| 2830 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 2831 |
(Pdb) k |
| 2832 |
0 |
| 2833 |
(Pdb) g |
| 2834 |
3 |
| 2835 |
(Pdb) y = y |
| 2836 |
*** NameError: name 'y' is not defined |
| 2837 |
(Pdb) global g; g |
| 2838 |
1 |
| 2839 |
(Pdb) global g; (lambda: g)() |
| 2840 |
1 |
| 2841 |
(Pdb) (lambda: x)() |
| 2842 |
2 |
| 2843 |
(Pdb) (lambda: g)() |
| 2844 |
3 |
| 2845 |
(Pdb) lst = [n for n in range(10) if (n % x) == 0] |
| 2846 |
(Pdb) lst |
| 2847 |
[0, 2, 4, 6, 8] |
| 2848 |
(Pdb) sum(n for n in lst if n > x) |
| 2849 |
18 |
| 2850 |
(Pdb) x = 1; raise Exception() |
| 2851 |
*** Exception |
| 2852 |
(Pdb) x |
| 2853 |
1 |
| 2854 |
(Pdb) def f(): |
| 2855 |
... return x |
| 2856 |
... |
| 2857 |
(Pdb) f() |
| 2858 |
1 |
| 2859 |
(Pdb) c |
| 2860 |
""" |
| 2861 |
|
| 2862 |
def test_pdb_show_attribute_and_item(): |
| 2863 |
"""Test for expressions with command prefix |
| 2864 |
|
| 2865 |
>>> def test_function(): |
| 2866 |
... n = lambda x: x |
| 2867 |
... c = {"a": 1} |
| 2868 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 2869 |
|
| 2870 |
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE |
| 2871 |
... 'c["a"]', |
| 2872 |
... 'c.get("a")', |
| 2873 |
... 'n(1)', |
| 2874 |
... 'j=1', |
| 2875 |
... 'j+1', |
| 2876 |
... 'r"a"', |
| 2877 |
... 'next(iter([1]))', |
| 2878 |
... 'list((0, 1))', |
| 2879 |
... 'c' |
| 2880 |
... ]): |
| 2881 |
... test_function() |
| 2882 |
> <doctest test.test_pdb.test_pdb_show_attribute_and_item[0]>(4)test_function() |
| 2883 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 2884 |
(Pdb) c["a"] |
| 2885 |
1 |
| 2886 |
(Pdb) c.get("a") |
| 2887 |
1 |
| 2888 |
(Pdb) n(1) |
| 2889 |
1 |
| 2890 |
(Pdb) j=1 |
| 2891 |
(Pdb) j+1 |
| 2892 |
2 |
| 2893 |
(Pdb) r"a" |
| 2894 |
'a' |
| 2895 |
(Pdb) next(iter([1])) |
| 2896 |
1 |
| 2897 |
(Pdb) list((0, 1)) |
| 2898 |
[0, 1] |
| 2899 |
(Pdb) c |
| 2900 |
""" |
| 2901 |
|
| 2902 |
# doctest will modify pdb.set_trace during the test, so we need to backup |
| 2903 |
# the original function to use it in the test |
| 2904 |
original_pdb_settrace = pdb.set_trace |
| 2905 |
|
| 2906 |
def test_pdb_with_inline_breakpoint(): |
| 2907 |
"""Hard-coded breakpoint() calls should invoke the same debugger instance |
| 2908 |
|
| 2909 |
>>> def test_function(): |
| 2910 |
... x = 1 |
| 2911 |
... import pdb; pdb.Pdb().set_trace() |
| 2912 |
... original_pdb_settrace() |
| 2913 |
... x = 2 |
| 2914 |
|
| 2915 |
>>> with PdbTestInput(['display x', |
| 2916 |
... 'n', |
| 2917 |
... 'n', |
| 2918 |
... 'n', |
| 2919 |
... 'n', |
| 2920 |
... 'undisplay', |
| 2921 |
... 'c']): |
| 2922 |
... test_function() |
| 2923 |
> <doctest test.test_pdb.test_pdb_with_inline_breakpoint[0]>(3)test_function() |
| 2924 |
-> import pdb; pdb.Pdb().set_trace() |
| 2925 |
(Pdb) display x |
| 2926 |
display x: 1 |
| 2927 |
(Pdb) n |
| 2928 |
> <doctest test.test_pdb.test_pdb_with_inline_breakpoint[0]>(4)test_function() |
| 2929 |
-> original_pdb_settrace() |
| 2930 |
(Pdb) n |
| 2931 |
> <doctest test.test_pdb.test_pdb_with_inline_breakpoint[0]>(4)test_function() |
| 2932 |
-> original_pdb_settrace() |
| 2933 |
(Pdb) n |
| 2934 |
> <doctest test.test_pdb.test_pdb_with_inline_breakpoint[0]>(5)test_function() |
| 2935 |
-> x = 2 |
| 2936 |
(Pdb) n |
| 2937 |
--Return-- |
| 2938 |
> <doctest test.test_pdb.test_pdb_with_inline_breakpoint[0]>(5)test_function()->None |
| 2939 |
-> x = 2 |
| 2940 |
display x: 2 [old: 1] |
| 2941 |
(Pdb) undisplay |
| 2942 |
(Pdb) c |
| 2943 |
""" |
| 2944 |
|
| 2945 |
def test_pdb_issue_20766(): |
| 2946 |
"""Test for reference leaks when the SIGINT handler is set. |
| 2947 |
|
| 2948 |
>>> def test_function(): |
| 2949 |
... i = 1 |
| 2950 |
... while i <= 2: |
| 2951 |
... sess = pdb.Pdb() |
| 2952 |
... sess.set_trace(sys._getframe()) |
| 2953 |
... print('pdb %d: %s' % (i, sess._previous_sigint_handler)) |
| 2954 |
... i += 1 |
| 2955 |
|
| 2956 |
>>> with PdbTestInput(['continue', |
| 2957 |
... 'continue']): |
| 2958 |
... test_function() |
| 2959 |
> <doctest test.test_pdb.test_pdb_issue_20766[0]>(5)test_function() |
| 2960 |
-> sess.set_trace(sys._getframe()) |
| 2961 |
(Pdb) continue |
| 2962 |
pdb 1: <built-in function default_int_handler> |
| 2963 |
> <doctest test.test_pdb.test_pdb_issue_20766[0]>(5)test_function() |
| 2964 |
-> sess.set_trace(sys._getframe()) |
| 2965 |
(Pdb) continue |
| 2966 |
pdb 2: <built-in function default_int_handler> |
| 2967 |
""" |
| 2968 |
|
| 2969 |
def test_pdb_issue_43318(): |
| 2970 |
"""echo breakpoints cleared with filename:lineno |
| 2971 |
|
| 2972 |
>>> def test_function(): |
| 2973 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 2974 |
... print(1) |
| 2975 |
... print(2) |
| 2976 |
... print(3) |
| 2977 |
... print(4) |
| 2978 |
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE |
| 2979 |
... 'break 3', |
| 2980 |
... 'clear <doctest test.test_pdb.test_pdb_issue_43318[0]>:3', |
| 2981 |
... 'continue' |
| 2982 |
... ]): |
| 2983 |
... test_function() |
| 2984 |
> <doctest test.test_pdb.test_pdb_issue_43318[0]>(2)test_function() |
| 2985 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 2986 |
(Pdb) break 3 |
| 2987 |
Breakpoint 1 at <doctest test.test_pdb.test_pdb_issue_43318[0]>:3 |
| 2988 |
(Pdb) clear <doctest test.test_pdb.test_pdb_issue_43318[0]>:3 |
| 2989 |
Deleted breakpoint 1 at <doctest test.test_pdb.test_pdb_issue_43318[0]>:3 |
| 2990 |
(Pdb) continue |
| 2991 |
1 |
| 2992 |
2 |
| 2993 |
3 |
| 2994 |
4 |
| 2995 |
""" |
| 2996 |
|
| 2997 |
def test_pdb_issue_gh_91742(): |
| 2998 |
"""See GH-91742 |
| 2999 |
|
| 3000 |
>>> def test_function(): |
| 3001 |
... __author__ = "pi" |
| 3002 |
... __version__ = "3.14" |
| 3003 |
... |
| 3004 |
... def about(): |
| 3005 |
... '''About''' |
| 3006 |
... print(f"Author: {__author__!r}", |
| 3007 |
... f"Version: {__version__!r}", |
| 3008 |
... sep=" ") |
| 3009 |
... |
| 3010 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 3011 |
... about() |
| 3012 |
|
| 3013 |
|
| 3014 |
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE |
| 3015 |
... 'step', |
| 3016 |
... 'step', |
| 3017 |
... 'next', |
| 3018 |
... 'next', |
| 3019 |
... 'jump 5', |
| 3020 |
... 'continue' |
| 3021 |
... ]): |
| 3022 |
... test_function() |
| 3023 |
> <doctest test.test_pdb.test_pdb_issue_gh_91742[0]>(11)test_function() |
| 3024 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 3025 |
(Pdb) step |
| 3026 |
> <doctest test.test_pdb.test_pdb_issue_gh_91742[0]>(12)test_function() |
| 3027 |
-> about() |
| 3028 |
(Pdb) step |
| 3029 |
--Call-- |
| 3030 |
> <doctest test.test_pdb.test_pdb_issue_gh_91742[0]>(5)about() |
| 3031 |
-> def about(): |
| 3032 |
(Pdb) next |
| 3033 |
> <doctest test.test_pdb.test_pdb_issue_gh_91742[0]>(7)about() |
| 3034 |
-> print(f"Author: {__author__!r}", |
| 3035 |
(Pdb) next |
| 3036 |
> <doctest test.test_pdb.test_pdb_issue_gh_91742[0]>(8)about() |
| 3037 |
-> f"Version: {__version__!r}", |
| 3038 |
(Pdb) jump 5 |
| 3039 |
> <doctest test.test_pdb.test_pdb_issue_gh_91742[0]>(5)about() |
| 3040 |
-> def about(): |
| 3041 |
(Pdb) continue |
| 3042 |
Author: 'pi' Version: '3.14' |
| 3043 |
""" |
| 3044 |
|
| 3045 |
def test_pdb_issue_gh_94215(): |
| 3046 |
"""See GH-94215 |
| 3047 |
|
| 3048 |
Check that frame_setlineno() does not leak references. |
| 3049 |
|
| 3050 |
>>> def test_function(): |
| 3051 |
... def func(): |
| 3052 |
... def inner(v): pass |
| 3053 |
... inner( |
| 3054 |
... 42 |
| 3055 |
... ) |
| 3056 |
... |
| 3057 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 3058 |
... func() |
| 3059 |
|
| 3060 |
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE |
| 3061 |
... 'step', |
| 3062 |
... 'step', |
| 3063 |
... 'next', |
| 3064 |
... 'next', |
| 3065 |
... 'jump 3', |
| 3066 |
... 'next', |
| 3067 |
... 'next', |
| 3068 |
... 'jump 3', |
| 3069 |
... 'next', |
| 3070 |
... 'next', |
| 3071 |
... 'jump 3', |
| 3072 |
... 'continue' |
| 3073 |
... ]): |
| 3074 |
... test_function() |
| 3075 |
> <doctest test.test_pdb.test_pdb_issue_gh_94215[0]>(8)test_function() |
| 3076 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 3077 |
(Pdb) step |
| 3078 |
> <doctest test.test_pdb.test_pdb_issue_gh_94215[0]>(9)test_function() |
| 3079 |
-> func() |
| 3080 |
(Pdb) step |
| 3081 |
--Call-- |
| 3082 |
> <doctest test.test_pdb.test_pdb_issue_gh_94215[0]>(2)func() |
| 3083 |
-> def func(): |
| 3084 |
(Pdb) next |
| 3085 |
> <doctest test.test_pdb.test_pdb_issue_gh_94215[0]>(3)func() |
| 3086 |
-> def inner(v): pass |
| 3087 |
(Pdb) next |
| 3088 |
> <doctest test.test_pdb.test_pdb_issue_gh_94215[0]>(4)func() |
| 3089 |
-> inner( |
| 3090 |
(Pdb) jump 3 |
| 3091 |
> <doctest test.test_pdb.test_pdb_issue_gh_94215[0]>(3)func() |
| 3092 |
-> def inner(v): pass |
| 3093 |
(Pdb) next |
| 3094 |
> <doctest test.test_pdb.test_pdb_issue_gh_94215[0]>(4)func() |
| 3095 |
-> inner( |
| 3096 |
(Pdb) next |
| 3097 |
> <doctest test.test_pdb.test_pdb_issue_gh_94215[0]>(5)func() |
| 3098 |
-> 42 |
| 3099 |
(Pdb) jump 3 |
| 3100 |
> <doctest test.test_pdb.test_pdb_issue_gh_94215[0]>(3)func() |
| 3101 |
-> def inner(v): pass |
| 3102 |
(Pdb) next |
| 3103 |
> <doctest test.test_pdb.test_pdb_issue_gh_94215[0]>(4)func() |
| 3104 |
-> inner( |
| 3105 |
(Pdb) next |
| 3106 |
> <doctest test.test_pdb.test_pdb_issue_gh_94215[0]>(5)func() |
| 3107 |
-> 42 |
| 3108 |
(Pdb) jump 3 |
| 3109 |
> <doctest test.test_pdb.test_pdb_issue_gh_94215[0]>(3)func() |
| 3110 |
-> def inner(v): pass |
| 3111 |
(Pdb) continue |
| 3112 |
""" |
| 3113 |
|
| 3114 |
def test_pdb_issue_gh_101673(): |
| 3115 |
"""See GH-101673 |
| 3116 |
|
| 3117 |
Make sure ll and switching frames won't revert local variable assignment |
| 3118 |
|
| 3119 |
>>> def test_function(): |
| 3120 |
... a = 1 |
| 3121 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 3122 |
|
| 3123 |
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE |
| 3124 |
... '!a = 2', |
| 3125 |
... 'll', |
| 3126 |
... 'p a', |
| 3127 |
... 'u', |
| 3128 |
... 'p a', |
| 3129 |
... 'd', |
| 3130 |
... 'p a', |
| 3131 |
... 'continue' |
| 3132 |
... ]): |
| 3133 |
... test_function() |
| 3134 |
> <doctest test.test_pdb.test_pdb_issue_gh_101673[0]>(3)test_function() |
| 3135 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 3136 |
(Pdb) !a = 2 |
| 3137 |
(Pdb) ll |
| 3138 |
1 def test_function(): |
| 3139 |
2 a = 1 |
| 3140 |
3 -> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 3141 |
(Pdb) p a |
| 3142 |
2 |
| 3143 |
(Pdb) u |
| 3144 |
> <doctest test.test_pdb.test_pdb_issue_gh_101673[1]>(11)<module>() |
| 3145 |
-> test_function() |
| 3146 |
(Pdb) p a |
| 3147 |
*** NameError: name 'a' is not defined |
| 3148 |
(Pdb) d |
| 3149 |
> <doctest test.test_pdb.test_pdb_issue_gh_101673[0]>(3)test_function() |
| 3150 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 3151 |
(Pdb) p a |
| 3152 |
2 |
| 3153 |
(Pdb) continue |
| 3154 |
""" |
| 3155 |
|
| 3156 |
def test_pdb_issue_gh_103225(): |
| 3157 |
"""See GH-103225 |
| 3158 |
|
| 3159 |
Make sure longlist uses 1-based line numbers in frames that correspond to a module |
| 3160 |
|
| 3161 |
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE |
| 3162 |
... 'longlist', |
| 3163 |
... 'continue' |
| 3164 |
... ]): |
| 3165 |
... a = 1 |
| 3166 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 3167 |
... b = 2 |
| 3168 |
> <doctest test.test_pdb.test_pdb_issue_gh_103225[0]>(6)<module>() |
| 3169 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 3170 |
(Pdb) longlist |
| 3171 |
1 with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE |
| 3172 |
2 'longlist', |
| 3173 |
3 'continue' |
| 3174 |
4 ]): |
| 3175 |
5 a = 1 |
| 3176 |
6 -> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 3177 |
7 b = 2 |
| 3178 |
(Pdb) continue |
| 3179 |
""" |
| 3180 |
|
| 3181 |
def test_pdb_issue_gh_101517(): |
| 3182 |
"""See GH-101517 |
| 3183 |
|
| 3184 |
Make sure pdb doesn't crash when the exception is caught in a try/except* block |
| 3185 |
|
| 3186 |
>>> def test_function(): |
| 3187 |
... try: |
| 3188 |
... raise KeyError |
| 3189 |
... except* Exception as e: |
| 3190 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 3191 |
|
| 3192 |
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE |
| 3193 |
... 'continue' |
| 3194 |
... ]): |
| 3195 |
... test_function() |
| 3196 |
> <doctest test.test_pdb.test_pdb_issue_gh_101517[0]>(5)test_function() |
| 3197 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 3198 |
(Pdb) continue |
| 3199 |
""" |
| 3200 |
|
| 3201 |
def test_pdb_issue_gh_108976(): |
| 3202 |
"""See GH-108976 |
| 3203 |
Make sure setting f_trace_opcodes = True won't crash pdb |
| 3204 |
>>> def test_function(): |
| 3205 |
... import sys |
| 3206 |
... sys._getframe().f_trace_opcodes = True |
| 3207 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 3208 |
... a = 1 |
| 3209 |
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE |
| 3210 |
... 'continue' |
| 3211 |
... ]): |
| 3212 |
... test_function() |
| 3213 |
> <doctest test.test_pdb.test_pdb_issue_gh_108976[0]>(4)test_function() |
| 3214 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 3215 |
(Pdb) continue |
| 3216 |
""" |
| 3217 |
|
| 3218 |
def test_pdb_issue_gh_127321(): |
| 3219 |
"""See GH-127321 |
| 3220 |
breakpoint() should stop at a opcode that has a line number |
| 3221 |
>>> def test_function(): |
| 3222 |
... import pdb; pdb_instance = pdb.Pdb(nosigint=True, readrc=False) |
| 3223 |
... [1, 2] and pdb_instance.set_trace() |
| 3224 |
... a = 1 |
| 3225 |
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE |
| 3226 |
... 'continue' |
| 3227 |
... ]): |
| 3228 |
... test_function() |
| 3229 |
> <doctest test.test_pdb.test_pdb_issue_gh_127321[0]>(4)test_function() |
| 3230 |
-> a = 1 |
| 3231 |
(Pdb) continue |
| 3232 |
""" |
| 3233 |
|
| 3234 |
|
| 3235 |
def test_pdb_issue_gh_136057(): |
| 3236 |
"""See GH-136057 |
| 3237 |
"step" and "next" commands should be able to get over list comprehensions |
| 3238 |
>>> def test_function(): |
| 3239 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 3240 |
... lst = [i for i in range(10)] |
| 3241 |
... for i in lst: pass |
| 3242 |
|
| 3243 |
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE |
| 3244 |
... 'next', |
| 3245 |
... 'next', |
| 3246 |
... 'step', |
| 3247 |
... 'continue', |
| 3248 |
... ]): |
| 3249 |
... test_function() |
| 3250 |
> <doctest test.test_pdb.test_pdb_issue_gh_136057[0]>(2)test_function() |
| 3251 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 3252 |
(Pdb) next |
| 3253 |
> <doctest test.test_pdb.test_pdb_issue_gh_136057[0]>(3)test_function() |
| 3254 |
-> lst = [i for i in range(10)] |
| 3255 |
(Pdb) next |
| 3256 |
> <doctest test.test_pdb.test_pdb_issue_gh_136057[0]>(4)test_function() |
| 3257 |
-> for i in lst: pass |
| 3258 |
(Pdb) step |
| 3259 |
--Return-- |
| 3260 |
> <doctest test.test_pdb.test_pdb_issue_gh_136057[0]>(4)test_function()->None |
| 3261 |
-> for i in lst: pass |
| 3262 |
(Pdb) continue |
| 3263 |
""" |
| 3264 |
|
| 3265 |
|
| 3266 |
def test_pdb_issue_gh_80731(): |
| 3267 |
"""See GH-80731 |
| 3268 |
|
| 3269 |
pdb should correctly print exception info if in an except block. |
| 3270 |
|
| 3271 |
>>> with PdbTestInput([ # doctest: +ELLIPSIS |
| 3272 |
... 'import sys', |
| 3273 |
... 'sys.exc_info()', |
| 3274 |
... 'continue' |
| 3275 |
... ]): |
| 3276 |
... try: |
| 3277 |
... raise ValueError('Correct') |
| 3278 |
... except ValueError: |
| 3279 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 3280 |
> <doctest test.test_pdb.test_pdb_issue_gh_80731[0]>(9)<module>() |
| 3281 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 3282 |
(Pdb) import sys |
| 3283 |
(Pdb) sys.exc_info() |
| 3284 |
(<class 'ValueError'>, ValueError('Correct'), <traceback object at ...>) |
| 3285 |
(Pdb) continue |
| 3286 |
""" |
| 3287 |
|
| 3288 |
|
| 3289 |
def test_pdb_ambiguous_statements(): |
| 3290 |
"""See GH-104301 |
| 3291 |
|
| 3292 |
Make sure that ambiguous statements prefixed by '!' are properly disambiguated |
| 3293 |
|
| 3294 |
>>> with PdbTestInput([ |
| 3295 |
... 's', # step to the print line |
| 3296 |
... '! n = 42', # disambiguated statement: reassign the name n |
| 3297 |
... 'n', # advance the debugger into the print() |
| 3298 |
... 'continue' |
| 3299 |
... ]): |
| 3300 |
... n = -1 |
| 3301 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 3302 |
... print(f"The value of n is {n}") |
| 3303 |
> <doctest test.test_pdb.test_pdb_ambiguous_statements[0]>(8)<module>() |
| 3304 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 3305 |
(Pdb) s |
| 3306 |
> <doctest test.test_pdb.test_pdb_ambiguous_statements[0]>(9)<module>() |
| 3307 |
-> print(f"The value of n is {n}") |
| 3308 |
(Pdb) ! n = 42 |
| 3309 |
(Pdb) n |
| 3310 |
The value of n is 42 |
| 3311 |
> <doctest test.test_pdb.test_pdb_ambiguous_statements[0]>(1)<module>() |
| 3312 |
-> with PdbTestInput([ |
| 3313 |
(Pdb) continue |
| 3314 |
""" |
| 3315 |
|
| 3316 |
def test_pdb_f_trace_lines(): |
| 3317 |
"""GH-80675 |
| 3318 |
|
| 3319 |
pdb should work even if f_trace_lines is set to False on some frames. |
| 3320 |
|
| 3321 |
>>> def test_function(): |
| 3322 |
... import sys |
| 3323 |
... frame = sys._getframe() |
| 3324 |
... frame.f_trace_lines = False |
| 3325 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 3326 |
... if frame.f_trace_lines != False: |
| 3327 |
... print("f_trace_lines is not reset after continue!") |
| 3328 |
|
| 3329 |
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE |
| 3330 |
... 'continue' |
| 3331 |
... ]): |
| 3332 |
... test_function() |
| 3333 |
> <doctest test.test_pdb.test_pdb_f_trace_lines[0]>(5)test_function() |
| 3334 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 3335 |
(Pdb) continue |
| 3336 |
""" |
| 3337 |
|
| 3338 |
def test_pdb_frame_refleak(): |
| 3339 |
""" |
| 3340 |
pdb should not leak reference to frames |
| 3341 |
|
| 3342 |
>>> def frame_leaker(container): |
| 3343 |
... import sys |
| 3344 |
... container.append(sys._getframe()) |
| 3345 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 3346 |
... pass |
| 3347 |
|
| 3348 |
>>> def test_function(): |
| 3349 |
... import gc |
| 3350 |
... container = [] |
| 3351 |
... frame_leaker(container) # c |
| 3352 |
... print(len(gc.get_referrers(container[0]))) |
| 3353 |
... container = [] |
| 3354 |
... frame_leaker(container) # n c |
| 3355 |
... print(len(gc.get_referrers(container[0]))) |
| 3356 |
... container = [] |
| 3357 |
... frame_leaker(container) # r c |
| 3358 |
... print(len(gc.get_referrers(container[0]))) |
| 3359 |
|
| 3360 |
>>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE |
| 3361 |
... 'continue', |
| 3362 |
... 'next', |
| 3363 |
... 'continue', |
| 3364 |
... 'return', |
| 3365 |
... 'continue', |
| 3366 |
... ]): |
| 3367 |
... test_function() |
| 3368 |
> <doctest test.test_pdb.test_pdb_frame_refleak[0]>(4)frame_leaker() |
| 3369 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 3370 |
(Pdb) continue |
| 3371 |
1 |
| 3372 |
> <doctest test.test_pdb.test_pdb_frame_refleak[0]>(4)frame_leaker() |
| 3373 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 3374 |
(Pdb) next |
| 3375 |
> <doctest test.test_pdb.test_pdb_frame_refleak[0]>(5)frame_leaker() |
| 3376 |
-> pass |
| 3377 |
(Pdb) continue |
| 3378 |
1 |
| 3379 |
> <doctest test.test_pdb.test_pdb_frame_refleak[0]>(4)frame_leaker() |
| 3380 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 3381 |
(Pdb) return |
| 3382 |
--Return-- |
| 3383 |
> <doctest test.test_pdb.test_pdb_frame_refleak[0]>(5)frame_leaker()->None |
| 3384 |
-> pass |
| 3385 |
(Pdb) continue |
| 3386 |
1 |
| 3387 |
""" |
| 3388 |
|
| 3389 |
def test_pdb_function_break(): |
| 3390 |
"""Testing the line number of break on function |
| 3391 |
|
| 3392 |
>>> def foo(): pass |
| 3393 |
|
| 3394 |
>>> def bar(): |
| 3395 |
... |
| 3396 |
... pass |
| 3397 |
|
| 3398 |
>>> def boo(): |
| 3399 |
... # comments |
| 3400 |
... global x |
| 3401 |
... x = 1 |
| 3402 |
|
| 3403 |
>>> def gen(): |
| 3404 |
... yield 42 |
| 3405 |
|
| 3406 |
>>> def test_function(): |
| 3407 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 3408 |
|
| 3409 |
>>> with PdbTestInput([ # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE |
| 3410 |
... 'break foo', |
| 3411 |
... 'break bar', |
| 3412 |
... 'break boo', |
| 3413 |
... 'break gen', |
| 3414 |
... 'continue' |
| 3415 |
... ]): |
| 3416 |
... test_function() |
| 3417 |
> <doctest test.test_pdb.test_pdb_function_break[4]>(2)test_function() |
| 3418 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 3419 |
(Pdb) break foo |
| 3420 |
Breakpoint ... at <doctest test.test_pdb.test_pdb_function_break[0]>:1 |
| 3421 |
(Pdb) break bar |
| 3422 |
Breakpoint ... at <doctest test.test_pdb.test_pdb_function_break[1]>:3 |
| 3423 |
(Pdb) break boo |
| 3424 |
Breakpoint ... at <doctest test.test_pdb.test_pdb_function_break[2]>:4 |
| 3425 |
(Pdb) break gen |
| 3426 |
Breakpoint ... at <doctest test.test_pdb.test_pdb_function_break[3]>:2 |
| 3427 |
(Pdb) continue |
| 3428 |
""" |
| 3429 |
|
| 3430 |
def test_pdb_issue_gh_65052(): |
| 3431 |
"""See GH-65052 |
| 3432 |
|
| 3433 |
args, retval and display should not crash if the object is not displayable |
| 3434 |
>>> class A: |
| 3435 |
... def __new__(cls): |
| 3436 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 3437 |
... return object.__new__(cls) |
| 3438 |
... def __init__(self): |
| 3439 |
... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 3440 |
... self.a = 1 |
| 3441 |
... def __repr__(self): |
| 3442 |
... return self.a |
| 3443 |
|
| 3444 |
>>> def test_function(): |
| 3445 |
... A() |
| 3446 |
>>> with PdbTestInput([ # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE |
| 3447 |
... 's', |
| 3448 |
... 's', |
| 3449 |
... 'retval', |
| 3450 |
... 'continue', |
| 3451 |
... 'args', |
| 3452 |
... 'display self', |
| 3453 |
... 'display', |
| 3454 |
... 'continue', |
| 3455 |
... ]): |
| 3456 |
... test_function() |
| 3457 |
> <doctest test.test_pdb.test_pdb_issue_gh_65052[0]>(3)__new__() |
| 3458 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 3459 |
(Pdb) s |
| 3460 |
> <doctest test.test_pdb.test_pdb_issue_gh_65052[0]>(4)__new__() |
| 3461 |
-> return object.__new__(cls) |
| 3462 |
(Pdb) s |
| 3463 |
--Return-- |
| 3464 |
> <doctest test.test_pdb.test_pdb_issue_gh_65052[0]>(4)__new__()-><A instance at ...> |
| 3465 |
-> return object.__new__(cls) |
| 3466 |
(Pdb) retval |
| 3467 |
*** repr(retval) failed: AttributeError: 'A' object has no attribute 'a' *** |
| 3468 |
(Pdb) continue |
| 3469 |
> <doctest test.test_pdb.test_pdb_issue_gh_65052[0]>(6)__init__() |
| 3470 |
-> import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 3471 |
(Pdb) args |
| 3472 |
self = *** repr(self) failed: AttributeError: 'A' object has no attribute 'a' *** |
| 3473 |
(Pdb) display self |
| 3474 |
display self: *** repr(self) failed: AttributeError: 'A' object has no attribute 'a' *** |
| 3475 |
(Pdb) display |
| 3476 |
Currently displaying: |
| 3477 |
self: *** repr(self) failed: AttributeError: 'A' object has no attribute 'a' *** |
| 3478 |
(Pdb) continue |
| 3479 |
""" |
| 3480 |
|
| 3481 |
|
| 3482 |
@support.force_not_colorized_test_class |
| 3483 |
@support.requires_subprocess() |
| 3484 |
class PdbTestCase(unittest.TestCase): |
| 3485 |
def tearDown(self): |
| 3486 |
os_helper.unlink(os_helper.TESTFN) |
| 3487 |
|
| 3488 |
@unittest.skipIf(sys.flags.safe_path, |
| 3489 |
'PYTHONSAFEPATH changes default sys.path') |
| 3490 |
def _run_pdb(self, pdb_args, commands, |
| 3491 |
expected_returncode=0, |
| 3492 |
extra_env=None): |
| 3493 |
self.addCleanup(os_helper.rmtree, '__pycache__') |
| 3494 |
cmd = [sys.executable, '-m', 'pdb'] + pdb_args |
| 3495 |
if extra_env is not None: |
| 3496 |
env = os.environ | extra_env |
| 3497 |
else: |
| 3498 |
env = os.environ |
| 3499 |
with subprocess.Popen( |
| 3500 |
cmd, |
| 3501 |
stdout=subprocess.PIPE, |
| 3502 |
stdin=subprocess.PIPE, |
| 3503 |
stderr=subprocess.PIPE, |
| 3504 |
env = {**env, 'PYTHONIOENCODING': 'utf-8'} |
| 3505 |
) as proc: |
| 3506 |
stdout, stderr = proc.communicate(str.encode(commands)) |
| 3507 |
stdout = bytes.decode(stdout) if isinstance(stdout, bytes) else stdout |
| 3508 |
stderr = bytes.decode(stderr) if isinstance(stderr, bytes) else stderr |
| 3509 |
self.assertEqual( |
| 3510 |
proc.returncode, |
| 3511 |
expected_returncode, |
| 3512 |
f"Unexpected return code\nstdout: {stdout}\nstderr: {stderr}" |
| 3513 |
) |
| 3514 |
return stdout, stderr |
| 3515 |
|
| 3516 |
def run_pdb_script(self, script, commands, |
| 3517 |
expected_returncode=0, |
| 3518 |
extra_env=None, |
| 3519 |
script_args=None, |
| 3520 |
pdbrc=None, |
| 3521 |
remove_home=False): |
| 3522 |
"""Run 'script' lines with pdb and the pdb 'commands'.""" |
| 3523 |
filename = 'main.py' |
| 3524 |
with open(filename, 'w') as f: |
| 3525 |
f.write(textwrap.dedent(script)) |
| 3526 |
|
| 3527 |
if pdbrc is not None: |
| 3528 |
with open('.pdbrc', 'w') as f: |
| 3529 |
f.write(textwrap.dedent(pdbrc)) |
| 3530 |
self.addCleanup(os_helper.unlink, '.pdbrc') |
| 3531 |
self.addCleanup(os_helper.unlink, filename) |
| 3532 |
|
| 3533 |
with os_helper.EnvironmentVarGuard() as env: |
| 3534 |
if remove_home: |
| 3535 |
env.unset('HOME') |
| 3536 |
if script_args is None: |
| 3537 |
script_args = [] |
| 3538 |
stdout, stderr = self._run_pdb([filename] + script_args, commands, expected_returncode, extra_env) |
| 3539 |
return stdout, stderr |
| 3540 |
|
| 3541 |
def run_pdb_module(self, script, commands): |
| 3542 |
"""Runs the script code as part of a module""" |
| 3543 |
self.module_name = 't_main' |
| 3544 |
os_helper.rmtree(self.module_name) |
| 3545 |
main_file = self.module_name + '/__main__.py' |
| 3546 |
init_file = self.module_name + '/__init__.py' |
| 3547 |
os.mkdir(self.module_name) |
| 3548 |
with open(init_file, 'w') as f: |
| 3549 |
pass |
| 3550 |
with open(main_file, 'w') as f: |
| 3551 |
f.write(textwrap.dedent(script)) |
| 3552 |
self.addCleanup(os_helper.rmtree, self.module_name) |
| 3553 |
return self._run_pdb(['-m', self.module_name], commands) |
| 3554 |
|
| 3555 |
def _assert_find_function(self, file_content, func_name, expected): |
| 3556 |
with open(os_helper.TESTFN, 'wb') as f: |
| 3557 |
f.write(file_content) |
| 3558 |
|
| 3559 |
expected = None if not expected else ( |
| 3560 |
expected[0], os_helper.TESTFN, expected[1]) |
| 3561 |
self.assertEqual( |
| 3562 |
expected, pdb.find_function(func_name, os_helper.TESTFN)) |
| 3563 |
|
| 3564 |
def _fd_dir_for_pipe_targets(self): |
| 3565 |
"""Return a directory exposing live file descriptors, if any.""" |
| 3566 |
return self._proc_fd_dir() or self._dev_fd_dir() |
| 3567 |
|
| 3568 |
def _proc_fd_dir(self): |
| 3569 |
"""Return /proc-backed fd dir when it can be used for pipes.""" |
| 3570 |
# GH-142836: Opening /proc/self/fd entries for pipes raises EACCES on |
| 3571 |
# Solaris, so prefer other mechanisms there. |
| 3572 |
if sys.platform.startswith("sunos"): |
| 3573 |
return None |
| 3574 |
|
| 3575 |
proc_fd = "/proc/self/fd" |
| 3576 |
if os.path.isdir(proc_fd) and os.path.exists(os.path.join(proc_fd, '0')): |
| 3577 |
return proc_fd |
| 3578 |
return None |
| 3579 |
|
| 3580 |
def _dev_fd_dir(self): |
| 3581 |
"""Return /dev-backed fd dir when usable.""" |
| 3582 |
dev_fd = "/dev/fd" |
| 3583 |
if os.path.isdir(dev_fd) and os.path.exists(os.path.join(dev_fd, '0')): |
| 3584 |
if sys.platform.startswith("freebsd"): |
| 3585 |
try: |
| 3586 |
if os.stat("/dev").st_dev == os.stat(dev_fd).st_dev: |
| 3587 |
return None |
| 3588 |
except FileNotFoundError: |
| 3589 |
return None |
| 3590 |
return dev_fd |
| 3591 |
return None |
| 3592 |
|
| 3593 |
def test_find_function_empty_file(self): |
| 3594 |
self._assert_find_function(b'', 'foo', None) |
| 3595 |
|
| 3596 |
def test_find_function_found(self): |
| 3597 |
self._assert_find_function( |
| 3598 |
"""\ |
| 3599 |
def foo(): |
| 3600 |
pass |
| 3601 |
|
| 3602 |
def bœr(): |
| 3603 |
pass |
| 3604 |
|
| 3605 |
def quux(): |
| 3606 |
pass |
| 3607 |
""".encode(), |
| 3608 |
'bœr', |
| 3609 |
('bœr', 5), |
| 3610 |
) |
| 3611 |
|
| 3612 |
def test_find_function_found_with_encoding_cookie(self): |
| 3613 |
self._assert_find_function( |
| 3614 |
"""\ |
| 3615 |
# coding: iso-8859-15 |
| 3616 |
def foo(): |
| 3617 |
pass |
| 3618 |
|
| 3619 |
def bœr(): |
| 3620 |
pass |
| 3621 |
|
| 3622 |
def quux(): |
| 3623 |
pass |
| 3624 |
""".encode('iso-8859-15'), |
| 3625 |
'bœr', |
| 3626 |
('bœr', 6), |
| 3627 |
) |
| 3628 |
|
| 3629 |
def test_find_function_found_with_bom(self): |
| 3630 |
self._assert_find_function( |
| 3631 |
codecs.BOM_UTF8 + """\ |
| 3632 |
def bœr(): |
| 3633 |
pass |
| 3634 |
""".encode(), |
| 3635 |
'bœr', |
| 3636 |
('bœr', 2), |
| 3637 |
) |
| 3638 |
|
| 3639 |
def test_spec(self): |
| 3640 |
# Test that __main__.__spec__ is set to None when running a script |
| 3641 |
script = """ |
| 3642 |
import __main__ |
| 3643 |
print(__main__.__spec__) |
| 3644 |
""" |
| 3645 |
|
| 3646 |
commands = "continue" |
| 3647 |
|
| 3648 |
stdout, _ = self.run_pdb_script(script, commands) |
| 3649 |
self.assertIn('None', stdout) |
| 3650 |
|
| 3651 |
def test_script_target_anonymous_pipe(self): |
| 3652 |
""" |
| 3653 |
_ScriptTarget doesn't fail on an anonymous pipe. |
| 3654 |
|
| 3655 |
GH-142315 |
| 3656 |
""" |
| 3657 |
fd_dir = self._fd_dir_for_pipe_targets() |
| 3658 |
if fd_dir is None: |
| 3659 |
self.skipTest('anonymous pipe targets require /proc/self/fd or /dev/fd') |
| 3660 |
|
| 3661 |
read_fd, write_fd = os.pipe() |
| 3662 |
|
| 3663 |
def safe_close(fd): |
| 3664 |
try: |
| 3665 |
os.close(fd) |
| 3666 |
except OSError: |
| 3667 |
pass |
| 3668 |
|
| 3669 |
self.addCleanup(safe_close, read_fd) |
| 3670 |
self.addCleanup(safe_close, write_fd) |
| 3671 |
|
| 3672 |
pipe_path = os.path.join(fd_dir, str(read_fd)) |
| 3673 |
if not os.path.exists(pipe_path): |
| 3674 |
self.skipTest('fd directory does not expose anonymous pipes') |
| 3675 |
|
| 3676 |
script_source = 'marker = "via_pipe"\n' |
| 3677 |
os.write(write_fd, script_source.encode('utf-8')) |
| 3678 |
os.close(write_fd) |
| 3679 |
|
| 3680 |
original_path0 = sys.path[0] |
| 3681 |
self.addCleanup(sys.path.__setitem__, 0, original_path0) |
| 3682 |
|
| 3683 |
target = pdb._ScriptTarget(pipe_path) |
| 3684 |
code_text = target.code |
| 3685 |
namespace = target.namespace |
| 3686 |
exec(code_text, namespace) |
| 3687 |
|
| 3688 |
self.assertEqual(namespace['marker'], 'via_pipe') |
| 3689 |
self.assertEqual(namespace['__file__'], target.filename) |
| 3690 |
self.assertIsNone(namespace['__spec__']) |
| 3691 |
|
| 3692 |
def test_find_function_first_executable_line(self): |
| 3693 |
code = textwrap.dedent("""\ |
| 3694 |
def foo(): pass |
| 3695 |
|
| 3696 |
def bar(): |
| 3697 |
pass # line 4 |
| 3698 |
|
| 3699 |
def baz(): |
| 3700 |
# comment |
| 3701 |
pass # line 8 |
| 3702 |
|
| 3703 |
def mul(): |
| 3704 |
# code on multiple lines |
| 3705 |
code = compile( # line 12 |
| 3706 |
'def f()', |
| 3707 |
'<string>', |
| 3708 |
'exec', |
| 3709 |
) |
| 3710 |
""").encode() |
| 3711 |
|
| 3712 |
self._assert_find_function(code, 'foo', ('foo', 1)) |
| 3713 |
self._assert_find_function(code, 'bar', ('bar', 4)) |
| 3714 |
self._assert_find_function(code, 'baz', ('baz', 8)) |
| 3715 |
self._assert_find_function(code, 'mul', ('mul', 12)) |
| 3716 |
|
| 3717 |
def test_issue7964(self): |
| 3718 |
# open the file as binary so we can force \r\n newline |
| 3719 |
with open(os_helper.TESTFN, 'wb') as f: |
| 3720 |
f.write(b'print("testing my pdb")\r\n') |
| 3721 |
cmd = [sys.executable, '-m', 'pdb', os_helper.TESTFN] |
| 3722 |
proc = subprocess.Popen(cmd, |
| 3723 |
stdout=subprocess.PIPE, |
| 3724 |
stdin=subprocess.PIPE, |
| 3725 |
stderr=subprocess.PIPE, |
| 3726 |
) |
| 3727 |
self.addCleanup(proc.stdout.close) |
| 3728 |
stdout, stderr = proc.communicate(b'quit\n') |
| 3729 |
self.assertNotIn(b'SyntaxError', stdout, |
| 3730 |
"Got a syntax error running test script under PDB") |
| 3731 |
|
| 3732 |
def test_issue46434(self): |
| 3733 |
# Temporarily patch in an extra help command which doesn't have a |
| 3734 |
# docstring to emulate what happens in an embeddable distribution |
| 3735 |
script = """ |
| 3736 |
def do_testcmdwithnodocs(self, arg): |
| 3737 |
pass |
| 3738 |
|
| 3739 |
import pdb |
| 3740 |
pdb.Pdb.do_testcmdwithnodocs = do_testcmdwithnodocs |
| 3741 |
""" |
| 3742 |
commands = """ |
| 3743 |
continue |
| 3744 |
help testcmdwithnodocs |
| 3745 |
""" |
| 3746 |
stdout, stderr = self.run_pdb_script(script, commands) |
| 3747 |
output = (stdout or '') + (stderr or '') |
| 3748 |
self.assertNotIn('AttributeError', output, |
| 3749 |
'Calling help on a command with no docs should be handled gracefully') |
| 3750 |
self.assertIn("*** No help for 'testcmdwithnodocs'; __doc__ string missing", output, |
| 3751 |
'Calling help on a command with no docs should print an error') |
| 3752 |
|
| 3753 |
def test_issue13183(self): |
| 3754 |
script = """ |
| 3755 |
from bar import bar |
| 3756 |
|
| 3757 |
def foo(): |
| 3758 |
bar() |
| 3759 |
|
| 3760 |
def nope(): |
| 3761 |
pass |
| 3762 |
|
| 3763 |
def foobar(): |
| 3764 |
foo() |
| 3765 |
nope() |
| 3766 |
|
| 3767 |
foobar() |
| 3768 |
""" |
| 3769 |
commands = """ |
| 3770 |
from bar import bar |
| 3771 |
break bar |
| 3772 |
continue |
| 3773 |
step |
| 3774 |
step |
| 3775 |
quit |
| 3776 |
""" |
| 3777 |
bar = """ |
| 3778 |
def bar(): |
| 3779 |
pass |
| 3780 |
""" |
| 3781 |
with open('bar.py', 'w') as f: |
| 3782 |
f.write(textwrap.dedent(bar)) |
| 3783 |
self.addCleanup(os_helper.unlink, 'bar.py') |
| 3784 |
stdout, stderr = self.run_pdb_script(script, commands) |
| 3785 |
self.assertTrue( |
| 3786 |
any('main.py(5)foo()->None' in l for l in stdout.splitlines()), |
| 3787 |
'Fail to step into the caller after a return') |
| 3788 |
|
| 3789 |
def test_issue13120(self): |
| 3790 |
# Invoking "continue" on a non-main thread triggered an exception |
| 3791 |
# inside signal.signal. |
| 3792 |
|
| 3793 |
with open(os_helper.TESTFN, 'wb') as f: |
| 3794 |
f.write(textwrap.dedent(""" |
| 3795 |
import threading |
| 3796 |
import pdb |
| 3797 |
|
| 3798 |
def start_pdb(): |
| 3799 |
pdb.Pdb(readrc=False).set_trace() |
| 3800 |
x = 1 |
| 3801 |
y = 1 |
| 3802 |
|
| 3803 |
t = threading.Thread(target=start_pdb) |
| 3804 |
t.start()""").encode('ascii')) |
| 3805 |
cmd = [sys.executable, '-u', os_helper.TESTFN] |
| 3806 |
proc = subprocess.Popen(cmd, |
| 3807 |
stdout=subprocess.PIPE, |
| 3808 |
stdin=subprocess.PIPE, |
| 3809 |
stderr=subprocess.PIPE, |
| 3810 |
env={**os.environ, 'PYTHONIOENCODING': 'utf-8'} |
| 3811 |
) |
| 3812 |
self.addCleanup(proc.stdout.close) |
| 3813 |
stdout, stderr = proc.communicate(b'cont\n') |
| 3814 |
self.assertNotIn(b'Error', stdout, |
| 3815 |
"Got an error running test script under PDB") |
| 3816 |
|
| 3817 |
def test_issue36250(self): |
| 3818 |
|
| 3819 |
with open(os_helper.TESTFN, 'wb') as f: |
| 3820 |
f.write(textwrap.dedent(""" |
| 3821 |
import threading |
| 3822 |
import pdb |
| 3823 |
|
| 3824 |
evt = threading.Event() |
| 3825 |
|
| 3826 |
def start_pdb(): |
| 3827 |
evt.wait() |
| 3828 |
pdb.Pdb(readrc=False).set_trace() |
| 3829 |
|
| 3830 |
t = threading.Thread(target=start_pdb) |
| 3831 |
t.start() |
| 3832 |
pdb.Pdb(readrc=False).set_trace() |
| 3833 |
evt.set() |
| 3834 |
t.join()""").encode('ascii')) |
| 3835 |
cmd = [sys.executable, '-u', os_helper.TESTFN] |
| 3836 |
proc = subprocess.Popen(cmd, |
| 3837 |
stdout=subprocess.PIPE, |
| 3838 |
stdin=subprocess.PIPE, |
| 3839 |
stderr=subprocess.PIPE, |
| 3840 |
env = {**os.environ, 'PYTHONIOENCODING': 'utf-8'} |
| 3841 |
) |
| 3842 |
self.addCleanup(proc.stdout.close) |
| 3843 |
stdout, stderr = proc.communicate(b'cont\ncont\n') |
| 3844 |
self.assertNotIn(b'Error', stdout, |
| 3845 |
"Got an error running test script under PDB") |
| 3846 |
|
| 3847 |
def test_issue16180(self): |
| 3848 |
# A syntax error in the debuggee. |
| 3849 |
script = "def f: pass\n" |
| 3850 |
commands = '' |
| 3851 |
expected = "SyntaxError:" |
| 3852 |
stdout, stderr = self.run_pdb_script( |
| 3853 |
script, commands |
| 3854 |
) |
| 3855 |
self.assertIn(expected, stderr, |
| 3856 |
'\n\nExpected:\n{}\nGot:\n{}\n' |
| 3857 |
'Fail to handle a syntax error in the debuggee.' |
| 3858 |
.format(expected, stderr)) |
| 3859 |
|
| 3860 |
def test_issue84583(self): |
| 3861 |
# A syntax error from ast.literal_eval should not make pdb exit. |
| 3862 |
script = "import ast; ast.literal_eval('')\n" |
| 3863 |
commands = """ |
| 3864 |
continue |
| 3865 |
where |
| 3866 |
quit |
| 3867 |
""" |
| 3868 |
stdout, stderr = self.run_pdb_script(script, commands) |
| 3869 |
# The code should appear 3 times in the stdout/stderr: |
| 3870 |
# 1. when pdb starts (stdout) |
| 3871 |
# 2. when the exception is raised, in trackback (stderr) |
| 3872 |
# 3. in where command (stdout) |
| 3873 |
self.assertEqual(stdout.count("ast.literal_eval('')"), 2) |
| 3874 |
self.assertEqual(stderr.count("ast.literal_eval('')"), 1) |
| 3875 |
|
| 3876 |
def test_issue26053(self): |
| 3877 |
# run command of pdb prompt echoes the correct args |
| 3878 |
script = "print('hello')" |
| 3879 |
commands = """ |
| 3880 |
continue |
| 3881 |
run a b c |
| 3882 |
run d e f |
| 3883 |
quit |
| 3884 |
""" |
| 3885 |
stdout, stderr = self.run_pdb_script(script, commands) |
| 3886 |
res = '\n'.join([x.strip() for x in stdout.splitlines()]) |
| 3887 |
self.assertRegex(res, "Restarting .* with arguments:\na b c") |
| 3888 |
self.assertRegex(res, "Restarting .* with arguments:\nd e f") |
| 3889 |
|
| 3890 |
def test_issue58956(self): |
| 3891 |
# Set a breakpoint in a function that already exists on the call stack |
| 3892 |
# should enable the trace function for the frame. |
| 3893 |
script = """ |
| 3894 |
import bar |
| 3895 |
def foo(): |
| 3896 |
ret = bar.bar() |
| 3897 |
pass |
| 3898 |
foo() |
| 3899 |
""" |
| 3900 |
commands = """ |
| 3901 |
b bar.bar |
| 3902 |
c |
| 3903 |
b main.py:5 |
| 3904 |
c |
| 3905 |
p ret |
| 3906 |
quit |
| 3907 |
""" |
| 3908 |
bar = """ |
| 3909 |
def bar(): |
| 3910 |
return 42 |
| 3911 |
""" |
| 3912 |
with open('bar.py', 'w') as f: |
| 3913 |
f.write(textwrap.dedent(bar)) |
| 3914 |
self.addCleanup(os_helper.unlink, 'bar.py') |
| 3915 |
stdout, stderr = self.run_pdb_script(script, commands) |
| 3916 |
lines = stdout.splitlines() |
| 3917 |
self.assertIn('-> pass', lines) |
| 3918 |
self.assertIn('(Pdb) 42', lines) |
| 3919 |
|
| 3920 |
def test_step_into_botframe(self): |
| 3921 |
# gh-125422 |
| 3922 |
# pdb should not be able to step into the botframe (bdb.py) |
| 3923 |
script = "x = 1" |
| 3924 |
commands = """ |
| 3925 |
step |
| 3926 |
step |
| 3927 |
step |
| 3928 |
quit |
| 3929 |
""" |
| 3930 |
stdout, _ = self.run_pdb_script(script, commands) |
| 3931 |
self.assertIn("The program finished", stdout) |
| 3932 |
self.assertNotIn("bdb.py", stdout) |
| 3933 |
|
| 3934 |
def test_pdbrc_basic(self): |
| 3935 |
script = textwrap.dedent(""" |
| 3936 |
a = 1 |
| 3937 |
b = 2 |
| 3938 |
""") |
| 3939 |
|
| 3940 |
pdbrc = textwrap.dedent(""" |
| 3941 |
# Comments should be fine |
| 3942 |
n |
| 3943 |
p f"{a+8=}" |
| 3944 |
""") |
| 3945 |
|
| 3946 |
stdout, stderr = self.run_pdb_script(script, 'q\n', pdbrc=pdbrc, remove_home=True) |
| 3947 |
self.assertNotIn("SyntaxError", stdout) |
| 3948 |
self.assertIn("a+8=9", stdout) |
| 3949 |
self.assertIn("-> b = 2", stdout) |
| 3950 |
|
| 3951 |
def test_pdbrc_empty_line(self): |
| 3952 |
"""Test that empty lines in .pdbrc are ignored.""" |
| 3953 |
|
| 3954 |
script = textwrap.dedent(""" |
| 3955 |
a = 1 |
| 3956 |
b = 2 |
| 3957 |
c = 3 |
| 3958 |
""") |
| 3959 |
|
| 3960 |
pdbrc = textwrap.dedent(""" |
| 3961 |
n |
| 3962 |
|
| 3963 |
""") |
| 3964 |
|
| 3965 |
stdout, stderr = self.run_pdb_script(script, 'q\n', pdbrc=pdbrc, remove_home=True) |
| 3966 |
self.assertIn("b = 2", stdout) |
| 3967 |
self.assertNotIn("c = 3", stdout) |
| 3968 |
|
| 3969 |
def test_pdbrc_alias(self): |
| 3970 |
script = textwrap.dedent(""" |
| 3971 |
class A: |
| 3972 |
def __init__(self): |
| 3973 |
self.attr = 1 |
| 3974 |
a = A() |
| 3975 |
b = 2 |
| 3976 |
""") |
| 3977 |
|
| 3978 |
pdbrc = textwrap.dedent(""" |
| 3979 |
alias pi for k in %1.__dict__.keys(): print(f"%1.{k} = {%1.__dict__[k]}") |
| 3980 |
until 6 |
| 3981 |
pi a |
| 3982 |
""") |
| 3983 |
|
| 3984 |
stdout, stderr = self.run_pdb_script(script, 'q\n', pdbrc=pdbrc, remove_home=True) |
| 3985 |
self.assertIn("a.attr = 1", stdout) |
| 3986 |
|
| 3987 |
def test_pdbrc_semicolon(self): |
| 3988 |
script = textwrap.dedent(""" |
| 3989 |
class A: |
| 3990 |
def __init__(self): |
| 3991 |
self.attr = 1 |
| 3992 |
a = A() |
| 3993 |
b = 2 |
| 3994 |
""") |
| 3995 |
|
| 3996 |
pdbrc = textwrap.dedent(""" |
| 3997 |
b 5;;c;;n |
| 3998 |
""") |
| 3999 |
|
| 4000 |
stdout, stderr = self.run_pdb_script(script, 'q\n', pdbrc=pdbrc, remove_home=True) |
| 4001 |
self.assertIn("-> b = 2", stdout) |
| 4002 |
|
| 4003 |
def test_pdbrc_commands(self): |
| 4004 |
script = textwrap.dedent(""" |
| 4005 |
class A: |
| 4006 |
def __init__(self): |
| 4007 |
self.attr = 1 |
| 4008 |
a = A() |
| 4009 |
b = 2 |
| 4010 |
""") |
| 4011 |
|
| 4012 |
pdbrc = textwrap.dedent(""" |
| 4013 |
b 6 |
| 4014 |
commands 1 ;; p a;; end |
| 4015 |
c |
| 4016 |
""") |
| 4017 |
|
| 4018 |
stdout, stderr = self.run_pdb_script(script, 'q\n', pdbrc=pdbrc, remove_home=True) |
| 4019 |
self.assertIn("<__main__.A object at", stdout) |
| 4020 |
|
| 4021 |
def test_readrc_kwarg(self): |
| 4022 |
script = textwrap.dedent(""" |
| 4023 |
print('hello') |
| 4024 |
""") |
| 4025 |
|
| 4026 |
stdout, stderr = self.run_pdb_script(script, 'q\n', pdbrc='invalid', remove_home=True) |
| 4027 |
self.assertIn("NameError: name 'invalid' is not defined", stdout) |
| 4028 |
|
| 4029 |
def test_readrc_homedir(self): |
| 4030 |
with os_helper.EnvironmentVarGuard() as env: |
| 4031 |
env.unset("HOME") |
| 4032 |
with os_helper.temp_dir() as temp_dir, patch("os.path.expanduser"): |
| 4033 |
rc_path = os.path.join(temp_dir, ".pdbrc") |
| 4034 |
os.path.expanduser.return_value = rc_path |
| 4035 |
with open(rc_path, "w") as f: |
| 4036 |
f.write("invalid") |
| 4037 |
self.assertEqual(pdb.Pdb().rcLines[0], "invalid") |
| 4038 |
|
| 4039 |
def test_header(self): |
| 4040 |
stdout = StringIO() |
| 4041 |
header = 'Nobody expects... blah, blah, blah' |
| 4042 |
with ExitStack() as resources: |
| 4043 |
resources.enter_context(patch('sys.stdout', stdout)) |
| 4044 |
# patch pdb.Pdb.set_trace() to avoid entering the debugger |
| 4045 |
resources.enter_context(patch.object(pdb.Pdb, 'set_trace')) |
| 4046 |
# We need to manually clear pdb.Pdb._last_pdb_instance so a |
| 4047 |
# new instance with stdout redirected could be created when |
| 4048 |
# pdb.set_trace() is called. |
| 4049 |
pdb.Pdb._last_pdb_instance = None |
| 4050 |
pdb.set_trace(header=header) |
| 4051 |
self.assertEqual(stdout.getvalue(), header + '\n') |
| 4052 |
|
| 4053 |
def test_run_module(self): |
| 4054 |
script = """print("SUCCESS")""" |
| 4055 |
commands = """ |
| 4056 |
continue |
| 4057 |
quit |
| 4058 |
""" |
| 4059 |
stdout, stderr = self.run_pdb_module(script, commands) |
| 4060 |
self.assertTrue(any("SUCCESS" in l for l in stdout.splitlines()), stdout) |
| 4061 |
|
| 4062 |
def test_module_is_run_as_main(self): |
| 4063 |
script = """ |
| 4064 |
if __name__ == '__main__': |
| 4065 |
print("SUCCESS") |
| 4066 |
""" |
| 4067 |
commands = """ |
| 4068 |
continue |
| 4069 |
quit |
| 4070 |
""" |
| 4071 |
stdout, stderr = self.run_pdb_module(script, commands) |
| 4072 |
self.assertTrue(any("SUCCESS" in l for l in stdout.splitlines()), stdout) |
| 4073 |
|
| 4074 |
def test_run_module_with_args(self): |
| 4075 |
commands = """ |
| 4076 |
continue |
| 4077 |
""" |
| 4078 |
self._run_pdb(["calendar", "-m"], commands, expected_returncode=1) |
| 4079 |
|
| 4080 |
_, stderr = self._run_pdb(["-m", "calendar", "-p", "1"], commands) |
| 4081 |
self.assertIn("unrecognized arguments: -p", stderr) |
| 4082 |
|
| 4083 |
stdout, _ = self._run_pdb(["-m", "calendar", "1"], commands) |
| 4084 |
self.assertIn("December", stdout) |
| 4085 |
|
| 4086 |
stdout, _ = self._run_pdb(["-m", "calendar", "--type", "text"], commands) |
| 4087 |
self.assertIn("December", stdout) |
| 4088 |
|
| 4089 |
def test_run_script_with_args(self): |
| 4090 |
script = """ |
| 4091 |
import sys |
| 4092 |
print(sys.argv[1:]) |
| 4093 |
""" |
| 4094 |
commands = """ |
| 4095 |
continue |
| 4096 |
quit |
| 4097 |
""" |
| 4098 |
|
| 4099 |
stdout, stderr = self.run_pdb_script(script, commands, script_args=["--bar", "foo"]) |
| 4100 |
self.assertIn("['--bar', 'foo']", stdout) |
| 4101 |
|
| 4102 |
def test_breakpoint(self): |
| 4103 |
script = """ |
| 4104 |
if __name__ == '__main__': |
| 4105 |
pass |
| 4106 |
print("SUCCESS") |
| 4107 |
pass |
| 4108 |
""" |
| 4109 |
commands = """ |
| 4110 |
b 3 |
| 4111 |
quit |
| 4112 |
""" |
| 4113 |
stdout, stderr = self.run_pdb_module(script, commands) |
| 4114 |
self.assertTrue(any("Breakpoint 1 at" in l for l in stdout.splitlines()), stdout) |
| 4115 |
self.assertTrue(all("SUCCESS" not in l for l in stdout.splitlines()), stdout) |
| 4116 |
|
| 4117 |
def test_run_pdb_with_pdb(self): |
| 4118 |
commands = """ |
| 4119 |
c |
| 4120 |
quit |
| 4121 |
""" |
| 4122 |
stdout, stderr = self._run_pdb(["-m", "pdb"], commands) |
| 4123 |
self.assertIn( |
| 4124 |
pdb._usage, |
| 4125 |
stdout.replace('\r', '') # remove \r for windows |
| 4126 |
) |
| 4127 |
|
| 4128 |
def test_module_without_a_main(self): |
| 4129 |
module_name = 't_main' |
| 4130 |
os_helper.rmtree(module_name) |
| 4131 |
init_file = module_name + '/__init__.py' |
| 4132 |
os.mkdir(module_name) |
| 4133 |
with open(init_file, 'w'): |
| 4134 |
pass |
| 4135 |
self.addCleanup(os_helper.rmtree, module_name) |
| 4136 |
stdout, stderr = self._run_pdb( |
| 4137 |
['-m', module_name], "", expected_returncode=1 |
| 4138 |
) |
| 4139 |
self.assertIn("ImportError: No module named t_main.__main__;", stdout) |
| 4140 |
|
| 4141 |
def test_package_without_a_main(self): |
| 4142 |
pkg_name = 't_pkg' |
| 4143 |
module_name = 't_main' |
| 4144 |
os_helper.rmtree(pkg_name) |
| 4145 |
modpath = pkg_name + '/' + module_name |
| 4146 |
os.makedirs(modpath) |
| 4147 |
with open(modpath + '/__init__.py', 'w'): |
| 4148 |
pass |
| 4149 |
self.addCleanup(os_helper.rmtree, pkg_name) |
| 4150 |
stdout, stderr = self._run_pdb( |
| 4151 |
['-m', modpath.replace('/', '.')], "", expected_returncode=1 |
| 4152 |
) |
| 4153 |
self.assertIn( |
| 4154 |
"'t_pkg.t_main' is a package and cannot be directly executed", |
| 4155 |
stdout) |
| 4156 |
|
| 4157 |
def test_nonexistent_module(self): |
| 4158 |
assert not os.path.exists(os_helper.TESTFN) |
| 4159 |
stdout, stderr = self._run_pdb(["-m", os_helper.TESTFN], "", expected_returncode=1) |
| 4160 |
self.assertIn(f"ImportError: No module named {os_helper.TESTFN}", stdout) |
| 4161 |
|
| 4162 |
def test_dir_as_script(self): |
| 4163 |
with os_helper.temp_dir() as temp_dir: |
| 4164 |
stdout, stderr = self._run_pdb([temp_dir], "", expected_returncode=1) |
| 4165 |
self.assertIn(f"Error: {temp_dir} is a directory", stdout) |
| 4166 |
|
| 4167 |
def test_invalid_cmd_line_options(self): |
| 4168 |
stdout, stderr = self._run_pdb(["-c"], "", expected_returncode=2) |
| 4169 |
self.assertIn(f"pdb: error: argument -c/--command: expected one argument", stderr.split('\n')[1]) |
| 4170 |
stdout, stderr = self._run_pdb(["--spam", "-m", "pdb"], "", expected_returncode=2) |
| 4171 |
self.assertIn(f"pdb: error: unrecognized arguments: --spam", stderr.split('\n')[1]) |
| 4172 |
|
| 4173 |
def test_blocks_at_first_code_line(self): |
| 4174 |
script = """ |
| 4175 |
#This is a comment, on line 2 |
| 4176 |
|
| 4177 |
print("SUCCESS") |
| 4178 |
""" |
| 4179 |
commands = """ |
| 4180 |
quit |
| 4181 |
""" |
| 4182 |
stdout, stderr = self.run_pdb_module(script, commands) |
| 4183 |
self.assertTrue(any("__main__.py(4)<module>()" |
| 4184 |
in l for l in stdout.splitlines()), stdout) |
| 4185 |
|
| 4186 |
def test_file_modified_after_execution(self): |
| 4187 |
script = """ |
| 4188 |
print("hello") |
| 4189 |
""" |
| 4190 |
|
| 4191 |
# the time.sleep is needed for low-resolution filesystems like HFS+ |
| 4192 |
commands = """ |
| 4193 |
filename = $_frame.f_code.co_filename |
| 4194 |
f = open(filename, "w") |
| 4195 |
f.write("print('goodbye')") |
| 4196 |
import time; time.sleep(1) |
| 4197 |
f.close() |
| 4198 |
ll |
| 4199 |
""" |
| 4200 |
|
| 4201 |
stdout, stderr = self.run_pdb_script(script, commands) |
| 4202 |
self.assertIn("WARNING:", stdout) |
| 4203 |
self.assertIn("was edited", stdout) |
| 4204 |
|
| 4205 |
def test_file_modified_and_immediately_restarted(self): |
| 4206 |
script = """ |
| 4207 |
print("hello") |
| 4208 |
""" |
| 4209 |
|
| 4210 |
# the time.sleep is needed for low-resolution filesystems like HFS+ |
| 4211 |
commands = """ |
| 4212 |
filename = $_frame.f_code.co_filename |
| 4213 |
f = open(filename, "w") |
| 4214 |
f.write("print('goodbye')") |
| 4215 |
import time; time.sleep(1) |
| 4216 |
f.close() |
| 4217 |
restart |
| 4218 |
""" |
| 4219 |
|
| 4220 |
stdout, stderr = self.run_pdb_script(script, commands) |
| 4221 |
self.assertNotIn("WARNING:", stdout) |
| 4222 |
self.assertNotIn("was edited", stdout) |
| 4223 |
|
| 4224 |
def test_file_modified_after_execution_with_multiple_instances(self): |
| 4225 |
# the time.sleep is needed for low-resolution filesystems like HFS+ |
| 4226 |
script = """ |
| 4227 |
import pdb; pdb.Pdb().set_trace() |
| 4228 |
with open(__file__, "w") as f: |
| 4229 |
f.write("print('goodbye')\\n" * 5) |
| 4230 |
import time; time.sleep(1) |
| 4231 |
import pdb; pdb.Pdb().set_trace() |
| 4232 |
""" |
| 4233 |
|
| 4234 |
commands = """ |
| 4235 |
continue |
| 4236 |
continue |
| 4237 |
""" |
| 4238 |
|
| 4239 |
filename = 'main.py' |
| 4240 |
with open(filename, 'w') as f: |
| 4241 |
f.write(textwrap.dedent(script)) |
| 4242 |
self.addCleanup(os_helper.unlink, filename) |
| 4243 |
self.addCleanup(os_helper.rmtree, '__pycache__') |
| 4244 |
cmd = [sys.executable, filename] |
| 4245 |
with subprocess.Popen( |
| 4246 |
cmd, |
| 4247 |
stdout=subprocess.PIPE, |
| 4248 |
stdin=subprocess.PIPE, |
| 4249 |
stderr=subprocess.PIPE, |
| 4250 |
env = {**os.environ, 'PYTHONIOENCODING': 'utf-8'}, |
| 4251 |
) as proc: |
| 4252 |
stdout, _ = proc.communicate(str.encode(commands)) |
| 4253 |
stdout = stdout and bytes.decode(stdout) |
| 4254 |
|
| 4255 |
self.assertEqual(proc.returncode, 0) |
| 4256 |
self.assertIn("WARNING:", stdout) |
| 4257 |
self.assertIn("was edited", stdout) |
| 4258 |
|
| 4259 |
def test_file_modified_after_execution_with_restart(self): |
| 4260 |
script = """ |
| 4261 |
import random |
| 4262 |
# Any code with a source to step into so this script is not checked |
| 4263 |
# for changes when it's being changed |
| 4264 |
random.randint(1, 4) |
| 4265 |
print("hello") |
| 4266 |
""" |
| 4267 |
|
| 4268 |
commands = """ |
| 4269 |
ll |
| 4270 |
n |
| 4271 |
s |
| 4272 |
filename = $_frame.f_back.f_code.co_filename |
| 4273 |
def change_file(content, filename): |
| 4274 |
with open(filename, "w") as f: |
| 4275 |
f.write(f"print({content})") |
| 4276 |
|
| 4277 |
change_file('world', filename) |
| 4278 |
restart |
| 4279 |
ll |
| 4280 |
""" |
| 4281 |
|
| 4282 |
stdout, stderr = self.run_pdb_script(script, commands) |
| 4283 |
# Make sure the code is running correctly and the file is edited |
| 4284 |
self.assertIn("hello", stdout) |
| 4285 |
self.assertIn("world", stdout) |
| 4286 |
# The file was edited, but restart should clear the state and consider |
| 4287 |
# the file as up to date |
| 4288 |
self.assertNotIn("WARNING:", stdout) |
| 4289 |
|
| 4290 |
def test_post_mortem_restart(self): |
| 4291 |
script = """ |
| 4292 |
def foo(): |
| 4293 |
raise ValueError("foo") |
| 4294 |
foo() |
| 4295 |
""" |
| 4296 |
|
| 4297 |
commands = """ |
| 4298 |
continue |
| 4299 |
restart |
| 4300 |
continue |
| 4301 |
quit |
| 4302 |
""" |
| 4303 |
|
| 4304 |
stdout, stderr = self.run_pdb_script(script, commands) |
| 4305 |
self.assertIn("Restarting", stdout) |
| 4306 |
|
| 4307 |
def test_relative_imports(self): |
| 4308 |
self.module_name = 't_main' |
| 4309 |
os_helper.rmtree(self.module_name) |
| 4310 |
main_file = self.module_name + '/__main__.py' |
| 4311 |
init_file = self.module_name + '/__init__.py' |
| 4312 |
module_file = self.module_name + '/module.py' |
| 4313 |
self.addCleanup(os_helper.rmtree, self.module_name) |
| 4314 |
os.mkdir(self.module_name) |
| 4315 |
with open(init_file, 'w') as f: |
| 4316 |
f.write(textwrap.dedent(""" |
| 4317 |
top_var = "VAR from top" |
| 4318 |
""")) |
| 4319 |
with open(main_file, 'w') as f: |
| 4320 |
f.write(textwrap.dedent(""" |
| 4321 |
from . import top_var |
| 4322 |
from .module import var |
| 4323 |
from . import module |
| 4324 |
pass # We'll stop here and print the vars |
| 4325 |
""")) |
| 4326 |
with open(module_file, 'w') as f: |
| 4327 |
f.write(textwrap.dedent(""" |
| 4328 |
var = "VAR from module" |
| 4329 |
var2 = "second var" |
| 4330 |
""")) |
| 4331 |
commands = """ |
| 4332 |
b 5 |
| 4333 |
c |
| 4334 |
p top_var |
| 4335 |
p var |
| 4336 |
p module.var2 |
| 4337 |
quit |
| 4338 |
""" |
| 4339 |
stdout, _ = self._run_pdb(['-m', self.module_name], commands) |
| 4340 |
self.assertTrue(any("VAR from module" in l for l in stdout.splitlines()), stdout) |
| 4341 |
self.assertTrue(any("VAR from top" in l for l in stdout.splitlines())) |
| 4342 |
self.assertTrue(any("second var" in l for l in stdout.splitlines())) |
| 4343 |
|
| 4344 |
def test_relative_imports_on_plain_module(self): |
| 4345 |
# Validates running a plain module. See bpo32691 |
| 4346 |
self.module_name = 't_main' |
| 4347 |
os_helper.rmtree(self.module_name) |
| 4348 |
main_file = self.module_name + '/runme.py' |
| 4349 |
init_file = self.module_name + '/__init__.py' |
| 4350 |
module_file = self.module_name + '/module.py' |
| 4351 |
self.addCleanup(os_helper.rmtree, self.module_name) |
| 4352 |
os.mkdir(self.module_name) |
| 4353 |
with open(init_file, 'w') as f: |
| 4354 |
f.write(textwrap.dedent(""" |
| 4355 |
top_var = "VAR from top" |
| 4356 |
""")) |
| 4357 |
with open(main_file, 'w') as f: |
| 4358 |
f.write(textwrap.dedent(""" |
| 4359 |
from . import module |
| 4360 |
pass # We'll stop here and print the vars |
| 4361 |
""")) |
| 4362 |
with open(module_file, 'w') as f: |
| 4363 |
f.write(textwrap.dedent(""" |
| 4364 |
var = "VAR from module" |
| 4365 |
""")) |
| 4366 |
commands = """ |
| 4367 |
b 3 |
| 4368 |
c |
| 4369 |
p module.var |
| 4370 |
quit |
| 4371 |
""" |
| 4372 |
stdout, _ = self._run_pdb(['-m', self.module_name + '.runme'], commands) |
| 4373 |
self.assertTrue(any("VAR from module" in l for l in stdout.splitlines()), stdout) |
| 4374 |
|
| 4375 |
def test_errors_in_command(self): |
| 4376 |
commands = "\n".join([ |
| 4377 |
'print(]', |
| 4378 |
'debug print(', |
| 4379 |
'debug doesnotexist', |
| 4380 |
'c', |
| 4381 |
]) |
| 4382 |
stdout, _ = self.run_pdb_script('pass', commands + '\n') |
| 4383 |
|
| 4384 |
self.assertEqual(stdout.splitlines()[1:], [ |
| 4385 |
'-> pass', |
| 4386 |
"(Pdb) *** SyntaxError: closing parenthesis ']' does not match opening " |
| 4387 |
"parenthesis '('", |
| 4388 |
|
| 4389 |
'(Pdb) ENTERING RECURSIVE DEBUGGER', |
| 4390 |
'*** SyntaxError: \'(\' was never closed', |
| 4391 |
'LEAVING RECURSIVE DEBUGGER', |
| 4392 |
|
| 4393 |
'(Pdb) ENTERING RECURSIVE DEBUGGER', |
| 4394 |
'> <string>(1)<module>()', |
| 4395 |
"((Pdb)) *** NameError: name 'doesnotexist' is not defined", |
| 4396 |
'LEAVING RECURSIVE DEBUGGER', |
| 4397 |
'(Pdb) ', |
| 4398 |
]) |
| 4399 |
|
| 4400 |
def test_issue34266(self): |
| 4401 |
'''do_run handles exceptions from parsing its arg''' |
| 4402 |
def check(bad_arg, msg): |
| 4403 |
commands = "\n".join([ |
| 4404 |
f'run {bad_arg}', |
| 4405 |
'q', |
| 4406 |
]) |
| 4407 |
stdout, _ = self.run_pdb_script('pass', commands + '\n') |
| 4408 |
self.assertEqual(stdout.splitlines()[1:], [ |
| 4409 |
'-> pass', |
| 4410 |
f'(Pdb) *** Cannot run {bad_arg}: {msg}', |
| 4411 |
'(Pdb) ', |
| 4412 |
]) |
| 4413 |
check('\\', 'No escaped character') |
| 4414 |
check('"', 'No closing quotation') |
| 4415 |
|
| 4416 |
def test_issue42384(self): |
| 4417 |
'''When running `python foo.py` sys.path[0] is an absolute path. `python -m pdb foo.py` should behave the same''' |
| 4418 |
script = textwrap.dedent(""" |
| 4419 |
import sys |
| 4420 |
print('sys.path[0] is', sys.path[0]) |
| 4421 |
""") |
| 4422 |
commands = 'c\nq' |
| 4423 |
|
| 4424 |
with os_helper.temp_cwd() as cwd: |
| 4425 |
expected = f'(Pdb) sys.path[0] is {os.path.realpath(cwd)}' |
| 4426 |
|
| 4427 |
stdout, stderr = self.run_pdb_script(script, commands) |
| 4428 |
|
| 4429 |
self.assertEqual(stdout.split('\n')[2].rstrip('\r'), expected) |
| 4430 |
|
| 4431 |
@os_helper.skip_unless_symlink |
| 4432 |
def test_issue42384_symlink(self): |
| 4433 |
'''When running `python foo.py` sys.path[0] resolves symlinks. `python -m pdb foo.py` should behave the same''' |
| 4434 |
script = textwrap.dedent(""" |
| 4435 |
import sys |
| 4436 |
print('sys.path[0] is', sys.path[0]) |
| 4437 |
""") |
| 4438 |
commands = 'c\nq' |
| 4439 |
|
| 4440 |
with os_helper.temp_cwd() as cwd: |
| 4441 |
cwd = os.path.realpath(cwd) |
| 4442 |
dir_one = os.path.join(cwd, 'dir_one') |
| 4443 |
dir_two = os.path.join(cwd, 'dir_two') |
| 4444 |
expected = f'(Pdb) sys.path[0] is {dir_one}' |
| 4445 |
|
| 4446 |
os.mkdir(dir_one) |
| 4447 |
with open(os.path.join(dir_one, 'foo.py'), 'w') as f: |
| 4448 |
f.write(script) |
| 4449 |
os.mkdir(dir_two) |
| 4450 |
os.symlink(os.path.join(dir_one, 'foo.py'), os.path.join(dir_two, 'foo.py')) |
| 4451 |
|
| 4452 |
stdout, stderr = self._run_pdb([os.path.join('dir_two', 'foo.py')], commands) |
| 4453 |
|
| 4454 |
self.assertEqual(stdout.split('\n')[2].rstrip('\r'), expected) |
| 4455 |
|
| 4456 |
def test_safe_path(self): |
| 4457 |
""" With safe_path set, pdb should not mangle sys.path[0]""" |
| 4458 |
|
| 4459 |
script = textwrap.dedent(""" |
| 4460 |
import sys |
| 4461 |
import random |
| 4462 |
print('sys.path[0] is', sys.path[0]) |
| 4463 |
""") |
| 4464 |
commands = 'c\n' |
| 4465 |
|
| 4466 |
|
| 4467 |
with os_helper.temp_cwd() as cwd: |
| 4468 |
stdout, _ = self.run_pdb_script(script, commands, extra_env={'PYTHONSAFEPATH': '1'}) |
| 4469 |
|
| 4470 |
unexpected = f'sys.path[0] is {os.path.realpath(cwd)}' |
| 4471 |
self.assertNotIn(unexpected, stdout) |
| 4472 |
|
| 4473 |
def test_issue42383(self): |
| 4474 |
with os_helper.temp_cwd() as cwd: |
| 4475 |
with open('foo.py', 'w') as f: |
| 4476 |
s = textwrap.dedent(""" |
| 4477 |
print('The correct file was executed') |
| 4478 |
|
| 4479 |
import os |
| 4480 |
os.chdir("subdir") |
| 4481 |
""") |
| 4482 |
f.write(s) |
| 4483 |
|
| 4484 |
subdir = os.path.join(cwd, 'subdir') |
| 4485 |
os.mkdir(subdir) |
| 4486 |
os.mkdir(os.path.join(subdir, 'subdir')) |
| 4487 |
wrong_file = os.path.join(subdir, 'foo.py') |
| 4488 |
|
| 4489 |
with open(wrong_file, 'w') as f: |
| 4490 |
f.write('print("The wrong file was executed")') |
| 4491 |
|
| 4492 |
stdout, stderr = self._run_pdb(['foo.py'], 'c\nc\nq') |
| 4493 |
expected = '(Pdb) The correct file was executed' |
| 4494 |
self.assertEqual(stdout.split('\n')[6].rstrip('\r'), expected) |
| 4495 |
|
| 4496 |
def test_gh_94215_crash(self): |
| 4497 |
script = """\ |
| 4498 |
def func(): |
| 4499 |
def inner(v): pass |
| 4500 |
inner( |
| 4501 |
42 |
| 4502 |
) |
| 4503 |
func() |
| 4504 |
""" |
| 4505 |
commands = textwrap.dedent(""" |
| 4506 |
break func |
| 4507 |
continue |
| 4508 |
next |
| 4509 |
next |
| 4510 |
jump 2 |
| 4511 |
""") |
| 4512 |
stdout, stderr = self.run_pdb_script(script, commands) |
| 4513 |
self.assertFalse(stderr) |
| 4514 |
|
| 4515 |
def test_gh_93696_frozen_list(self): |
| 4516 |
frozen_src = """ |
| 4517 |
def func(): |
| 4518 |
x = "Sentinel string for gh-93696" |
| 4519 |
print(x) |
| 4520 |
""" |
| 4521 |
host_program = """ |
| 4522 |
import os |
| 4523 |
import sys |
| 4524 |
|
| 4525 |
def _create_fake_frozen_module(): |
| 4526 |
with open('gh93696.py') as f: |
| 4527 |
src = f.read() |
| 4528 |
|
| 4529 |
# this function has a co_filename as if it were in a frozen module |
| 4530 |
dummy_mod = compile(src, "<frozen gh93696>", "exec") |
| 4531 |
func_code = dummy_mod.co_consts[0] |
| 4532 |
|
| 4533 |
mod = type(sys)("gh93696") |
| 4534 |
mod.func = type(lambda: None)(func_code, mod.__dict__) |
| 4535 |
mod.__file__ = 'gh93696.py' |
| 4536 |
|
| 4537 |
return mod |
| 4538 |
|
| 4539 |
mod = _create_fake_frozen_module() |
| 4540 |
mod.func() |
| 4541 |
""" |
| 4542 |
commands_list = """ |
| 4543 |
break 20 |
| 4544 |
continue |
| 4545 |
step |
| 4546 |
break 4 |
| 4547 |
list |
| 4548 |
quit |
| 4549 |
""" |
| 4550 |
commands_longlist = """ |
| 4551 |
break 20 |
| 4552 |
continue |
| 4553 |
step |
| 4554 |
break 4 |
| 4555 |
longlist |
| 4556 |
quit |
| 4557 |
""" |
| 4558 |
with open('gh93696.py', 'w') as f: |
| 4559 |
f.write(textwrap.dedent(frozen_src)) |
| 4560 |
|
| 4561 |
with open('gh93696_host.py', 'w') as f: |
| 4562 |
f.write(textwrap.dedent(host_program)) |
| 4563 |
|
| 4564 |
self.addCleanup(os_helper.unlink, 'gh93696.py') |
| 4565 |
self.addCleanup(os_helper.unlink, 'gh93696_host.py') |
| 4566 |
|
| 4567 |
# verify that pdb found the source of the "frozen" function and it |
| 4568 |
# shows the breakpoint at the correct line for both list and longlist |
| 4569 |
for commands in (commands_list, commands_longlist): |
| 4570 |
stdout, _ = self._run_pdb(["gh93696_host.py"], commands) |
| 4571 |
self.assertIn('x = "Sentinel string for gh-93696"', stdout, "Sentinel statement not found") |
| 4572 |
self.assertIn('4 B', stdout, "breakpoint not found") |
| 4573 |
self.assertIn('-> def func():', stdout, "stack entry not found") |
| 4574 |
|
| 4575 |
def test_empty_file(self): |
| 4576 |
script = '' |
| 4577 |
commands = 'q\n' |
| 4578 |
# We check that pdb stopped at line 0, but anything reasonable |
| 4579 |
# is acceptable here, as long as it does not halt |
| 4580 |
stdout, _ = self.run_pdb_script(script, commands) |
| 4581 |
self.assertIn('main.py(0)', stdout) |
| 4582 |
stdout, _ = self.run_pdb_module(script, commands) |
| 4583 |
self.assertIn('__main__.py(0)', stdout) |
| 4584 |
|
| 4585 |
def test_non_utf8_encoding(self): |
| 4586 |
script_dir = os.path.join(os.path.dirname(__file__), 'encoded_modules') |
| 4587 |
for filename in os.listdir(script_dir): |
| 4588 |
if filename.endswith(".py"): |
| 4589 |
self._run_pdb([os.path.join(script_dir, filename)], 'q') |
| 4590 |
|
| 4591 |
def test_zipapp(self): |
| 4592 |
with os_helper.temp_dir() as temp_dir: |
| 4593 |
os.mkdir(os.path.join(temp_dir, 'source')) |
| 4594 |
script = textwrap.dedent( |
| 4595 |
""" |
| 4596 |
def f(x): |
| 4597 |
return x + 1 |
| 4598 |
f(21 + 21) |
| 4599 |
""" |
| 4600 |
) |
| 4601 |
with open(os.path.join(temp_dir, 'source', '__main__.py'), 'w') as f: |
| 4602 |
f.write(script) |
| 4603 |
zipapp.create_archive(os.path.join(temp_dir, 'source'), |
| 4604 |
os.path.join(temp_dir, 'zipapp.pyz')) |
| 4605 |
stdout, _ = self._run_pdb([os.path.join(temp_dir, 'zipapp.pyz')], '\n'.join([ |
| 4606 |
'b f', |
| 4607 |
'c', |
| 4608 |
'p x', |
| 4609 |
'q' |
| 4610 |
])) |
| 4611 |
self.assertIn('42', stdout) |
| 4612 |
self.assertIn('return x + 1', stdout) |
| 4613 |
|
| 4614 |
def test_zipimport(self): |
| 4615 |
with os_helper.temp_dir() as temp_dir: |
| 4616 |
os.mkdir(os.path.join(temp_dir, 'source')) |
| 4617 |
zipmodule = textwrap.dedent( |
| 4618 |
""" |
| 4619 |
def bar(): |
| 4620 |
pass |
| 4621 |
""" |
| 4622 |
) |
| 4623 |
script = textwrap.dedent( |
| 4624 |
f""" |
| 4625 |
import sys; sys.path.insert(0, {repr(os.path.join(temp_dir, 'zipmodule.zip'))}) |
| 4626 |
import foo |
| 4627 |
foo.bar() |
| 4628 |
""" |
| 4629 |
) |
| 4630 |
|
| 4631 |
with zipfile.ZipFile(os.path.join(temp_dir, 'zipmodule.zip'), 'w') as zf: |
| 4632 |
zf.writestr('foo.py', zipmodule) |
| 4633 |
with open(os.path.join(temp_dir, 'script.py'), 'w') as f: |
| 4634 |
f.write(script) |
| 4635 |
|
| 4636 |
stdout, _ = self._run_pdb([os.path.join(temp_dir, 'script.py')], '\n'.join([ |
| 4637 |
'n', |
| 4638 |
'n', |
| 4639 |
'b foo.bar', |
| 4640 |
'c', |
| 4641 |
'p f"break in {$_frame.f_code.co_name}"', |
| 4642 |
'q' |
| 4643 |
])) |
| 4644 |
self.assertIn('break in bar', stdout) |
| 4645 |
|
| 4646 |
def test_end_of_options_separator(self): |
| 4647 |
# gh-148615: Test parsing when '--' separator is used |
| 4648 |
script = "import sys; print(f'ARGS: {sys.argv[1:]}')" |
| 4649 |
with open(os_helper.TESTFN, 'w', encoding='utf-8') as f: |
| 4650 |
f.write(script) |
| 4651 |
stdout, _ = self._run_pdb(['--', os_helper.TESTFN, '-foo'], 'c\nq') |
| 4652 |
self.assertIn("ARGS: ['-foo']", stdout) |
| 4653 |
stdout, _ = self._run_pdb(['-c', 'continue', '--', os_helper.TESTFN, '-c', 'foo'], 'q') |
| 4654 |
self.assertIn("ARGS: ['-c', 'foo']", stdout) |
| 4655 |
stdout, stderr = self._run_pdb(['--'], 'q', expected_returncode=2) |
| 4656 |
self.assertIn("missing script or module to run", stderr) |
| 4657 |
stdout, stderr = self._run_pdb(['-x', '--', os_helper.TESTFN], 'q', expected_returncode=2) |
| 4658 |
self.assertIn("unrecognized arguments: -x", stderr) |
| 4659 |
stdout, _ = self._run_pdb([os_helper.TESTFN, '--', 'arg'], 'c\nq') |
| 4660 |
self.assertIn("ARGS: ['--', 'arg']", stdout) |
| 4661 |
with os_helper.temp_cwd(): |
| 4662 |
with open('mymod.py', 'w', encoding='utf-8') as f: |
| 4663 |
f.write(script) |
| 4664 |
stdout, _ = self._run_pdb(['-m', 'mymod', '--', 'arg'], 'c\nq') |
| 4665 |
self.assertIn("ARGS: ['--', 'arg']", stdout) |
| 4666 |
|
| 4667 |
def test_issue_59000(self): |
| 4668 |
script = """ |
| 4669 |
def foo(): |
| 4670 |
pass |
| 4671 |
|
| 4672 |
class C: |
| 4673 |
def foo(self): |
| 4674 |
pass |
| 4675 |
""" |
| 4676 |
commands = """ |
| 4677 |
break C.foo |
| 4678 |
quit |
| 4679 |
""" |
| 4680 |
stdout, stderr = self.run_pdb_script(script, commands) |
| 4681 |
self.assertIn("The specified object 'C.foo' is not a function", stdout) |
| 4682 |
|
| 4683 |
|
| 4684 |
class ChecklineTests(unittest.TestCase): |
| 4685 |
def setUp(self): |
| 4686 |
linecache.clearcache() # Pdb.checkline() uses linecache.getline() |
| 4687 |
|
| 4688 |
def tearDown(self): |
| 4689 |
os_helper.unlink(os_helper.TESTFN) |
| 4690 |
|
| 4691 |
def test_checkline_before_debugging(self): |
| 4692 |
with open(os_helper.TESTFN, "w") as f: |
| 4693 |
f.write("print(123)") |
| 4694 |
db = pdb.Pdb() |
| 4695 |
self.assertEqual(db.checkline(os_helper.TESTFN, 1), 1) |
| 4696 |
|
| 4697 |
def test_checkline_after_reset(self): |
| 4698 |
with open(os_helper.TESTFN, "w") as f: |
| 4699 |
f.write("print(123)") |
| 4700 |
db = pdb.Pdb() |
| 4701 |
db.reset() |
| 4702 |
self.assertEqual(db.checkline(os_helper.TESTFN, 1), 1) |
| 4703 |
|
| 4704 |
def test_checkline_is_not_executable(self): |
| 4705 |
# Test for comments, docstrings and empty lines |
| 4706 |
s = textwrap.dedent(""" |
| 4707 |
# Comment |
| 4708 |
\"\"\" docstring \"\"\" |
| 4709 |
''' docstring ''' |
| 4710 |
|
| 4711 |
""") |
| 4712 |
with open(os_helper.TESTFN, "w") as f: |
| 4713 |
f.write(s) |
| 4714 |
num_lines = len(s.splitlines()) + 2 # Test for EOF |
| 4715 |
with redirect_stdout(StringIO()): |
| 4716 |
db = pdb.Pdb() |
| 4717 |
for lineno in range(num_lines): |
| 4718 |
self.assertFalse(db.checkline(os_helper.TESTFN, lineno)) |
| 4719 |
|
| 4720 |
|
| 4721 |
@support.requires_subprocess() |
| 4722 |
class PdbTestInline(unittest.TestCase): |
| 4723 |
@unittest.skipIf(sys.flags.safe_path, |
| 4724 |
'PYTHONSAFEPATH changes default sys.path') |
| 4725 |
def _run_script(self, script, commands, |
| 4726 |
expected_returncode=0, |
| 4727 |
extra_env=None): |
| 4728 |
self.addCleanup(os_helper.rmtree, '__pycache__') |
| 4729 |
filename = 'main.py' |
| 4730 |
with open(filename, 'w') as f: |
| 4731 |
f.write(textwrap.dedent(script)) |
| 4732 |
self.addCleanup(os_helper.unlink, filename) |
| 4733 |
|
| 4734 |
commands = textwrap.dedent(commands) |
| 4735 |
|
| 4736 |
cmd = [sys.executable, 'main.py'] |
| 4737 |
if extra_env is not None: |
| 4738 |
env = os.environ | extra_env |
| 4739 |
else: |
| 4740 |
env = os.environ |
| 4741 |
with subprocess.Popen( |
| 4742 |
cmd, |
| 4743 |
stdout=subprocess.PIPE, |
| 4744 |
stdin=subprocess.PIPE, |
| 4745 |
stderr=subprocess.PIPE, |
| 4746 |
env = {**env, 'PYTHONIOENCODING': 'utf-8'} |
| 4747 |
) as proc: |
| 4748 |
stdout, stderr = proc.communicate(str.encode(commands)) |
| 4749 |
stdout = bytes.decode(stdout) if isinstance(stdout, bytes) else stdout |
| 4750 |
stderr = bytes.decode(stderr) if isinstance(stderr, bytes) else stderr |
| 4751 |
self.assertEqual( |
| 4752 |
proc.returncode, |
| 4753 |
expected_returncode, |
| 4754 |
f"Unexpected return code\nstdout: {stdout}\nstderr: {stderr}" |
| 4755 |
) |
| 4756 |
return stdout, stderr |
| 4757 |
|
| 4758 |
def test_quit(self): |
| 4759 |
script = """ |
| 4760 |
x = 1 |
| 4761 |
breakpoint() |
| 4762 |
""" |
| 4763 |
|
| 4764 |
commands = """ |
| 4765 |
quit |
| 4766 |
n |
| 4767 |
p x + 1 |
| 4768 |
quit |
| 4769 |
y |
| 4770 |
""" |
| 4771 |
|
| 4772 |
stdout, stderr = self._run_script(script, commands, expected_returncode=1) |
| 4773 |
self.assertIn("2", stdout) |
| 4774 |
self.assertIn("Quit anyway", stdout) |
| 4775 |
# Closing stdin will quit the debugger anyway so we need to confirm |
| 4776 |
# it's the quit command that does the job |
| 4777 |
# call/return event will print --Call-- and --Return-- |
| 4778 |
self.assertNotIn("--", stdout) |
| 4779 |
# Normal exit should not print anything to stderr |
| 4780 |
self.assertEqual(stderr, "") |
| 4781 |
# The quit prompt should be printed exactly twice |
| 4782 |
self.assertEqual(stdout.count("Quit anyway"), 2) |
| 4783 |
|
| 4784 |
def test_quit_after_interact(self): |
| 4785 |
""" |
| 4786 |
interact command will set sys.ps1 temporarily, we need to make sure |
| 4787 |
that it's restored and pdb does not believe it's in interactive mode |
| 4788 |
after interact is done. |
| 4789 |
""" |
| 4790 |
script = """ |
| 4791 |
x = 1 |
| 4792 |
breakpoint() |
| 4793 |
""" |
| 4794 |
|
| 4795 |
commands = """ |
| 4796 |
interact |
| 4797 |
quit() |
| 4798 |
q |
| 4799 |
y |
| 4800 |
""" |
| 4801 |
|
| 4802 |
stdout, stderr = self._run_script(script, commands, expected_returncode=1) |
| 4803 |
# Normal exit should not print anything to stderr |
| 4804 |
self.assertEqual(stderr, "") |
| 4805 |
# The quit prompt should be printed exactly once |
| 4806 |
self.assertEqual(stdout.count("Quit anyway"), 1) |
| 4807 |
# BdbQuit should not be printed |
| 4808 |
self.assertNotIn("BdbQuit", stdout) |
| 4809 |
|
| 4810 |
def test_set_trace_with_skip(self): |
| 4811 |
"""GH-82897 |
| 4812 |
Inline set_trace() should break unconditionally. This example is a |
| 4813 |
bit oversimplified, but as `pdb.set_trace()` uses the previous Pdb |
| 4814 |
instance, it's possible that we had a previous pdb instance with |
| 4815 |
skip values when we use `pdb.set_trace()` - it would be confusing |
| 4816 |
to users when such inline breakpoints won't break immediately. |
| 4817 |
""" |
| 4818 |
script = textwrap.dedent(""" |
| 4819 |
import pdb |
| 4820 |
def foo(): |
| 4821 |
x = 40 + 2 |
| 4822 |
pdb.Pdb(skip=['__main__']).set_trace() |
| 4823 |
foo() |
| 4824 |
""") |
| 4825 |
commands = """ |
| 4826 |
p x |
| 4827 |
c |
| 4828 |
""" |
| 4829 |
stdout, _ = self._run_script(script, commands) |
| 4830 |
self.assertIn("42", stdout) |
| 4831 |
|
| 4832 |
def test_readline_not_imported(self): |
| 4833 |
"""GH-138860 |
| 4834 |
Directly or indirectly importing readline might deadlock a subprocess |
| 4835 |
if it's launched with process_group=0 or preexec_fn=setpgrp |
| 4836 |
|
| 4837 |
It's also a pattern that readline is never imported with just import pdb. |
| 4838 |
|
| 4839 |
This test is to ensure that readline is not imported for import pdb. |
| 4840 |
It's possible that we have a good reason to do that in the future. |
| 4841 |
""" |
| 4842 |
|
| 4843 |
script = textwrap.dedent(""" |
| 4844 |
import sys |
| 4845 |
import pdb |
| 4846 |
if "readline" in sys.modules: |
| 4847 |
print("readline imported") |
| 4848 |
""") |
| 4849 |
commands = "" |
| 4850 |
stdout, stderr = self._run_script(script, commands) |
| 4851 |
self.assertNotIn("readline imported", stdout) |
| 4852 |
self.assertEqual(stderr, "") |
| 4853 |
|
| 4854 |
|
| 4855 |
@support.force_colorized_test_class |
| 4856 |
class PdbTestColorize(unittest.TestCase): |
| 4857 |
def setUp(self): |
| 4858 |
self._original_can_colorize = _colorize.can_colorize |
| 4859 |
# Force colorize to be enabled because we are sending data |
| 4860 |
# to a StringIO |
| 4861 |
_colorize.can_colorize = lambda *args, **kwargs: True |
| 4862 |
|
| 4863 |
def tearDown(self): |
| 4864 |
_colorize.can_colorize = self._original_can_colorize |
| 4865 |
|
| 4866 |
def test_code_display(self): |
| 4867 |
output = io.StringIO() |
| 4868 |
p = pdb.Pdb(stdout=output, colorize=True) |
| 4869 |
p.set_trace(commands=['ll', 'c']) |
| 4870 |
self.assertIn("\x1b", output.getvalue()) |
| 4871 |
|
| 4872 |
output = io.StringIO() |
| 4873 |
p = pdb.Pdb(stdout=output, colorize=False) |
| 4874 |
p.set_trace(commands=['ll', 'c']) |
| 4875 |
self.assertNotIn("\x1b", output.getvalue()) |
| 4876 |
|
| 4877 |
output = io.StringIO() |
| 4878 |
p = pdb.Pdb(stdout=output) |
| 4879 |
p.set_trace(commands=['ll', 'c']) |
| 4880 |
self.assertNotIn("\x1b", output.getvalue()) |
| 4881 |
|
| 4882 |
def test_stack_entry(self): |
| 4883 |
output = io.StringIO() |
| 4884 |
p = pdb.Pdb(stdout=output, colorize=True) |
| 4885 |
p.set_trace(commands=['w', 'c']) |
| 4886 |
self.assertIn("\x1b", output.getvalue()) |
| 4887 |
|
| 4888 |
|
| 4889 |
@support.force_not_colorized_test_class |
| 4890 |
@support.requires_subprocess() |
| 4891 |
class TestREPLSession(unittest.TestCase): |
| 4892 |
def test_return_from_inline_mode_to_REPL(self): |
| 4893 |
# GH-124703: Raise BdbQuit when exiting pdb in REPL session. |
| 4894 |
# This allows the REPL session to continue. |
| 4895 |
from test.test_repl import spawn_repl |
| 4896 |
p = spawn_repl() |
| 4897 |
user_input = """ |
| 4898 |
x = 'Spam' |
| 4899 |
import pdb |
| 4900 |
pdb.set_trace(commands=['x + "During"', 'q']) |
| 4901 |
x + 'After' |
| 4902 |
""" |
| 4903 |
p.stdin.write(textwrap.dedent(user_input)) |
| 4904 |
output = kill_python(p) |
| 4905 |
self.assertIn('SpamDuring', output) |
| 4906 |
self.assertNotIn("Quit anyway", output) |
| 4907 |
self.assertIn('BdbQuit', output) |
| 4908 |
self.assertIn('SpamAfter', output) |
| 4909 |
self.assertEqual(p.returncode, 0) |
| 4910 |
|
| 4911 |
|
| 4912 |
@support.force_not_colorized_test_class |
| 4913 |
@support.requires_subprocess() |
| 4914 |
class PdbTestReadline(unittest.TestCase): |
| 4915 |
|
| 4916 |
@classmethod |
| 4917 |
def setUpClass(cls): |
| 4918 |
# Ensure that the readline module is loaded |
| 4919 |
# If this fails, the test is skipped because SkipTest will be raised |
| 4920 |
readline = import_module('readline') |
| 4921 |
if readline.backend == "editline": |
| 4922 |
raise unittest.SkipTest("libedit readline is not supported for pdb") |
| 4923 |
|
| 4924 |
def test_basic_completion(self): |
| 4925 |
script = textwrap.dedent(""" |
| 4926 |
import pdb; pdb.Pdb().set_trace() |
| 4927 |
# Concatenate strings so that the output doesn't appear in the source |
| 4928 |
print('hello' + '!') |
| 4929 |
""") |
| 4930 |
|
| 4931 |
# List everything starting with 'co', there should be multiple matches |
| 4932 |
# then add ntin and complete 'contin' to 'continue' |
| 4933 |
input = b"co\t\tntin\t\n" |
| 4934 |
|
| 4935 |
output = run_pty(script, input) |
| 4936 |
|
| 4937 |
self.assertIn(b'commands', output) |
| 4938 |
self.assertIn(b'condition', output) |
| 4939 |
self.assertIn(b'continue', output) |
| 4940 |
self.assertIn(b'hello!', output) |
| 4941 |
|
| 4942 |
def test_expression_completion(self): |
| 4943 |
script = textwrap.dedent(""" |
| 4944 |
value = "speci" |
| 4945 |
import pdb; pdb.Pdb().set_trace() |
| 4946 |
""") |
| 4947 |
|
| 4948 |
# Complete: value + 'al' |
| 4949 |
input = b"val\t + 'al'\n" |
| 4950 |
# Complete: p value + 'es' |
| 4951 |
input += b"p val\t + 'es'\n" |
| 4952 |
# Complete: $_frame |
| 4953 |
input += b"$_fra\t\n" |
| 4954 |
# Continue |
| 4955 |
input += b"c\n" |
| 4956 |
|
| 4957 |
output = run_pty(script, input) |
| 4958 |
|
| 4959 |
self.assertIn(b'special', output) |
| 4960 |
self.assertIn(b'species', output) |
| 4961 |
self.assertIn(b'$_frame', output) |
| 4962 |
|
| 4963 |
def test_builtin_completion(self): |
| 4964 |
script = textwrap.dedent(""" |
| 4965 |
value = "speci" |
| 4966 |
import pdb; pdb.Pdb().set_trace() |
| 4967 |
""") |
| 4968 |
|
| 4969 |
# Complete: print(value + 'al') |
| 4970 |
input = b"pri\tval\t + 'al')\n" |
| 4971 |
|
| 4972 |
# Continue |
| 4973 |
input += b"c\n" |
| 4974 |
|
| 4975 |
output = run_pty(script, input) |
| 4976 |
|
| 4977 |
self.assertIn(b'special', output) |
| 4978 |
|
| 4979 |
def test_convvar_completion(self): |
| 4980 |
script = textwrap.dedent(""" |
| 4981 |
import pdb; pdb.Pdb().set_trace() |
| 4982 |
""") |
| 4983 |
|
| 4984 |
# Complete: $_frame |
| 4985 |
input = b"$_fram\t\n" |
| 4986 |
|
| 4987 |
# Complete: $_frame.f_lineno + 100 |
| 4988 |
input += b"$_frame.f_line\t + 100\n" |
| 4989 |
|
| 4990 |
# Continue |
| 4991 |
input += b"c\n" |
| 4992 |
|
| 4993 |
output = run_pty(script, input) |
| 4994 |
|
| 4995 |
self.assertIn(b'<frame at 0x', output) |
| 4996 |
self.assertIn(b'102', output) |
| 4997 |
|
| 4998 |
def test_local_namespace(self): |
| 4999 |
script = textwrap.dedent(""" |
| 5000 |
def f(): |
| 5001 |
original = "I live Pythin" |
| 5002 |
import pdb; pdb.Pdb().set_trace() |
| 5003 |
f() |
| 5004 |
""") |
| 5005 |
|
| 5006 |
# Complete: original.replace('i', 'o') |
| 5007 |
input = b"orig\t.repl\t('i', 'o')\n" |
| 5008 |
|
| 5009 |
# Continue |
| 5010 |
input += b"c\n" |
| 5011 |
|
| 5012 |
output = run_pty(script, input) |
| 5013 |
|
| 5014 |
self.assertIn(b'I love Python', output) |
| 5015 |
|
| 5016 |
@unittest.skipIf(sys.platform.startswith('freebsd'), |
| 5017 |
'\\x08 is not interpreted as backspace on FreeBSD') |
| 5018 |
def test_multiline_auto_indent(self): |
| 5019 |
script = textwrap.dedent(""" |
| 5020 |
import pdb; pdb.Pdb().set_trace() |
| 5021 |
""") |
| 5022 |
|
| 5023 |
input = b"def f(x):\n" |
| 5024 |
input += b"if x > 0:\n" |
| 5025 |
input += b"x += 1\n" |
| 5026 |
input += b"return x\n" |
| 5027 |
# We need to do backspaces to remove the auto-indentation |
| 5028 |
input += b"\x08\x08\x08\x08else:\n" |
| 5029 |
input += b"return -x\n" |
| 5030 |
input += b"\n" |
| 5031 |
input += b"f(-21-21)\n" |
| 5032 |
input += b"c\n" |
| 5033 |
|
| 5034 |
output = run_pty(script, input) |
| 5035 |
|
| 5036 |
self.assertIn(b'42', output) |
| 5037 |
|
| 5038 |
def test_multiline_completion(self): |
| 5039 |
script = textwrap.dedent(""" |
| 5040 |
import pdb; pdb.Pdb().set_trace() |
| 5041 |
""") |
| 5042 |
|
| 5043 |
input = b"def func():\n" |
| 5044 |
# Auto-indent |
| 5045 |
# Complete: return 40 + 2 |
| 5046 |
input += b"ret\t 40 + 2\n" |
| 5047 |
input += b"\n" |
| 5048 |
# Complete: func() |
| 5049 |
input += b"fun\t()\n" |
| 5050 |
input += b"c\n" |
| 5051 |
|
| 5052 |
output = run_pty(script, input) |
| 5053 |
|
| 5054 |
self.assertIn(b'42', output) |
| 5055 |
|
| 5056 |
@unittest.skipIf(sys.platform.startswith('freebsd'), |
| 5057 |
'\\x08 is not interpreted as backspace on FreeBSD') |
| 5058 |
def test_multiline_indent_completion(self): |
| 5059 |
script = textwrap.dedent(""" |
| 5060 |
import pdb; pdb.Pdb().set_trace() |
| 5061 |
""") |
| 5062 |
|
| 5063 |
# \t should always complete a 4-space indent |
| 5064 |
# This piece of code will raise an IndentationError or a SyntaxError |
| 5065 |
# if the completion is not working as expected |
| 5066 |
input = textwrap.dedent("""\ |
| 5067 |
def func(): |
| 5068 |
a = 1 |
| 5069 |
\x08\ta += 1 |
| 5070 |
\x08\x08\ta += 1 |
| 5071 |
\x08\x08\x08\ta += 1 |
| 5072 |
\x08\x08\x08\x08\tif a > 0: |
| 5073 |
a += 1 |
| 5074 |
\x08\x08\x08\x08return a |
| 5075 |
|
| 5076 |
func() |
| 5077 |
c |
| 5078 |
""").encode() |
| 5079 |
|
| 5080 |
output = run_pty(script, input) |
| 5081 |
|
| 5082 |
self.assertIn(b'5', output) |
| 5083 |
self.assertNotIn(b'Error', output) |
| 5084 |
|
| 5085 |
def test_interact_completion(self): |
| 5086 |
script = textwrap.dedent(""" |
| 5087 |
value = "speci" |
| 5088 |
import pdb; pdb.Pdb().set_trace() |
| 5089 |
""") |
| 5090 |
|
| 5091 |
# Enter interact mode |
| 5092 |
input = b"interact\n" |
| 5093 |
# Should fail to complete 'display' because that's a pdb command |
| 5094 |
input += b"disp\t\n" |
| 5095 |
# 'value' should still work |
| 5096 |
input += b"val\t + 'al'\n" |
| 5097 |
# Let's define a function to test <tab> |
| 5098 |
input += b"def f():\n" |
| 5099 |
input += b"\treturn 42\n" |
| 5100 |
input += b"\n" |
| 5101 |
input += b"f() * 2\n" |
| 5102 |
# Exit interact mode |
| 5103 |
input += b"exit()\n" |
| 5104 |
# continue |
| 5105 |
input += b"c\n" |
| 5106 |
|
| 5107 |
output = run_pty(script, input) |
| 5108 |
|
| 5109 |
self.assertIn(b"'disp' is not defined", output) |
| 5110 |
self.assertIn(b'special', output) |
| 5111 |
self.assertIn(b'84', output) |
| 5112 |
|
| 5113 |
|
| 5114 |
def load_tests(loader, tests, pattern): |
| 5115 |
from test import test_pdb |
| 5116 |
|
| 5117 |
def setUpPdbBackend(backend): |
| 5118 |
def setUp(test): |
| 5119 |
import pdb |
| 5120 |
pdb.set_default_backend(backend) |
| 5121 |
return setUp |
| 5122 |
|
| 5123 |
def tearDown(test): |
| 5124 |
# Ensure that asyncio state has been cleared at the end of the test. |
| 5125 |
# This prevents a "test altered the execution environment" warning if |
| 5126 |
# asyncio features are used. |
| 5127 |
_set_event_loop_policy(None) |
| 5128 |
|
| 5129 |
# A doctest of pdb could have residues. For example, pdb could still |
| 5130 |
# be running, or breakpoints might be left uncleared. These residues |
| 5131 |
# could potentially interfere with the following test, especially |
| 5132 |
# when we switch backends. Here we clear all the residues to restore |
| 5133 |
# to its pre-test state. |
| 5134 |
|
| 5135 |
# clear all the breakpoints left |
| 5136 |
import bdb |
| 5137 |
bdb.Breakpoint.clearBreakpoints() |
| 5138 |
|
| 5139 |
# Stop tracing and clear the pdb instance cache |
| 5140 |
if pdb.Pdb._last_pdb_instance: |
| 5141 |
pdb.Pdb._last_pdb_instance.stop_trace() |
| 5142 |
pdb.Pdb._last_pdb_instance = None |
| 5143 |
|
| 5144 |
# If garbage objects are collected right after we start tracing, we |
| 5145 |
# could stop at __del__ of the object which would fail the test. |
| 5146 |
gc.collect() |
| 5147 |
|
| 5148 |
tests.addTest( |
| 5149 |
doctest.DocTestSuite( |
| 5150 |
test_pdb, |
| 5151 |
setUp=setUpPdbBackend('monitoring'), |
| 5152 |
tearDown=tearDown, |
| 5153 |
) |
| 5154 |
) |
| 5155 |
tests.addTest( |
| 5156 |
doctest.DocTestSuite( |
| 5157 |
test_pdb, |
| 5158 |
setUp=setUpPdbBackend('settrace'), |
| 5159 |
tearDown=tearDown, |
| 5160 |
) |
| 5161 |
) |
| 5162 |
return tests |
| 5163 |
|
| 5164 |
|
| 5165 |
if __name__ == '__main__': |
| 5166 |
unittest.main() |