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