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