Georg Brandl | 46b9afc | 2010-07-30 09:14:20 +0000 | [diff] [blame] | 1 | # A test suite for pdb; not very comprehensive at the moment. |
Martin v. Löwis | 67880cc | 2012-05-02 07:41:22 +0200 | [diff] [blame] | 2 | |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 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 |
Senthil Kumaran | 42d7081 | 2012-05-01 10:07:49 +0800 | [diff] [blame] | 8 | import textwrap |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 9 | |
| 10 | from test import support |
| 11 | # This little helper class is essential for testing pdb under doctest. |
| 12 | from test.test_doctest import _FakeInput |
| 13 | |
| 14 | |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 15 | class PdbTestInput(object): |
| 16 | """Context manager that makes testing Pdb in doctests easier.""" |
| 17 | |
| 18 | def __init__(self, input): |
| 19 | self.input = input |
| 20 | |
| 21 | def __enter__(self): |
| 22 | self.real_stdin = sys.stdin |
| 23 | sys.stdin = _FakeInput(self.input) |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 24 | self.orig_trace = sys.gettrace() if hasattr(sys, 'gettrace') else None |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 25 | |
| 26 | def __exit__(self, *exc): |
| 27 | sys.stdin = self.real_stdin |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 28 | if self.orig_trace: |
| 29 | sys.settrace(self.orig_trace) |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 30 | |
| 31 | |
| 32 | def test_pdb_displayhook(): |
| 33 | """This tests the custom displayhook for pdb. |
| 34 | |
| 35 | >>> def test_function(foo, bar): |
Georg Brandl | 34748cd | 2010-12-04 17:11:36 +0000 | [diff] [blame] | 36 | ... import pdb; pdb.Pdb(nosigint=True).set_trace() |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 37 | ... pass |
| 38 | |
| 39 | >>> with PdbTestInput([ |
| 40 | ... 'foo', |
| 41 | ... 'bar', |
| 42 | ... 'for i in range(5): print(i)', |
| 43 | ... 'continue', |
| 44 | ... ]): |
| 45 | ... test_function(1, None) |
| 46 | > <doctest test.test_pdb.test_pdb_displayhook[0]>(3)test_function() |
| 47 | -> pass |
| 48 | (Pdb) foo |
| 49 | 1 |
| 50 | (Pdb) bar |
| 51 | (Pdb) for i in range(5): print(i) |
| 52 | 0 |
| 53 | 1 |
| 54 | 2 |
| 55 | 3 |
| 56 | 4 |
| 57 | (Pdb) continue |
| 58 | """ |
| 59 | |
| 60 | |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 61 | def test_pdb_basic_commands(): |
| 62 | """Test the basic commands of pdb. |
| 63 | |
| 64 | >>> def test_function_2(foo, bar='default'): |
| 65 | ... print(foo) |
| 66 | ... for i in range(5): |
| 67 | ... print(i) |
| 68 | ... print(bar) |
| 69 | ... for i in range(10): |
| 70 | ... never_executed |
| 71 | ... print('after for') |
| 72 | ... print('...') |
| 73 | ... return foo.upper() |
| 74 | |
| 75 | >>> def test_function(): |
Georg Brandl | 34748cd | 2010-12-04 17:11:36 +0000 | [diff] [blame] | 76 | ... import pdb; pdb.Pdb(nosigint=True).set_trace() |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 77 | ... ret = test_function_2('baz') |
| 78 | ... print(ret) |
| 79 | |
| 80 | >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
| 81 | ... 'step', # entering the function call |
| 82 | ... 'args', # display function args |
| 83 | ... 'list', # list function source |
| 84 | ... 'bt', # display backtrace |
| 85 | ... 'up', # step up to test_function() |
| 86 | ... 'down', # step down to test_function_2() again |
| 87 | ... 'next', # stepping to print(foo) |
| 88 | ... 'next', # stepping to the for loop |
| 89 | ... 'step', # stepping into the for loop |
| 90 | ... 'until', # continuing until out of the for loop |
| 91 | ... 'next', # executing the print(bar) |
| 92 | ... 'jump 8', # jump over second for loop |
| 93 | ... 'return', # return out of function |
| 94 | ... 'retval', # display return value |
| 95 | ... 'continue', |
| 96 | ... ]): |
| 97 | ... test_function() |
| 98 | > <doctest test.test_pdb.test_pdb_basic_commands[1]>(3)test_function() |
| 99 | -> ret = test_function_2('baz') |
| 100 | (Pdb) step |
| 101 | --Call-- |
| 102 | > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2() |
| 103 | -> def test_function_2(foo, bar='default'): |
| 104 | (Pdb) args |
| 105 | foo = 'baz' |
| 106 | bar = 'default' |
| 107 | (Pdb) list |
| 108 | 1 -> def test_function_2(foo, bar='default'): |
| 109 | 2 print(foo) |
| 110 | 3 for i in range(5): |
| 111 | 4 print(i) |
| 112 | 5 print(bar) |
| 113 | 6 for i in range(10): |
| 114 | 7 never_executed |
| 115 | 8 print('after for') |
| 116 | 9 print('...') |
| 117 | 10 return foo.upper() |
| 118 | [EOF] |
| 119 | (Pdb) bt |
| 120 | ... |
| 121 | <doctest test.test_pdb.test_pdb_basic_commands[2]>(18)<module>() |
| 122 | -> test_function() |
| 123 | <doctest test.test_pdb.test_pdb_basic_commands[1]>(3)test_function() |
| 124 | -> ret = test_function_2('baz') |
| 125 | > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2() |
| 126 | -> def test_function_2(foo, bar='default'): |
| 127 | (Pdb) up |
| 128 | > <doctest test.test_pdb.test_pdb_basic_commands[1]>(3)test_function() |
| 129 | -> ret = test_function_2('baz') |
| 130 | (Pdb) down |
| 131 | > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2() |
| 132 | -> def test_function_2(foo, bar='default'): |
| 133 | (Pdb) next |
| 134 | > <doctest test.test_pdb.test_pdb_basic_commands[0]>(2)test_function_2() |
| 135 | -> print(foo) |
| 136 | (Pdb) next |
| 137 | baz |
| 138 | > <doctest test.test_pdb.test_pdb_basic_commands[0]>(3)test_function_2() |
| 139 | -> for i in range(5): |
| 140 | (Pdb) step |
| 141 | > <doctest test.test_pdb.test_pdb_basic_commands[0]>(4)test_function_2() |
| 142 | -> print(i) |
| 143 | (Pdb) until |
| 144 | 0 |
| 145 | 1 |
| 146 | 2 |
| 147 | 3 |
| 148 | 4 |
| 149 | > <doctest test.test_pdb.test_pdb_basic_commands[0]>(5)test_function_2() |
| 150 | -> print(bar) |
| 151 | (Pdb) next |
| 152 | default |
| 153 | > <doctest test.test_pdb.test_pdb_basic_commands[0]>(6)test_function_2() |
| 154 | -> for i in range(10): |
| 155 | (Pdb) jump 8 |
| 156 | > <doctest test.test_pdb.test_pdb_basic_commands[0]>(8)test_function_2() |
| 157 | -> print('after for') |
| 158 | (Pdb) return |
| 159 | after for |
| 160 | ... |
| 161 | --Return-- |
| 162 | > <doctest test.test_pdb.test_pdb_basic_commands[0]>(10)test_function_2()->'BAZ' |
| 163 | -> return foo.upper() |
| 164 | (Pdb) retval |
| 165 | 'BAZ' |
| 166 | (Pdb) continue |
| 167 | BAZ |
| 168 | """ |
| 169 | |
| 170 | |
| 171 | def test_pdb_breakpoint_commands(): |
| 172 | """Test basic commands related to breakpoints. |
| 173 | |
| 174 | >>> def test_function(): |
Georg Brandl | 34748cd | 2010-12-04 17:11:36 +0000 | [diff] [blame] | 175 | ... import pdb; pdb.Pdb(nosigint=True).set_trace() |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 176 | ... print(1) |
| 177 | ... print(2) |
| 178 | ... print(3) |
| 179 | ... print(4) |
| 180 | |
| 181 | First, need to clear bdb state that might be left over from previous tests. |
| 182 | Otherwise, the new breakpoints might get assigned different numbers. |
| 183 | |
| 184 | >>> from bdb import Breakpoint |
| 185 | >>> Breakpoint.next = 1 |
| 186 | >>> Breakpoint.bplist = {} |
| 187 | >>> Breakpoint.bpbynumber = [None] |
| 188 | |
| 189 | Now test the breakpoint commands. NORMALIZE_WHITESPACE is needed because |
| 190 | the breakpoint list outputs a tab for the "stop only" and "ignore next" |
| 191 | lines, which we don't want to put in here. |
| 192 | |
| 193 | >>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE |
| 194 | ... 'break 3', |
| 195 | ... 'disable 1', |
| 196 | ... 'ignore 1 10', |
| 197 | ... 'condition 1 1 < 2', |
| 198 | ... 'break 4', |
Senthil Kumaran | 6f10704 | 2010-11-29 11:54:17 +0000 | [diff] [blame] | 199 | ... 'break 4', |
| 200 | ... 'break', |
| 201 | ... 'clear 3', |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 202 | ... 'break', |
| 203 | ... 'condition 1', |
| 204 | ... 'enable 1', |
| 205 | ... 'clear 1', |
| 206 | ... 'commands 2', |
| 207 | ... 'print 42', |
| 208 | ... 'end', |
| 209 | ... 'continue', # will stop at breakpoint 2 (line 4) |
| 210 | ... 'clear', # clear all! |
| 211 | ... 'y', |
| 212 | ... 'tbreak 5', |
| 213 | ... 'continue', # will stop at temporary breakpoint |
| 214 | ... 'break', # make sure breakpoint is gone |
| 215 | ... 'continue', |
| 216 | ... ]): |
| 217 | ... test_function() |
| 218 | > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(3)test_function() |
| 219 | -> print(1) |
| 220 | (Pdb) break 3 |
| 221 | Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 |
| 222 | (Pdb) disable 1 |
| 223 | Disabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 |
| 224 | (Pdb) ignore 1 10 |
| 225 | Will ignore next 10 crossings of breakpoint 1. |
| 226 | (Pdb) condition 1 1 < 2 |
| 227 | New condition set for breakpoint 1. |
| 228 | (Pdb) break 4 |
| 229 | Breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 |
Senthil Kumaran | 6f10704 | 2010-11-29 11:54:17 +0000 | [diff] [blame] | 230 | (Pdb) break 4 |
| 231 | Breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 |
| 232 | (Pdb) break |
| 233 | Num Type Disp Enb Where |
| 234 | 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 |
| 235 | stop only if 1 < 2 |
| 236 | ignore next 10 hits |
| 237 | 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 |
| 238 | 3 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 |
| 239 | (Pdb) clear 3 |
| 240 | Deleted breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 241 | (Pdb) break |
| 242 | Num Type Disp Enb Where |
| 243 | 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 |
| 244 | stop only if 1 < 2 |
| 245 | ignore next 10 hits |
| 246 | 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 |
| 247 | (Pdb) condition 1 |
| 248 | Breakpoint 1 is now unconditional. |
| 249 | (Pdb) enable 1 |
| 250 | Enabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 |
| 251 | (Pdb) clear 1 |
| 252 | Deleted breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 |
| 253 | (Pdb) commands 2 |
| 254 | (com) print 42 |
| 255 | (com) end |
| 256 | (Pdb) continue |
| 257 | 1 |
| 258 | 42 |
| 259 | > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(4)test_function() |
| 260 | -> print(2) |
| 261 | (Pdb) clear |
| 262 | Clear all breaks? y |
| 263 | Deleted breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 |
| 264 | (Pdb) tbreak 5 |
Senthil Kumaran | 6f10704 | 2010-11-29 11:54:17 +0000 | [diff] [blame] | 265 | Breakpoint 4 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5 |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 266 | (Pdb) continue |
| 267 | 2 |
Senthil Kumaran | 6f10704 | 2010-11-29 11:54:17 +0000 | [diff] [blame] | 268 | Deleted breakpoint 4 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5 |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 269 | > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(5)test_function() |
| 270 | -> print(3) |
| 271 | (Pdb) break |
| 272 | (Pdb) continue |
| 273 | 3 |
| 274 | 4 |
| 275 | """ |
| 276 | |
| 277 | |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 278 | def do_nothing(): |
| 279 | pass |
| 280 | |
| 281 | def do_something(): |
| 282 | print(42) |
| 283 | |
| 284 | def test_list_commands(): |
| 285 | """Test the list and source commands of pdb. |
| 286 | |
| 287 | >>> def test_function_2(foo): |
Georg Brandl | a8fbc6a | 2010-07-31 11:52:46 +0000 | [diff] [blame] | 288 | ... import test.test_pdb |
| 289 | ... test.test_pdb.do_nothing() |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 290 | ... 'some...' |
| 291 | ... 'more...' |
| 292 | ... 'code...' |
| 293 | ... 'to...' |
| 294 | ... 'make...' |
| 295 | ... 'a...' |
| 296 | ... 'long...' |
| 297 | ... 'listing...' |
| 298 | ... 'useful...' |
| 299 | ... '...' |
| 300 | ... '...' |
| 301 | ... return foo |
| 302 | |
| 303 | >>> def test_function(): |
Georg Brandl | 34748cd | 2010-12-04 17:11:36 +0000 | [diff] [blame] | 304 | ... import pdb; pdb.Pdb(nosigint=True).set_trace() |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 305 | ... ret = test_function_2('baz') |
| 306 | |
| 307 | >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
| 308 | ... 'list', # list first function |
| 309 | ... 'step', # step into second function |
| 310 | ... 'list', # list second function |
| 311 | ... 'list', # continue listing to EOF |
| 312 | ... 'list 1,3', # list specific lines |
| 313 | ... 'list x', # invalid argument |
| 314 | ... 'next', # step to import |
| 315 | ... 'next', # step over import |
| 316 | ... 'step', # step into do_nothing |
| 317 | ... 'longlist', # list all lines |
| 318 | ... 'source do_something', # list all lines of function |
Georg Brandl | cdf66a9 | 2010-07-30 18:15:16 +0000 | [diff] [blame] | 319 | ... 'source fooxxx', # something that doesn't exit |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 320 | ... 'continue', |
| 321 | ... ]): |
| 322 | ... test_function() |
| 323 | > <doctest test.test_pdb.test_list_commands[1]>(3)test_function() |
| 324 | -> ret = test_function_2('baz') |
| 325 | (Pdb) list |
| 326 | 1 def test_function(): |
Georg Brandl | 34748cd | 2010-12-04 17:11:36 +0000 | [diff] [blame] | 327 | 2 import pdb; pdb.Pdb(nosigint=True).set_trace() |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 328 | 3 -> ret = test_function_2('baz') |
| 329 | [EOF] |
| 330 | (Pdb) step |
| 331 | --Call-- |
| 332 | > <doctest test.test_pdb.test_list_commands[0]>(1)test_function_2() |
| 333 | -> def test_function_2(foo): |
| 334 | (Pdb) list |
| 335 | 1 -> def test_function_2(foo): |
Georg Brandl | a8fbc6a | 2010-07-31 11:52:46 +0000 | [diff] [blame] | 336 | 2 import test.test_pdb |
| 337 | 3 test.test_pdb.do_nothing() |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 338 | 4 'some...' |
| 339 | 5 'more...' |
| 340 | 6 'code...' |
| 341 | 7 'to...' |
| 342 | 8 'make...' |
| 343 | 9 'a...' |
| 344 | 10 'long...' |
| 345 | 11 'listing...' |
| 346 | (Pdb) list |
| 347 | 12 'useful...' |
| 348 | 13 '...' |
| 349 | 14 '...' |
| 350 | 15 return foo |
| 351 | [EOF] |
| 352 | (Pdb) list 1,3 |
| 353 | 1 -> def test_function_2(foo): |
Georg Brandl | a8fbc6a | 2010-07-31 11:52:46 +0000 | [diff] [blame] | 354 | 2 import test.test_pdb |
| 355 | 3 test.test_pdb.do_nothing() |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 356 | (Pdb) list x |
| 357 | *** ... |
| 358 | (Pdb) next |
| 359 | > <doctest test.test_pdb.test_list_commands[0]>(2)test_function_2() |
Georg Brandl | a8fbc6a | 2010-07-31 11:52:46 +0000 | [diff] [blame] | 360 | -> import test.test_pdb |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 361 | (Pdb) next |
| 362 | > <doctest test.test_pdb.test_list_commands[0]>(3)test_function_2() |
Georg Brandl | a8fbc6a | 2010-07-31 11:52:46 +0000 | [diff] [blame] | 363 | -> test.test_pdb.do_nothing() |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 364 | (Pdb) step |
| 365 | --Call-- |
Georg Brandl | e1e8df1 | 2010-07-31 08:14:16 +0000 | [diff] [blame] | 366 | > ...test_pdb.py(...)do_nothing() |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 367 | -> def do_nothing(): |
| 368 | (Pdb) longlist |
| 369 | ... -> def do_nothing(): |
| 370 | ... pass |
| 371 | (Pdb) source do_something |
| 372 | ... def do_something(): |
| 373 | ... print(42) |
Georg Brandl | cdf66a9 | 2010-07-30 18:15:16 +0000 | [diff] [blame] | 374 | (Pdb) source fooxxx |
| 375 | *** ... |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 376 | (Pdb) continue |
| 377 | """ |
| 378 | |
| 379 | |
Georg Brandl | 0a9c3e9 | 2010-07-30 18:46:38 +0000 | [diff] [blame] | 380 | def test_post_mortem(): |
| 381 | """Test post mortem traceback debugging. |
| 382 | |
| 383 | >>> def test_function_2(): |
| 384 | ... try: |
| 385 | ... 1/0 |
| 386 | ... finally: |
| 387 | ... print('Exception!') |
| 388 | |
| 389 | >>> def test_function(): |
Georg Brandl | 34748cd | 2010-12-04 17:11:36 +0000 | [diff] [blame] | 390 | ... import pdb; pdb.Pdb(nosigint=True).set_trace() |
Georg Brandl | 0a9c3e9 | 2010-07-30 18:46:38 +0000 | [diff] [blame] | 391 | ... test_function_2() |
| 392 | ... print('Not reached.') |
| 393 | |
| 394 | >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
| 395 | ... 'next', # step over exception-raising call |
| 396 | ... 'bt', # get a backtrace |
| 397 | ... 'list', # list code of test_function() |
| 398 | ... 'down', # step into test_function_2() |
| 399 | ... 'list', # list code of test_function_2() |
| 400 | ... 'continue', |
| 401 | ... ]): |
| 402 | ... try: |
| 403 | ... test_function() |
| 404 | ... except ZeroDivisionError: |
| 405 | ... print('Correctly reraised.') |
| 406 | > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function() |
| 407 | -> test_function_2() |
| 408 | (Pdb) next |
| 409 | Exception! |
| 410 | ZeroDivisionError: division by zero |
| 411 | > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function() |
| 412 | -> test_function_2() |
| 413 | (Pdb) bt |
| 414 | ... |
| 415 | <doctest test.test_pdb.test_post_mortem[2]>(10)<module>() |
| 416 | -> test_function() |
| 417 | > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function() |
| 418 | -> test_function_2() |
| 419 | <doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2() |
| 420 | -> 1/0 |
| 421 | (Pdb) list |
| 422 | 1 def test_function(): |
Georg Brandl | 34748cd | 2010-12-04 17:11:36 +0000 | [diff] [blame] | 423 | 2 import pdb; pdb.Pdb(nosigint=True).set_trace() |
Georg Brandl | 0a9c3e9 | 2010-07-30 18:46:38 +0000 | [diff] [blame] | 424 | 3 -> test_function_2() |
| 425 | 4 print('Not reached.') |
| 426 | [EOF] |
| 427 | (Pdb) down |
| 428 | > <doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2() |
| 429 | -> 1/0 |
| 430 | (Pdb) list |
| 431 | 1 def test_function_2(): |
| 432 | 2 try: |
| 433 | 3 >> 1/0 |
| 434 | 4 finally: |
| 435 | 5 -> print('Exception!') |
| 436 | [EOF] |
| 437 | (Pdb) continue |
| 438 | Correctly reraised. |
| 439 | """ |
| 440 | |
| 441 | |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 442 | def test_pdb_skip_modules(): |
| 443 | """This illustrates the simple case of module skipping. |
| 444 | |
| 445 | >>> def skip_module(): |
| 446 | ... import string |
Georg Brandl | 34748cd | 2010-12-04 17:11:36 +0000 | [diff] [blame] | 447 | ... import pdb; pdb.Pdb(skip=['stri*'], nosigint=True).set_trace() |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 448 | ... string.capwords('FOO') |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 449 | |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 450 | >>> with PdbTestInput([ |
| 451 | ... 'step', |
| 452 | ... 'continue', |
| 453 | ... ]): |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 454 | ... skip_module() |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 455 | > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module() |
| 456 | -> string.capwords('FOO') |
| 457 | (Pdb) step |
| 458 | --Return-- |
| 459 | > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None |
| 460 | -> string.capwords('FOO') |
| 461 | (Pdb) continue |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 462 | """ |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 463 | |
| 464 | |
| 465 | # Module for testing skipping of module that makes a callback |
| 466 | mod = imp.new_module('module_to_skip') |
| 467 | exec('def foo_pony(callback): x = 1; callback(); return None', mod.__dict__) |
| 468 | |
| 469 | |
| 470 | def test_pdb_skip_modules_with_callback(): |
| 471 | """This illustrates skipping of modules that call into other code. |
| 472 | |
| 473 | >>> def skip_module(): |
| 474 | ... def callback(): |
| 475 | ... return None |
Georg Brandl | 34748cd | 2010-12-04 17:11:36 +0000 | [diff] [blame] | 476 | ... import pdb; pdb.Pdb(skip=['module_to_skip*'], nosigint=True).set_trace() |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 477 | ... mod.foo_pony(callback) |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 478 | |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 479 | >>> with PdbTestInput([ |
| 480 | ... 'step', |
| 481 | ... 'step', |
| 482 | ... 'step', |
| 483 | ... 'step', |
| 484 | ... 'step', |
| 485 | ... 'continue', |
| 486 | ... ]): |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 487 | ... skip_module() |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 488 | ... pass # provides something to "step" to |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 489 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module() |
| 490 | -> mod.foo_pony(callback) |
| 491 | (Pdb) step |
| 492 | --Call-- |
| 493 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback() |
| 494 | -> def callback(): |
| 495 | (Pdb) step |
| 496 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback() |
| 497 | -> return None |
| 498 | (Pdb) step |
| 499 | --Return-- |
| 500 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None |
| 501 | -> return None |
| 502 | (Pdb) step |
| 503 | --Return-- |
| 504 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None |
| 505 | -> mod.foo_pony(callback) |
| 506 | (Pdb) step |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 507 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>() |
| 508 | -> pass # provides something to "step" to |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 509 | (Pdb) continue |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 510 | """ |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 511 | |
| 512 | |
Georg Brandl | 3f94089 | 2010-07-30 10:29:19 +0000 | [diff] [blame] | 513 | def test_pdb_continue_in_bottomframe(): |
| 514 | """Test that "continue" and "next" work properly in bottom frame (issue #5294). |
| 515 | |
| 516 | >>> def test_function(): |
Georg Brandl | 34748cd | 2010-12-04 17:11:36 +0000 | [diff] [blame] | 517 | ... import pdb, sys; inst = pdb.Pdb(nosigint=True) |
Georg Brandl | 3f94089 | 2010-07-30 10:29:19 +0000 | [diff] [blame] | 518 | ... inst.set_trace() |
| 519 | ... inst.botframe = sys._getframe() # hackery to get the right botframe |
| 520 | ... print(1) |
| 521 | ... print(2) |
| 522 | ... print(3) |
| 523 | ... print(4) |
| 524 | |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 525 | >>> with PdbTestInput([ # doctest: +ELLIPSIS |
Georg Brandl | 3f94089 | 2010-07-30 10:29:19 +0000 | [diff] [blame] | 526 | ... 'next', |
| 527 | ... 'break 7', |
| 528 | ... 'continue', |
| 529 | ... 'next', |
| 530 | ... 'continue', |
| 531 | ... 'continue', |
| 532 | ... ]): |
| 533 | ... test_function() |
| 534 | > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(4)test_function() |
| 535 | -> inst.botframe = sys._getframe() # hackery to get the right botframe |
| 536 | (Pdb) next |
| 537 | > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(5)test_function() |
| 538 | -> print(1) |
| 539 | (Pdb) break 7 |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 540 | 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] | 541 | (Pdb) continue |
| 542 | 1 |
| 543 | 2 |
| 544 | > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(7)test_function() |
| 545 | -> print(3) |
| 546 | (Pdb) next |
| 547 | 3 |
| 548 | > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(8)test_function() |
| 549 | -> print(4) |
| 550 | (Pdb) continue |
| 551 | 4 |
| 552 | """ |
| 553 | |
| 554 | |
Georg Brandl | 46b9afc | 2010-07-30 09:14:20 +0000 | [diff] [blame] | 555 | def pdb_invoke(method, arg): |
| 556 | """Run pdb.method(arg).""" |
Georg Brandl | 34748cd | 2010-12-04 17:11:36 +0000 | [diff] [blame] | 557 | import pdb |
| 558 | getattr(pdb.Pdb(nosigint=True), method)(arg) |
Georg Brandl | 46b9afc | 2010-07-30 09:14:20 +0000 | [diff] [blame] | 559 | |
| 560 | |
| 561 | def test_pdb_run_with_incorrect_argument(): |
| 562 | """Testing run and runeval with incorrect first argument. |
| 563 | |
| 564 | >>> pti = PdbTestInput(['continue',]) |
| 565 | >>> with pti: |
| 566 | ... pdb_invoke('run', lambda x: x) |
| 567 | Traceback (most recent call last): |
| 568 | TypeError: exec() arg 1 must be a string, bytes or code object |
| 569 | |
| 570 | >>> with pti: |
| 571 | ... pdb_invoke('runeval', lambda x: x) |
| 572 | Traceback (most recent call last): |
| 573 | TypeError: eval() arg 1 must be a string, bytes or code object |
| 574 | """ |
| 575 | |
| 576 | |
| 577 | def test_pdb_run_with_code_object(): |
| 578 | """Testing run and runeval with code object as a first argument. |
| 579 | |
Georg Brandl | e1e8df1 | 2010-07-31 08:14:16 +0000 | [diff] [blame] | 580 | >>> with PdbTestInput(['step','x', 'continue']): # doctest: +ELLIPSIS |
Georg Brandl | 46b9afc | 2010-07-30 09:14:20 +0000 | [diff] [blame] | 581 | ... pdb_invoke('run', compile('x=1', '<string>', 'exec')) |
Georg Brandl | e1e8df1 | 2010-07-31 08:14:16 +0000 | [diff] [blame] | 582 | > <string>(1)<module>()... |
Georg Brandl | 46b9afc | 2010-07-30 09:14:20 +0000 | [diff] [blame] | 583 | (Pdb) step |
| 584 | --Return-- |
| 585 | > <string>(1)<module>()->None |
| 586 | (Pdb) x |
| 587 | 1 |
| 588 | (Pdb) continue |
| 589 | |
| 590 | >>> with PdbTestInput(['x', 'continue']): |
| 591 | ... x=0 |
| 592 | ... pdb_invoke('runeval', compile('x+1', '<string>', 'eval')) |
| 593 | > <string>(1)<module>()->None |
| 594 | (Pdb) x |
| 595 | 1 |
| 596 | (Pdb) continue |
| 597 | """ |
| 598 | |
| 599 | |
Georg Brandl | 6cccb86 | 2010-07-30 14:16:43 +0000 | [diff] [blame] | 600 | class PdbTestCase(unittest.TestCase): |
| 601 | |
Senthil Kumaran | 42d7081 | 2012-05-01 10:07:49 +0800 | [diff] [blame] | 602 | def run_pdb(self, script, commands): |
| 603 | """Run 'script' lines with pdb and the pdb 'commands'.""" |
| 604 | filename = 'main.py' |
| 605 | with open(filename, 'w') as f: |
| 606 | f.write(textwrap.dedent(script)) |
Georg Brandl | 4bde9ca | 2012-05-01 09:21:16 +0200 | [diff] [blame] | 607 | self.addCleanup(support.unlink, filename) |
Senthil Kumaran | 42d7081 | 2012-05-01 10:07:49 +0800 | [diff] [blame] | 608 | cmd = [sys.executable, '-m', 'pdb', filename] |
| 609 | stdout = stderr = None |
| 610 | with subprocess.Popen(cmd, stdout=subprocess.PIPE, |
| 611 | stdin=subprocess.PIPE, |
| 612 | stderr=subprocess.STDOUT, |
| 613 | ) as proc: |
| 614 | stdout, stderr = proc.communicate(str.encode(commands)) |
| 615 | stdout = stdout and bytes.decode(stdout) |
| 616 | stderr = stderr and bytes.decode(stderr) |
| 617 | return stdout, stderr |
| 618 | |
Georg Brandl | 6cccb86 | 2010-07-30 14:16:43 +0000 | [diff] [blame] | 619 | def test_issue7964(self): |
| 620 | # open the file as binary so we can force \r\n newline |
| 621 | with open(support.TESTFN, 'wb') as f: |
| 622 | f.write(b'print("testing my pdb")\r\n') |
| 623 | cmd = [sys.executable, '-m', 'pdb', support.TESTFN] |
| 624 | proc = subprocess.Popen(cmd, |
| 625 | stdout=subprocess.PIPE, |
| 626 | stdin=subprocess.PIPE, |
| 627 | stderr=subprocess.STDOUT, |
| 628 | ) |
Brian Curtin | 994ad6c | 2010-11-05 15:38:47 +0000 | [diff] [blame] | 629 | self.addCleanup(proc.stdout.close) |
Georg Brandl | 6cccb86 | 2010-07-30 14:16:43 +0000 | [diff] [blame] | 630 | stdout, stderr = proc.communicate(b'quit\n') |
| 631 | self.assertNotIn(b'SyntaxError', stdout, |
| 632 | "Got a syntax error running test script under PDB") |
| 633 | |
Senthil Kumaran | 42d7081 | 2012-05-01 10:07:49 +0800 | [diff] [blame] | 634 | def test_issue13183(self): |
| 635 | script = """ |
| 636 | from bar import bar |
| 637 | |
| 638 | def foo(): |
| 639 | bar() |
| 640 | |
| 641 | def nope(): |
| 642 | pass |
| 643 | |
| 644 | def foobar(): |
| 645 | foo() |
| 646 | nope() |
| 647 | |
| 648 | foobar() |
| 649 | """ |
| 650 | commands = """ |
| 651 | from bar import bar |
| 652 | break bar |
| 653 | continue |
| 654 | step |
| 655 | step |
| 656 | quit |
| 657 | """ |
| 658 | bar = """ |
| 659 | def bar(): |
Senthil Kumaran | cb17204 | 2012-05-02 08:00:22 +0800 | [diff] [blame] | 660 | pass |
Senthil Kumaran | 42d7081 | 2012-05-01 10:07:49 +0800 | [diff] [blame] | 661 | """ |
| 662 | with open('bar.py', 'w') as f: |
| 663 | f.write(textwrap.dedent(bar)) |
Georg Brandl | 4bde9ca | 2012-05-01 09:21:16 +0200 | [diff] [blame] | 664 | self.addCleanup(support.unlink, 'bar.py') |
Senthil Kumaran | 42d7081 | 2012-05-01 10:07:49 +0800 | [diff] [blame] | 665 | stdout, stderr = self.run_pdb(script, commands) |
Georg Brandl | 4bde9ca | 2012-05-01 09:21:16 +0200 | [diff] [blame] | 666 | self.assertTrue( |
| 667 | any('main.py(5)foo()->None' in l for l in stdout.splitlines()), |
| 668 | 'Fail to step into the caller after a return') |
Senthil Kumaran | 42d7081 | 2012-05-01 10:07:49 +0800 | [diff] [blame] | 669 | |
Andrew Svetlov | 539ee5d | 2012-12-04 21:08:28 +0200 | [diff] [blame] | 670 | def test_issue13210(self): |
| 671 | # invoking "continue" on a non-main thread triggered an exception |
| 672 | # inside signal.signal |
| 673 | |
Andrew Svetlov | 96bc043 | 2012-12-05 15:06:23 +0200 | [diff] [blame] | 674 | # raises SkipTest if python was built without threads |
| 675 | support.import_module('threading') |
| 676 | |
Andrew Svetlov | 539ee5d | 2012-12-04 21:08:28 +0200 | [diff] [blame] | 677 | with open(support.TESTFN, 'wb') as f: |
| 678 | f.write(textwrap.dedent(""" |
| 679 | import threading |
| 680 | import pdb |
| 681 | |
| 682 | def start_pdb(): |
| 683 | pdb.Pdb().set_trace() |
| 684 | x = 1 |
| 685 | y = 1 |
| 686 | |
| 687 | t = threading.Thread(target=start_pdb) |
| 688 | t.start()""").encode('ascii')) |
| 689 | cmd = [sys.executable, '-u', support.TESTFN] |
| 690 | proc = subprocess.Popen(cmd, |
| 691 | stdout=subprocess.PIPE, |
| 692 | stdin=subprocess.PIPE, |
| 693 | stderr=subprocess.STDOUT, |
| 694 | ) |
| 695 | self.addCleanup(proc.stdout.close) |
| 696 | stdout, stderr = proc.communicate(b'cont\n') |
| 697 | self.assertNotIn('Error', stdout.decode(), |
| 698 | "Got an error running test script under PDB") |
| 699 | |
Georg Brandl | 6cccb86 | 2010-07-30 14:16:43 +0000 | [diff] [blame] | 700 | def tearDown(self): |
| 701 | support.unlink(support.TESTFN) |
| 702 | |
| 703 | |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 704 | def test_main(): |
| 705 | from test import test_pdb |
| 706 | support.run_doctest(test_pdb, verbosity=True) |
Georg Brandl | 6cccb86 | 2010-07-30 14:16:43 +0000 | [diff] [blame] | 707 | support.run_unittest(PdbTestCase) |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 708 | |
| 709 | |
| 710 | if __name__ == '__main__': |
| 711 | test_main() |