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