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