1# A test suite for pdb; not very comprehensive at the moment. 2 3import doctest 4import os 5import pdb 6import sys 7import types 8import codecs 9import unittest 10import subprocess 11import textwrap 12 13from contextlib import ExitStack 14from io import StringIO 15from test import support 16# This little helper class is essential for testing pdb under doctest. 17from test.test_doctest import _FakeInput 18from unittest.mock import patch 19 20 21class PdbTestInput(object): 22 """Context manager that makes testing Pdb in doctests easier.""" 23 24 def __init__(self, input): 25 self.input = input 26 27 def __enter__(self): 28 self.real_stdin = sys.stdin 29 sys.stdin = _FakeInput(self.input) 30 self.orig_trace = sys.gettrace() if hasattr(sys, 'gettrace') else None 31 32 def __exit__(self, *exc): 33 sys.stdin = self.real_stdin 34 if self.orig_trace: 35 sys.settrace(self.orig_trace) 36 37 38def test_pdb_displayhook(): 39 """This tests the custom displayhook for pdb. 40 41 >>> def test_function(foo, bar): 42 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() 43 ... pass 44 45 >>> with PdbTestInput([ 46 ... 'foo', 47 ... 'bar', 48 ... 'for i in range(5): print(i)', 49 ... 'continue', 50 ... ]): 51 ... test_function(1, None) 52 > <doctest test.test_pdb.test_pdb_displayhook[0]>(3)test_function() 53 -> pass 54 (Pdb) foo 55 1 56 (Pdb) bar 57 (Pdb) for i in range(5): print(i) 58 0 59 1 60 2 61 3 62 4 63 (Pdb) continue 64 """ 65 66 67def test_pdb_basic_commands(): 68 """Test the basic commands of pdb. 69 70 >>> def test_function_2(foo, bar='default'): 71 ... print(foo) 72 ... for i in range(5): 73 ... print(i) 74 ... print(bar) 75 ... for i in range(10): 76 ... never_executed 77 ... print('after for') 78 ... print('...') 79 ... return foo.upper() 80 81 >>> def test_function3(arg=None, *, kwonly=None): 82 ... pass 83 84 >>> def test_function4(a, b, c, /): 85 ... pass 86 87 >>> def test_function(): 88 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() 89 ... ret = test_function_2('baz') 90 ... test_function3(kwonly=True) 91 ... test_function4(1, 2, 3) 92 ... print(ret) 93 94 >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE 95 ... 'step', # entering the function call 96 ... 'args', # display function args 97 ... 'list', # list function source 98 ... 'bt', # display backtrace 99 ... 'up', # step up to test_function() 100 ... 'down', # step down to test_function_2() again 101 ... 'next', # stepping to print(foo) 102 ... 'next', # stepping to the for loop 103 ... 'step', # stepping into the for loop 104 ... 'until', # continuing until out of the for loop 105 ... 'next', # executing the print(bar) 106 ... 'jump 8', # jump over second for loop 107 ... 'return', # return out of function 108 ... 'retval', # display return value 109 ... 'next', # step to test_function3() 110 ... 'step', # stepping into test_function3() 111 ... 'args', # display function args 112 ... 'return', # return out of function 113 ... 'next', # step to test_function4() 114 ... 'step', # stepping to test_function4() 115 ... 'args', # display function args 116 ... 'continue', 117 ... ]): 118 ... test_function() 119 > <doctest test.test_pdb.test_pdb_basic_commands[3]>(3)test_function() 120 -> ret = test_function_2('baz') 121 (Pdb) step 122 --Call-- 123 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2() 124 -> def test_function_2(foo, bar='default'): 125 (Pdb) args 126 foo = 'baz' 127 bar = 'default' 128 (Pdb) list 129 1 -> def test_function_2(foo, bar='default'): 130 2 print(foo) 131 3 for i in range(5): 132 4 print(i) 133 5 print(bar) 134 6 for i in range(10): 135 7 never_executed 136 8 print('after for') 137 9 print('...') 138 10 return foo.upper() 139 [EOF] 140 (Pdb) bt 141 ... 142 <doctest test.test_pdb.test_pdb_basic_commands[4]>(25)<module>() 143 -> test_function() 144 <doctest test.test_pdb.test_pdb_basic_commands[3]>(3)test_function() 145 -> ret = test_function_2('baz') 146 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2() 147 -> def test_function_2(foo, bar='default'): 148 (Pdb) up 149 > <doctest test.test_pdb.test_pdb_basic_commands[3]>(3)test_function() 150 -> ret = test_function_2('baz') 151 (Pdb) down 152 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2() 153 -> def test_function_2(foo, bar='default'): 154 (Pdb) next 155 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(2)test_function_2() 156 -> print(foo) 157 (Pdb) next 158 baz 159 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(3)test_function_2() 160 -> for i in range(5): 161 (Pdb) step 162 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(4)test_function_2() 163 -> print(i) 164 (Pdb) until 165 0 166 1 167 2 168 3 169 4 170 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(5)test_function_2() 171 -> print(bar) 172 (Pdb) next 173 default 174 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(6)test_function_2() 175 -> for i in range(10): 176 (Pdb) jump 8 177 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(8)test_function_2() 178 -> print('after for') 179 (Pdb) return 180 after for 181 ... 182 --Return-- 183 > <doctest test.test_pdb.test_pdb_basic_commands[0]>(10)test_function_2()->'BAZ' 184 -> return foo.upper() 185 (Pdb) retval 186 'BAZ' 187 (Pdb) next 188 > <doctest test.test_pdb.test_pdb_basic_commands[3]>(4)test_function() 189 -> test_function3(kwonly=True) 190 (Pdb) step 191 --Call-- 192 > <doctest test.test_pdb.test_pdb_basic_commands[1]>(1)test_function3() 193 -> def test_function3(arg=None, *, kwonly=None): 194 (Pdb) args 195 arg = None 196 kwonly = True 197 (Pdb) return 198 --Return-- 199 > <doctest test.test_pdb.test_pdb_basic_commands[1]>(2)test_function3()->None 200 -> pass 201 (Pdb) next 202 > <doctest test.test_pdb.test_pdb_basic_commands[3]>(5)test_function() 203 -> test_function4(1, 2, 3) 204 (Pdb) step 205 --Call-- 206 > <doctest test.test_pdb.test_pdb_basic_commands[2]>(1)test_function4() 207 -> def test_function4(a, b, c, /): 208 (Pdb) args 209 a = 1 210 b = 2 211 c = 3 212 (Pdb) continue 213 BAZ 214 """ 215 216 217def test_pdb_breakpoint_commands(): 218 """Test basic commands related to breakpoints. 219 220 >>> def test_function(): 221 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() 222 ... print(1) 223 ... print(2) 224 ... print(3) 225 ... print(4) 226 227 First, need to clear bdb state that might be left over from previous tests. 228 Otherwise, the new breakpoints might get assigned different numbers. 229 230 >>> from bdb import Breakpoint 231 >>> Breakpoint.next = 1 232 >>> Breakpoint.bplist = {} 233 >>> Breakpoint.bpbynumber = [None] 234 235 Now test the breakpoint commands. NORMALIZE_WHITESPACE is needed because 236 the breakpoint list outputs a tab for the "stop only" and "ignore next" 237 lines, which we don't want to put in here. 238 239 >>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE 240 ... 'break 3', 241 ... 'disable 1', 242 ... 'ignore 1 10', 243 ... 'condition 1 1 < 2', 244 ... 'break 4', 245 ... 'break 4', 246 ... 'break', 247 ... 'clear 3', 248 ... 'break', 249 ... 'condition 1', 250 ... 'enable 1', 251 ... 'clear 1', 252 ... 'commands 2', 253 ... 'p "42"', 254 ... 'print("42", 7*6)', # Issue 18764 (not about breakpoints) 255 ... 'end', 256 ... 'continue', # will stop at breakpoint 2 (line 4) 257 ... 'clear', # clear all! 258 ... 'y', 259 ... 'tbreak 5', 260 ... 'continue', # will stop at temporary breakpoint 261 ... 'break', # make sure breakpoint is gone 262 ... 'continue', 263 ... ]): 264 ... test_function() 265 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(3)test_function() 266 -> print(1) 267 (Pdb) break 3 268 Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 269 (Pdb) disable 1 270 Disabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 271 (Pdb) ignore 1 10 272 Will ignore next 10 crossings of breakpoint 1. 273 (Pdb) condition 1 1 < 2 274 New condition set for breakpoint 1. 275 (Pdb) break 4 276 Breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 277 (Pdb) break 4 278 Breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 279 (Pdb) break 280 Num Type Disp Enb Where 281 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 282 stop only if 1 < 2 283 ignore next 10 hits 284 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 285 3 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 286 (Pdb) clear 3 287 Deleted breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 288 (Pdb) break 289 Num Type Disp Enb Where 290 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 291 stop only if 1 < 2 292 ignore next 10 hits 293 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 294 (Pdb) condition 1 295 Breakpoint 1 is now unconditional. 296 (Pdb) enable 1 297 Enabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 298 (Pdb) clear 1 299 Deleted breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 300 (Pdb) commands 2 301 (com) p "42" 302 (com) print("42", 7*6) 303 (com) end 304 (Pdb) continue 305 1 306 '42' 307 42 42 308 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(4)test_function() 309 -> print(2) 310 (Pdb) clear 311 Clear all breaks? y 312 Deleted breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 313 (Pdb) tbreak 5 314 Breakpoint 4 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5 315 (Pdb) continue 316 2 317 Deleted breakpoint 4 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5 318 > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(5)test_function() 319 -> print(3) 320 (Pdb) break 321 (Pdb) continue 322 3 323 4 324 """ 325 326 327def do_nothing(): 328 pass 329 330def do_something(): 331 print(42) 332 333def test_list_commands(): 334 """Test the list and source commands of pdb. 335 336 >>> def test_function_2(foo): 337 ... import test.test_pdb 338 ... test.test_pdb.do_nothing() 339 ... 'some...' 340 ... 'more...' 341 ... 'code...' 342 ... 'to...' 343 ... 'make...' 344 ... 'a...' 345 ... 'long...' 346 ... 'listing...' 347 ... 'useful...' 348 ... '...' 349 ... '...' 350 ... return foo 351 352 >>> def test_function(): 353 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() 354 ... ret = test_function_2('baz') 355 356 >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE 357 ... 'list', # list first function 358 ... 'step', # step into second function 359 ... 'list', # list second function 360 ... 'list', # continue listing to EOF 361 ... 'list 1,3', # list specific lines 362 ... 'list x', # invalid argument 363 ... 'next', # step to import 364 ... 'next', # step over import 365 ... 'step', # step into do_nothing 366 ... 'longlist', # list all lines 367 ... 'source do_something', # list all lines of function 368 ... 'source fooxxx', # something that doesn't exit 369 ... 'continue', 370 ... ]): 371 ... test_function() 372 > <doctest test.test_pdb.test_list_commands[1]>(3)test_function() 373 -> ret = test_function_2('baz') 374 (Pdb) list 375 1 def test_function(): 376 2 import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() 377 3 -> ret = test_function_2('baz') 378 [EOF] 379 (Pdb) step 380 --Call-- 381 > <doctest test.test_pdb.test_list_commands[0]>(1)test_function_2() 382 -> def test_function_2(foo): 383 (Pdb) list 384 1 -> def test_function_2(foo): 385 2 import test.test_pdb 386 3 test.test_pdb.do_nothing() 387 4 'some...' 388 5 'more...' 389 6 'code...' 390 7 'to...' 391 8 'make...' 392 9 'a...' 393 10 'long...' 394 11 'listing...' 395 (Pdb) list 396 12 'useful...' 397 13 '...' 398 14 '...' 399 15 return foo 400 [EOF] 401 (Pdb) list 1,3 402 1 -> def test_function_2(foo): 403 2 import test.test_pdb 404 3 test.test_pdb.do_nothing() 405 (Pdb) list x 406 *** ... 407 (Pdb) next 408 > <doctest test.test_pdb.test_list_commands[0]>(2)test_function_2() 409 -> import test.test_pdb 410 (Pdb) next 411 > <doctest test.test_pdb.test_list_commands[0]>(3)test_function_2() 412 -> test.test_pdb.do_nothing() 413 (Pdb) step 414 --Call-- 415 > ...test_pdb.py(...)do_nothing() 416 -> def do_nothing(): 417 (Pdb) longlist 418 ... -> def do_nothing(): 419 ... pass 420 (Pdb) source do_something 421 ... def do_something(): 422 ... print(42) 423 (Pdb) source fooxxx 424 *** ... 425 (Pdb) continue 426 """ 427 428 429def test_post_mortem(): 430 """Test post mortem traceback debugging. 431 432 >>> def test_function_2(): 433 ... try: 434 ... 1/0 435 ... finally: 436 ... print('Exception!') 437 438 >>> def test_function(): 439 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() 440 ... test_function_2() 441 ... print('Not reached.') 442 443 >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE 444 ... 'next', # step over exception-raising call 445 ... 'bt', # get a backtrace 446 ... 'list', # list code of test_function() 447 ... 'down', # step into test_function_2() 448 ... 'list', # list code of test_function_2() 449 ... 'continue', 450 ... ]): 451 ... try: 452 ... test_function() 453 ... except ZeroDivisionError: 454 ... print('Correctly reraised.') 455 > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function() 456 -> test_function_2() 457 (Pdb) next 458 Exception! 459 ZeroDivisionError: division by zero 460 > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function() 461 -> test_function_2() 462 (Pdb) bt 463 ... 464 <doctest test.test_pdb.test_post_mortem[2]>(10)<module>() 465 -> test_function() 466 > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function() 467 -> test_function_2() 468 <doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2() 469 -> 1/0 470 (Pdb) list 471 1 def test_function(): 472 2 import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() 473 3 -> test_function_2() 474 4 print('Not reached.') 475 [EOF] 476 (Pdb) down 477 > <doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2() 478 -> 1/0 479 (Pdb) list 480 1 def test_function_2(): 481 2 try: 482 3 >> 1/0 483 4 finally: 484 5 -> print('Exception!') 485 [EOF] 486 (Pdb) continue 487 Correctly reraised. 488 """ 489 490 491def test_pdb_skip_modules(): 492 """This illustrates the simple case of module skipping. 493 494 >>> def skip_module(): 495 ... import string 496 ... import pdb; pdb.Pdb(skip=['stri*'], nosigint=True, readrc=False).set_trace() 497 ... string.capwords('FOO') 498 499 >>> with PdbTestInput([ 500 ... 'step', 501 ... 'continue', 502 ... ]): 503 ... skip_module() 504 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module() 505 -> string.capwords('FOO') 506 (Pdb) step 507 --Return-- 508 > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None 509 -> string.capwords('FOO') 510 (Pdb) continue 511 """ 512 513 514# Module for testing skipping of module that makes a callback 515mod = types.ModuleType('module_to_skip') 516exec('def foo_pony(callback): x = 1; callback(); return None', mod.__dict__) 517 518 519def test_pdb_skip_modules_with_callback(): 520 """This illustrates skipping of modules that call into other code. 521 522 >>> def skip_module(): 523 ... def callback(): 524 ... return None 525 ... import pdb; pdb.Pdb(skip=['module_to_skip*'], nosigint=True, readrc=False).set_trace() 526 ... mod.foo_pony(callback) 527 528 >>> with PdbTestInput([ 529 ... 'step', 530 ... 'step', 531 ... 'step', 532 ... 'step', 533 ... 'step', 534 ... 'continue', 535 ... ]): 536 ... skip_module() 537 ... pass # provides something to "step" to 538 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module() 539 -> mod.foo_pony(callback) 540 (Pdb) step 541 --Call-- 542 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback() 543 -> def callback(): 544 (Pdb) step 545 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback() 546 -> return None 547 (Pdb) step 548 --Return-- 549 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None 550 -> return None 551 (Pdb) step 552 --Return-- 553 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None 554 -> mod.foo_pony(callback) 555 (Pdb) step 556 > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>() 557 -> pass # provides something to "step" to 558 (Pdb) continue 559 """ 560 561 562def test_pdb_continue_in_bottomframe(): 563 """Test that "continue" and "next" work properly in bottom frame (issue #5294). 564 565 >>> def test_function(): 566 ... import pdb, sys; inst = pdb.Pdb(nosigint=True, readrc=False) 567 ... inst.set_trace() 568 ... inst.botframe = sys._getframe() # hackery to get the right botframe 569 ... print(1) 570 ... print(2) 571 ... print(3) 572 ... print(4) 573 574 >>> with PdbTestInput([ # doctest: +ELLIPSIS 575 ... 'next', 576 ... 'break 7', 577 ... 'continue', 578 ... 'next', 579 ... 'continue', 580 ... 'continue', 581 ... ]): 582 ... test_function() 583 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(4)test_function() 584 -> inst.botframe = sys._getframe() # hackery to get the right botframe 585 (Pdb) next 586 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(5)test_function() 587 -> print(1) 588 (Pdb) break 7 589 Breakpoint ... at <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>:7 590 (Pdb) continue 591 1 592 2 593 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(7)test_function() 594 -> print(3) 595 (Pdb) next 596 3 597 > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(8)test_function() 598 -> print(4) 599 (Pdb) continue 600 4 601 """ 602 603 604def pdb_invoke(method, arg): 605 """Run pdb.method(arg).""" 606 getattr(pdb.Pdb(nosigint=True, readrc=False), method)(arg) 607 608 609def test_pdb_run_with_incorrect_argument(): 610 """Testing run and runeval with incorrect first argument. 611 612 >>> pti = PdbTestInput(['continue',]) 613 >>> with pti: 614 ... pdb_invoke('run', lambda x: x) 615 Traceback (most recent call last): 616 TypeError: exec() arg 1 must be a string, bytes or code object 617 618 >>> with pti: 619 ... pdb_invoke('runeval', lambda x: x) 620 Traceback (most recent call last): 621 TypeError: eval() arg 1 must be a string, bytes or code object 622 """ 623 624 625def test_pdb_run_with_code_object(): 626 """Testing run and runeval with code object as a first argument. 627 628 >>> with PdbTestInput(['step','x', 'continue']): # doctest: +ELLIPSIS 629 ... pdb_invoke('run', compile('x=1', '<string>', 'exec')) 630 > <string>(1)<module>()... 631 (Pdb) step 632 --Return-- 633 > <string>(1)<module>()->None 634 (Pdb) x 635 1 636 (Pdb) continue 637 638 >>> with PdbTestInput(['x', 'continue']): 639 ... x=0 640 ... pdb_invoke('runeval', compile('x+1', '<string>', 'eval')) 641 > <string>(1)<module>()->None 642 (Pdb) x 643 1 644 (Pdb) continue 645 """ 646 647def test_next_until_return_at_return_event(): 648 """Test that pdb stops after a next/until/return issued at a return debug event. 649 650 >>> def test_function_2(): 651 ... x = 1 652 ... x = 2 653 654 >>> def test_function(): 655 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() 656 ... test_function_2() 657 ... test_function_2() 658 ... test_function_2() 659 ... end = 1 660 661 >>> from bdb import Breakpoint 662 >>> Breakpoint.next = 1 663 >>> with PdbTestInput(['break test_function_2', 664 ... 'continue', 665 ... 'return', 666 ... 'next', 667 ... 'continue', 668 ... 'return', 669 ... 'until', 670 ... 'continue', 671 ... 'return', 672 ... 'return', 673 ... 'continue']): 674 ... test_function() 675 > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(3)test_function() 676 -> test_function_2() 677 (Pdb) break test_function_2 678 Breakpoint 1 at <doctest test.test_pdb.test_next_until_return_at_return_event[0]>:1 679 (Pdb) continue 680 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2() 681 -> x = 1 682 (Pdb) return 683 --Return-- 684 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(3)test_function_2()->None 685 -> x = 2 686 (Pdb) next 687 > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(4)test_function() 688 -> test_function_2() 689 (Pdb) continue 690 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2() 691 -> x = 1 692 (Pdb) return 693 --Return-- 694 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(3)test_function_2()->None 695 -> x = 2 696 (Pdb) until 697 > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(5)test_function() 698 -> test_function_2() 699 (Pdb) continue 700 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2() 701 -> x = 1 702 (Pdb) return 703 --Return-- 704 > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(3)test_function_2()->None 705 -> x = 2 706 (Pdb) return 707 > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(6)test_function() 708 -> end = 1 709 (Pdb) continue 710 """ 711 712def test_pdb_next_command_for_generator(): 713 """Testing skip unwindng stack on yield for generators for "next" command 714 715 >>> def test_gen(): 716 ... yield 0 717 ... return 1 718 ... yield 2 719 720 >>> def test_function(): 721 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() 722 ... it = test_gen() 723 ... try: 724 ... if next(it) != 0: 725 ... raise AssertionError 726 ... next(it) 727 ... except StopIteration as ex: 728 ... if ex.value != 1: 729 ... raise AssertionError 730 ... print("finished") 731 732 >>> with PdbTestInput(['step', 733 ... 'step', 734 ... 'step', 735 ... 'next', 736 ... 'next', 737 ... 'step', 738 ... 'step', 739 ... 'continue']): 740 ... test_function() 741 > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(3)test_function() 742 -> it = test_gen() 743 (Pdb) step 744 > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(4)test_function() 745 -> try: 746 (Pdb) step 747 > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(5)test_function() 748 -> if next(it) != 0: 749 (Pdb) step 750 --Call-- 751 > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(1)test_gen() 752 -> def test_gen(): 753 (Pdb) next 754 > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(2)test_gen() 755 -> yield 0 756 (Pdb) next 757 > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(3)test_gen() 758 -> return 1 759 (Pdb) step 760 --Return-- 761 > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(3)test_gen()->1 762 -> return 1 763 (Pdb) step 764 StopIteration: 1 765 > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(7)test_function() 766 -> next(it) 767 (Pdb) continue 768 finished 769 """ 770 771def test_pdb_next_command_for_coroutine(): 772 """Testing skip unwindng stack on yield for coroutines for "next" command 773 774 >>> import asyncio 775 776 >>> async def test_coro(): 777 ... await asyncio.sleep(0) 778 ... await asyncio.sleep(0) 779 ... await asyncio.sleep(0) 780 781 >>> async def test_main(): 782 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() 783 ... await test_coro() 784 785 >>> def test_function(): 786 ... loop = asyncio.new_event_loop() 787 ... loop.run_until_complete(test_main()) 788 ... loop.close() 789 ... asyncio.set_event_loop_policy(None) 790 ... print("finished") 791 792 >>> with PdbTestInput(['step', 793 ... 'step', 794 ... 'next', 795 ... 'next', 796 ... 'next', 797 ... 'step', 798 ... 'continue']): 799 ... test_function() 800 > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[2]>(3)test_main() 801 -> await test_coro() 802 (Pdb) step 803 --Call-- 804 > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(1)test_coro() 805 -> async def test_coro(): 806 (Pdb) step 807 > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(2)test_coro() 808 -> await asyncio.sleep(0) 809 (Pdb) next 810 > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(3)test_coro() 811 -> await asyncio.sleep(0) 812 (Pdb) next 813 > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(4)test_coro() 814 -> await asyncio.sleep(0) 815 (Pdb) next 816 Internal StopIteration 817 > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[2]>(3)test_main() 818 -> await test_coro() 819 (Pdb) step 820 --Return-- 821 > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[2]>(3)test_main()->None 822 -> await test_coro() 823 (Pdb) continue 824 finished 825 """ 826 827def test_pdb_next_command_for_asyncgen(): 828 """Testing skip unwindng stack on yield for coroutines for "next" command 829 830 >>> import asyncio 831 832 >>> async def agen(): 833 ... yield 1 834 ... await asyncio.sleep(0) 835 ... yield 2 836 837 >>> async def test_coro(): 838 ... async for x in agen(): 839 ... print(x) 840 841 >>> async def test_main(): 842 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() 843 ... await test_coro() 844 845 >>> def test_function(): 846 ... loop = asyncio.new_event_loop() 847 ... loop.run_until_complete(test_main()) 848 ... loop.close() 849 ... asyncio.set_event_loop_policy(None) 850 ... print("finished") 851 852 >>> with PdbTestInput(['step', 853 ... 'step', 854 ... 'next', 855 ... 'next', 856 ... 'step', 857 ... 'next', 858 ... 'continue']): 859 ... test_function() 860 > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[3]>(3)test_main() 861 -> await test_coro() 862 (Pdb) step 863 --Call-- 864 > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[2]>(1)test_coro() 865 -> async def test_coro(): 866 (Pdb) step 867 > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[2]>(2)test_coro() 868 -> async for x in agen(): 869 (Pdb) next 870 > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[2]>(3)test_coro() 871 -> print(x) 872 (Pdb) next 873 1 874 > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[2]>(2)test_coro() 875 -> async for x in agen(): 876 (Pdb) step 877 --Call-- 878 > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[1]>(2)agen() 879 -> yield 1 880 (Pdb) next 881 > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[1]>(3)agen() 882 -> await asyncio.sleep(0) 883 (Pdb) continue 884 2 885 finished 886 """ 887 888def test_pdb_return_command_for_generator(): 889 """Testing no unwindng stack on yield for generators 890 for "return" command 891 892 >>> def test_gen(): 893 ... yield 0 894 ... return 1 895 ... yield 2 896 897 >>> def test_function(): 898 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() 899 ... it = test_gen() 900 ... try: 901 ... if next(it) != 0: 902 ... raise AssertionError 903 ... next(it) 904 ... except StopIteration as ex: 905 ... if ex.value != 1: 906 ... raise AssertionError 907 ... print("finished") 908 909 >>> with PdbTestInput(['step', 910 ... 'step', 911 ... 'step', 912 ... 'return', 913 ... 'step', 914 ... 'step', 915 ... 'continue']): 916 ... test_function() 917 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(3)test_function() 918 -> it = test_gen() 919 (Pdb) step 920 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(4)test_function() 921 -> try: 922 (Pdb) step 923 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(5)test_function() 924 -> if next(it) != 0: 925 (Pdb) step 926 --Call-- 927 > <doctest test.test_pdb.test_pdb_return_command_for_generator[0]>(1)test_gen() 928 -> def test_gen(): 929 (Pdb) return 930 StopIteration: 1 931 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(7)test_function() 932 -> next(it) 933 (Pdb) step 934 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(8)test_function() 935 -> except StopIteration as ex: 936 (Pdb) step 937 > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(9)test_function() 938 -> if ex.value != 1: 939 (Pdb) continue 940 finished 941 """ 942 943def test_pdb_return_command_for_coroutine(): 944 """Testing no unwindng stack on yield for coroutines for "return" command 945 946 >>> import asyncio 947 948 >>> async def test_coro(): 949 ... await asyncio.sleep(0) 950 ... await asyncio.sleep(0) 951 ... await asyncio.sleep(0) 952 953 >>> async def test_main(): 954 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() 955 ... await test_coro() 956 957 >>> def test_function(): 958 ... loop = asyncio.new_event_loop() 959 ... loop.run_until_complete(test_main()) 960 ... loop.close() 961 ... asyncio.set_event_loop_policy(None) 962 ... print("finished") 963 964 >>> with PdbTestInput(['step', 965 ... 'step', 966 ... 'next', 967 ... 'continue']): 968 ... test_function() 969 > <doctest test.test_pdb.test_pdb_return_command_for_coroutine[2]>(3)test_main() 970 -> await test_coro() 971 (Pdb) step 972 --Call-- 973 > <doctest test.test_pdb.test_pdb_return_command_for_coroutine[1]>(1)test_coro() 974 -> async def test_coro(): 975 (Pdb) step 976 > <doctest test.test_pdb.test_pdb_return_command_for_coroutine[1]>(2)test_coro() 977 -> await asyncio.sleep(0) 978 (Pdb) next 979 > <doctest test.test_pdb.test_pdb_return_command_for_coroutine[1]>(3)test_coro() 980 -> await asyncio.sleep(0) 981 (Pdb) continue 982 finished 983 """ 984 985def test_pdb_until_command_for_generator(): 986 """Testing no unwindng stack on yield for generators 987 for "until" command if target breakpoing is not reached 988 989 >>> def test_gen(): 990 ... yield 0 991 ... yield 1 992 ... yield 2 993 994 >>> def test_function(): 995 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() 996 ... for i in test_gen(): 997 ... print(i) 998 ... print("finished") 999 1000 >>> with PdbTestInput(['step', 1001 ... 'until 4', 1002 ... 'step', 1003 ... 'step', 1004 ... 'continue']): 1005 ... test_function() 1006 > <doctest test.test_pdb.test_pdb_until_command_for_generator[1]>(3)test_function() 1007 -> for i in test_gen(): 1008 (Pdb) step 1009 --Call-- 1010 > <doctest test.test_pdb.test_pdb_until_command_for_generator[0]>(1)test_gen() 1011 -> def test_gen(): 1012 (Pdb) until 4 1013 0 1014 1 1015 > <doctest test.test_pdb.test_pdb_until_command_for_generator[0]>(4)test_gen() 1016 -> yield 2 1017 (Pdb) step 1018 --Return-- 1019 > <doctest test.test_pdb.test_pdb_until_command_for_generator[0]>(4)test_gen()->2 1020 -> yield 2 1021 (Pdb) step 1022 > <doctest test.test_pdb.test_pdb_until_command_for_generator[1]>(4)test_function() 1023 -> print(i) 1024 (Pdb) continue 1025 2 1026 finished 1027 """ 1028 1029def test_pdb_until_command_for_coroutine(): 1030 """Testing no unwindng stack for coroutines 1031 for "until" command if target breakpoing is not reached 1032 1033 >>> import asyncio 1034 1035 >>> async def test_coro(): 1036 ... print(0) 1037 ... await asyncio.sleep(0) 1038 ... print(1) 1039 ... await asyncio.sleep(0) 1040 ... print(2) 1041 ... await asyncio.sleep(0) 1042 ... print(3) 1043 1044 >>> async def test_main(): 1045 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() 1046 ... await test_coro() 1047 1048 >>> def test_function(): 1049 ... loop = asyncio.new_event_loop() 1050 ... loop.run_until_complete(test_main()) 1051 ... loop.close() 1052 ... asyncio.set_event_loop_policy(None) 1053 ... print("finished") 1054 1055 >>> with PdbTestInput(['step', 1056 ... 'until 8', 1057 ... 'continue']): 1058 ... test_function() 1059 > <doctest test.test_pdb.test_pdb_until_command_for_coroutine[2]>(3)test_main() 1060 -> await test_coro() 1061 (Pdb) step 1062 --Call-- 1063 > <doctest test.test_pdb.test_pdb_until_command_for_coroutine[1]>(1)test_coro() 1064 -> async def test_coro(): 1065 (Pdb) until 8 1066 0 1067 1 1068 2 1069 > <doctest test.test_pdb.test_pdb_until_command_for_coroutine[1]>(8)test_coro() 1070 -> print(3) 1071 (Pdb) continue 1072 3 1073 finished 1074 """ 1075 1076def test_pdb_next_command_in_generator_for_loop(): 1077 """The next command on returning from a generator controlled by a for loop. 1078 1079 >>> def test_gen(): 1080 ... yield 0 1081 ... return 1 1082 1083 >>> def test_function(): 1084 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() 1085 ... for i in test_gen(): 1086 ... print('value', i) 1087 ... x = 123 1088 1089 >>> with PdbTestInput(['break test_gen', 1090 ... 'continue', 1091 ... 'next', 1092 ... 'next', 1093 ... 'next', 1094 ... 'continue']): 1095 ... test_function() 1096 > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(3)test_function() 1097 -> for i in test_gen(): 1098 (Pdb) break test_gen 1099 Breakpoint 6 at <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>:1 1100 (Pdb) continue 1101 > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>(2)test_gen() 1102 -> yield 0 1103 (Pdb) next 1104 value 0 1105 > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>(3)test_gen() 1106 -> return 1 1107 (Pdb) next 1108 Internal StopIteration: 1 1109 > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(3)test_function() 1110 -> for i in test_gen(): 1111 (Pdb) next 1112 > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(5)test_function() 1113 -> x = 123 1114 (Pdb) continue 1115 """ 1116 1117def test_pdb_next_command_subiterator(): 1118 """The next command in a generator with a subiterator. 1119 1120 >>> def test_subgenerator(): 1121 ... yield 0 1122 ... return 1 1123 1124 >>> def test_gen(): 1125 ... x = yield from test_subgenerator() 1126 ... return x 1127 1128 >>> def test_function(): 1129 ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() 1130 ... for i in test_gen(): 1131 ... print('value', i) 1132 ... x = 123 1133 1134 >>> with PdbTestInput(['step', 1135 ... 'step', 1136 ... 'next', 1137 ... 'next', 1138 ... 'next', 1139 ... 'continue']): 1140 ... test_function() 1141 > <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(3)test_function() 1142 -> for i in test_gen(): 1143 (Pdb) step 1144 --Call-- 1145 > <doctest test.test_pdb.test_pdb_next_command_subiterator[1]>(1)test_gen() 1146 -> def test_gen(): 1147 (Pdb) step 1148 > <doctest test.test_pdb.test_pdb_next_command_subiterator[1]>(2)test_gen() 1149 -> x = yield from test_subgenerator() 1150 (Pdb) next 1151 value 0 1152 > <doctest test.test_pdb.test_pdb_next_command_subiterator[1]>(3)test_gen() 1153 -> return x 1154 (Pdb) next 1155 Internal StopIteration: 1 1156 > <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(3)test_function() 1157 -> for i in test_gen(): 1158 (Pdb) next 1159 > <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(5)test_function() 1160 -> x = 123 1161 (Pdb) continue 1162 """ 1163 1164def test_pdb_issue_20766(): 1165 """Test for reference leaks when the SIGINT handler is set. 1166 1167 >>> def test_function(): 1168 ... i = 1 1169 ... while i <= 2: 1170 ... sess = pdb.Pdb() 1171 ... sess.set_trace(sys._getframe()) 1172 ... print('pdb %d: %s' % (i, sess._previous_sigint_handler)) 1173 ... i += 1 1174 1175 >>> with PdbTestInput(['continue', 1176 ... 'continue']): 1177 ... test_function() 1178 > <doctest test.test_pdb.test_pdb_issue_20766[0]>(6)test_function() 1179 -> print('pdb %d: %s' % (i, sess._previous_sigint_handler)) 1180 (Pdb) continue 1181 pdb 1: <built-in function default_int_handler> 1182 > <doctest test.test_pdb.test_pdb_issue_20766[0]>(5)test_function() 1183 -> sess.set_trace(sys._getframe()) 1184 (Pdb) continue 1185 pdb 2: <built-in function default_int_handler> 1186 """ 1187 1188 1189class PdbTestCase(unittest.TestCase): 1190 def tearDown(self): 1191 support.unlink(support.TESTFN) 1192 1193 def _run_pdb(self, pdb_args, commands): 1194 self.addCleanup(support.rmtree, '__pycache__') 1195 cmd = [sys.executable, '-m', 'pdb'] + pdb_args 1196 with subprocess.Popen( 1197 cmd, 1198 stdout=subprocess.PIPE, 1199 stdin=subprocess.PIPE, 1200 stderr=subprocess.STDOUT, 1201 ) as proc: 1202 stdout, stderr = proc.communicate(str.encode(commands)) 1203 stdout = stdout and bytes.decode(stdout) 1204 stderr = stderr and bytes.decode(stderr) 1205 return stdout, stderr 1206 1207 def run_pdb_script(self, script, commands): 1208 """Run 'script' lines with pdb and the pdb 'commands'.""" 1209 filename = 'main.py' 1210 with open(filename, 'w') as f: 1211 f.write(textwrap.dedent(script)) 1212 self.addCleanup(support.unlink, filename) 1213 return self._run_pdb([filename], commands) 1214 1215 def run_pdb_module(self, script, commands): 1216 """Runs the script code as part of a module""" 1217 self.module_name = 't_main' 1218 support.rmtree(self.module_name) 1219 main_file = self.module_name + '/__main__.py' 1220 init_file = self.module_name + '/__init__.py' 1221 os.mkdir(self.module_name) 1222 with open(init_file, 'w') as f: 1223 pass 1224 with open(main_file, 'w') as f: 1225 f.write(textwrap.dedent(script)) 1226 self.addCleanup(support.rmtree, self.module_name) 1227 return self._run_pdb(['-m', self.module_name], commands) 1228 1229 def _assert_find_function(self, file_content, func_name, expected): 1230 with open(support.TESTFN, 'wb') as f: 1231 f.write(file_content) 1232 1233 expected = None if not expected else ( 1234 expected[0], support.TESTFN, expected[1]) 1235 self.assertEqual( 1236 expected, pdb.find_function(func_name, support.TESTFN)) 1237 1238 def test_find_function_empty_file(self): 1239 self._assert_find_function(b'', 'foo', None) 1240 1241 def test_find_function_found(self): 1242 self._assert_find_function( 1243 """\ 1244def foo(): 1245 pass 1246 1247def bœr(): 1248 pass 1249 1250def quux(): 1251 pass 1252""".encode(), 1253 'bœr', 1254 ('bœr', 4), 1255 ) 1256 1257 def test_find_function_found_with_encoding_cookie(self): 1258 self._assert_find_function( 1259 """\ 1260# coding: iso-8859-15 1261def foo(): 1262 pass 1263 1264def bœr(): 1265 pass 1266 1267def quux(): 1268 pass 1269""".encode('iso-8859-15'), 1270 'bœr', 1271 ('bœr', 5), 1272 ) 1273 1274 def test_find_function_found_with_bom(self): 1275 self._assert_find_function( 1276 codecs.BOM_UTF8 + """\ 1277def bœr(): 1278 pass 1279""".encode(), 1280 'bœr', 1281 ('bœr', 1), 1282 ) 1283 1284 def test_issue7964(self): 1285 # open the file as binary so we can force \r\n newline 1286 with open(support.TESTFN, 'wb') as f: 1287 f.write(b'print("testing my pdb")\r\n') 1288 cmd = [sys.executable, '-m', 'pdb', support.TESTFN] 1289 proc = subprocess.Popen(cmd, 1290 stdout=subprocess.PIPE, 1291 stdin=subprocess.PIPE, 1292 stderr=subprocess.STDOUT, 1293 ) 1294 self.addCleanup(proc.stdout.close) 1295 stdout, stderr = proc.communicate(b'quit\n') 1296 self.assertNotIn(b'SyntaxError', stdout, 1297 "Got a syntax error running test script under PDB") 1298 1299 def test_issue13183(self): 1300 script = """ 1301 from bar import bar 1302 1303 def foo(): 1304 bar() 1305 1306 def nope(): 1307 pass 1308 1309 def foobar(): 1310 foo() 1311 nope() 1312 1313 foobar() 1314 """ 1315 commands = """ 1316 from bar import bar 1317 break bar 1318 continue 1319 step 1320 step 1321 quit 1322 """ 1323 bar = """ 1324 def bar(): 1325 pass 1326 """ 1327 with open('bar.py', 'w') as f: 1328 f.write(textwrap.dedent(bar)) 1329 self.addCleanup(support.unlink, 'bar.py') 1330 stdout, stderr = self.run_pdb_script(script, commands) 1331 self.assertTrue( 1332 any('main.py(5)foo()->None' in l for l in stdout.splitlines()), 1333 'Fail to step into the caller after a return') 1334 1335 def test_issue13120(self): 1336 # Invoking "continue" on a non-main thread triggered an exception 1337 # inside signal.signal. 1338 1339 with open(support.TESTFN, 'wb') as f: 1340 f.write(textwrap.dedent(""" 1341 import threading 1342 import pdb 1343 1344 def start_pdb(): 1345 pdb.Pdb(readrc=False).set_trace() 1346 x = 1 1347 y = 1 1348 1349 t = threading.Thread(target=start_pdb) 1350 t.start()""").encode('ascii')) 1351 cmd = [sys.executable, '-u', support.TESTFN] 1352 proc = subprocess.Popen(cmd, 1353 stdout=subprocess.PIPE, 1354 stdin=subprocess.PIPE, 1355 stderr=subprocess.STDOUT, 1356 ) 1357 self.addCleanup(proc.stdout.close) 1358 stdout, stderr = proc.communicate(b'cont\n') 1359 self.assertNotIn('Error', stdout.decode(), 1360 "Got an error running test script under PDB") 1361 1362 def test_issue36250(self): 1363 1364 with open(support.TESTFN, 'wb') as f: 1365 f.write(textwrap.dedent(""" 1366 import threading 1367 import pdb 1368 1369 evt = threading.Event() 1370 1371 def start_pdb(): 1372 evt.wait() 1373 pdb.Pdb(readrc=False).set_trace() 1374 1375 t = threading.Thread(target=start_pdb) 1376 t.start() 1377 pdb.Pdb(readrc=False).set_trace() 1378 evt.set() 1379 t.join()""").encode('ascii')) 1380 cmd = [sys.executable, '-u', support.TESTFN] 1381 proc = subprocess.Popen(cmd, 1382 stdout=subprocess.PIPE, 1383 stdin=subprocess.PIPE, 1384 stderr=subprocess.STDOUT, 1385 ) 1386 self.addCleanup(proc.stdout.close) 1387 stdout, stderr = proc.communicate(b'cont\ncont\n') 1388 self.assertNotIn('Error', stdout.decode(), 1389 "Got an error running test script under PDB") 1390 1391 def test_issue16180(self): 1392 # A syntax error in the debuggee. 1393 script = "def f: pass\n" 1394 commands = '' 1395 expected = "SyntaxError:" 1396 stdout, stderr = self.run_pdb_script(script, commands) 1397 self.assertIn(expected, stdout, 1398 '\n\nExpected:\n{}\nGot:\n{}\n' 1399 'Fail to handle a syntax error in the debuggee.' 1400 .format(expected, stdout)) 1401 1402 1403 def test_readrc_kwarg(self): 1404 script = textwrap.dedent(""" 1405 import pdb; pdb.Pdb(readrc=False).set_trace() 1406 1407 print('hello') 1408 """) 1409 1410 save_home = os.environ.pop('HOME', None) 1411 try: 1412 with support.temp_cwd(): 1413 with open('.pdbrc', 'w') as f: 1414 f.write("invalid\n") 1415 1416 with open('main.py', 'w') as f: 1417 f.write(script) 1418 1419 cmd = [sys.executable, 'main.py'] 1420 proc = subprocess.Popen( 1421 cmd, 1422 stdout=subprocess.PIPE, 1423 stdin=subprocess.PIPE, 1424 stderr=subprocess.PIPE, 1425 ) 1426 with proc: 1427 stdout, stderr = proc.communicate(b'q\n') 1428 self.assertNotIn("NameError: name 'invalid' is not defined", 1429 stdout.decode()) 1430 1431 finally: 1432 if save_home is not None: 1433 os.environ['HOME'] = save_home 1434 1435 def test_readrc_homedir(self): 1436 save_home = os.environ.pop("HOME", None) 1437 with support.temp_dir() as temp_dir, patch("os.path.expanduser"): 1438 rc_path = os.path.join(temp_dir, ".pdbrc") 1439 os.path.expanduser.return_value = rc_path 1440 try: 1441 with open(rc_path, "w") as f: 1442 f.write("invalid") 1443 self.assertEqual(pdb.Pdb().rcLines[0], "invalid") 1444 finally: 1445 if save_home is not None: 1446 os.environ["HOME"] = save_home 1447 1448 def test_header(self): 1449 stdout = StringIO() 1450 header = 'Nobody expects... blah, blah, blah' 1451 with ExitStack() as resources: 1452 resources.enter_context(patch('sys.stdout', stdout)) 1453 resources.enter_context(patch.object(pdb.Pdb, 'set_trace')) 1454 pdb.set_trace(header=header) 1455 self.assertEqual(stdout.getvalue(), header + '\n') 1456 1457 def test_run_module(self): 1458 script = """print("SUCCESS")""" 1459 commands = """ 1460 continue 1461 quit 1462 """ 1463 stdout, stderr = self.run_pdb_module(script, commands) 1464 self.assertTrue(any("SUCCESS" in l for l in stdout.splitlines()), stdout) 1465 1466 def test_module_is_run_as_main(self): 1467 script = """ 1468 if __name__ == '__main__': 1469 print("SUCCESS") 1470 """ 1471 commands = """ 1472 continue 1473 quit 1474 """ 1475 stdout, stderr = self.run_pdb_module(script, commands) 1476 self.assertTrue(any("SUCCESS" in l for l in stdout.splitlines()), stdout) 1477 1478 def test_breakpoint(self): 1479 script = """ 1480 if __name__ == '__main__': 1481 pass 1482 print("SUCCESS") 1483 pass 1484 """ 1485 commands = """ 1486 b 3 1487 quit 1488 """ 1489 stdout, stderr = self.run_pdb_module(script, commands) 1490 self.assertTrue(any("Breakpoint 1 at" in l for l in stdout.splitlines()), stdout) 1491 self.assertTrue(all("SUCCESS" not in l for l in stdout.splitlines()), stdout) 1492 1493 def test_run_pdb_with_pdb(self): 1494 commands = """ 1495 c 1496 quit 1497 """ 1498 stdout, stderr = self._run_pdb(["-m", "pdb"], commands) 1499 self.assertIn( 1500 pdb._usage, 1501 stdout.replace('\r', '') # remove \r for windows 1502 ) 1503 1504 def test_module_without_a_main(self): 1505 module_name = 't_main' 1506 support.rmtree(module_name) 1507 init_file = module_name + '/__init__.py' 1508 os.mkdir(module_name) 1509 with open(init_file, 'w') as f: 1510 pass 1511 self.addCleanup(support.rmtree, module_name) 1512 stdout, stderr = self._run_pdb(['-m', module_name], "") 1513 self.assertIn("ImportError: No module named t_main.__main__", 1514 stdout.splitlines()) 1515 1516 def test_blocks_at_first_code_line(self): 1517 script = """ 1518 #This is a comment, on line 2 1519 1520 print("SUCCESS") 1521 """ 1522 commands = """ 1523 quit 1524 """ 1525 stdout, stderr = self.run_pdb_module(script, commands) 1526 self.assertTrue(any("__main__.py(4)<module>()" 1527 in l for l in stdout.splitlines()), stdout) 1528 1529 def test_relative_imports(self): 1530 self.module_name = 't_main' 1531 support.rmtree(self.module_name) 1532 main_file = self.module_name + '/__main__.py' 1533 init_file = self.module_name + '/__init__.py' 1534 module_file = self.module_name + '/module.py' 1535 self.addCleanup(support.rmtree, self.module_name) 1536 os.mkdir(self.module_name) 1537 with open(init_file, 'w') as f: 1538 f.write(textwrap.dedent(""" 1539 top_var = "VAR from top" 1540 """)) 1541 with open(main_file, 'w') as f: 1542 f.write(textwrap.dedent(""" 1543 from . import top_var 1544 from .module import var 1545 from . import module 1546 pass # We'll stop here and print the vars 1547 """)) 1548 with open(module_file, 'w') as f: 1549 f.write(textwrap.dedent(""" 1550 var = "VAR from module" 1551 var2 = "second var" 1552 """)) 1553 commands = """ 1554 b 5 1555 c 1556 p top_var 1557 p var 1558 p module.var2 1559 quit 1560 """ 1561 stdout, _ = self._run_pdb(['-m', self.module_name], commands) 1562 self.assertTrue(any("VAR from module" in l for l in stdout.splitlines()), stdout) 1563 self.assertTrue(any("VAR from top" in l for l in stdout.splitlines())) 1564 self.assertTrue(any("second var" in l for l in stdout.splitlines())) 1565 1566 def test_relative_imports_on_plain_module(self): 1567 # Validates running a plain module. See bpo32691 1568 self.module_name = 't_main' 1569 support.rmtree(self.module_name) 1570 main_file = self.module_name + '/runme.py' 1571 init_file = self.module_name + '/__init__.py' 1572 module_file = self.module_name + '/module.py' 1573 self.addCleanup(support.rmtree, self.module_name) 1574 os.mkdir(self.module_name) 1575 with open(init_file, 'w') as f: 1576 f.write(textwrap.dedent(""" 1577 top_var = "VAR from top" 1578 """)) 1579 with open(main_file, 'w') as f: 1580 f.write(textwrap.dedent(""" 1581 from . import module 1582 pass # We'll stop here and print the vars 1583 """)) 1584 with open(module_file, 'w') as f: 1585 f.write(textwrap.dedent(""" 1586 var = "VAR from module" 1587 """)) 1588 commands = """ 1589 b 3 1590 c 1591 p module.var 1592 quit 1593 """ 1594 stdout, _ = self._run_pdb(['-m', self.module_name + '.runme'], commands) 1595 self.assertTrue(any("VAR from module" in l for l in stdout.splitlines()), stdout) 1596 1597 def test_errors_in_command(self): 1598 commands = "\n".join([ 1599 'print(', 1600 'debug print(', 1601 'debug doesnotexist', 1602 'c', 1603 ]) 1604 stdout, _ = self.run_pdb_script('', commands + '\n') 1605 1606 self.assertEqual(stdout.splitlines()[1:], [ 1607 '(Pdb) *** SyntaxError: unexpected EOF while parsing', 1608 1609 '(Pdb) ENTERING RECURSIVE DEBUGGER', 1610 '*** SyntaxError: unexpected EOF while parsing', 1611 'LEAVING RECURSIVE DEBUGGER', 1612 1613 '(Pdb) ENTERING RECURSIVE DEBUGGER', 1614 '> <string>(1)<module>()', 1615 "((Pdb)) *** NameError: name 'doesnotexist' is not defined", 1616 'LEAVING RECURSIVE DEBUGGER', 1617 '(Pdb) ', 1618 ]) 1619 1620def load_tests(*args): 1621 from test import test_pdb 1622 suites = [ 1623 unittest.makeSuite(PdbTestCase), 1624 doctest.DocTestSuite(test_pdb) 1625 ] 1626 return unittest.TestSuite(suites) 1627 1628 1629if __name__ == '__main__': 1630 unittest.main() 1631