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