Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 1 | # A test suite for pdb; at the moment, this only validates skipping of |
| 2 | # specified test modules (RFE #5142). |
| 3 | |
| 4 | import imp |
Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 5 | import sys |
Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 6 | |
| 7 | from test import test_support |
| 8 | # This little helper class is essential for testing pdb under doctest. |
| 9 | from test_doctest import _FakeInput |
| 10 | |
| 11 | |
Georg Brandl | 6c39f06 | 2009-09-16 16:22:12 +0000 | [diff] [blame] | 12 | class PdbTestInput(object): |
| 13 | """Context manager that makes testing Pdb in doctests easier.""" |
| 14 | |
| 15 | def __init__(self, input): |
| 16 | self.input = input |
| 17 | |
| 18 | def __enter__(self): |
| 19 | self.real_stdin = sys.stdin |
| 20 | sys.stdin = _FakeInput(self.input) |
| 21 | |
| 22 | def __exit__(self, *exc): |
| 23 | sys.stdin = self.real_stdin |
| 24 | |
| 25 | |
Georg Brandl | 69dfe8d | 2009-09-16 16:36:39 +0000 | [diff] [blame] | 26 | def write(x): |
| 27 | print x |
| 28 | |
| 29 | def test_pdb_displayhook(): |
| 30 | """This tests the custom displayhook for pdb. |
| 31 | |
| 32 | >>> def test_function(foo, bar): |
| 33 | ... import pdb; pdb.Pdb().set_trace() |
| 34 | ... pass |
| 35 | |
| 36 | >>> with PdbTestInput([ |
| 37 | ... 'foo', |
| 38 | ... 'bar', |
| 39 | ... 'for i in range(5): write(i)', |
| 40 | ... 'continue', |
| 41 | ... ]): |
| 42 | ... test_function(1, None) |
| 43 | > <doctest test.test_pdb.test_pdb_displayhook[0]>(3)test_function() |
| 44 | -> pass |
| 45 | (Pdb) foo |
| 46 | 1 |
| 47 | (Pdb) bar |
| 48 | (Pdb) for i in range(5): write(i) |
| 49 | 0 |
| 50 | 1 |
| 51 | 2 |
| 52 | 3 |
| 53 | 4 |
| 54 | (Pdb) continue |
| 55 | """ |
| 56 | |
Senthil Kumaran | 9a5897b | 2010-11-29 12:41:03 +0000 | [diff] [blame] | 57 | def test_pdb_breakpoint_commands(): |
| 58 | """Test basic commands related to breakpoints. |
| 59 | |
| 60 | >>> def test_function(): |
| 61 | ... import pdb; pdb.Pdb().set_trace() |
| 62 | ... print(1) |
| 63 | ... print(2) |
| 64 | ... print(3) |
| 65 | ... print(4) |
| 66 | |
| 67 | First, need to clear bdb state that might be left over from previous tests. |
| 68 | Otherwise, the new breakpoints might get assigned different numbers. |
| 69 | |
| 70 | >>> from bdb import Breakpoint |
| 71 | >>> Breakpoint.next = 1 |
| 72 | >>> Breakpoint.bplist = {} |
| 73 | >>> Breakpoint.bpbynumber = [None] |
| 74 | |
| 75 | Now test the breakpoint commands. NORMALIZE_WHITESPACE is needed because |
| 76 | the breakpoint list outputs a tab for the "stop only" and "ignore next" |
| 77 | lines, which we don't want to put in here. |
| 78 | |
| 79 | >>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE |
| 80 | ... 'break 3', |
| 81 | ... 'disable 1', |
| 82 | ... 'ignore 1 10', |
| 83 | ... 'condition 1 1 < 2', |
| 84 | ... 'break 4', |
| 85 | ... 'break 4', |
| 86 | ... 'break', |
| 87 | ... 'clear 3', |
| 88 | ... 'break', |
| 89 | ... 'condition 1', |
| 90 | ... 'enable 1', |
| 91 | ... 'clear 1', |
| 92 | ... 'commands 2', |
| 93 | ... 'print 42', |
| 94 | ... 'end', |
| 95 | ... 'continue', # will stop at breakpoint 2 (line 4) |
| 96 | ... 'clear', # clear all! |
| 97 | ... 'y', |
| 98 | ... 'tbreak 5', |
| 99 | ... 'continue', # will stop at temporary breakpoint |
| 100 | ... 'break', # make sure breakpoint is gone |
| 101 | ... 'continue', |
| 102 | ... ]): |
| 103 | ... test_function() |
| 104 | > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(3)test_function() |
| 105 | -> print(1) |
| 106 | (Pdb) break 3 |
| 107 | Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 |
| 108 | (Pdb) disable 1 |
| 109 | (Pdb) ignore 1 10 |
| 110 | Will ignore next 10 crossings of breakpoint 1. |
| 111 | (Pdb) condition 1 1 < 2 |
| 112 | (Pdb) break 4 |
| 113 | Breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 |
| 114 | (Pdb) break 4 |
| 115 | Breakpoint 3 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 |
| 116 | (Pdb) break |
| 117 | Num Type Disp Enb Where |
| 118 | 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 |
| 119 | stop only if 1 < 2 |
| 120 | ignore next 10 hits |
| 121 | 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 |
| 122 | 3 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 |
| 123 | (Pdb) clear 3 |
| 124 | Deleted breakpoint 3 |
| 125 | (Pdb) break |
| 126 | Num Type Disp Enb Where |
| 127 | 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:3 |
| 128 | stop only if 1 < 2 |
| 129 | ignore next 10 hits |
| 130 | 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:4 |
| 131 | (Pdb) condition 1 |
| 132 | Breakpoint 1 is now unconditional. |
| 133 | (Pdb) enable 1 |
| 134 | (Pdb) clear 1 |
| 135 | Deleted breakpoint 1 |
| 136 | (Pdb) commands 2 |
| 137 | (com) print 42 |
| 138 | (com) end |
| 139 | (Pdb) continue |
| 140 | 1 |
| 141 | 42 |
| 142 | > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(4)test_function() |
| 143 | -> print(2) |
| 144 | (Pdb) clear |
| 145 | Clear all breaks? y |
| 146 | (Pdb) tbreak 5 |
| 147 | Breakpoint 4 at <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>:5 |
| 148 | (Pdb) continue |
| 149 | 2 |
| 150 | Deleted breakpoint 4 |
| 151 | > <doctest test.test_pdb.test_pdb_breakpoint_commands[0]>(5)test_function() |
| 152 | -> print(3) |
| 153 | (Pdb) break |
| 154 | (Pdb) continue |
| 155 | 3 |
| 156 | 4 |
| 157 | """ |
| 158 | |
Georg Brandl | 69dfe8d | 2009-09-16 16:36:39 +0000 | [diff] [blame] | 159 | |
Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 160 | def test_pdb_skip_modules(): |
| 161 | """This illustrates the simple case of module skipping. |
| 162 | |
| 163 | >>> def skip_module(): |
| 164 | ... import string |
Georg Brandl | 6c39f06 | 2009-09-16 16:22:12 +0000 | [diff] [blame] | 165 | ... import pdb; pdb.Pdb(skip=['string*']).set_trace() |
Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 166 | ... string.lower('FOO') |
Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 167 | |
Georg Brandl | 6c39f06 | 2009-09-16 16:22:12 +0000 | [diff] [blame] | 168 | >>> with PdbTestInput([ |
| 169 | ... 'step', |
Georg Brandl | 69dfe8d | 2009-09-16 16:36:39 +0000 | [diff] [blame] | 170 | ... 'continue', |
Georg Brandl | 6c39f06 | 2009-09-16 16:22:12 +0000 | [diff] [blame] | 171 | ... ]): |
Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 172 | ... skip_module() |
Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 173 | > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module() |
| 174 | -> string.lower('FOO') |
| 175 | (Pdb) step |
| 176 | --Return-- |
| 177 | > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None |
| 178 | -> string.lower('FOO') |
| 179 | (Pdb) continue |
Georg Brandl | 6c39f06 | 2009-09-16 16:22:12 +0000 | [diff] [blame] | 180 | """ |
Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 181 | |
| 182 | |
| 183 | # Module for testing skipping of module that makes a callback |
| 184 | mod = imp.new_module('module_to_skip') |
| 185 | exec 'def foo_pony(callback): x = 1; callback(); return None' in mod.__dict__ |
| 186 | |
| 187 | |
| 188 | def test_pdb_skip_modules_with_callback(): |
| 189 | """This illustrates skipping of modules that call into other code. |
| 190 | |
| 191 | >>> def skip_module(): |
| 192 | ... def callback(): |
| 193 | ... return None |
Georg Brandl | 6c39f06 | 2009-09-16 16:22:12 +0000 | [diff] [blame] | 194 | ... import pdb; pdb.Pdb(skip=['module_to_skip*']).set_trace() |
Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 195 | ... mod.foo_pony(callback) |
Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 196 | |
Georg Brandl | 6c39f06 | 2009-09-16 16:22:12 +0000 | [diff] [blame] | 197 | >>> with PdbTestInput([ |
| 198 | ... 'step', |
| 199 | ... 'step', |
| 200 | ... 'step', |
| 201 | ... 'step', |
| 202 | ... 'step', |
| 203 | ... 'continue', |
| 204 | ... ]): |
Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 205 | ... skip_module() |
Georg Brandl | 6c39f06 | 2009-09-16 16:22:12 +0000 | [diff] [blame] | 206 | ... pass # provides something to "step" to |
Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 207 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module() |
| 208 | -> mod.foo_pony(callback) |
| 209 | (Pdb) step |
| 210 | --Call-- |
| 211 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback() |
| 212 | -> def callback(): |
| 213 | (Pdb) step |
| 214 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback() |
| 215 | -> return None |
| 216 | (Pdb) step |
| 217 | --Return-- |
| 218 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None |
| 219 | -> return None |
| 220 | (Pdb) step |
| 221 | --Return-- |
| 222 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None |
| 223 | -> mod.foo_pony(callback) |
| 224 | (Pdb) step |
Georg Brandl | 6c39f06 | 2009-09-16 16:22:12 +0000 | [diff] [blame] | 225 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>() |
| 226 | -> pass # provides something to "step" to |
Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 227 | (Pdb) continue |
Georg Brandl | 6c39f06 | 2009-09-16 16:22:12 +0000 | [diff] [blame] | 228 | """ |
Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 229 | |
| 230 | |
Georg Brandl | 5077599 | 2010-08-01 19:33:15 +0000 | [diff] [blame] | 231 | def test_pdb_continue_in_bottomframe(): |
| 232 | """Test that "continue" and "next" work properly in bottom frame (issue #5294). |
| 233 | |
| 234 | >>> def test_function(): |
| 235 | ... import pdb, sys; inst = pdb.Pdb() |
| 236 | ... inst.set_trace() |
| 237 | ... inst.botframe = sys._getframe() # hackery to get the right botframe |
| 238 | ... print(1) |
| 239 | ... print(2) |
| 240 | ... print(3) |
| 241 | ... print(4) |
| 242 | |
Senthil Kumaran | 9a5897b | 2010-11-29 12:41:03 +0000 | [diff] [blame] | 243 | First, need to clear bdb state that might be left over from previous tests. |
| 244 | Otherwise, the new breakpoints might get assigned different numbers. |
| 245 | |
| 246 | >>> from bdb import Breakpoint |
| 247 | >>> Breakpoint.next = 1 |
| 248 | >>> Breakpoint.bplist = {} |
| 249 | >>> Breakpoint.bpbynumber = [None] |
| 250 | |
Georg Brandl | 5077599 | 2010-08-01 19:33:15 +0000 | [diff] [blame] | 251 | >>> with PdbTestInput([ |
| 252 | ... 'next', |
| 253 | ... 'break 7', |
| 254 | ... 'continue', |
| 255 | ... 'next', |
| 256 | ... 'continue', |
| 257 | ... 'continue', |
| 258 | ... ]): |
| 259 | ... test_function() |
| 260 | > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(4)test_function() |
| 261 | -> inst.botframe = sys._getframe() # hackery to get the right botframe |
| 262 | (Pdb) next |
| 263 | > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(5)test_function() |
| 264 | -> print(1) |
| 265 | (Pdb) break 7 |
| 266 | Breakpoint 1 at <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>:7 |
| 267 | (Pdb) continue |
| 268 | 1 |
| 269 | 2 |
| 270 | > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(7)test_function() |
| 271 | -> print(3) |
| 272 | (Pdb) next |
| 273 | 3 |
| 274 | > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(8)test_function() |
| 275 | -> print(4) |
| 276 | (Pdb) continue |
| 277 | 4 |
| 278 | """ |
| 279 | |
| 280 | |
Georg Brandl | 4d4313d | 2009-05-05 08:54:11 +0000 | [diff] [blame] | 281 | def test_main(): |
| 282 | from test import test_pdb |
| 283 | test_support.run_doctest(test_pdb, verbosity=True) |
| 284 | |
| 285 | |
| 286 | if __name__ == '__main__': |
| 287 | test_main() |