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 |
Brett Cannon | 9529fbf | 2013-06-15 17:11:25 -0400 | [diff] [blame] | 7 | import types |
Georg Brandl | 6cccb86 | 2010-07-30 14:16:43 +0000 | [diff] [blame] | 8 | import unittest |
| 9 | import subprocess |
Senthil Kumaran | 42d7081 | 2012-05-01 10:07:49 +0800 | [diff] [blame] | 10 | import textwrap |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 11 | |
Barry Warsaw | 35425d6 | 2017-09-22 12:29:42 -0400 | [diff] [blame] | 12 | from contextlib import ExitStack |
| 13 | from io import StringIO |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 14 | from test import support |
| 15 | # This little helper class is essential for testing pdb under doctest. |
| 16 | from test.test_doctest import _FakeInput |
Barry Warsaw | 35425d6 | 2017-09-22 12:29:42 -0400 | [diff] [blame] | 17 | from unittest.mock import patch |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 18 | |
| 19 | |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 20 | class PdbTestInput(object): |
| 21 | """Context manager that makes testing Pdb in doctests easier.""" |
| 22 | |
| 23 | def __init__(self, input): |
| 24 | self.input = input |
| 25 | |
| 26 | def __enter__(self): |
| 27 | self.real_stdin = sys.stdin |
| 28 | sys.stdin = _FakeInput(self.input) |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 29 | self.orig_trace = sys.gettrace() if hasattr(sys, 'gettrace') else None |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 30 | |
| 31 | def __exit__(self, *exc): |
| 32 | sys.stdin = self.real_stdin |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 33 | if self.orig_trace: |
| 34 | sys.settrace(self.orig_trace) |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 35 | |
| 36 | |
| 37 | def test_pdb_displayhook(): |
| 38 | """This tests the custom displayhook for pdb. |
| 39 | |
| 40 | >>> def test_function(foo, bar): |
Łukasz Langa | 2eb6eca | 2016-09-09 22:21:17 -0700 | [diff] [blame] | 41 | ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 42 | ... pass |
| 43 | |
| 44 | >>> with PdbTestInput([ |
| 45 | ... 'foo', |
| 46 | ... 'bar', |
| 47 | ... 'for i in range(5): print(i)', |
| 48 | ... 'continue', |
| 49 | ... ]): |
| 50 | ... test_function(1, None) |
| 51 | > <doctest test.test_pdb.test_pdb_displayhook[0]>(3)test_function() |
| 52 | -> pass |
| 53 | (Pdb) foo |
| 54 | 1 |
| 55 | (Pdb) bar |
| 56 | (Pdb) for i in range(5): print(i) |
| 57 | 0 |
| 58 | 1 |
| 59 | 2 |
| 60 | 3 |
| 61 | 4 |
| 62 | (Pdb) continue |
| 63 | """ |
| 64 | |
| 65 | |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 66 | def test_pdb_basic_commands(): |
| 67 | """Test the basic commands of pdb. |
| 68 | |
| 69 | >>> def test_function_2(foo, bar='default'): |
| 70 | ... print(foo) |
| 71 | ... for i in range(5): |
| 72 | ... print(i) |
| 73 | ... print(bar) |
| 74 | ... for i in range(10): |
| 75 | ... never_executed |
| 76 | ... print('after for') |
| 77 | ... print('...') |
| 78 | ... return foo.upper() |
| 79 | |
Rémi Lapeyre | bf457c7 | 2019-05-21 00:17:30 +0200 | [diff] [blame] | 80 | >>> def test_function3(arg=None, *, kwonly=None): |
| 81 | ... pass |
| 82 | |
Rémi Lapeyre | 4585603 | 2019-05-24 22:44:31 +0200 | [diff] [blame] | 83 | >>> def test_function4(a, b, c, /): |
| 84 | ... pass |
| 85 | |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 86 | >>> def test_function(): |
Łukasz Langa | 2eb6eca | 2016-09-09 22:21:17 -0700 | [diff] [blame] | 87 | ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 88 | ... ret = test_function_2('baz') |
Rémi Lapeyre | bf457c7 | 2019-05-21 00:17:30 +0200 | [diff] [blame] | 89 | ... test_function3(kwonly=True) |
Rémi Lapeyre | 4585603 | 2019-05-24 22:44:31 +0200 | [diff] [blame] | 90 | ... test_function4(1, 2, 3) |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 91 | ... print(ret) |
| 92 | |
| 93 | >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
| 94 | ... 'step', # entering the function call |
| 95 | ... 'args', # display function args |
| 96 | ... 'list', # list function source |
| 97 | ... 'bt', # display backtrace |
| 98 | ... 'up', # step up to test_function() |
| 99 | ... 'down', # step down to test_function_2() again |
| 100 | ... 'next', # stepping to print(foo) |
| 101 | ... 'next', # stepping to the for loop |
| 102 | ... 'step', # stepping into the for loop |
| 103 | ... 'until', # continuing until out of the for loop |
| 104 | ... 'next', # executing the print(bar) |
| 105 | ... 'jump 8', # jump over second for loop |
| 106 | ... 'return', # return out of function |
| 107 | ... 'retval', # display return value |
Rémi Lapeyre | bf457c7 | 2019-05-21 00:17:30 +0200 | [diff] [blame] | 108 | ... 'next', # step to test_function3() |
| 109 | ... 'step', # stepping into test_function3() |
| 110 | ... 'args', # display function args |
Rémi Lapeyre | 4585603 | 2019-05-24 22:44:31 +0200 | [diff] [blame] | 111 | ... 'return', # return out of function |
| 112 | ... 'next', # step to test_function4() |
| 113 | ... 'step', # stepping to test_function4() |
| 114 | ... 'args', # display function args |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 115 | ... 'continue', |
| 116 | ... ]): |
| 117 | ... test_function() |
Rémi Lapeyre | 4585603 | 2019-05-24 22:44:31 +0200 | [diff] [blame] | 118 | > <doctest test.test_pdb.test_pdb_basic_commands[3]>(3)test_function() |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 119 | -> ret = test_function_2('baz') |
| 120 | (Pdb) step |
| 121 | --Call-- |
| 122 | > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2() |
| 123 | -> def test_function_2(foo, bar='default'): |
| 124 | (Pdb) args |
| 125 | foo = 'baz' |
| 126 | bar = 'default' |
| 127 | (Pdb) list |
| 128 | 1 -> def test_function_2(foo, bar='default'): |
| 129 | 2 print(foo) |
| 130 | 3 for i in range(5): |
| 131 | 4 print(i) |
| 132 | 5 print(bar) |
| 133 | 6 for i in range(10): |
| 134 | 7 never_executed |
| 135 | 8 print('after for') |
| 136 | 9 print('...') |
| 137 | 10 return foo.upper() |
| 138 | [EOF] |
| 139 | (Pdb) bt |
| 140 | ... |
Rémi Lapeyre | 4585603 | 2019-05-24 22:44:31 +0200 | [diff] [blame] | 141 | <doctest test.test_pdb.test_pdb_basic_commands[4]>(25)<module>() |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 142 | -> test_function() |
Rémi Lapeyre | 4585603 | 2019-05-24 22:44:31 +0200 | [diff] [blame] | 143 | <doctest test.test_pdb.test_pdb_basic_commands[3]>(3)test_function() |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 144 | -> ret = test_function_2('baz') |
| 145 | > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2() |
| 146 | -> def test_function_2(foo, bar='default'): |
| 147 | (Pdb) up |
Rémi Lapeyre | 4585603 | 2019-05-24 22:44:31 +0200 | [diff] [blame] | 148 | > <doctest test.test_pdb.test_pdb_basic_commands[3]>(3)test_function() |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 149 | -> ret = test_function_2('baz') |
| 150 | (Pdb) down |
| 151 | > <doctest test.test_pdb.test_pdb_basic_commands[0]>(1)test_function_2() |
| 152 | -> def test_function_2(foo, bar='default'): |
| 153 | (Pdb) next |
| 154 | > <doctest test.test_pdb.test_pdb_basic_commands[0]>(2)test_function_2() |
| 155 | -> print(foo) |
| 156 | (Pdb) next |
| 157 | baz |
| 158 | > <doctest test.test_pdb.test_pdb_basic_commands[0]>(3)test_function_2() |
| 159 | -> for i in range(5): |
| 160 | (Pdb) step |
| 161 | > <doctest test.test_pdb.test_pdb_basic_commands[0]>(4)test_function_2() |
| 162 | -> print(i) |
| 163 | (Pdb) until |
| 164 | 0 |
| 165 | 1 |
| 166 | 2 |
| 167 | 3 |
| 168 | 4 |
| 169 | > <doctest test.test_pdb.test_pdb_basic_commands[0]>(5)test_function_2() |
| 170 | -> print(bar) |
| 171 | (Pdb) next |
| 172 | default |
| 173 | > <doctest test.test_pdb.test_pdb_basic_commands[0]>(6)test_function_2() |
| 174 | -> for i in range(10): |
| 175 | (Pdb) jump 8 |
| 176 | > <doctest test.test_pdb.test_pdb_basic_commands[0]>(8)test_function_2() |
| 177 | -> print('after for') |
| 178 | (Pdb) return |
| 179 | after for |
| 180 | ... |
| 181 | --Return-- |
| 182 | > <doctest test.test_pdb.test_pdb_basic_commands[0]>(10)test_function_2()->'BAZ' |
| 183 | -> return foo.upper() |
| 184 | (Pdb) retval |
| 185 | 'BAZ' |
Rémi Lapeyre | bf457c7 | 2019-05-21 00:17:30 +0200 | [diff] [blame] | 186 | (Pdb) next |
Rémi Lapeyre | 4585603 | 2019-05-24 22:44:31 +0200 | [diff] [blame] | 187 | > <doctest test.test_pdb.test_pdb_basic_commands[3]>(4)test_function() |
Rémi Lapeyre | bf457c7 | 2019-05-21 00:17:30 +0200 | [diff] [blame] | 188 | -> test_function3(kwonly=True) |
| 189 | (Pdb) step |
| 190 | --Call-- |
| 191 | > <doctest test.test_pdb.test_pdb_basic_commands[1]>(1)test_function3() |
| 192 | -> def test_function3(arg=None, *, kwonly=None): |
| 193 | (Pdb) args |
| 194 | arg = None |
| 195 | kwonly = True |
Rémi Lapeyre | 4585603 | 2019-05-24 22:44:31 +0200 | [diff] [blame] | 196 | (Pdb) return |
| 197 | --Return-- |
| 198 | > <doctest test.test_pdb.test_pdb_basic_commands[1]>(2)test_function3()->None |
| 199 | -> pass |
| 200 | (Pdb) next |
| 201 | > <doctest test.test_pdb.test_pdb_basic_commands[3]>(5)test_function() |
| 202 | -> test_function4(1, 2, 3) |
| 203 | (Pdb) step |
| 204 | --Call-- |
| 205 | > <doctest test.test_pdb.test_pdb_basic_commands[2]>(1)test_function4() |
| 206 | -> def test_function4(a, b, c, /): |
| 207 | (Pdb) args |
| 208 | a = 1 |
| 209 | b = 2 |
| 210 | c = 3 |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 211 | (Pdb) continue |
| 212 | BAZ |
| 213 | """ |
| 214 | |
| 215 | |
| 216 | def test_pdb_breakpoint_commands(): |
| 217 | """Test basic commands related to breakpoints. |
| 218 | |
| 219 | >>> def test_function(): |
Łukasz Langa | 2eb6eca | 2016-09-09 22:21:17 -0700 | [diff] [blame] | 220 | ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 221 | ... print(1) |
| 222 | ... print(2) |
| 223 | ... print(3) |
| 224 | ... print(4) |
| 225 | |
| 226 | First, need to clear bdb state that might be left over from previous tests. |
| 227 | Otherwise, the new breakpoints might get assigned different numbers. |
| 228 | |
| 229 | >>> from bdb import Breakpoint |
| 230 | >>> Breakpoint.next = 1 |
| 231 | >>> Breakpoint.bplist = {} |
| 232 | >>> Breakpoint.bpbynumber = [None] |
| 233 | |
| 234 | Now test the breakpoint commands. NORMALIZE_WHITESPACE is needed because |
| 235 | the breakpoint list outputs a tab for the "stop only" and "ignore next" |
| 236 | lines, which we don't want to put in here. |
| 237 | |
| 238 | >>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE |
| 239 | ... 'break 3', |
| 240 | ... 'disable 1', |
| 241 | ... 'ignore 1 10', |
| 242 | ... 'condition 1 1 < 2', |
| 243 | ... 'break 4', |
Senthil Kumaran | 6f10704 | 2010-11-29 11:54:17 +0000 | [diff] [blame] | 244 | ... 'break 4', |
| 245 | ... 'break', |
| 246 | ... 'clear 3', |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 247 | ... 'break', |
| 248 | ... 'condition 1', |
| 249 | ... 'enable 1', |
| 250 | ... 'clear 1', |
| 251 | ... 'commands 2', |
R David Murray | 78d692f | 2013-10-10 17:23:26 -0400 | [diff] [blame] | 252 | ... 'p "42"', |
| 253 | ... 'print("42", 7*6)', # Issue 18764 (not about breakpoints) |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 254 | ... 'end', |
| 255 | ... 'continue', # will stop at breakpoint 2 (line 4) |
| 256 | ... 'clear', # clear all! |
| 257 | ... 'y', |
| 258 | ... 'tbreak 5', |
| 259 | ... 'continue', # will stop at temporary breakpoint |
| 260 | ... 'break', # make sure breakpoint is gone |
| 261 | ... 'continue', |
| 262 | ... ]): |
| 263 | ... test_function() |
| 264 | > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(3)test_function() |
| 265 | -> print(1) |
| 266 | (Pdb) break 3 |
| 267 | Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 |
| 268 | (Pdb) disable 1 |
| 269 | Disabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 |
| 270 | (Pdb) ignore 1 10 |
| 271 | Will ignore next 10 crossings of breakpoint 1. |
| 272 | (Pdb) condition 1 1 < 2 |
| 273 | New condition set for breakpoint 1. |
| 274 | (Pdb) break 4 |
| 275 | 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] | 276 | (Pdb) break 4 |
| 277 | Breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 |
| 278 | (Pdb) break |
| 279 | Num Type Disp Enb Where |
| 280 | 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 |
| 281 | stop only if 1 < 2 |
| 282 | ignore next 10 hits |
| 283 | 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 |
| 284 | 3 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 |
| 285 | (Pdb) clear 3 |
| 286 | 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] | 287 | (Pdb) break |
| 288 | Num Type Disp Enb Where |
| 289 | 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 |
| 290 | stop only if 1 < 2 |
| 291 | ignore next 10 hits |
| 292 | 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 |
| 293 | (Pdb) condition 1 |
| 294 | Breakpoint 1 is now unconditional. |
| 295 | (Pdb) enable 1 |
| 296 | Enabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 |
| 297 | (Pdb) clear 1 |
| 298 | Deleted breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 |
| 299 | (Pdb) commands 2 |
R David Murray | 78d692f | 2013-10-10 17:23:26 -0400 | [diff] [blame] | 300 | (com) p "42" |
| 301 | (com) print("42", 7*6) |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 302 | (com) end |
| 303 | (Pdb) continue |
| 304 | 1 |
R David Murray | 78d692f | 2013-10-10 17:23:26 -0400 | [diff] [blame] | 305 | '42' |
| 306 | 42 42 |
Georg Brandl | 0d08962 | 2010-07-30 16:00:46 +0000 | [diff] [blame] | 307 | > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(4)test_function() |
| 308 | -> print(2) |
| 309 | (Pdb) clear |
| 310 | Clear all breaks? y |
| 311 | Deleted breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 |
| 312 | (Pdb) tbreak 5 |
Senthil Kumaran | 6f10704 | 2010-11-29 11:54:17 +0000 | [diff] [blame] | 313 | 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] | 314 | (Pdb) continue |
| 315 | 2 |
Senthil Kumaran | 6f10704 | 2010-11-29 11:54:17 +0000 | [diff] [blame] | 316 | 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] | 317 | > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(5)test_function() |
| 318 | -> print(3) |
| 319 | (Pdb) break |
| 320 | (Pdb) continue |
| 321 | 3 |
| 322 | 4 |
| 323 | """ |
| 324 | |
| 325 | |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 326 | def do_nothing(): |
| 327 | pass |
| 328 | |
| 329 | def do_something(): |
| 330 | print(42) |
| 331 | |
| 332 | def test_list_commands(): |
| 333 | """Test the list and source commands of pdb. |
| 334 | |
| 335 | >>> def test_function_2(foo): |
Georg Brandl | a8fbc6a | 2010-07-31 11:52:46 +0000 | [diff] [blame] | 336 | ... import test.test_pdb |
| 337 | ... test.test_pdb.do_nothing() |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 338 | ... 'some...' |
| 339 | ... 'more...' |
| 340 | ... 'code...' |
| 341 | ... 'to...' |
| 342 | ... 'make...' |
| 343 | ... 'a...' |
| 344 | ... 'long...' |
| 345 | ... 'listing...' |
| 346 | ... 'useful...' |
| 347 | ... '...' |
| 348 | ... '...' |
| 349 | ... return foo |
| 350 | |
| 351 | >>> def test_function(): |
Łukasz Langa | 2eb6eca | 2016-09-09 22:21:17 -0700 | [diff] [blame] | 352 | ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 353 | ... ret = test_function_2('baz') |
| 354 | |
| 355 | >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
| 356 | ... 'list', # list first function |
| 357 | ... 'step', # step into second function |
| 358 | ... 'list', # list second function |
| 359 | ... 'list', # continue listing to EOF |
| 360 | ... 'list 1,3', # list specific lines |
| 361 | ... 'list x', # invalid argument |
| 362 | ... 'next', # step to import |
| 363 | ... 'next', # step over import |
| 364 | ... 'step', # step into do_nothing |
| 365 | ... 'longlist', # list all lines |
| 366 | ... 'source do_something', # list all lines of function |
Georg Brandl | cdf66a9 | 2010-07-30 18:15:16 +0000 | [diff] [blame] | 367 | ... 'source fooxxx', # something that doesn't exit |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 368 | ... 'continue', |
| 369 | ... ]): |
| 370 | ... test_function() |
| 371 | > <doctest test.test_pdb.test_list_commands[1]>(3)test_function() |
| 372 | -> ret = test_function_2('baz') |
| 373 | (Pdb) list |
| 374 | 1 def test_function(): |
Łukasz Langa | 2eb6eca | 2016-09-09 22:21:17 -0700 | [diff] [blame] | 375 | 2 import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 376 | 3 -> ret = test_function_2('baz') |
| 377 | [EOF] |
| 378 | (Pdb) step |
| 379 | --Call-- |
| 380 | > <doctest test.test_pdb.test_list_commands[0]>(1)test_function_2() |
| 381 | -> def test_function_2(foo): |
| 382 | (Pdb) list |
| 383 | 1 -> def test_function_2(foo): |
Georg Brandl | a8fbc6a | 2010-07-31 11:52:46 +0000 | [diff] [blame] | 384 | 2 import test.test_pdb |
| 385 | 3 test.test_pdb.do_nothing() |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 386 | 4 'some...' |
| 387 | 5 'more...' |
| 388 | 6 'code...' |
| 389 | 7 'to...' |
| 390 | 8 'make...' |
| 391 | 9 'a...' |
| 392 | 10 'long...' |
| 393 | 11 'listing...' |
| 394 | (Pdb) list |
| 395 | 12 'useful...' |
| 396 | 13 '...' |
| 397 | 14 '...' |
| 398 | 15 return foo |
| 399 | [EOF] |
| 400 | (Pdb) list 1,3 |
| 401 | 1 -> def test_function_2(foo): |
Georg Brandl | a8fbc6a | 2010-07-31 11:52:46 +0000 | [diff] [blame] | 402 | 2 import test.test_pdb |
| 403 | 3 test.test_pdb.do_nothing() |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 404 | (Pdb) list x |
| 405 | *** ... |
| 406 | (Pdb) next |
| 407 | > <doctest test.test_pdb.test_list_commands[0]>(2)test_function_2() |
Georg Brandl | a8fbc6a | 2010-07-31 11:52:46 +0000 | [diff] [blame] | 408 | -> import test.test_pdb |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 409 | (Pdb) next |
| 410 | > <doctest test.test_pdb.test_list_commands[0]>(3)test_function_2() |
Georg Brandl | a8fbc6a | 2010-07-31 11:52:46 +0000 | [diff] [blame] | 411 | -> test.test_pdb.do_nothing() |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 412 | (Pdb) step |
| 413 | --Call-- |
Georg Brandl | e1e8df1 | 2010-07-31 08:14:16 +0000 | [diff] [blame] | 414 | > ...test_pdb.py(...)do_nothing() |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 415 | -> def do_nothing(): |
| 416 | (Pdb) longlist |
| 417 | ... -> def do_nothing(): |
| 418 | ... pass |
| 419 | (Pdb) source do_something |
| 420 | ... def do_something(): |
| 421 | ... print(42) |
Georg Brandl | cdf66a9 | 2010-07-30 18:15:16 +0000 | [diff] [blame] | 422 | (Pdb) source fooxxx |
| 423 | *** ... |
Georg Brandl | e59ca2a | 2010-07-30 17:04:28 +0000 | [diff] [blame] | 424 | (Pdb) continue |
| 425 | """ |
| 426 | |
| 427 | |
Georg Brandl | 0a9c3e9 | 2010-07-30 18:46:38 +0000 | [diff] [blame] | 428 | def test_post_mortem(): |
| 429 | """Test post mortem traceback debugging. |
| 430 | |
| 431 | >>> def test_function_2(): |
| 432 | ... try: |
| 433 | ... 1/0 |
| 434 | ... finally: |
| 435 | ... print('Exception!') |
| 436 | |
| 437 | >>> def test_function(): |
Łukasz Langa | 2eb6eca | 2016-09-09 22:21:17 -0700 | [diff] [blame] | 438 | ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
Georg Brandl | 0a9c3e9 | 2010-07-30 18:46:38 +0000 | [diff] [blame] | 439 | ... test_function_2() |
| 440 | ... print('Not reached.') |
| 441 | |
| 442 | >>> with PdbTestInput([ # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
| 443 | ... 'next', # step over exception-raising call |
| 444 | ... 'bt', # get a backtrace |
| 445 | ... 'list', # list code of test_function() |
| 446 | ... 'down', # step into test_function_2() |
| 447 | ... 'list', # list code of test_function_2() |
| 448 | ... 'continue', |
| 449 | ... ]): |
| 450 | ... try: |
| 451 | ... test_function() |
| 452 | ... except ZeroDivisionError: |
| 453 | ... print('Correctly reraised.') |
| 454 | > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function() |
| 455 | -> test_function_2() |
| 456 | (Pdb) next |
| 457 | Exception! |
| 458 | ZeroDivisionError: division by zero |
| 459 | > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function() |
| 460 | -> test_function_2() |
| 461 | (Pdb) bt |
| 462 | ... |
| 463 | <doctest test.test_pdb.test_post_mortem[2]>(10)<module>() |
| 464 | -> test_function() |
| 465 | > <doctest test.test_pdb.test_post_mortem[1]>(3)test_function() |
| 466 | -> test_function_2() |
| 467 | <doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2() |
| 468 | -> 1/0 |
| 469 | (Pdb) list |
| 470 | 1 def test_function(): |
Łukasz Langa | 2eb6eca | 2016-09-09 22:21:17 -0700 | [diff] [blame] | 471 | 2 import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
Georg Brandl | 0a9c3e9 | 2010-07-30 18:46:38 +0000 | [diff] [blame] | 472 | 3 -> test_function_2() |
| 473 | 4 print('Not reached.') |
| 474 | [EOF] |
| 475 | (Pdb) down |
| 476 | > <doctest test.test_pdb.test_post_mortem[0]>(3)test_function_2() |
| 477 | -> 1/0 |
| 478 | (Pdb) list |
| 479 | 1 def test_function_2(): |
| 480 | 2 try: |
| 481 | 3 >> 1/0 |
| 482 | 4 finally: |
| 483 | 5 -> print('Exception!') |
| 484 | [EOF] |
| 485 | (Pdb) continue |
| 486 | Correctly reraised. |
| 487 | """ |
| 488 | |
| 489 | |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 490 | def test_pdb_skip_modules(): |
| 491 | """This illustrates the simple case of module skipping. |
| 492 | |
| 493 | >>> def skip_module(): |
| 494 | ... import string |
Łukasz Langa | 2eb6eca | 2016-09-09 22:21:17 -0700 | [diff] [blame] | 495 | ... import pdb; pdb.Pdb(skip=['stri*'], nosigint=True, readrc=False).set_trace() |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 496 | ... string.capwords('FOO') |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 497 | |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 498 | >>> with PdbTestInput([ |
| 499 | ... 'step', |
| 500 | ... 'continue', |
| 501 | ... ]): |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 502 | ... skip_module() |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 503 | > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module() |
| 504 | -> string.capwords('FOO') |
| 505 | (Pdb) step |
| 506 | --Return-- |
| 507 | > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None |
| 508 | -> string.capwords('FOO') |
| 509 | (Pdb) continue |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 510 | """ |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 511 | |
| 512 | |
| 513 | # Module for testing skipping of module that makes a callback |
Brett Cannon | 9529fbf | 2013-06-15 17:11:25 -0400 | [diff] [blame] | 514 | mod = types.ModuleType('module_to_skip') |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 515 | exec('def foo_pony(callback): x = 1; callback(); return None', mod.__dict__) |
| 516 | |
| 517 | |
| 518 | def test_pdb_skip_modules_with_callback(): |
| 519 | """This illustrates skipping of modules that call into other code. |
| 520 | |
| 521 | >>> def skip_module(): |
| 522 | ... def callback(): |
| 523 | ... return None |
Łukasz Langa | 2eb6eca | 2016-09-09 22:21:17 -0700 | [diff] [blame] | 524 | ... 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] | 525 | ... mod.foo_pony(callback) |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 526 | |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 527 | >>> with PdbTestInput([ |
| 528 | ... 'step', |
| 529 | ... 'step', |
| 530 | ... 'step', |
| 531 | ... 'step', |
| 532 | ... 'step', |
| 533 | ... 'continue', |
| 534 | ... ]): |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 535 | ... skip_module() |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 536 | ... pass # provides something to "step" to |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 537 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module() |
| 538 | -> mod.foo_pony(callback) |
| 539 | (Pdb) step |
| 540 | --Call-- |
| 541 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback() |
| 542 | -> def callback(): |
| 543 | (Pdb) step |
| 544 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback() |
| 545 | -> return None |
| 546 | (Pdb) step |
| 547 | --Return-- |
| 548 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None |
| 549 | -> return None |
| 550 | (Pdb) step |
| 551 | --Return-- |
| 552 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None |
| 553 | -> mod.foo_pony(callback) |
| 554 | (Pdb) step |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 555 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>() |
| 556 | -> pass # provides something to "step" to |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 557 | (Pdb) continue |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 558 | """ |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 559 | |
| 560 | |
Georg Brandl | 3f94089 | 2010-07-30 10:29:19 +0000 | [diff] [blame] | 561 | def test_pdb_continue_in_bottomframe(): |
| 562 | """Test that "continue" and "next" work properly in bottom frame (issue #5294). |
| 563 | |
| 564 | >>> def test_function(): |
Łukasz Langa | 2eb6eca | 2016-09-09 22:21:17 -0700 | [diff] [blame] | 565 | ... import pdb, sys; inst = pdb.Pdb(nosigint=True, readrc=False) |
Georg Brandl | 3f94089 | 2010-07-30 10:29:19 +0000 | [diff] [blame] | 566 | ... inst.set_trace() |
| 567 | ... inst.botframe = sys._getframe() # hackery to get the right botframe |
| 568 | ... print(1) |
| 569 | ... print(2) |
| 570 | ... print(3) |
| 571 | ... print(4) |
| 572 | |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 573 | >>> with PdbTestInput([ # doctest: +ELLIPSIS |
Georg Brandl | 3f94089 | 2010-07-30 10:29:19 +0000 | [diff] [blame] | 574 | ... 'next', |
| 575 | ... 'break 7', |
| 576 | ... 'continue', |
| 577 | ... 'next', |
| 578 | ... 'continue', |
| 579 | ... 'continue', |
| 580 | ... ]): |
| 581 | ... test_function() |
| 582 | > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(4)test_function() |
| 583 | -> inst.botframe = sys._getframe() # hackery to get the right botframe |
| 584 | (Pdb) next |
| 585 | > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(5)test_function() |
| 586 | -> print(1) |
| 587 | (Pdb) break 7 |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 588 | 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] | 589 | (Pdb) continue |
| 590 | 1 |
| 591 | 2 |
| 592 | > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(7)test_function() |
| 593 | -> print(3) |
| 594 | (Pdb) next |
| 595 | 3 |
| 596 | > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(8)test_function() |
| 597 | -> print(4) |
| 598 | (Pdb) continue |
| 599 | 4 |
| 600 | """ |
| 601 | |
| 602 | |
Georg Brandl | 46b9afc | 2010-07-30 09:14:20 +0000 | [diff] [blame] | 603 | def pdb_invoke(method, arg): |
| 604 | """Run pdb.method(arg).""" |
Łukasz Langa | 2eb6eca | 2016-09-09 22:21:17 -0700 | [diff] [blame] | 605 | getattr(pdb.Pdb(nosigint=True, readrc=False), method)(arg) |
Georg Brandl | 46b9afc | 2010-07-30 09:14:20 +0000 | [diff] [blame] | 606 | |
| 607 | |
| 608 | def test_pdb_run_with_incorrect_argument(): |
| 609 | """Testing run and runeval with incorrect first argument. |
| 610 | |
| 611 | >>> pti = PdbTestInput(['continue',]) |
| 612 | >>> with pti: |
| 613 | ... pdb_invoke('run', lambda x: x) |
| 614 | Traceback (most recent call last): |
| 615 | TypeError: exec() arg 1 must be a string, bytes or code object |
| 616 | |
| 617 | >>> with pti: |
| 618 | ... pdb_invoke('runeval', lambda x: x) |
| 619 | Traceback (most recent call last): |
| 620 | TypeError: eval() arg 1 must be a string, bytes or code object |
| 621 | """ |
| 622 | |
| 623 | |
| 624 | def test_pdb_run_with_code_object(): |
| 625 | """Testing run and runeval with code object as a first argument. |
| 626 | |
Georg Brandl | e1e8df1 | 2010-07-31 08:14:16 +0000 | [diff] [blame] | 627 | >>> with PdbTestInput(['step','x', 'continue']): # doctest: +ELLIPSIS |
Georg Brandl | 46b9afc | 2010-07-30 09:14:20 +0000 | [diff] [blame] | 628 | ... pdb_invoke('run', compile('x=1', '<string>', 'exec')) |
Georg Brandl | e1e8df1 | 2010-07-31 08:14:16 +0000 | [diff] [blame] | 629 | > <string>(1)<module>()... |
Georg Brandl | 46b9afc | 2010-07-30 09:14:20 +0000 | [diff] [blame] | 630 | (Pdb) step |
| 631 | --Return-- |
| 632 | > <string>(1)<module>()->None |
| 633 | (Pdb) x |
| 634 | 1 |
| 635 | (Pdb) continue |
| 636 | |
| 637 | >>> with PdbTestInput(['x', 'continue']): |
| 638 | ... x=0 |
| 639 | ... pdb_invoke('runeval', compile('x+1', '<string>', 'eval')) |
| 640 | > <string>(1)<module>()->None |
| 641 | (Pdb) x |
| 642 | 1 |
| 643 | (Pdb) continue |
| 644 | """ |
| 645 | |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 646 | def test_next_until_return_at_return_event(): |
| 647 | """Test that pdb stops after a next/until/return issued at a return debug event. |
| 648 | |
| 649 | >>> def test_function_2(): |
| 650 | ... x = 1 |
| 651 | ... x = 2 |
| 652 | |
| 653 | >>> def test_function(): |
Łukasz Langa | 2eb6eca | 2016-09-09 22:21:17 -0700 | [diff] [blame] | 654 | ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 655 | ... test_function_2() |
| 656 | ... test_function_2() |
| 657 | ... test_function_2() |
| 658 | ... end = 1 |
| 659 | |
Antoine Pitrou | c04d468 | 2014-08-11 21:40:38 -0400 | [diff] [blame] | 660 | >>> from bdb import Breakpoint |
| 661 | >>> Breakpoint.next = 1 |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 662 | >>> with PdbTestInput(['break test_function_2', |
| 663 | ... 'continue', |
| 664 | ... 'return', |
| 665 | ... 'next', |
| 666 | ... 'continue', |
| 667 | ... 'return', |
| 668 | ... 'until', |
| 669 | ... 'continue', |
| 670 | ... 'return', |
| 671 | ... 'return', |
| 672 | ... 'continue']): |
| 673 | ... test_function() |
| 674 | > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(3)test_function() |
| 675 | -> test_function_2() |
| 676 | (Pdb) break test_function_2 |
| 677 | Breakpoint 1 at <doctest test.test_pdb.test_next_until_return_at_return_event[0]>:1 |
| 678 | (Pdb) continue |
| 679 | > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2() |
| 680 | -> x = 1 |
| 681 | (Pdb) return |
| 682 | --Return-- |
| 683 | > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(3)test_function_2()->None |
| 684 | -> x = 2 |
| 685 | (Pdb) next |
| 686 | > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(4)test_function() |
| 687 | -> test_function_2() |
| 688 | (Pdb) continue |
| 689 | > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2() |
| 690 | -> x = 1 |
| 691 | (Pdb) return |
| 692 | --Return-- |
| 693 | > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(3)test_function_2()->None |
| 694 | -> x = 2 |
| 695 | (Pdb) until |
| 696 | > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(5)test_function() |
| 697 | -> test_function_2() |
| 698 | (Pdb) continue |
| 699 | > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(2)test_function_2() |
| 700 | -> x = 1 |
| 701 | (Pdb) return |
| 702 | --Return-- |
| 703 | > <doctest test.test_pdb.test_next_until_return_at_return_event[0]>(3)test_function_2()->None |
| 704 | -> x = 2 |
| 705 | (Pdb) return |
| 706 | > <doctest test.test_pdb.test_next_until_return_at_return_event[1]>(6)test_function() |
| 707 | -> end = 1 |
| 708 | (Pdb) continue |
| 709 | """ |
| 710 | |
| 711 | def test_pdb_next_command_for_generator(): |
| 712 | """Testing skip unwindng stack on yield for generators for "next" command |
| 713 | |
| 714 | >>> def test_gen(): |
| 715 | ... yield 0 |
| 716 | ... return 1 |
| 717 | ... yield 2 |
| 718 | |
| 719 | >>> def test_function(): |
Łukasz Langa | 2eb6eca | 2016-09-09 22:21:17 -0700 | [diff] [blame] | 720 | ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 721 | ... it = test_gen() |
| 722 | ... try: |
Serhiy Storchaka | a16de5d | 2015-04-01 16:58:19 +0300 | [diff] [blame] | 723 | ... if next(it) != 0: |
| 724 | ... raise AssertionError |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 725 | ... next(it) |
| 726 | ... except StopIteration as ex: |
Serhiy Storchaka | a16de5d | 2015-04-01 16:58:19 +0300 | [diff] [blame] | 727 | ... if ex.value != 1: |
| 728 | ... raise AssertionError |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 729 | ... print("finished") |
| 730 | |
| 731 | >>> with PdbTestInput(['step', |
| 732 | ... 'step', |
| 733 | ... 'step', |
| 734 | ... 'next', |
| 735 | ... 'next', |
| 736 | ... 'step', |
| 737 | ... 'step', |
| 738 | ... 'continue']): |
| 739 | ... test_function() |
| 740 | > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(3)test_function() |
| 741 | -> it = test_gen() |
| 742 | (Pdb) step |
| 743 | > <doctest test.test_pdb.test_pdb_next_command_for_generator[1]>(4)test_function() |
| 744 | -> try: |
| 745 | (Pdb) step |
| 746 | > <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] | 747 | -> if next(it) != 0: |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 748 | (Pdb) step |
| 749 | --Call-- |
| 750 | > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(1)test_gen() |
| 751 | -> def test_gen(): |
| 752 | (Pdb) next |
| 753 | > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(2)test_gen() |
| 754 | -> yield 0 |
| 755 | (Pdb) next |
| 756 | > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(3)test_gen() |
| 757 | -> return 1 |
| 758 | (Pdb) step |
| 759 | --Return-- |
| 760 | > <doctest test.test_pdb.test_pdb_next_command_for_generator[0]>(3)test_gen()->1 |
| 761 | -> return 1 |
| 762 | (Pdb) step |
| 763 | StopIteration: 1 |
Serhiy Storchaka | a16de5d | 2015-04-01 16:58:19 +0300 | [diff] [blame] | 764 | > <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] | 765 | -> next(it) |
| 766 | (Pdb) continue |
| 767 | finished |
| 768 | """ |
| 769 | |
Pablo Galindo | 4687702 | 2018-01-29 00:25:05 +0000 | [diff] [blame] | 770 | def test_pdb_next_command_for_coroutine(): |
| 771 | """Testing skip unwindng stack on yield for coroutines for "next" command |
| 772 | |
| 773 | >>> import asyncio |
| 774 | |
| 775 | >>> async def test_coro(): |
| 776 | ... await asyncio.sleep(0) |
| 777 | ... await asyncio.sleep(0) |
| 778 | ... await asyncio.sleep(0) |
| 779 | |
| 780 | >>> async def test_main(): |
| 781 | ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 782 | ... await test_coro() |
| 783 | |
| 784 | >>> def test_function(): |
Pablo Galindo | c7ab581 | 2018-01-29 01:31:00 +0000 | [diff] [blame] | 785 | ... loop = asyncio.new_event_loop() |
Pablo Galindo | 4687702 | 2018-01-29 00:25:05 +0000 | [diff] [blame] | 786 | ... loop.run_until_complete(test_main()) |
| 787 | ... loop.close() |
Brett Cannon | 8425de4 | 2018-06-01 20:34:09 -0700 | [diff] [blame] | 788 | ... asyncio.set_event_loop_policy(None) |
Pablo Galindo | 4687702 | 2018-01-29 00:25:05 +0000 | [diff] [blame] | 789 | ... print("finished") |
| 790 | |
| 791 | >>> with PdbTestInput(['step', |
| 792 | ... 'step', |
| 793 | ... 'next', |
| 794 | ... 'next', |
| 795 | ... 'next', |
| 796 | ... 'step', |
| 797 | ... 'continue']): |
| 798 | ... test_function() |
| 799 | > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[2]>(3)test_main() |
| 800 | -> await test_coro() |
| 801 | (Pdb) step |
| 802 | --Call-- |
| 803 | > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(1)test_coro() |
| 804 | -> async def test_coro(): |
| 805 | (Pdb) step |
| 806 | > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(2)test_coro() |
| 807 | -> await asyncio.sleep(0) |
| 808 | (Pdb) next |
| 809 | > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(3)test_coro() |
| 810 | -> await asyncio.sleep(0) |
| 811 | (Pdb) next |
| 812 | > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[1]>(4)test_coro() |
| 813 | -> await asyncio.sleep(0) |
| 814 | (Pdb) next |
| 815 | Internal StopIteration |
| 816 | > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[2]>(3)test_main() |
| 817 | -> await test_coro() |
| 818 | (Pdb) step |
| 819 | --Return-- |
| 820 | > <doctest test.test_pdb.test_pdb_next_command_for_coroutine[2]>(3)test_main()->None |
| 821 | -> await test_coro() |
| 822 | (Pdb) continue |
| 823 | finished |
| 824 | """ |
| 825 | |
Yury Selivanov | 9ee1bf9 | 2018-01-28 22:43:46 -0500 | [diff] [blame] | 826 | def test_pdb_next_command_for_asyncgen(): |
| 827 | """Testing skip unwindng stack on yield for coroutines for "next" command |
| 828 | |
| 829 | >>> import asyncio |
| 830 | |
| 831 | >>> async def agen(): |
| 832 | ... yield 1 |
| 833 | ... await asyncio.sleep(0) |
| 834 | ... yield 2 |
| 835 | |
| 836 | >>> async def test_coro(): |
| 837 | ... async for x in agen(): |
| 838 | ... print(x) |
| 839 | |
| 840 | >>> async def test_main(): |
| 841 | ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 842 | ... await test_coro() |
| 843 | |
| 844 | >>> def test_function(): |
| 845 | ... loop = asyncio.new_event_loop() |
| 846 | ... loop.run_until_complete(test_main()) |
| 847 | ... loop.close() |
Brett Cannon | 8425de4 | 2018-06-01 20:34:09 -0700 | [diff] [blame] | 848 | ... asyncio.set_event_loop_policy(None) |
Yury Selivanov | 9ee1bf9 | 2018-01-28 22:43:46 -0500 | [diff] [blame] | 849 | ... print("finished") |
| 850 | |
| 851 | >>> with PdbTestInput(['step', |
| 852 | ... 'step', |
| 853 | ... 'next', |
| 854 | ... 'next', |
| 855 | ... 'step', |
| 856 | ... 'next', |
| 857 | ... 'continue']): |
| 858 | ... test_function() |
| 859 | > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[3]>(3)test_main() |
| 860 | -> await test_coro() |
| 861 | (Pdb) step |
| 862 | --Call-- |
| 863 | > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[2]>(1)test_coro() |
| 864 | -> async def test_coro(): |
| 865 | (Pdb) step |
| 866 | > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[2]>(2)test_coro() |
| 867 | -> async for x in agen(): |
| 868 | (Pdb) next |
| 869 | > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[2]>(3)test_coro() |
| 870 | -> print(x) |
| 871 | (Pdb) next |
| 872 | 1 |
| 873 | > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[2]>(2)test_coro() |
| 874 | -> async for x in agen(): |
| 875 | (Pdb) step |
| 876 | --Call-- |
| 877 | > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[1]>(2)agen() |
| 878 | -> yield 1 |
| 879 | (Pdb) next |
| 880 | > <doctest test.test_pdb.test_pdb_next_command_for_asyncgen[1]>(3)agen() |
| 881 | -> await asyncio.sleep(0) |
| 882 | (Pdb) continue |
| 883 | 2 |
| 884 | finished |
| 885 | """ |
| 886 | |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 887 | def test_pdb_return_command_for_generator(): |
| 888 | """Testing no unwindng stack on yield for generators |
| 889 | for "return" command |
| 890 | |
| 891 | >>> def test_gen(): |
| 892 | ... yield 0 |
| 893 | ... return 1 |
| 894 | ... yield 2 |
| 895 | |
| 896 | >>> def test_function(): |
Łukasz Langa | 2eb6eca | 2016-09-09 22:21:17 -0700 | [diff] [blame] | 897 | ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 898 | ... it = test_gen() |
| 899 | ... try: |
Serhiy Storchaka | a16de5d | 2015-04-01 16:58:19 +0300 | [diff] [blame] | 900 | ... if next(it) != 0: |
| 901 | ... raise AssertionError |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 902 | ... next(it) |
| 903 | ... except StopIteration as ex: |
Serhiy Storchaka | a16de5d | 2015-04-01 16:58:19 +0300 | [diff] [blame] | 904 | ... if ex.value != 1: |
| 905 | ... raise AssertionError |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 906 | ... print("finished") |
| 907 | |
| 908 | >>> with PdbTestInput(['step', |
| 909 | ... 'step', |
| 910 | ... 'step', |
| 911 | ... 'return', |
| 912 | ... 'step', |
| 913 | ... 'step', |
| 914 | ... 'continue']): |
| 915 | ... test_function() |
| 916 | > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(3)test_function() |
| 917 | -> it = test_gen() |
| 918 | (Pdb) step |
| 919 | > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(4)test_function() |
| 920 | -> try: |
| 921 | (Pdb) step |
| 922 | > <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] | 923 | -> if next(it) != 0: |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 924 | (Pdb) step |
| 925 | --Call-- |
| 926 | > <doctest test.test_pdb.test_pdb_return_command_for_generator[0]>(1)test_gen() |
| 927 | -> def test_gen(): |
| 928 | (Pdb) return |
| 929 | StopIteration: 1 |
Serhiy Storchaka | a16de5d | 2015-04-01 16:58:19 +0300 | [diff] [blame] | 930 | > <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] | 931 | -> next(it) |
| 932 | (Pdb) step |
Serhiy Storchaka | a16de5d | 2015-04-01 16:58:19 +0300 | [diff] [blame] | 933 | > <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] | 934 | -> except StopIteration as ex: |
| 935 | (Pdb) step |
Serhiy Storchaka | a16de5d | 2015-04-01 16:58:19 +0300 | [diff] [blame] | 936 | > <doctest test.test_pdb.test_pdb_return_command_for_generator[1]>(9)test_function() |
| 937 | -> if ex.value != 1: |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 938 | (Pdb) continue |
| 939 | finished |
| 940 | """ |
| 941 | |
Pablo Galindo | c7ab581 | 2018-01-29 01:31:00 +0000 | [diff] [blame] | 942 | def test_pdb_return_command_for_coroutine(): |
| 943 | """Testing no unwindng stack on yield for coroutines for "return" command |
| 944 | |
| 945 | >>> import asyncio |
| 946 | |
| 947 | >>> async def test_coro(): |
| 948 | ... await asyncio.sleep(0) |
| 949 | ... await asyncio.sleep(0) |
| 950 | ... await asyncio.sleep(0) |
| 951 | |
| 952 | >>> async def test_main(): |
| 953 | ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 954 | ... await test_coro() |
| 955 | |
| 956 | >>> def test_function(): |
| 957 | ... loop = asyncio.new_event_loop() |
| 958 | ... loop.run_until_complete(test_main()) |
| 959 | ... loop.close() |
Brett Cannon | 8425de4 | 2018-06-01 20:34:09 -0700 | [diff] [blame] | 960 | ... asyncio.set_event_loop_policy(None) |
Pablo Galindo | c7ab581 | 2018-01-29 01:31:00 +0000 | [diff] [blame] | 961 | ... print("finished") |
| 962 | |
| 963 | >>> with PdbTestInput(['step', |
| 964 | ... 'step', |
| 965 | ... 'next', |
| 966 | ... 'continue']): |
| 967 | ... test_function() |
| 968 | > <doctest test.test_pdb.test_pdb_return_command_for_coroutine[2]>(3)test_main() |
| 969 | -> await test_coro() |
| 970 | (Pdb) step |
| 971 | --Call-- |
| 972 | > <doctest test.test_pdb.test_pdb_return_command_for_coroutine[1]>(1)test_coro() |
| 973 | -> async def test_coro(): |
| 974 | (Pdb) step |
| 975 | > <doctest test.test_pdb.test_pdb_return_command_for_coroutine[1]>(2)test_coro() |
| 976 | -> await asyncio.sleep(0) |
| 977 | (Pdb) next |
| 978 | > <doctest test.test_pdb.test_pdb_return_command_for_coroutine[1]>(3)test_coro() |
| 979 | -> await asyncio.sleep(0) |
| 980 | (Pdb) continue |
| 981 | finished |
| 982 | """ |
| 983 | |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 984 | def test_pdb_until_command_for_generator(): |
| 985 | """Testing no unwindng stack on yield for generators |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame^] | 986 | for "until" command if target breakpoint is not reached |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 987 | |
| 988 | >>> def test_gen(): |
| 989 | ... yield 0 |
| 990 | ... yield 1 |
| 991 | ... yield 2 |
| 992 | |
| 993 | >>> def test_function(): |
Łukasz Langa | 2eb6eca | 2016-09-09 22:21:17 -0700 | [diff] [blame] | 994 | ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 995 | ... for i in test_gen(): |
| 996 | ... print(i) |
| 997 | ... print("finished") |
| 998 | |
| 999 | >>> with PdbTestInput(['step', |
| 1000 | ... 'until 4', |
| 1001 | ... 'step', |
| 1002 | ... 'step', |
| 1003 | ... 'continue']): |
| 1004 | ... test_function() |
| 1005 | > <doctest test.test_pdb.test_pdb_until_command_for_generator[1]>(3)test_function() |
| 1006 | -> for i in test_gen(): |
| 1007 | (Pdb) step |
| 1008 | --Call-- |
| 1009 | > <doctest test.test_pdb.test_pdb_until_command_for_generator[0]>(1)test_gen() |
| 1010 | -> def test_gen(): |
| 1011 | (Pdb) until 4 |
| 1012 | 0 |
| 1013 | 1 |
| 1014 | > <doctest test.test_pdb.test_pdb_until_command_for_generator[0]>(4)test_gen() |
| 1015 | -> yield 2 |
| 1016 | (Pdb) step |
| 1017 | --Return-- |
| 1018 | > <doctest test.test_pdb.test_pdb_until_command_for_generator[0]>(4)test_gen()->2 |
| 1019 | -> yield 2 |
| 1020 | (Pdb) step |
| 1021 | > <doctest test.test_pdb.test_pdb_until_command_for_generator[1]>(4)test_function() |
| 1022 | -> print(i) |
| 1023 | (Pdb) continue |
| 1024 | 2 |
| 1025 | finished |
| 1026 | """ |
| 1027 | |
Andrew Svetlov | 4f4ef0a | 2018-01-29 16:17:45 +0200 | [diff] [blame] | 1028 | def test_pdb_until_command_for_coroutine(): |
| 1029 | """Testing no unwindng stack for coroutines |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame^] | 1030 | for "until" command if target breakpoint is not reached |
Andrew Svetlov | 4f4ef0a | 2018-01-29 16:17:45 +0200 | [diff] [blame] | 1031 | |
| 1032 | >>> import asyncio |
| 1033 | |
| 1034 | >>> async def test_coro(): |
| 1035 | ... print(0) |
| 1036 | ... await asyncio.sleep(0) |
| 1037 | ... print(1) |
| 1038 | ... await asyncio.sleep(0) |
| 1039 | ... print(2) |
| 1040 | ... await asyncio.sleep(0) |
| 1041 | ... print(3) |
| 1042 | |
| 1043 | >>> async def test_main(): |
| 1044 | ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
| 1045 | ... await test_coro() |
| 1046 | |
| 1047 | >>> def test_function(): |
| 1048 | ... loop = asyncio.new_event_loop() |
| 1049 | ... loop.run_until_complete(test_main()) |
| 1050 | ... loop.close() |
Brett Cannon | 8425de4 | 2018-06-01 20:34:09 -0700 | [diff] [blame] | 1051 | ... asyncio.set_event_loop_policy(None) |
Andrew Svetlov | 4f4ef0a | 2018-01-29 16:17:45 +0200 | [diff] [blame] | 1052 | ... print("finished") |
| 1053 | |
| 1054 | >>> with PdbTestInput(['step', |
| 1055 | ... 'until 8', |
| 1056 | ... 'continue']): |
| 1057 | ... test_function() |
| 1058 | > <doctest test.test_pdb.test_pdb_until_command_for_coroutine[2]>(3)test_main() |
| 1059 | -> await test_coro() |
| 1060 | (Pdb) step |
| 1061 | --Call-- |
| 1062 | > <doctest test.test_pdb.test_pdb_until_command_for_coroutine[1]>(1)test_coro() |
| 1063 | -> async def test_coro(): |
| 1064 | (Pdb) until 8 |
| 1065 | 0 |
| 1066 | 1 |
| 1067 | 2 |
| 1068 | > <doctest test.test_pdb.test_pdb_until_command_for_coroutine[1]>(8)test_coro() |
| 1069 | -> print(3) |
| 1070 | (Pdb) continue |
| 1071 | 3 |
| 1072 | finished |
| 1073 | """ |
| 1074 | |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 1075 | def test_pdb_next_command_in_generator_for_loop(): |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 1076 | """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] | 1077 | |
| 1078 | >>> def test_gen(): |
| 1079 | ... yield 0 |
| 1080 | ... return 1 |
| 1081 | |
| 1082 | >>> def test_function(): |
Łukasz Langa | 2eb6eca | 2016-09-09 22:21:17 -0700 | [diff] [blame] | 1083 | ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 1084 | ... for i in test_gen(): |
| 1085 | ... print('value', i) |
| 1086 | ... x = 123 |
| 1087 | |
| 1088 | >>> with PdbTestInput(['break test_gen', |
| 1089 | ... 'continue', |
| 1090 | ... 'next', |
| 1091 | ... 'next', |
| 1092 | ... 'next', |
| 1093 | ... 'continue']): |
| 1094 | ... test_function() |
| 1095 | > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(3)test_function() |
| 1096 | -> for i in test_gen(): |
| 1097 | (Pdb) break test_gen |
| 1098 | Breakpoint 6 at <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>:1 |
| 1099 | (Pdb) continue |
| 1100 | > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>(2)test_gen() |
| 1101 | -> yield 0 |
| 1102 | (Pdb) next |
| 1103 | value 0 |
| 1104 | > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[0]>(3)test_gen() |
| 1105 | -> return 1 |
| 1106 | (Pdb) next |
| 1107 | Internal StopIteration: 1 |
| 1108 | > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(3)test_function() |
| 1109 | -> for i in test_gen(): |
| 1110 | (Pdb) next |
| 1111 | > <doctest test.test_pdb.test_pdb_next_command_in_generator_for_loop[1]>(5)test_function() |
| 1112 | -> x = 123 |
| 1113 | (Pdb) continue |
| 1114 | """ |
| 1115 | |
| 1116 | def test_pdb_next_command_subiterator(): |
| 1117 | """The next command in a generator with a subiterator. |
| 1118 | |
| 1119 | >>> def test_subgenerator(): |
| 1120 | ... yield 0 |
| 1121 | ... return 1 |
| 1122 | |
| 1123 | >>> def test_gen(): |
| 1124 | ... x = yield from test_subgenerator() |
| 1125 | ... return x |
| 1126 | |
| 1127 | >>> def test_function(): |
Łukasz Langa | 2eb6eca | 2016-09-09 22:21:17 -0700 | [diff] [blame] | 1128 | ... import pdb; pdb.Pdb(nosigint=True, readrc=False).set_trace() |
Guido van Rossum | 8820c23 | 2013-11-21 11:30:06 -0800 | [diff] [blame] | 1129 | ... for i in test_gen(): |
| 1130 | ... print('value', i) |
| 1131 | ... x = 123 |
| 1132 | |
| 1133 | >>> with PdbTestInput(['step', |
| 1134 | ... 'step', |
| 1135 | ... 'next', |
| 1136 | ... 'next', |
| 1137 | ... 'next', |
| 1138 | ... 'continue']): |
| 1139 | ... test_function() |
| 1140 | > <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(3)test_function() |
| 1141 | -> for i in test_gen(): |
| 1142 | (Pdb) step |
| 1143 | --Call-- |
| 1144 | > <doctest test.test_pdb.test_pdb_next_command_subiterator[1]>(1)test_gen() |
| 1145 | -> def test_gen(): |
| 1146 | (Pdb) step |
| 1147 | > <doctest test.test_pdb.test_pdb_next_command_subiterator[1]>(2)test_gen() |
| 1148 | -> x = yield from test_subgenerator() |
| 1149 | (Pdb) next |
| 1150 | value 0 |
| 1151 | > <doctest test.test_pdb.test_pdb_next_command_subiterator[1]>(3)test_gen() |
| 1152 | -> return x |
| 1153 | (Pdb) next |
| 1154 | Internal StopIteration: 1 |
| 1155 | > <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(3)test_function() |
| 1156 | -> for i in test_gen(): |
| 1157 | (Pdb) next |
| 1158 | > <doctest test.test_pdb.test_pdb_next_command_subiterator[2]>(5)test_function() |
| 1159 | -> x = 123 |
| 1160 | (Pdb) continue |
| 1161 | """ |
| 1162 | |
Xavier de Gaye | 10e54ae | 2016-10-12 20:13:24 +0200 | [diff] [blame] | 1163 | def test_pdb_issue_20766(): |
| 1164 | """Test for reference leaks when the SIGINT handler is set. |
| 1165 | |
| 1166 | >>> def test_function(): |
| 1167 | ... i = 1 |
| 1168 | ... while i <= 2: |
| 1169 | ... sess = pdb.Pdb() |
| 1170 | ... sess.set_trace(sys._getframe()) |
| 1171 | ... print('pdb %d: %s' % (i, sess._previous_sigint_handler)) |
| 1172 | ... i += 1 |
| 1173 | |
| 1174 | >>> with PdbTestInput(['continue', |
| 1175 | ... 'continue']): |
| 1176 | ... test_function() |
| 1177 | > <doctest test.test_pdb.test_pdb_issue_20766[0]>(6)test_function() |
| 1178 | -> print('pdb %d: %s' % (i, sess._previous_sigint_handler)) |
| 1179 | (Pdb) continue |
| 1180 | pdb 1: <built-in function default_int_handler> |
| 1181 | > <doctest test.test_pdb.test_pdb_issue_20766[0]>(5)test_function() |
| 1182 | -> sess.set_trace(sys._getframe()) |
| 1183 | (Pdb) continue |
| 1184 | pdb 2: <built-in function default_int_handler> |
| 1185 | """ |
Georg Brandl | 46b9afc | 2010-07-30 09:14:20 +0000 | [diff] [blame] | 1186 | |
Georg Brandl | 6cccb86 | 2010-07-30 14:16:43 +0000 | [diff] [blame] | 1187 | |
Mario Corchero | 9f1e5f1 | 2018-01-06 07:53:05 +0000 | [diff] [blame] | 1188 | class PdbTestCase(unittest.TestCase): |
| 1189 | def tearDown(self): |
| 1190 | support.unlink(support.TESTFN) |
| 1191 | |
| 1192 | def _run_pdb(self, pdb_args, commands): |
| 1193 | self.addCleanup(support.rmtree, '__pycache__') |
| 1194 | cmd = [sys.executable, '-m', 'pdb'] + pdb_args |
| 1195 | with subprocess.Popen( |
| 1196 | cmd, |
| 1197 | stdout=subprocess.PIPE, |
| 1198 | stdin=subprocess.PIPE, |
| 1199 | stderr=subprocess.STDOUT, |
| 1200 | ) as proc: |
| 1201 | stdout, stderr = proc.communicate(str.encode(commands)) |
| 1202 | stdout = stdout and bytes.decode(stdout) |
| 1203 | stderr = stderr and bytes.decode(stderr) |
| 1204 | return stdout, stderr |
| 1205 | |
| 1206 | def run_pdb_script(self, script, commands): |
Senthil Kumaran | 42d7081 | 2012-05-01 10:07:49 +0800 | [diff] [blame] | 1207 | """Run 'script' lines with pdb and the pdb 'commands'.""" |
| 1208 | filename = 'main.py' |
| 1209 | with open(filename, 'w') as f: |
| 1210 | f.write(textwrap.dedent(script)) |
Georg Brandl | 4bde9ca | 2012-05-01 09:21:16 +0200 | [diff] [blame] | 1211 | self.addCleanup(support.unlink, filename) |
Mario Corchero | 9f1e5f1 | 2018-01-06 07:53:05 +0000 | [diff] [blame] | 1212 | return self._run_pdb([filename], commands) |
| 1213 | |
| 1214 | def run_pdb_module(self, script, commands): |
| 1215 | """Runs the script code as part of a module""" |
| 1216 | self.module_name = 't_main' |
| 1217 | support.rmtree(self.module_name) |
| 1218 | main_file = self.module_name + '/__main__.py' |
| 1219 | init_file = self.module_name + '/__init__.py' |
| 1220 | os.mkdir(self.module_name) |
| 1221 | with open(init_file, 'w') as f: |
| 1222 | pass |
| 1223 | with open(main_file, 'w') as f: |
| 1224 | f.write(textwrap.dedent(script)) |
| 1225 | self.addCleanup(support.rmtree, self.module_name) |
| 1226 | return self._run_pdb(['-m', self.module_name], commands) |
Senthil Kumaran | 42d7081 | 2012-05-01 10:07:49 +0800 | [diff] [blame] | 1227 | |
Georg Brandl | 6e22055 | 2013-10-13 20:51:47 +0200 | [diff] [blame] | 1228 | def _assert_find_function(self, file_content, func_name, expected): |
| 1229 | file_content = textwrap.dedent(file_content) |
| 1230 | |
| 1231 | with open(support.TESTFN, 'w') as f: |
| 1232 | f.write(file_content) |
| 1233 | |
| 1234 | expected = None if not expected else ( |
| 1235 | expected[0], support.TESTFN, expected[1]) |
| 1236 | self.assertEqual( |
| 1237 | expected, pdb.find_function(func_name, support.TESTFN)) |
| 1238 | |
| 1239 | def test_find_function_empty_file(self): |
| 1240 | self._assert_find_function('', 'foo', None) |
| 1241 | |
| 1242 | def test_find_function_found(self): |
| 1243 | self._assert_find_function( |
| 1244 | """\ |
| 1245 | def foo(): |
| 1246 | pass |
| 1247 | |
| 1248 | def bar(): |
| 1249 | pass |
| 1250 | |
| 1251 | def quux(): |
| 1252 | pass |
| 1253 | """, |
| 1254 | 'bar', |
| 1255 | ('bar', 4), |
| 1256 | ) |
| 1257 | |
Georg Brandl | 6cccb86 | 2010-07-30 14:16:43 +0000 | [diff] [blame] | 1258 | def test_issue7964(self): |
| 1259 | # open the file as binary so we can force \r\n newline |
| 1260 | with open(support.TESTFN, 'wb') as f: |
| 1261 | f.write(b'print("testing my pdb")\r\n') |
| 1262 | cmd = [sys.executable, '-m', 'pdb', support.TESTFN] |
| 1263 | proc = subprocess.Popen(cmd, |
| 1264 | stdout=subprocess.PIPE, |
| 1265 | stdin=subprocess.PIPE, |
| 1266 | stderr=subprocess.STDOUT, |
| 1267 | ) |
Brian Curtin | 994ad6c | 2010-11-05 15:38:47 +0000 | [diff] [blame] | 1268 | self.addCleanup(proc.stdout.close) |
Georg Brandl | 6cccb86 | 2010-07-30 14:16:43 +0000 | [diff] [blame] | 1269 | stdout, stderr = proc.communicate(b'quit\n') |
| 1270 | self.assertNotIn(b'SyntaxError', stdout, |
| 1271 | "Got a syntax error running test script under PDB") |
| 1272 | |
Senthil Kumaran | 42d7081 | 2012-05-01 10:07:49 +0800 | [diff] [blame] | 1273 | def test_issue13183(self): |
| 1274 | script = """ |
| 1275 | from bar import bar |
| 1276 | |
| 1277 | def foo(): |
| 1278 | bar() |
| 1279 | |
| 1280 | def nope(): |
| 1281 | pass |
| 1282 | |
| 1283 | def foobar(): |
| 1284 | foo() |
| 1285 | nope() |
| 1286 | |
| 1287 | foobar() |
| 1288 | """ |
| 1289 | commands = """ |
| 1290 | from bar import bar |
| 1291 | break bar |
| 1292 | continue |
| 1293 | step |
| 1294 | step |
| 1295 | quit |
| 1296 | """ |
| 1297 | bar = """ |
| 1298 | def bar(): |
Senthil Kumaran | cb17204 | 2012-05-02 08:00:22 +0800 | [diff] [blame] | 1299 | pass |
Senthil Kumaran | 42d7081 | 2012-05-01 10:07:49 +0800 | [diff] [blame] | 1300 | """ |
| 1301 | with open('bar.py', 'w') as f: |
| 1302 | f.write(textwrap.dedent(bar)) |
Georg Brandl | 4bde9ca | 2012-05-01 09:21:16 +0200 | [diff] [blame] | 1303 | self.addCleanup(support.unlink, 'bar.py') |
Mario Corchero | 9f1e5f1 | 2018-01-06 07:53:05 +0000 | [diff] [blame] | 1304 | stdout, stderr = self.run_pdb_script(script, commands) |
Georg Brandl | 4bde9ca | 2012-05-01 09:21:16 +0200 | [diff] [blame] | 1305 | self.assertTrue( |
| 1306 | any('main.py(5)foo()->None' in l for l in stdout.splitlines()), |
| 1307 | 'Fail to step into the caller after a return') |
Senthil Kumaran | 42d7081 | 2012-05-01 10:07:49 +0800 | [diff] [blame] | 1308 | |
Daniel Hahler | 9139f92 | 2019-04-01 23:59:50 +0200 | [diff] [blame] | 1309 | def test_issue13120(self): |
| 1310 | # Invoking "continue" on a non-main thread triggered an exception |
| 1311 | # inside signal.signal. |
Andrew Svetlov | 539ee5d | 2012-12-04 21:08:28 +0200 | [diff] [blame] | 1312 | |
| 1313 | with open(support.TESTFN, 'wb') as f: |
| 1314 | f.write(textwrap.dedent(""" |
| 1315 | import threading |
| 1316 | import pdb |
| 1317 | |
| 1318 | def start_pdb(): |
Łukasz Langa | 2eb6eca | 2016-09-09 22:21:17 -0700 | [diff] [blame] | 1319 | pdb.Pdb(readrc=False).set_trace() |
Andrew Svetlov | 539ee5d | 2012-12-04 21:08:28 +0200 | [diff] [blame] | 1320 | x = 1 |
| 1321 | y = 1 |
| 1322 | |
| 1323 | t = threading.Thread(target=start_pdb) |
| 1324 | t.start()""").encode('ascii')) |
| 1325 | cmd = [sys.executable, '-u', support.TESTFN] |
| 1326 | proc = subprocess.Popen(cmd, |
| 1327 | stdout=subprocess.PIPE, |
| 1328 | stdin=subprocess.PIPE, |
| 1329 | stderr=subprocess.STDOUT, |
| 1330 | ) |
| 1331 | self.addCleanup(proc.stdout.close) |
| 1332 | stdout, stderr = proc.communicate(b'cont\n') |
| 1333 | self.assertNotIn('Error', stdout.decode(), |
| 1334 | "Got an error running test script under PDB") |
| 1335 | |
Terry Jan Reedy | ca3f435 | 2015-09-05 19:13:26 -0400 | [diff] [blame] | 1336 | def test_issue16180(self): |
| 1337 | # A syntax error in the debuggee. |
| 1338 | script = "def f: pass\n" |
| 1339 | commands = '' |
| 1340 | expected = "SyntaxError:" |
Mario Corchero | 9f1e5f1 | 2018-01-06 07:53:05 +0000 | [diff] [blame] | 1341 | stdout, stderr = self.run_pdb_script(script, commands) |
Terry Jan Reedy | ca3f435 | 2015-09-05 19:13:26 -0400 | [diff] [blame] | 1342 | self.assertIn(expected, stdout, |
| 1343 | '\n\nExpected:\n{}\nGot:\n{}\n' |
| 1344 | 'Fail to handle a syntax error in the debuggee.' |
| 1345 | .format(expected, stdout)) |
| 1346 | |
| 1347 | |
Łukasz Langa | 2eb6eca | 2016-09-09 22:21:17 -0700 | [diff] [blame] | 1348 | def test_readrc_kwarg(self): |
Victor Stinner | 11ea044 | 2016-09-09 22:56:54 -0700 | [diff] [blame] | 1349 | script = textwrap.dedent(""" |
| 1350 | import pdb; pdb.Pdb(readrc=False).set_trace() |
Łukasz Langa | 2eb6eca | 2016-09-09 22:21:17 -0700 | [diff] [blame] | 1351 | |
Victor Stinner | 11ea044 | 2016-09-09 22:56:54 -0700 | [diff] [blame] | 1352 | print('hello') |
| 1353 | """) |
Victor Stinner | 11ea044 | 2016-09-09 22:56:54 -0700 | [diff] [blame] | 1354 | |
Victor Stinner | bc62626 | 2016-09-09 23:22:09 -0700 | [diff] [blame] | 1355 | save_home = os.environ.pop('HOME', None) |
| 1356 | try: |
| 1357 | with support.temp_cwd(): |
Łukasz Langa | 2eb6eca | 2016-09-09 22:21:17 -0700 | [diff] [blame] | 1358 | with open('.pdbrc', 'w') as f: |
| 1359 | f.write("invalid\n") |
| 1360 | |
| 1361 | with open('main.py', 'w') as f: |
| 1362 | f.write(script) |
| 1363 | |
| 1364 | cmd = [sys.executable, 'main.py'] |
| 1365 | proc = subprocess.Popen( |
| 1366 | cmd, |
| 1367 | stdout=subprocess.PIPE, |
| 1368 | stdin=subprocess.PIPE, |
| 1369 | stderr=subprocess.PIPE, |
| 1370 | ) |
Victor Stinner | bc62626 | 2016-09-09 23:22:09 -0700 | [diff] [blame] | 1371 | with proc: |
| 1372 | stdout, stderr = proc.communicate(b'q\n') |
| 1373 | self.assertNotIn("NameError: name 'invalid' is not defined", |
| 1374 | stdout.decode()) |
Łukasz Langa | 2eb6eca | 2016-09-09 22:21:17 -0700 | [diff] [blame] | 1375 | |
| 1376 | finally: |
Victor Stinner | 11ea044 | 2016-09-09 22:56:54 -0700 | [diff] [blame] | 1377 | if save_home is not None: |
| 1378 | os.environ['HOME'] = save_home |
Łukasz Langa | 2eb6eca | 2016-09-09 22:21:17 -0700 | [diff] [blame] | 1379 | |
Barry Warsaw | 35425d6 | 2017-09-22 12:29:42 -0400 | [diff] [blame] | 1380 | def test_header(self): |
| 1381 | stdout = StringIO() |
| 1382 | header = 'Nobody expects... blah, blah, blah' |
| 1383 | with ExitStack() as resources: |
| 1384 | resources.enter_context(patch('sys.stdout', stdout)) |
| 1385 | resources.enter_context(patch.object(pdb.Pdb, 'set_trace')) |
| 1386 | pdb.set_trace(header=header) |
| 1387 | self.assertEqual(stdout.getvalue(), header + '\n') |
| 1388 | |
Mario Corchero | 9f1e5f1 | 2018-01-06 07:53:05 +0000 | [diff] [blame] | 1389 | def test_run_module(self): |
| 1390 | script = """print("SUCCESS")""" |
| 1391 | commands = """ |
| 1392 | continue |
| 1393 | quit |
| 1394 | """ |
| 1395 | stdout, stderr = self.run_pdb_module(script, commands) |
| 1396 | self.assertTrue(any("SUCCESS" in l for l in stdout.splitlines()), stdout) |
| 1397 | |
| 1398 | def test_module_is_run_as_main(self): |
| 1399 | script = """ |
| 1400 | if __name__ == '__main__': |
| 1401 | print("SUCCESS") |
| 1402 | """ |
| 1403 | commands = """ |
| 1404 | continue |
| 1405 | quit |
| 1406 | """ |
| 1407 | stdout, stderr = self.run_pdb_module(script, commands) |
| 1408 | self.assertTrue(any("SUCCESS" in l for l in stdout.splitlines()), stdout) |
| 1409 | |
| 1410 | def test_breakpoint(self): |
| 1411 | script = """ |
| 1412 | if __name__ == '__main__': |
| 1413 | pass |
| 1414 | print("SUCCESS") |
| 1415 | pass |
| 1416 | """ |
| 1417 | commands = """ |
| 1418 | b 3 |
| 1419 | quit |
| 1420 | """ |
| 1421 | stdout, stderr = self.run_pdb_module(script, commands) |
| 1422 | self.assertTrue(any("Breakpoint 1 at" in l for l in stdout.splitlines()), stdout) |
| 1423 | self.assertTrue(all("SUCCESS" not in l for l in stdout.splitlines()), stdout) |
| 1424 | |
| 1425 | def test_run_pdb_with_pdb(self): |
| 1426 | commands = """ |
| 1427 | c |
| 1428 | quit |
| 1429 | """ |
| 1430 | stdout, stderr = self._run_pdb(["-m", "pdb"], commands) |
Mario Corchero | fcf8b4c | 2018-01-28 04:58:47 +0000 | [diff] [blame] | 1431 | self.assertIn( |
| 1432 | pdb._usage, |
| 1433 | stdout.replace('\r', '') # remove \r for windows |
| 1434 | ) |
Mario Corchero | 9f1e5f1 | 2018-01-06 07:53:05 +0000 | [diff] [blame] | 1435 | |
| 1436 | def test_module_without_a_main(self): |
| 1437 | module_name = 't_main' |
| 1438 | support.rmtree(module_name) |
| 1439 | init_file = module_name + '/__init__.py' |
| 1440 | os.mkdir(module_name) |
| 1441 | with open(init_file, 'w') as f: |
| 1442 | pass |
| 1443 | self.addCleanup(support.rmtree, module_name) |
| 1444 | stdout, stderr = self._run_pdb(['-m', module_name], "") |
| 1445 | self.assertIn("ImportError: No module named t_main.__main__", |
| 1446 | stdout.splitlines()) |
| 1447 | |
| 1448 | def test_blocks_at_first_code_line(self): |
| 1449 | script = """ |
| 1450 | #This is a comment, on line 2 |
| 1451 | |
| 1452 | print("SUCCESS") |
| 1453 | """ |
| 1454 | commands = """ |
| 1455 | quit |
| 1456 | """ |
| 1457 | stdout, stderr = self.run_pdb_module(script, commands) |
| 1458 | self.assertTrue(any("__main__.py(4)<module>()" |
| 1459 | in l for l in stdout.splitlines()), stdout) |
| 1460 | |
| 1461 | def test_relative_imports(self): |
| 1462 | self.module_name = 't_main' |
| 1463 | support.rmtree(self.module_name) |
| 1464 | main_file = self.module_name + '/__main__.py' |
| 1465 | init_file = self.module_name + '/__init__.py' |
| 1466 | module_file = self.module_name + '/module.py' |
| 1467 | self.addCleanup(support.rmtree, self.module_name) |
| 1468 | os.mkdir(self.module_name) |
| 1469 | with open(init_file, 'w') as f: |
| 1470 | f.write(textwrap.dedent(""" |
| 1471 | top_var = "VAR from top" |
| 1472 | """)) |
| 1473 | with open(main_file, 'w') as f: |
| 1474 | f.write(textwrap.dedent(""" |
| 1475 | from . import top_var |
| 1476 | from .module import var |
| 1477 | from . import module |
| 1478 | pass # We'll stop here and print the vars |
| 1479 | """)) |
| 1480 | with open(module_file, 'w') as f: |
| 1481 | f.write(textwrap.dedent(""" |
| 1482 | var = "VAR from module" |
| 1483 | var2 = "second var" |
| 1484 | """)) |
| 1485 | commands = """ |
| 1486 | b 5 |
| 1487 | c |
| 1488 | p top_var |
| 1489 | p var |
| 1490 | p module.var2 |
| 1491 | quit |
| 1492 | """ |
| 1493 | stdout, _ = self._run_pdb(['-m', self.module_name], commands) |
Mario Corchero | 38bfa84 | 2018-02-03 06:40:11 +0000 | [diff] [blame] | 1494 | self.assertTrue(any("VAR from module" in l for l in stdout.splitlines()), stdout) |
Mario Corchero | 9f1e5f1 | 2018-01-06 07:53:05 +0000 | [diff] [blame] | 1495 | self.assertTrue(any("VAR from top" in l for l in stdout.splitlines())) |
| 1496 | self.assertTrue(any("second var" in l for l in stdout.splitlines())) |
Georg Brandl | 6cccb86 | 2010-07-30 14:16:43 +0000 | [diff] [blame] | 1497 | |
Mario Corchero | 38bfa84 | 2018-02-03 06:40:11 +0000 | [diff] [blame] | 1498 | def test_relative_imports_on_plain_module(self): |
| 1499 | # Validates running a plain module. See bpo32691 |
| 1500 | self.module_name = 't_main' |
| 1501 | support.rmtree(self.module_name) |
| 1502 | main_file = self.module_name + '/runme.py' |
| 1503 | init_file = self.module_name + '/__init__.py' |
| 1504 | module_file = self.module_name + '/module.py' |
| 1505 | self.addCleanup(support.rmtree, self.module_name) |
| 1506 | os.mkdir(self.module_name) |
| 1507 | with open(init_file, 'w') as f: |
| 1508 | f.write(textwrap.dedent(""" |
| 1509 | top_var = "VAR from top" |
| 1510 | """)) |
| 1511 | with open(main_file, 'w') as f: |
| 1512 | f.write(textwrap.dedent(""" |
| 1513 | from . import module |
| 1514 | pass # We'll stop here and print the vars |
| 1515 | """)) |
| 1516 | with open(module_file, 'w') as f: |
| 1517 | f.write(textwrap.dedent(""" |
| 1518 | var = "VAR from module" |
| 1519 | """)) |
| 1520 | commands = """ |
| 1521 | b 3 |
| 1522 | c |
| 1523 | p module.var |
| 1524 | quit |
| 1525 | """ |
| 1526 | stdout, _ = self._run_pdb(['-m', self.module_name + '.runme'], commands) |
| 1527 | self.assertTrue(any("VAR from module" in l for l in stdout.splitlines()), stdout) |
| 1528 | |
Daniel Hahler | 3e93643 | 2019-03-12 04:29:04 +0100 | [diff] [blame] | 1529 | def test_errors_in_command(self): |
| 1530 | commands = "\n".join([ |
| 1531 | 'print(', |
| 1532 | 'debug print(', |
| 1533 | 'debug doesnotexist', |
| 1534 | 'c', |
| 1535 | ]) |
| 1536 | stdout, _ = self.run_pdb_script('', commands + '\n') |
| 1537 | |
Daniel Hahler | 4327705 | 2019-02-15 21:52:53 +0100 | [diff] [blame] | 1538 | self.assertEqual(stdout.splitlines()[1:], [ |
| 1539 | '(Pdb) *** SyntaxError: unexpected EOF while parsing', |
Daniel Hahler | 3e93643 | 2019-03-12 04:29:04 +0100 | [diff] [blame] | 1540 | |
| 1541 | '(Pdb) ENTERING RECURSIVE DEBUGGER', |
| 1542 | '*** SyntaxError: unexpected EOF while parsing', |
| 1543 | 'LEAVING RECURSIVE DEBUGGER', |
| 1544 | |
| 1545 | '(Pdb) ENTERING RECURSIVE DEBUGGER', |
| 1546 | '> <string>(1)<module>()', |
| 1547 | "((Pdb)) *** NameError: name 'doesnotexist' is not defined", |
| 1548 | 'LEAVING RECURSIVE DEBUGGER', |
Daniel Hahler | 4327705 | 2019-02-15 21:52:53 +0100 | [diff] [blame] | 1549 | '(Pdb) ', |
| 1550 | ]) |
Georg Brandl | 6cccb86 | 2010-07-30 14:16:43 +0000 | [diff] [blame] | 1551 | |
Andrew Svetlov | f0efea0 | 2013-03-18 10:09:50 -0700 | [diff] [blame] | 1552 | def load_tests(*args): |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 1553 | from test import test_pdb |
Mario Corchero | 9f1e5f1 | 2018-01-06 07:53:05 +0000 | [diff] [blame] | 1554 | suites = [ |
| 1555 | unittest.makeSuite(PdbTestCase), |
| 1556 | doctest.DocTestSuite(test_pdb) |
| 1557 | ] |
Andrew Svetlov | f0efea0 | 2013-03-18 10:09:50 -0700 | [diff] [blame] | 1558 | return unittest.TestSuite(suites) |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 1559 | |
| 1560 | |
| 1561 | if __name__ == '__main__': |
Andrew Svetlov | f0efea0 | 2013-03-18 10:09:50 -0700 | [diff] [blame] | 1562 | unittest.main() |