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