Georg Brandl | 46b9afc | 2010-07-30 09:14:20 +0000 | [diff] [blame] | 1 | # A test suite for pdb; not very comprehensive at the moment. |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 2 | |
| 3 | import imp |
Georg Brandl | 6cccb86 | 2010-07-30 14:16:43 +0000 | [diff] [blame] | 4 | import pdb |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 5 | import sys |
Georg Brandl | 6cccb86 | 2010-07-30 14:16:43 +0000 | [diff] [blame] | 6 | import unittest |
| 7 | import subprocess |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 8 | |
| 9 | from test import support |
| 10 | # This little helper class is essential for testing pdb under doctest. |
| 11 | from test.test_doctest import _FakeInput |
| 12 | |
| 13 | |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 14 | class PdbTestInput(object): |
| 15 | """Context manager that makes testing Pdb in doctests easier.""" |
| 16 | |
| 17 | def __init__(self, input): |
| 18 | self.input = input |
| 19 | |
| 20 | def __enter__(self): |
| 21 | self.real_stdin = sys.stdin |
| 22 | sys.stdin = _FakeInput(self.input) |
| 23 | |
| 24 | def __exit__(self, *exc): |
| 25 | sys.stdin = self.real_stdin |
| 26 | |
| 27 | |
| 28 | def test_pdb_displayhook(): |
| 29 | """This tests the custom displayhook for pdb. |
| 30 | |
| 31 | >>> def test_function(foo, bar): |
| 32 | ... import pdb; pdb.Pdb().set_trace() |
| 33 | ... pass |
| 34 | |
| 35 | >>> with PdbTestInput([ |
| 36 | ... 'foo', |
| 37 | ... 'bar', |
| 38 | ... 'for i in range(5): print(i)', |
| 39 | ... 'continue', |
| 40 | ... ]): |
| 41 | ... test_function(1, None) |
| 42 | > <doctest test.test_pdb.test_pdb_displayhook[0]>(3)test_function() |
| 43 | -> pass |
| 44 | (Pdb) foo |
| 45 | 1 |
| 46 | (Pdb) bar |
| 47 | (Pdb) for i in range(5): print(i) |
| 48 | 0 |
| 49 | 1 |
| 50 | 2 |
| 51 | 3 |
| 52 | 4 |
| 53 | (Pdb) continue |
| 54 | """ |
| 55 | |
| 56 | |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame^] | 57 | def test_pdb_basic_commands(): |
| 58 | """Test the basic commands of pdb. |
| 59 | |
| 60 | >>> def test_function_2(foo, bar='default'): |
| 61 | ... print(foo) |
| 62 | ... for i in range(5): |
| 63 | ... print(i) |
| 64 | ... print(bar) |
| 65 | ... for i in range(10): |
| 66 | ... never_executed |
| 67 | ... print('after for') |
| 68 | ... print('...') |
| 69 | ... return foo.upper() |
| 70 | |
| 71 | >>> def test_function(): |
| 72 | ... import pdb; pdb.Pdb().set_trace() |
| 73 | ... ret = test_function_2('baz') |
| 74 | ... print(ret) |
| 75 | |
| 76 | >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
| 77 | ... 'step', # entering the function call |
| 78 | ... 'args', # display function args |
| 79 | ... 'list', # list function source |
| 80 | ... 'bt', # display backtrace |
| 81 | ... 'up', # step up to test_function() |
| 82 | ... 'down', # step down to test_function_2() again |
| 83 | ... 'next', # stepping to print(foo) |
| 84 | ... 'next', # stepping to the for loop |
| 85 | ... 'step', # stepping into the for loop |
| 86 | ... 'until', # continuing until out of the for loop |
| 87 | ... 'next', # executing the print(bar) |
| 88 | ... 'jump 8', # jump over second for loop |
| 89 | ... 'return', # return out of function |
| 90 | ... 'retval', # display return value |
| 91 | ... 'continue', |
| 92 | ... ]): |
| 93 | ... test_function() |
| 94 | > <doctest test.test_pdb.test_pdb_basic_commands[1]>(3)test_function() |
| 95 | -> ret = test_function_2('baz') |
| 96 | (Pdb) step |
| 97 | --Call-- |
| 98 | > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2() |
| 99 | -> def test_function_2(foo, bar='default'): |
| 100 | (Pdb) args |
| 101 | foo = 'baz' |
| 102 | bar = 'default' |
| 103 | (Pdb) list |
| 104 | 1 -> def test_function_2(foo, bar='default'): |
| 105 | 2 print(foo) |
| 106 | 3 for i in range(5): |
| 107 | 4 print(i) |
| 108 | 5 print(bar) |
| 109 | 6 for i in range(10): |
| 110 | 7 never_executed |
| 111 | 8 print('after for') |
| 112 | 9 print('...') |
| 113 | 10 return foo.upper() |
| 114 | [EOF] |
| 115 | (Pdb) bt |
| 116 | ... |
| 117 | <doctest test.test_pdb.test_pdb_basic_commands[2]>(18)<module>() |
| 118 | -> test_function() |
| 119 | <doctest test.test_pdb.test_pdb_basic_commands[1]>(3)test_function() |
| 120 | -> ret = test_function_2('baz') |
| 121 | > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2() |
| 122 | -> def test_function_2(foo, bar='default'): |
| 123 | (Pdb) up |
| 124 | > <doctest test.test_pdb.test_pdb_basic_commands[1]>(3)test_function() |
| 125 | -> ret = test_function_2('baz') |
| 126 | (Pdb) down |
| 127 | > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2() |
| 128 | -> def test_function_2(foo, bar='default'): |
| 129 | (Pdb) next |
| 130 | > <doctest test.test_pdb.test_pdb_basic_commands[0]>(2)test_function_2() |
| 131 | -> print(foo) |
| 132 | (Pdb) next |
| 133 | baz |
| 134 | > <doctest test.test_pdb.test_pdb_basic_commands[0]>(3)test_function_2() |
| 135 | -> for i in range(5): |
| 136 | (Pdb) step |
| 137 | > <doctest test.test_pdb.test_pdb_basic_commands[0]>(4)test_function_2() |
| 138 | -> print(i) |
| 139 | (Pdb) until |
| 140 | 0 |
| 141 | 1 |
| 142 | 2 |
| 143 | 3 |
| 144 | 4 |
| 145 | > <doctest test.test_pdb.test_pdb_basic_commands[0]>(5)test_function_2() |
| 146 | -> print(bar) |
| 147 | (Pdb) next |
| 148 | default |
| 149 | > <doctest test.test_pdb.test_pdb_basic_commands[0]>(6)test_function_2() |
| 150 | -> for i in range(10): |
| 151 | (Pdb) jump 8 |
| 152 | > <doctest test.test_pdb.test_pdb_basic_commands[0]>(8)test_function_2() |
| 153 | -> print('after for') |
| 154 | (Pdb) return |
| 155 | after for |
| 156 | ... |
| 157 | --Return-- |
| 158 | > <doctest test.test_pdb.test_pdb_basic_commands[0]>(10)test_function_2()->'BAZ' |
| 159 | -> return foo.upper() |
| 160 | (Pdb) retval |
| 161 | 'BAZ' |
| 162 | (Pdb) continue |
| 163 | BAZ |
| 164 | """ |
| 165 | |
| 166 | |
| 167 | def test_pdb_breakpoint_commands(): |
| 168 | """Test basic commands related to breakpoints. |
| 169 | |
| 170 | >>> def test_function(): |
| 171 | ... import pdb; pdb.Pdb().set_trace() |
| 172 | ... print(1) |
| 173 | ... print(2) |
| 174 | ... print(3) |
| 175 | ... print(4) |
| 176 | |
| 177 | First, need to clear bdb state that might be left over from previous tests. |
| 178 | Otherwise, the new breakpoints might get assigned different numbers. |
| 179 | |
| 180 | >>> from bdb import Breakpoint |
| 181 | >>> Breakpoint.next = 1 |
| 182 | >>> Breakpoint.bplist = {} |
| 183 | >>> Breakpoint.bpbynumber = [None] |
| 184 | |
| 185 | Now test the breakpoint commands. NORMALIZE_WHITESPACE is needed because |
| 186 | the breakpoint list outputs a tab for the "stop only" and "ignore next" |
| 187 | lines, which we don't want to put in here. |
| 188 | |
| 189 | >>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE |
| 190 | ... 'break 3', |
| 191 | ... 'disable 1', |
| 192 | ... 'ignore 1 10', |
| 193 | ... 'condition 1 1 < 2', |
| 194 | ... 'break 4', |
| 195 | ... 'break', |
| 196 | ... 'condition 1', |
| 197 | ... 'enable 1', |
| 198 | ... 'clear 1', |
| 199 | ... 'commands 2', |
| 200 | ... 'print 42', |
| 201 | ... 'end', |
| 202 | ... 'continue', # will stop at breakpoint 2 (line 4) |
| 203 | ... 'clear', # clear all! |
| 204 | ... 'y', |
| 205 | ... 'tbreak 5', |
| 206 | ... 'continue', # will stop at temporary breakpoint |
| 207 | ... 'break', # make sure breakpoint is gone |
| 208 | ... 'continue', |
| 209 | ... ]): |
| 210 | ... test_function() |
| 211 | > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(3)test_function() |
| 212 | -> print(1) |
| 213 | (Pdb) break 3 |
| 214 | Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 |
| 215 | (Pdb) disable 1 |
| 216 | Disabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 |
| 217 | (Pdb) ignore 1 10 |
| 218 | Will ignore next 10 crossings of breakpoint 1. |
| 219 | (Pdb) condition 1 1 < 2 |
| 220 | New condition set for breakpoint 1. |
| 221 | (Pdb) break 4 |
| 222 | Breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 |
| 223 | (Pdb) break |
| 224 | Num Type Disp Enb Where |
| 225 | 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 |
| 226 | stop only if 1 < 2 |
| 227 | ignore next 10 hits |
| 228 | 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 |
| 229 | (Pdb) condition 1 |
| 230 | Breakpoint 1 is now unconditional. |
| 231 | (Pdb) enable 1 |
| 232 | Enabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 |
| 233 | (Pdb) clear 1 |
| 234 | Deleted breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 |
| 235 | (Pdb) commands 2 |
| 236 | (com) print 42 |
| 237 | (com) end |
| 238 | (Pdb) continue |
| 239 | 1 |
| 240 | 42 |
| 241 | > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(4)test_function() |
| 242 | -> print(2) |
| 243 | (Pdb) clear |
| 244 | Clear all breaks? y |
| 245 | Deleted breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 |
| 246 | (Pdb) tbreak 5 |
| 247 | Breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5 |
| 248 | (Pdb) continue |
| 249 | 2 |
| 250 | Deleted breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5 |
| 251 | > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(5)test_function() |
| 252 | -> print(3) |
| 253 | (Pdb) break |
| 254 | (Pdb) continue |
| 255 | 3 |
| 256 | 4 |
| 257 | """ |
| 258 | |
| 259 | |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 260 | def test_pdb_skip_modules(): |
| 261 | """This illustrates the simple case of module skipping. |
| 262 | |
| 263 | >>> def skip_module(): |
| 264 | ... import string |
| 265 | ... import pdb; pdb.Pdb(skip=['stri*']).set_trace() |
| 266 | ... string.capwords('FOO') |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 267 | |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 268 | >>> with PdbTestInput([ |
| 269 | ... 'step', |
| 270 | ... 'continue', |
| 271 | ... ]): |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 272 | ... skip_module() |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 273 | > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module() |
| 274 | -> string.capwords('FOO') |
| 275 | (Pdb) step |
| 276 | --Return-- |
| 277 | > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None |
| 278 | -> string.capwords('FOO') |
| 279 | (Pdb) continue |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 280 | """ |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 281 | |
| 282 | |
| 283 | # Module for testing skipping of module that makes a callback |
| 284 | mod = imp.new_module('module_to_skip') |
| 285 | exec('def foo_pony(callback): x = 1; callback(); return None', mod.__dict__) |
| 286 | |
| 287 | |
| 288 | def test_pdb_skip_modules_with_callback(): |
| 289 | """This illustrates skipping of modules that call into other code. |
| 290 | |
| 291 | >>> def skip_module(): |
| 292 | ... def callback(): |
| 293 | ... return None |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 294 | ... import pdb; pdb.Pdb(skip=['module_to_skip*']).set_trace() |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 295 | ... mod.foo_pony(callback) |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 296 | |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 297 | >>> with PdbTestInput([ |
| 298 | ... 'step', |
| 299 | ... 'step', |
| 300 | ... 'step', |
| 301 | ... 'step', |
| 302 | ... 'step', |
| 303 | ... 'continue', |
| 304 | ... ]): |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 305 | ... skip_module() |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 306 | ... pass # provides something to "step" to |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 307 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module() |
| 308 | -> mod.foo_pony(callback) |
| 309 | (Pdb) step |
| 310 | --Call-- |
| 311 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback() |
| 312 | -> def callback(): |
| 313 | (Pdb) step |
| 314 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback() |
| 315 | -> return None |
| 316 | (Pdb) step |
| 317 | --Return-- |
| 318 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None |
| 319 | -> return None |
| 320 | (Pdb) step |
| 321 | --Return-- |
| 322 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None |
| 323 | -> mod.foo_pony(callback) |
| 324 | (Pdb) step |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 325 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>() |
| 326 | -> pass # provides something to "step" to |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 327 | (Pdb) continue |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 328 | """ |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 329 | |
| 330 | |
Georg Brandl | 3f94089 | 2010-07-30 10:29:19 +0000 | [diff] [blame] | 331 | def test_pdb_continue_in_bottomframe(): |
| 332 | """Test that "continue" and "next" work properly in bottom frame (issue #5294). |
| 333 | |
| 334 | >>> def test_function(): |
| 335 | ... import pdb, sys; inst = pdb.Pdb() |
| 336 | ... inst.set_trace() |
| 337 | ... inst.botframe = sys._getframe() # hackery to get the right botframe |
| 338 | ... print(1) |
| 339 | ... print(2) |
| 340 | ... print(3) |
| 341 | ... print(4) |
| 342 | |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 343 | >>> with PdbTestInput([ # doctest: +ELLIPSIS |
Georg Brandl | 3f94089 | 2010-07-30 10:29:19 +0000 | [diff] [blame] | 344 | ... 'next', |
| 345 | ... 'break 7', |
| 346 | ... 'continue', |
| 347 | ... 'next', |
| 348 | ... 'continue', |
| 349 | ... 'continue', |
| 350 | ... ]): |
| 351 | ... test_function() |
| 352 | > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(4)test_function() |
| 353 | -> inst.botframe = sys._getframe() # hackery to get the right botframe |
| 354 | (Pdb) next |
| 355 | > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(5)test_function() |
| 356 | -> print(1) |
| 357 | (Pdb) break 7 |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 358 | Breakpoint ... at <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>:7 |
Georg Brandl | 3f94089 | 2010-07-30 10:29:19 +0000 | [diff] [blame] | 359 | (Pdb) continue |
| 360 | 1 |
| 361 | 2 |
| 362 | > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(7)test_function() |
| 363 | -> print(3) |
| 364 | (Pdb) next |
| 365 | 3 |
| 366 | > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(8)test_function() |
| 367 | -> print(4) |
| 368 | (Pdb) continue |
| 369 | 4 |
| 370 | """ |
| 371 | |
| 372 | |
Georg Brandl | 46b9afc | 2010-07-30 09:14:20 +0000 | [diff] [blame] | 373 | def pdb_invoke(method, arg): |
| 374 | """Run pdb.method(arg).""" |
| 375 | import pdb; getattr(pdb, method)(arg) |
| 376 | |
| 377 | |
| 378 | def test_pdb_run_with_incorrect_argument(): |
| 379 | """Testing run and runeval with incorrect first argument. |
| 380 | |
| 381 | >>> pti = PdbTestInput(['continue',]) |
| 382 | >>> with pti: |
| 383 | ... pdb_invoke('run', lambda x: x) |
| 384 | Traceback (most recent call last): |
| 385 | TypeError: exec() arg 1 must be a string, bytes or code object |
| 386 | |
| 387 | >>> with pti: |
| 388 | ... pdb_invoke('runeval', lambda x: x) |
| 389 | Traceback (most recent call last): |
| 390 | TypeError: eval() arg 1 must be a string, bytes or code object |
| 391 | """ |
| 392 | |
| 393 | |
| 394 | def test_pdb_run_with_code_object(): |
| 395 | """Testing run and runeval with code object as a first argument. |
| 396 | |
| 397 | >>> with PdbTestInput(['step','x', 'continue']): |
| 398 | ... pdb_invoke('run', compile('x=1', '<string>', 'exec')) |
| 399 | > <string>(1)<module>() |
| 400 | (Pdb) step |
| 401 | --Return-- |
| 402 | > <string>(1)<module>()->None |
| 403 | (Pdb) x |
| 404 | 1 |
| 405 | (Pdb) continue |
| 406 | |
| 407 | >>> with PdbTestInput(['x', 'continue']): |
| 408 | ... x=0 |
| 409 | ... pdb_invoke('runeval', compile('x+1', '<string>', 'eval')) |
| 410 | > <string>(1)<module>()->None |
| 411 | (Pdb) x |
| 412 | 1 |
| 413 | (Pdb) continue |
| 414 | """ |
| 415 | |
| 416 | |
Georg Brandl | 6cccb86 | 2010-07-30 14:16:43 +0000 | [diff] [blame] | 417 | class PdbTestCase(unittest.TestCase): |
| 418 | |
| 419 | def test_issue7964(self): |
| 420 | # open the file as binary so we can force \r\n newline |
| 421 | with open(support.TESTFN, 'wb') as f: |
| 422 | f.write(b'print("testing my pdb")\r\n') |
| 423 | cmd = [sys.executable, '-m', 'pdb', support.TESTFN] |
| 424 | proc = subprocess.Popen(cmd, |
| 425 | stdout=subprocess.PIPE, |
| 426 | stdin=subprocess.PIPE, |
| 427 | stderr=subprocess.STDOUT, |
| 428 | ) |
| 429 | stdout, stderr = proc.communicate(b'quit\n') |
| 430 | self.assertNotIn(b'SyntaxError', stdout, |
| 431 | "Got a syntax error running test script under PDB") |
| 432 | |
| 433 | def tearDown(self): |
| 434 | support.unlink(support.TESTFN) |
| 435 | |
| 436 | |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 437 | def test_main(): |
| 438 | from test import test_pdb |
| 439 | support.run_doctest(test_pdb, verbosity=True) |
Georg Brandl | 6cccb86 | 2010-07-30 14:16:43 +0000 | [diff] [blame] | 440 | support.run_unittest(PdbTestCase) |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 441 | |
| 442 | |
| 443 | if __name__ == '__main__': |
| 444 | test_main() |