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 | 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 |
Brett Cannon | 9529fbf | 2013-06-15 17:11:25 -0400 | [diff] [blame] | 6 | import types |
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', |
R David Murray | 78d692f | 2013-10-10 17:23:26 -0400 | [diff] [blame] | 208 | ... 'p "42"', |
| 209 | ... 'print("42", 7*6)', # Issue 18764 (not about breakpoints) |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 210 | ... 'end', |
| 211 | ... 'continue', # will stop at breakpoint 2 (line 4) |
| 212 | ... 'clear', # clear all! |
| 213 | ... 'y', |
| 214 | ... 'tbreak 5', |
| 215 | ... 'continue', # will stop at temporary breakpoint |
| 216 | ... 'break', # make sure breakpoint is gone |
| 217 | ... 'continue', |
| 218 | ... ]): |
| 219 | ... test_function() |
| 220 | > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(3)test_function() |
| 221 | -> print(1) |
| 222 | (Pdb) break 3 |
| 223 | Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 |
| 224 | (Pdb) disable 1 |
| 225 | Disabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 |
| 226 | (Pdb) ignore 1 10 |
| 227 | Will ignore next 10 crossings of breakpoint 1. |
| 228 | (Pdb) condition 1 1 < 2 |
| 229 | New condition set for breakpoint 1. |
| 230 | (Pdb) break 4 |
| 231 | 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] | 232 | (Pdb) break 4 |
| 233 | Breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 |
| 234 | (Pdb) break |
| 235 | Num Type Disp Enb Where |
| 236 | 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 |
| 237 | stop only if 1 < 2 |
| 238 | ignore next 10 hits |
| 239 | 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 |
| 240 | 3 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 |
| 241 | (Pdb) clear 3 |
| 242 | 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] | 243 | (Pdb) break |
| 244 | Num Type Disp Enb Where |
| 245 | 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 |
| 246 | stop only if 1 < 2 |
| 247 | ignore next 10 hits |
| 248 | 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 |
| 249 | (Pdb) condition 1 |
| 250 | Breakpoint 1 is now unconditional. |
| 251 | (Pdb) enable 1 |
| 252 | Enabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 |
| 253 | (Pdb) clear 1 |
| 254 | Deleted breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 |
| 255 | (Pdb) commands 2 |
R David Murray | 78d692f | 2013-10-10 17:23:26 -0400 | [diff] [blame] | 256 | (com) p "42" |
| 257 | (com) print("42", 7*6) |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 258 | (com) end |
| 259 | (Pdb) continue |
| 260 | 1 |
R David Murray | 78d692f | 2013-10-10 17:23:26 -0400 | [diff] [blame] | 261 | '42' |
| 262 | 42 42 |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 263 | > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(4)test_function() |
| 264 | -> print(2) |
| 265 | (Pdb) clear |
| 266 | Clear all breaks? y |
| 267 | Deleted breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 |
| 268 | (Pdb) tbreak 5 |
Senthil Kumaran | 6f10704 | 2010-11-29 11:54:17 +0000 | [diff] [blame] | 269 | 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 | (Pdb) continue |
| 271 | 2 |
Senthil Kumaran | 6f10704 | 2010-11-29 11:54:17 +0000 | [diff] [blame] | 272 | 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] | 273 | > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(5)test_function() |
| 274 | -> print(3) |
| 275 | (Pdb) break |
| 276 | (Pdb) continue |
| 277 | 3 |
| 278 | 4 |
| 279 | """ |
| 280 | |
| 281 | |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 282 | def do_nothing(): |
| 283 | pass |
| 284 | |
| 285 | def do_something(): |
| 286 | print(42) |
| 287 | |
| 288 | def test_list_commands(): |
| 289 | """Test the list and source commands of pdb. |
| 290 | |
| 291 | >>> def test_function_2(foo): |
Georg Brandl | a8fbc6a | 2010-07-31 11:52:46 +0000 | [diff] [blame] | 292 | ... import test.test_pdb |
| 293 | ... test.test_pdb.do_nothing() |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 294 | ... 'some...' |
| 295 | ... 'more...' |
| 296 | ... 'code...' |
| 297 | ... 'to...' |
| 298 | ... 'make...' |
| 299 | ... 'a...' |
| 300 | ... 'long...' |
| 301 | ... 'listing...' |
| 302 | ... 'useful...' |
| 303 | ... '...' |
| 304 | ... '...' |
| 305 | ... return foo |
| 306 | |
| 307 | >>> def test_function(): |
Georg Brandl | 34748cd | 2010-12-04 17:11:36 +0000 | [diff] [blame] | 308 | ... import pdb; pdb.Pdb(nosigint=True).set_trace() |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 309 | ... ret = test_function_2('baz') |
| 310 | |
| 311 | >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
| 312 | ... 'list', # list first function |
| 313 | ... 'step', # step into second function |
| 314 | ... 'list', # list second function |
| 315 | ... 'list', # continue listing to EOF |
| 316 | ... 'list 1,3', # list specific lines |
| 317 | ... 'list x', # invalid argument |
| 318 | ... 'next', # step to import |
| 319 | ... 'next', # step over import |
| 320 | ... 'step', # step into do_nothing |
| 321 | ... 'longlist', # list all lines |
| 322 | ... 'source do_something', # list all lines of function |
Georg Brandl | cdf66a9 | 2010-07-30 18:15:16 +0000 | [diff] [blame] | 323 | ... 'source fooxxx', # something that doesn't exit |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 324 | ... 'continue', |
| 325 | ... ]): |
| 326 | ... test_function() |
| 327 | > <doctest test.test_pdb.test_list_commands[1]>(3)test_function() |
| 328 | -> ret = test_function_2('baz') |
| 329 | (Pdb) list |
| 330 | 1 def test_function(): |
Georg Brandl | 34748cd | 2010-12-04 17:11:36 +0000 | [diff] [blame] | 331 | 2 import pdb; pdb.Pdb(nosigint=True).set_trace() |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 332 | 3 -> ret = test_function_2('baz') |
| 333 | [EOF] |
| 334 | (Pdb) step |
| 335 | --Call-- |
| 336 | > <doctest test.test_pdb.test_list_commands[0]>(1)test_function_2() |
| 337 | -> def test_function_2(foo): |
| 338 | (Pdb) list |
| 339 | 1 -> def test_function_2(foo): |
Georg Brandl | a8fbc6a | 2010-07-31 11:52:46 +0000 | [diff] [blame] | 340 | 2 import test.test_pdb |
| 341 | 3 test.test_pdb.do_nothing() |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 342 | 4 'some...' |
| 343 | 5 'more...' |
| 344 | 6 'code...' |
| 345 | 7 'to...' |
| 346 | 8 'make...' |
| 347 | 9 'a...' |
| 348 | 10 'long...' |
| 349 | 11 'listing...' |
| 350 | (Pdb) list |
| 351 | 12 'useful...' |
| 352 | 13 '...' |
| 353 | 14 '...' |
| 354 | 15 return foo |
| 355 | [EOF] |
| 356 | (Pdb) list 1,3 |
| 357 | 1 -> def test_function_2(foo): |
Georg Brandl | a8fbc6a | 2010-07-31 11:52:46 +0000 | [diff] [blame] | 358 | 2 import test.test_pdb |
| 359 | 3 test.test_pdb.do_nothing() |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 360 | (Pdb) list x |
| 361 | *** ... |
| 362 | (Pdb) next |
| 363 | > <doctest test.test_pdb.test_list_commands[0]>(2)test_function_2() |
Georg Brandl | a8fbc6a | 2010-07-31 11:52:46 +0000 | [diff] [blame] | 364 | -> import test.test_pdb |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 365 | (Pdb) next |
| 366 | > <doctest test.test_pdb.test_list_commands[0]>(3)test_function_2() |
Georg Brandl | a8fbc6a | 2010-07-31 11:52:46 +0000 | [diff] [blame] | 367 | -> test.test_pdb.do_nothing() |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 368 | (Pdb) step |
| 369 | --Call-- |
Georg Brandl | e1e8df1 | 2010-07-31 08:14:16 +0000 | [diff] [blame] | 370 | > ...test_pdb.py(...)do_nothing() |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 371 | -> def do_nothing(): |
| 372 | (Pdb) longlist |
| 373 | ... -> def do_nothing(): |
| 374 | ... pass |
| 375 | (Pdb) source do_something |
| 376 | ... def do_something(): |
| 377 | ... print(42) |
Georg Brandl | cdf66a9 | 2010-07-30 18:15:16 +0000 | [diff] [blame] | 378 | (Pdb) source fooxxx |
| 379 | *** ... |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 380 | (Pdb) continue |
| 381 | """ |
| 382 | |
| 383 | |
Georg Brandl | 0a9c3e9 | 2010-07-30 18:46:38 +0000 | [diff] [blame] | 384 | def test_post_mortem(): |
| 385 | """Test post mortem traceback debugging. |
| 386 | |
| 387 | >>> def test_function_2(): |
| 388 | ... try: |
| 389 | ... 1/0 |
| 390 | ... finally: |
| 391 | ... print('Exception!') |
| 392 | |
| 393 | >>> def test_function(): |
Georg Brandl | 34748cd | 2010-12-04 17:11:36 +0000 | [diff] [blame] | 394 | ... import pdb; pdb.Pdb(nosigint=True).set_trace() |
Georg Brandl | 0a9c3e9 | 2010-07-30 18:46:38 +0000 | [diff] [blame] | 395 | ... test_function_2() |
| 396 | ... print('Not reached.') |
| 397 | |
| 398 | >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
| 399 | ... 'next', # step over exception-raising call |
| 400 | ... 'bt', # get a backtrace |
| 401 | ... 'list', # list code of test_function() |
| 402 | ... 'down', # step into test_function_2() |
| 403 | ... 'list', # list code of test_function_2() |
| 404 | ... 'continue', |
| 405 | ... ]): |
| 406 | ... try: |
| 407 | ... test_function() |
| 408 | ... except ZeroDivisionError: |
| 409 | ... print('Correctly reraised.') |
| 410 | > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function() |
| 411 | -> test_function_2() |
| 412 | (Pdb) next |
| 413 | Exception! |
| 414 | ZeroDivisionError: division by zero |
| 415 | > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function() |
| 416 | -> test_function_2() |
| 417 | (Pdb) bt |
| 418 | ... |
| 419 | <doctest test.test_pdb.test_post_mortem[2]>(10)<module>() |
| 420 | -> test_function() |
| 421 | > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function() |
| 422 | -> test_function_2() |
| 423 | <doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2() |
| 424 | -> 1/0 |
| 425 | (Pdb) list |
| 426 | 1 def test_function(): |
Georg Brandl | 34748cd | 2010-12-04 17:11:36 +0000 | [diff] [blame] | 427 | 2 import pdb; pdb.Pdb(nosigint=True).set_trace() |
Georg Brandl | 0a9c3e9 | 2010-07-30 18:46:38 +0000 | [diff] [blame] | 428 | 3 -> test_function_2() |
| 429 | 4 print('Not reached.') |
| 430 | [EOF] |
| 431 | (Pdb) down |
| 432 | > <doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2() |
| 433 | -> 1/0 |
| 434 | (Pdb) list |
| 435 | 1 def test_function_2(): |
| 436 | 2 try: |
| 437 | 3 >> 1/0 |
| 438 | 4 finally: |
| 439 | 5 -> print('Exception!') |
| 440 | [EOF] |
| 441 | (Pdb) continue |
| 442 | Correctly reraised. |
| 443 | """ |
| 444 | |
| 445 | |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 446 | def test_pdb_skip_modules(): |
| 447 | """This illustrates the simple case of module skipping. |
| 448 | |
| 449 | >>> def skip_module(): |
| 450 | ... import string |
Georg Brandl | 34748cd | 2010-12-04 17:11:36 +0000 | [diff] [blame] | 451 | ... import pdb; pdb.Pdb(skip=['stri*'], nosigint=True).set_trace() |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 452 | ... string.capwords('FOO') |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 453 | |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 454 | >>> with PdbTestInput([ |
| 455 | ... 'step', |
| 456 | ... 'continue', |
| 457 | ... ]): |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 458 | ... skip_module() |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 459 | > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module() |
| 460 | -> string.capwords('FOO') |
| 461 | (Pdb) step |
| 462 | --Return-- |
| 463 | > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None |
| 464 | -> string.capwords('FOO') |
| 465 | (Pdb) continue |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 466 | """ |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 467 | |
| 468 | |
| 469 | # Module for testing skipping of module that makes a callback |
Brett Cannon | 9529fbf | 2013-06-15 17:11:25 -0400 | [diff] [blame] | 470 | mod = types.ModuleType('module_to_skip') |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 471 | exec('def foo_pony(callback): x = 1; callback(); return None', mod.__dict__) |
| 472 | |
| 473 | |
| 474 | def test_pdb_skip_modules_with_callback(): |
| 475 | """This illustrates skipping of modules that call into other code. |
| 476 | |
| 477 | >>> def skip_module(): |
| 478 | ... def callback(): |
| 479 | ... return None |
Georg Brandl | 34748cd | 2010-12-04 17:11:36 +0000 | [diff] [blame] | 480 | ... import pdb; pdb.Pdb(skip=['module_to_skip*'], nosigint=True).set_trace() |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 481 | ... mod.foo_pony(callback) |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 482 | |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 483 | >>> with PdbTestInput([ |
| 484 | ... 'step', |
| 485 | ... 'step', |
| 486 | ... 'step', |
| 487 | ... 'step', |
| 488 | ... 'step', |
| 489 | ... 'continue', |
| 490 | ... ]): |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 491 | ... skip_module() |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 492 | ... pass # provides something to "step" to |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 493 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module() |
| 494 | -> mod.foo_pony(callback) |
| 495 | (Pdb) step |
| 496 | --Call-- |
| 497 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback() |
| 498 | -> def callback(): |
| 499 | (Pdb) step |
| 500 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback() |
| 501 | -> return None |
| 502 | (Pdb) step |
| 503 | --Return-- |
| 504 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None |
| 505 | -> return None |
| 506 | (Pdb) step |
| 507 | --Return-- |
| 508 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None |
| 509 | -> mod.foo_pony(callback) |
| 510 | (Pdb) step |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 511 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>() |
| 512 | -> pass # provides something to "step" to |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 513 | (Pdb) continue |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 514 | """ |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 515 | |
| 516 | |
Georg Brandl | 3f94089 | 2010-07-30 10:29:19 +0000 | [diff] [blame] | 517 | def test_pdb_continue_in_bottomframe(): |
| 518 | """Test that "continue" and "next" work properly in bottom frame (issue #5294). |
| 519 | |
| 520 | >>> def test_function(): |
Georg Brandl | 34748cd | 2010-12-04 17:11:36 +0000 | [diff] [blame] | 521 | ... import pdb, sys; inst = pdb.Pdb(nosigint=True) |
Georg Brandl | 3f94089 | 2010-07-30 10:29:19 +0000 | [diff] [blame] | 522 | ... inst.set_trace() |
| 523 | ... inst.botframe = sys._getframe() # hackery to get the right botframe |
| 524 | ... print(1) |
| 525 | ... print(2) |
| 526 | ... print(3) |
| 527 | ... print(4) |
| 528 | |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 529 | >>> with PdbTestInput([ # doctest: +ELLIPSIS |
Georg Brandl | 3f94089 | 2010-07-30 10:29:19 +0000 | [diff] [blame] | 530 | ... 'next', |
| 531 | ... 'break 7', |
| 532 | ... 'continue', |
| 533 | ... 'next', |
| 534 | ... 'continue', |
| 535 | ... 'continue', |
| 536 | ... ]): |
| 537 | ... test_function() |
| 538 | > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(4)test_function() |
| 539 | -> inst.botframe = sys._getframe() # hackery to get the right botframe |
| 540 | (Pdb) next |
| 541 | > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(5)test_function() |
| 542 | -> print(1) |
| 543 | (Pdb) break 7 |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 544 | 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] | 545 | (Pdb) continue |
| 546 | 1 |
| 547 | 2 |
| 548 | > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(7)test_function() |
| 549 | -> print(3) |
| 550 | (Pdb) next |
| 551 | 3 |
| 552 | > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(8)test_function() |
| 553 | -> print(4) |
| 554 | (Pdb) continue |
| 555 | 4 |
| 556 | """ |
| 557 | |
| 558 | |
Georg Brandl | 46b9afc | 2010-07-30 09:14:20 +0000 | [diff] [blame] | 559 | def pdb_invoke(method, arg): |
| 560 | """Run pdb.method(arg).""" |
Georg Brandl | 34748cd | 2010-12-04 17:11:36 +0000 | [diff] [blame] | 561 | import pdb |
| 562 | getattr(pdb.Pdb(nosigint=True), method)(arg) |
Georg Brandl | 46b9afc | 2010-07-30 09:14:20 +0000 | [diff] [blame] | 563 | |
| 564 | |
| 565 | def test_pdb_run_with_incorrect_argument(): |
| 566 | """Testing run and runeval with incorrect first argument. |
| 567 | |
| 568 | >>> pti = PdbTestInput(['continue',]) |
| 569 | >>> with pti: |
| 570 | ... pdb_invoke('run', lambda x: x) |
| 571 | Traceback (most recent call last): |
| 572 | TypeError: exec() arg 1 must be a string, bytes or code object |
| 573 | |
| 574 | >>> with pti: |
| 575 | ... pdb_invoke('runeval', lambda x: x) |
| 576 | Traceback (most recent call last): |
| 577 | TypeError: eval() arg 1 must be a string, bytes or code object |
| 578 | """ |
| 579 | |
| 580 | |
| 581 | def test_pdb_run_with_code_object(): |
| 582 | """Testing run and runeval with code object as a first argument. |
| 583 | |
Georg Brandl | e1e8df1 | 2010-07-31 08:14:16 +0000 | [diff] [blame] | 584 | >>> with PdbTestInput(['step','x', 'continue']): # doctest: +ELLIPSIS |
Georg Brandl | 46b9afc | 2010-07-30 09:14:20 +0000 | [diff] [blame] | 585 | ... pdb_invoke('run', compile('x=1', '<string>', 'exec')) |
Georg Brandl | e1e8df1 | 2010-07-31 08:14:16 +0000 | [diff] [blame] | 586 | > <string>(1)<module>()... |
Georg Brandl | 46b9afc | 2010-07-30 09:14:20 +0000 | [diff] [blame] | 587 | (Pdb) step |
| 588 | --Return-- |
| 589 | > <string>(1)<module>()->None |
| 590 | (Pdb) x |
| 591 | 1 |
| 592 | (Pdb) continue |
| 593 | |
| 594 | >>> with PdbTestInput(['x', 'continue']): |
| 595 | ... x=0 |
| 596 | ... pdb_invoke('runeval', compile('x+1', '<string>', 'eval')) |
| 597 | > <string>(1)<module>()->None |
| 598 | (Pdb) x |
| 599 | 1 |
| 600 | (Pdb) continue |
| 601 | """ |
| 602 | |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 603 | def test_next_until_return_at_return_event(): |
| 604 | """Test that pdb stops after a next/until/return issued at a return debug event. |
| 605 | |
| 606 | >>> def test_function_2(): |
| 607 | ... x = 1 |
| 608 | ... x = 2 |
| 609 | |
| 610 | >>> def test_function(): |
| 611 | ... import pdb; pdb.Pdb(nosigint=True).set_trace() |
| 612 | ... test_function_2() |
| 613 | ... test_function_2() |
| 614 | ... test_function_2() |
| 615 | ... end = 1 |
| 616 | |
Antoine Pitrou | c04d468 | 2014-08-11 21:40:38 -0400 | [diff] [blame] | 617 | >>> from bdb import Breakpoint |
| 618 | >>> Breakpoint.next = 1 |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 619 | >>> with PdbTestInput(['break test_function_2', |
| 620 | ... 'continue', |
| 621 | ... 'return', |
| 622 | ... 'next', |
| 623 | ... 'continue', |
| 624 | ... 'return', |
| 625 | ... 'until', |
| 626 | ... 'continue', |
| 627 | ... 'return', |
| 628 | ... 'return', |
| 629 | ... 'continue']): |
| 630 | ... test_function() |
| 631 | > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(3)test_function() |
| 632 | -> test_function_2() |
| 633 | (Pdb) break test_function_2 |
| 634 | Breakpoint 1 at <doctest test.test_pdb.test_next_until_return_at_return_event[0]>:1 |
| 635 | (Pdb) continue |
| 636 | > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2() |
| 637 | -> x = 1 |
| 638 | (Pdb) return |
| 639 | --Return-- |
| 640 | > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(3)test_function_2()->None |
| 641 | -> x = 2 |
| 642 | (Pdb) next |
| 643 | > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(4)test_function() |
| 644 | -> test_function_2() |
| 645 | (Pdb) continue |
| 646 | > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2() |
| 647 | -> x = 1 |
| 648 | (Pdb) return |
| 649 | --Return-- |
| 650 | > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(3)test_function_2()->None |
| 651 | -> x = 2 |
| 652 | (Pdb) until |
| 653 | > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(5)test_function() |
| 654 | -> test_function_2() |
| 655 | (Pdb) continue |
| 656 | > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2() |
| 657 | -> x = 1 |
| 658 | (Pdb) return |
| 659 | --Return-- |
| 660 | > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(3)test_function_2()->None |
| 661 | -> x = 2 |
| 662 | (Pdb) return |
| 663 | > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(6)test_function() |
| 664 | -> end = 1 |
| 665 | (Pdb) continue |
| 666 | """ |
| 667 | |
| 668 | def test_pdb_next_command_for_generator(): |
| 669 | """Testing skip unwindng stack on yield for generators for "next" command |
| 670 | |
| 671 | >>> def test_gen(): |
| 672 | ... yield 0 |
| 673 | ... return 1 |
| 674 | ... yield 2 |
| 675 | |
| 676 | >>> def test_function(): |
| 677 | ... import pdb; pdb.Pdb(nosigint=True).set_trace() |
| 678 | ... it = test_gen() |
| 679 | ... try: |
Serhiy Storchaka | a16de5d | 2015-04-01 16:58:19 +0300 | [diff] [blame] | 680 | ... if next(it) != 0: |
| 681 | ... raise AssertionError |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 682 | ... next(it) |
| 683 | ... except StopIteration as ex: |
Serhiy Storchaka | a16de5d | 2015-04-01 16:58:19 +0300 | [diff] [blame] | 684 | ... if ex.value != 1: |
| 685 | ... raise AssertionError |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 686 | ... print("finished") |
| 687 | |
| 688 | >>> with PdbTestInput(['step', |
| 689 | ... 'step', |
| 690 | ... 'step', |
| 691 | ... 'next', |
| 692 | ... 'next', |
| 693 | ... 'step', |
| 694 | ... 'step', |
| 695 | ... 'continue']): |
| 696 | ... test_function() |
| 697 | > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(3)test_function() |
| 698 | -> it = test_gen() |
| 699 | (Pdb) step |
| 700 | > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(4)test_function() |
| 701 | -> try: |
| 702 | (Pdb) step |
| 703 | > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(5)test_function() |
Serhiy Storchaka | a16de5d | 2015-04-01 16:58:19 +0300 | [diff] [blame] | 704 | -> if next(it) != 0: |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 705 | (Pdb) step |
| 706 | --Call-- |
| 707 | > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(1)test_gen() |
| 708 | -> def test_gen(): |
| 709 | (Pdb) next |
| 710 | > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(2)test_gen() |
| 711 | -> yield 0 |
| 712 | (Pdb) next |
| 713 | > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(3)test_gen() |
| 714 | -> return 1 |
| 715 | (Pdb) step |
| 716 | --Return-- |
| 717 | > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(3)test_gen()->1 |
| 718 | -> return 1 |
| 719 | (Pdb) step |
| 720 | StopIteration: 1 |
Serhiy Storchaka | a16de5d | 2015-04-01 16:58:19 +0300 | [diff] [blame] | 721 | > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(7)test_function() |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 722 | -> next(it) |
| 723 | (Pdb) continue |
| 724 | finished |
| 725 | """ |
| 726 | |
| 727 | def test_pdb_return_command_for_generator(): |
| 728 | """Testing no unwindng stack on yield for generators |
| 729 | for "return" command |
| 730 | |
| 731 | >>> def test_gen(): |
| 732 | ... yield 0 |
| 733 | ... return 1 |
| 734 | ... yield 2 |
| 735 | |
| 736 | >>> def test_function(): |
| 737 | ... import pdb; pdb.Pdb(nosigint=True).set_trace() |
| 738 | ... it = test_gen() |
| 739 | ... try: |
Serhiy Storchaka | a16de5d | 2015-04-01 16:58:19 +0300 | [diff] [blame] | 740 | ... if next(it) != 0: |
| 741 | ... raise AssertionError |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 742 | ... next(it) |
| 743 | ... except StopIteration as ex: |
Serhiy Storchaka | a16de5d | 2015-04-01 16:58:19 +0300 | [diff] [blame] | 744 | ... if ex.value != 1: |
| 745 | ... raise AssertionError |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 746 | ... print("finished") |
| 747 | |
| 748 | >>> with PdbTestInput(['step', |
| 749 | ... 'step', |
| 750 | ... 'step', |
| 751 | ... 'return', |
| 752 | ... 'step', |
| 753 | ... 'step', |
| 754 | ... 'continue']): |
| 755 | ... test_function() |
| 756 | > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(3)test_function() |
| 757 | -> it = test_gen() |
| 758 | (Pdb) step |
| 759 | > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(4)test_function() |
| 760 | -> try: |
| 761 | (Pdb) step |
| 762 | > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(5)test_function() |
Serhiy Storchaka | a16de5d | 2015-04-01 16:58:19 +0300 | [diff] [blame] | 763 | -> if next(it) != 0: |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 764 | (Pdb) step |
| 765 | --Call-- |
| 766 | > <doctest test.test_pdb.test_pdb_return_command_for_generator[0]>(1)test_gen() |
| 767 | -> def test_gen(): |
| 768 | (Pdb) return |
| 769 | StopIteration: 1 |
Serhiy Storchaka | a16de5d | 2015-04-01 16:58:19 +0300 | [diff] [blame] | 770 | > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(7)test_function() |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 771 | -> next(it) |
| 772 | (Pdb) step |
Serhiy Storchaka | a16de5d | 2015-04-01 16:58:19 +0300 | [diff] [blame] | 773 | > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(8)test_function() |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 774 | -> except StopIteration as ex: |
| 775 | (Pdb) step |
Serhiy Storchaka | a16de5d | 2015-04-01 16:58:19 +0300 | [diff] [blame] | 776 | > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(9)test_function() |
| 777 | -> if ex.value != 1: |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 778 | (Pdb) continue |
| 779 | finished |
| 780 | """ |
| 781 | |
| 782 | def test_pdb_until_command_for_generator(): |
| 783 | """Testing no unwindng stack on yield for generators |
| 784 | for "until" command if target breakpoing is not reached |
| 785 | |
| 786 | >>> def test_gen(): |
| 787 | ... yield 0 |
| 788 | ... yield 1 |
| 789 | ... yield 2 |
| 790 | |
| 791 | >>> def test_function(): |
| 792 | ... import pdb; pdb.Pdb(nosigint=True).set_trace() |
| 793 | ... for i in test_gen(): |
| 794 | ... print(i) |
| 795 | ... print("finished") |
| 796 | |
| 797 | >>> with PdbTestInput(['step', |
| 798 | ... 'until 4', |
| 799 | ... 'step', |
| 800 | ... 'step', |
| 801 | ... 'continue']): |
| 802 | ... test_function() |
| 803 | > <doctest test.test_pdb.test_pdb_until_command_for_generator[1]>(3)test_function() |
| 804 | -> for i in test_gen(): |
| 805 | (Pdb) step |
| 806 | --Call-- |
| 807 | > <doctest test.test_pdb.test_pdb_until_command_for_generator[0]>(1)test_gen() |
| 808 | -> def test_gen(): |
| 809 | (Pdb) until 4 |
| 810 | 0 |
| 811 | 1 |
| 812 | > <doctest test.test_pdb.test_pdb_until_command_for_generator[0]>(4)test_gen() |
| 813 | -> yield 2 |
| 814 | (Pdb) step |
| 815 | --Return-- |
| 816 | > <doctest test.test_pdb.test_pdb_until_command_for_generator[0]>(4)test_gen()->2 |
| 817 | -> yield 2 |
| 818 | (Pdb) step |
| 819 | > <doctest test.test_pdb.test_pdb_until_command_for_generator[1]>(4)test_function() |
| 820 | -> print(i) |
| 821 | (Pdb) continue |
| 822 | 2 |
| 823 | finished |
| 824 | """ |
| 825 | |
| 826 | def test_pdb_next_command_in_generator_for_loop(): |
| 827 | """The next command on returning from a generator controled by a for loop. |
| 828 | |
| 829 | >>> def test_gen(): |
| 830 | ... yield 0 |
| 831 | ... return 1 |
| 832 | |
| 833 | >>> def test_function(): |
| 834 | ... import pdb; pdb.Pdb(nosigint=True).set_trace() |
| 835 | ... for i in test_gen(): |
| 836 | ... print('value', i) |
| 837 | ... x = 123 |
| 838 | |
| 839 | >>> with PdbTestInput(['break test_gen', |
| 840 | ... 'continue', |
| 841 | ... 'next', |
| 842 | ... 'next', |
| 843 | ... 'next', |
| 844 | ... 'continue']): |
| 845 | ... test_function() |
| 846 | > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(3)test_function() |
| 847 | -> for i in test_gen(): |
| 848 | (Pdb) break test_gen |
| 849 | Breakpoint 6 at <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>:1 |
| 850 | (Pdb) continue |
| 851 | > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>(2)test_gen() |
| 852 | -> yield 0 |
| 853 | (Pdb) next |
| 854 | value 0 |
| 855 | > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>(3)test_gen() |
| 856 | -> return 1 |
| 857 | (Pdb) next |
| 858 | Internal StopIteration: 1 |
| 859 | > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(3)test_function() |
| 860 | -> for i in test_gen(): |
| 861 | (Pdb) next |
| 862 | > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(5)test_function() |
| 863 | -> x = 123 |
| 864 | (Pdb) continue |
| 865 | """ |
| 866 | |
| 867 | def test_pdb_next_command_subiterator(): |
| 868 | """The next command in a generator with a subiterator. |
| 869 | |
| 870 | >>> def test_subgenerator(): |
| 871 | ... yield 0 |
| 872 | ... return 1 |
| 873 | |
| 874 | >>> def test_gen(): |
| 875 | ... x = yield from test_subgenerator() |
| 876 | ... return x |
| 877 | |
| 878 | >>> def test_function(): |
| 879 | ... import pdb; pdb.Pdb(nosigint=True).set_trace() |
| 880 | ... for i in test_gen(): |
| 881 | ... print('value', i) |
| 882 | ... x = 123 |
| 883 | |
| 884 | >>> with PdbTestInput(['step', |
| 885 | ... 'step', |
| 886 | ... 'next', |
| 887 | ... 'next', |
| 888 | ... 'next', |
| 889 | ... 'continue']): |
| 890 | ... test_function() |
| 891 | > <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(3)test_function() |
| 892 | -> for i in test_gen(): |
| 893 | (Pdb) step |
| 894 | --Call-- |
| 895 | > <doctest test.test_pdb.test_pdb_next_command_subiterator[1]>(1)test_gen() |
| 896 | -> def test_gen(): |
| 897 | (Pdb) step |
| 898 | > <doctest test.test_pdb.test_pdb_next_command_subiterator[1]>(2)test_gen() |
| 899 | -> x = yield from test_subgenerator() |
| 900 | (Pdb) next |
| 901 | value 0 |
| 902 | > <doctest test.test_pdb.test_pdb_next_command_subiterator[1]>(3)test_gen() |
| 903 | -> return x |
| 904 | (Pdb) next |
| 905 | Internal StopIteration: 1 |
| 906 | > <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(3)test_function() |
| 907 | -> for i in test_gen(): |
| 908 | (Pdb) next |
| 909 | > <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(5)test_function() |
| 910 | -> x = 123 |
| 911 | (Pdb) continue |
| 912 | """ |
| 913 | |
Georg Brandl | 46b9afc | 2010-07-30 09:14:20 +0000 | [diff] [blame] | 914 | |
Georg Brandl | 6cccb86 | 2010-07-30 14:16:43 +0000 | [diff] [blame] | 915 | class PdbTestCase(unittest.TestCase): |
| 916 | |
Senthil Kumaran | 42d7081 | 2012-05-01 10:07:49 +0800 | [diff] [blame] | 917 | def run_pdb(self, script, commands): |
| 918 | """Run 'script' lines with pdb and the pdb 'commands'.""" |
| 919 | filename = 'main.py' |
| 920 | with open(filename, 'w') as f: |
| 921 | f.write(textwrap.dedent(script)) |
Georg Brandl | 4bde9ca | 2012-05-01 09:21:16 +0200 | [diff] [blame] | 922 | self.addCleanup(support.unlink, filename) |
Victor Stinner | 047b7ae | 2014-10-05 17:37:41 +0200 | [diff] [blame] | 923 | self.addCleanup(support.rmtree, '__pycache__') |
Senthil Kumaran | 42d7081 | 2012-05-01 10:07:49 +0800 | [diff] [blame] | 924 | cmd = [sys.executable, '-m', 'pdb', filename] |
| 925 | stdout = stderr = None |
| 926 | with subprocess.Popen(cmd, stdout=subprocess.PIPE, |
| 927 | stdin=subprocess.PIPE, |
| 928 | stderr=subprocess.STDOUT, |
| 929 | ) as proc: |
| 930 | stdout, stderr = proc.communicate(str.encode(commands)) |
| 931 | stdout = stdout and bytes.decode(stdout) |
| 932 | stderr = stderr and bytes.decode(stderr) |
| 933 | return stdout, stderr |
| 934 | |
Georg Brandl | 6e22055 | 2013-10-13 20:51:47 +0200 | [diff] [blame] | 935 | def _assert_find_function(self, file_content, func_name, expected): |
| 936 | file_content = textwrap.dedent(file_content) |
| 937 | |
| 938 | with open(support.TESTFN, 'w') as f: |
| 939 | f.write(file_content) |
| 940 | |
| 941 | expected = None if not expected else ( |
| 942 | expected[0], support.TESTFN, expected[1]) |
| 943 | self.assertEqual( |
| 944 | expected, pdb.find_function(func_name, support.TESTFN)) |
| 945 | |
| 946 | def test_find_function_empty_file(self): |
| 947 | self._assert_find_function('', 'foo', None) |
| 948 | |
| 949 | def test_find_function_found(self): |
| 950 | self._assert_find_function( |
| 951 | """\ |
| 952 | def foo(): |
| 953 | pass |
| 954 | |
| 955 | def bar(): |
| 956 | pass |
| 957 | |
| 958 | def quux(): |
| 959 | pass |
| 960 | """, |
| 961 | 'bar', |
| 962 | ('bar', 4), |
| 963 | ) |
| 964 | |
Georg Brandl | 6cccb86 | 2010-07-30 14:16:43 +0000 | [diff] [blame] | 965 | def test_issue7964(self): |
| 966 | # open the file as binary so we can force \r\n newline |
| 967 | with open(support.TESTFN, 'wb') as f: |
| 968 | f.write(b'print("testing my pdb")\r\n') |
| 969 | cmd = [sys.executable, '-m', 'pdb', support.TESTFN] |
| 970 | proc = subprocess.Popen(cmd, |
| 971 | stdout=subprocess.PIPE, |
| 972 | stdin=subprocess.PIPE, |
| 973 | stderr=subprocess.STDOUT, |
| 974 | ) |
Brian Curtin | 994ad6c | 2010-11-05 15:38:47 +0000 | [diff] [blame] | 975 | self.addCleanup(proc.stdout.close) |
Georg Brandl | 6cccb86 | 2010-07-30 14:16:43 +0000 | [diff] [blame] | 976 | stdout, stderr = proc.communicate(b'quit\n') |
| 977 | self.assertNotIn(b'SyntaxError', stdout, |
| 978 | "Got a syntax error running test script under PDB") |
| 979 | |
Senthil Kumaran | 42d7081 | 2012-05-01 10:07:49 +0800 | [diff] [blame] | 980 | def test_issue13183(self): |
| 981 | script = """ |
| 982 | from bar import bar |
| 983 | |
| 984 | def foo(): |
| 985 | bar() |
| 986 | |
| 987 | def nope(): |
| 988 | pass |
| 989 | |
| 990 | def foobar(): |
| 991 | foo() |
| 992 | nope() |
| 993 | |
| 994 | foobar() |
| 995 | """ |
| 996 | commands = """ |
| 997 | from bar import bar |
| 998 | break bar |
| 999 | continue |
| 1000 | step |
| 1001 | step |
| 1002 | quit |
| 1003 | """ |
| 1004 | bar = """ |
| 1005 | def bar(): |
Senthil Kumaran | cb17204 | 2012-05-02 08:00:22 +0800 | [diff] [blame] | 1006 | pass |
Senthil Kumaran | 42d7081 | 2012-05-01 10:07:49 +0800 | [diff] [blame] | 1007 | """ |
| 1008 | with open('bar.py', 'w') as f: |
| 1009 | f.write(textwrap.dedent(bar)) |
Georg Brandl | 4bde9ca | 2012-05-01 09:21:16 +0200 | [diff] [blame] | 1010 | self.addCleanup(support.unlink, 'bar.py') |
Senthil Kumaran | 42d7081 | 2012-05-01 10:07:49 +0800 | [diff] [blame] | 1011 | stdout, stderr = self.run_pdb(script, commands) |
Georg Brandl | 4bde9ca | 2012-05-01 09:21:16 +0200 | [diff] [blame] | 1012 | self.assertTrue( |
| 1013 | any('main.py(5)foo()->None' in l for l in stdout.splitlines()), |
| 1014 | 'Fail to step into the caller after a return') |
Senthil Kumaran | 42d7081 | 2012-05-01 10:07:49 +0800 | [diff] [blame] | 1015 | |
Andrew Svetlov | 539ee5d | 2012-12-04 21:08:28 +0200 | [diff] [blame] | 1016 | def test_issue13210(self): |
| 1017 | # invoking "continue" on a non-main thread triggered an exception |
| 1018 | # inside signal.signal |
| 1019 | |
Andrew Svetlov | 96bc043 | 2012-12-05 15:06:23 +0200 | [diff] [blame] | 1020 | # raises SkipTest if python was built without threads |
| 1021 | support.import_module('threading') |
| 1022 | |
Andrew Svetlov | 539ee5d | 2012-12-04 21:08:28 +0200 | [diff] [blame] | 1023 | with open(support.TESTFN, 'wb') as f: |
| 1024 | f.write(textwrap.dedent(""" |
| 1025 | import threading |
| 1026 | import pdb |
| 1027 | |
| 1028 | def start_pdb(): |
| 1029 | pdb.Pdb().set_trace() |
| 1030 | x = 1 |
| 1031 | y = 1 |
| 1032 | |
| 1033 | t = threading.Thread(target=start_pdb) |
| 1034 | t.start()""").encode('ascii')) |
| 1035 | cmd = [sys.executable, '-u', support.TESTFN] |
| 1036 | proc = subprocess.Popen(cmd, |
| 1037 | stdout=subprocess.PIPE, |
| 1038 | stdin=subprocess.PIPE, |
| 1039 | stderr=subprocess.STDOUT, |
| 1040 | ) |
| 1041 | self.addCleanup(proc.stdout.close) |
| 1042 | stdout, stderr = proc.communicate(b'cont\n') |
| 1043 | self.assertNotIn('Error', stdout.decode(), |
| 1044 | "Got an error running test script under PDB") |
| 1045 | |
Terry Jan Reedy | ca3f435 | 2015-09-05 19:13:26 -0400 | [diff] [blame] | 1046 | def test_issue16180(self): |
| 1047 | # A syntax error in the debuggee. |
| 1048 | script = "def f: pass\n" |
| 1049 | commands = '' |
| 1050 | expected = "SyntaxError:" |
| 1051 | stdout, stderr = self.run_pdb(script, commands) |
| 1052 | self.assertIn(expected, stdout, |
| 1053 | '\n\nExpected:\n{}\nGot:\n{}\n' |
| 1054 | 'Fail to handle a syntax error in the debuggee.' |
| 1055 | .format(expected, stdout)) |
| 1056 | |
| 1057 | |
Georg Brandl | 6cccb86 | 2010-07-30 14:16:43 +0000 | [diff] [blame] | 1058 | def tearDown(self): |
| 1059 | support.unlink(support.TESTFN) |
| 1060 | |
| 1061 | |
Andrew Svetlov | f0efea0 | 2013-03-18 10:09:50 -0700 | [diff] [blame] | 1062 | def load_tests(*args): |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 1063 | from test import test_pdb |
Andrew Svetlov | f0efea0 | 2013-03-18 10:09:50 -0700 | [diff] [blame] | 1064 | suites = [unittest.makeSuite(PdbTestCase), doctest.DocTestSuite(test_pdb)] |
| 1065 | return unittest.TestSuite(suites) |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 1066 | |
| 1067 | |
| 1068 | if __name__ == '__main__': |
Andrew Svetlov | f0efea0 | 2013-03-18 10:09:50 -0700 | [diff] [blame] | 1069 | unittest.main() |