Georg Brandl | 46b9afc | 2010-07-30 09:14:20 +0000 | [diff] [blame] | 1 | # A test suite for pdb; not very comprehensive at the moment. |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 2 | |
| 3 | import imp |
Georg Brandl | 6cccb86 | 2010-07-30 14:16:43 +0000 | [diff] [blame] | 4 | import pdb |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 5 | import sys |
Georg Brandl | 6cccb86 | 2010-07-30 14:16:43 +0000 | [diff] [blame] | 6 | import unittest |
| 7 | import subprocess |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 8 | |
| 9 | from test import support |
| 10 | # This little helper class is essential for testing pdb under doctest. |
| 11 | from test.test_doctest import _FakeInput |
| 12 | |
| 13 | |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 14 | class PdbTestInput(object): |
| 15 | """Context manager that makes testing Pdb in doctests easier.""" |
| 16 | |
| 17 | def __init__(self, input): |
| 18 | self.input = input |
| 19 | |
| 20 | def __enter__(self): |
| 21 | self.real_stdin = sys.stdin |
| 22 | sys.stdin = _FakeInput(self.input) |
| 23 | |
| 24 | def __exit__(self, *exc): |
| 25 | sys.stdin = self.real_stdin |
| 26 | |
| 27 | |
| 28 | def test_pdb_displayhook(): |
| 29 | """This tests the custom displayhook for pdb. |
| 30 | |
| 31 | >>> def test_function(foo, bar): |
| 32 | ... import pdb; pdb.Pdb().set_trace() |
| 33 | ... pass |
| 34 | |
| 35 | >>> with PdbTestInput([ |
| 36 | ... 'foo', |
| 37 | ... 'bar', |
| 38 | ... 'for i in range(5): print(i)', |
| 39 | ... 'continue', |
| 40 | ... ]): |
| 41 | ... test_function(1, None) |
| 42 | > <doctest test.test_pdb.test_pdb_displayhook[0]>(3)test_function() |
| 43 | -> pass |
| 44 | (Pdb) foo |
| 45 | 1 |
| 46 | (Pdb) bar |
| 47 | (Pdb) for i in range(5): print(i) |
| 48 | 0 |
| 49 | 1 |
| 50 | 2 |
| 51 | 3 |
| 52 | 4 |
| 53 | (Pdb) continue |
| 54 | """ |
| 55 | |
| 56 | |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 57 | def test_pdb_skip_modules(): |
| 58 | """This illustrates the simple case of module skipping. |
| 59 | |
| 60 | >>> def skip_module(): |
| 61 | ... import string |
| 62 | ... import pdb; pdb.Pdb(skip=['stri*']).set_trace() |
| 63 | ... string.capwords('FOO') |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 64 | |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 65 | >>> with PdbTestInput([ |
| 66 | ... 'step', |
| 67 | ... 'continue', |
| 68 | ... ]): |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 69 | ... skip_module() |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 70 | > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module() |
| 71 | -> string.capwords('FOO') |
| 72 | (Pdb) step |
| 73 | --Return-- |
| 74 | > <doctest test.test_pdb.test_pdb_skip_modules[0]>(4)skip_module()->None |
| 75 | -> string.capwords('FOO') |
| 76 | (Pdb) continue |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 77 | """ |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 78 | |
| 79 | |
| 80 | # Module for testing skipping of module that makes a callback |
| 81 | mod = imp.new_module('module_to_skip') |
| 82 | exec('def foo_pony(callback): x = 1; callback(); return None', mod.__dict__) |
| 83 | |
| 84 | |
| 85 | def test_pdb_skip_modules_with_callback(): |
| 86 | """This illustrates skipping of modules that call into other code. |
| 87 | |
| 88 | >>> def skip_module(): |
| 89 | ... def callback(): |
| 90 | ... return None |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 91 | ... import pdb; pdb.Pdb(skip=['module_to_skip*']).set_trace() |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 92 | ... mod.foo_pony(callback) |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 93 | |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 94 | >>> with PdbTestInput([ |
| 95 | ... 'step', |
| 96 | ... 'step', |
| 97 | ... 'step', |
| 98 | ... 'step', |
| 99 | ... 'step', |
| 100 | ... 'continue', |
| 101 | ... ]): |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 102 | ... skip_module() |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 103 | ... pass # provides something to "step" to |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 104 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module() |
| 105 | -> mod.foo_pony(callback) |
| 106 | (Pdb) step |
| 107 | --Call-- |
| 108 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(2)callback() |
| 109 | -> def callback(): |
| 110 | (Pdb) step |
| 111 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback() |
| 112 | -> return None |
| 113 | (Pdb) step |
| 114 | --Return-- |
| 115 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(3)callback()->None |
| 116 | -> return None |
| 117 | (Pdb) step |
| 118 | --Return-- |
| 119 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[0]>(5)skip_module()->None |
| 120 | -> mod.foo_pony(callback) |
| 121 | (Pdb) step |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 122 | > <doctest test.test_pdb.test_pdb_skip_modules_with_callback[1]>(10)<module>() |
| 123 | -> pass # provides something to "step" to |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 124 | (Pdb) continue |
Georg Brandl | 9fa2e02 | 2009-09-16 16:40:45 +0000 | [diff] [blame] | 125 | """ |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 126 | |
| 127 | |
Georg Brandl | 3f94089 | 2010-07-30 10:29:19 +0000 | [diff] [blame] | 128 | def test_pdb_continue_in_bottomframe(): |
| 129 | """Test that "continue" and "next" work properly in bottom frame (issue #5294). |
| 130 | |
| 131 | >>> def test_function(): |
| 132 | ... import pdb, sys; inst = pdb.Pdb() |
| 133 | ... inst.set_trace() |
| 134 | ... inst.botframe = sys._getframe() # hackery to get the right botframe |
| 135 | ... print(1) |
| 136 | ... print(2) |
| 137 | ... print(3) |
| 138 | ... print(4) |
| 139 | |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 140 | >>> with PdbTestInput([ # doctest: +ELLIPSIS |
Georg Brandl | 3f94089 | 2010-07-30 10:29:19 +0000 | [diff] [blame] | 141 | ... 'next', |
| 142 | ... 'break 7', |
| 143 | ... 'continue', |
| 144 | ... 'next', |
| 145 | ... 'continue', |
| 146 | ... 'continue', |
| 147 | ... ]): |
| 148 | ... test_function() |
| 149 | > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(4)test_function() |
| 150 | -> inst.botframe = sys._getframe() # hackery to get the right botframe |
| 151 | (Pdb) next |
| 152 | > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(5)test_function() |
| 153 | -> print(1) |
| 154 | (Pdb) break 7 |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 155 | 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] | 156 | (Pdb) continue |
| 157 | 1 |
| 158 | 2 |
| 159 | > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(7)test_function() |
| 160 | -> print(3) |
| 161 | (Pdb) next |
| 162 | 3 |
| 163 | > <doctest test.test_pdb.test_pdb_continue_in_bottomframe[0]>(8)test_function() |
| 164 | -> print(4) |
| 165 | (Pdb) continue |
| 166 | 4 |
| 167 | """ |
| 168 | |
| 169 | |
Georg Brandl | 7410dd1 | 2010-07-30 12:01:20 +0000 | [diff] [blame] | 170 | def test_pdb_breakpoints(): |
| 171 | """Test handling of breakpoints. |
| 172 | |
| 173 | >>> def test_function(): |
| 174 | ... import pdb; pdb.Pdb().set_trace() |
| 175 | ... print(1) |
| 176 | ... print(2) |
| 177 | ... print(3) |
| 178 | ... print(4) |
| 179 | |
| 180 | First, need to clear bdb state that might be left over from previous tests. |
| 181 | Otherwise, the new breakpoints might get assigned different numbers. |
| 182 | |
| 183 | >>> from bdb import Breakpoint |
| 184 | >>> Breakpoint.next = 1 |
| 185 | >>> Breakpoint.bplist = {} |
| 186 | >>> Breakpoint.bpbynumber = [None] |
| 187 | |
| 188 | Now test the breakpoint commands. NORMALIZE_WHITESPACE is needed because |
| 189 | the breakpoint list outputs a tab for the "stop only" and "ignore next" |
| 190 | lines, which we don't want to put in here. |
| 191 | |
| 192 | >>> with PdbTestInput([ # doctest: +NORMALIZE_WHITESPACE |
| 193 | ... 'break 3', |
| 194 | ... 'disable 1', |
| 195 | ... 'ignore 1 10', |
| 196 | ... 'condition 1 1 < 2', |
| 197 | ... 'break 4', |
| 198 | ... 'break', |
| 199 | ... 'condition 1', |
| 200 | ... 'enable 1', |
| 201 | ... 'clear 1', |
| 202 | ... 'commands 2', |
| 203 | ... 'print 42', |
| 204 | ... 'end', |
| 205 | ... 'continue', # will stop at breakpoint 2 |
| 206 | ... 'continue', |
| 207 | ... ]): |
| 208 | ... test_function() |
| 209 | > <doctest test.test_pdb.test_pdb_breakpoints[0]>(3)test_function() |
| 210 | -> print(1) |
| 211 | (Pdb) break 3 |
| 212 | Breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoints[0]>:3 |
| 213 | (Pdb) disable 1 |
| 214 | Disabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoints[0]>:3 |
| 215 | (Pdb) ignore 1 10 |
| 216 | Will ignore next 10 crossings of breakpoint 1. |
| 217 | (Pdb) condition 1 1 < 2 |
| 218 | New condition set for breakpoint 1. |
| 219 | (Pdb) break 4 |
| 220 | Breakpoint 2 at <doctest test.test_pdb.test_pdb_breakpoints[0]>:4 |
| 221 | (Pdb) break |
| 222 | Num Type Disp Enb Where |
| 223 | 1 breakpoint keep no at <doctest test.test_pdb.test_pdb_breakpoints[0]>:3 |
| 224 | stop only if 1 < 2 |
| 225 | ignore next 10 hits |
| 226 | 2 breakpoint keep yes at <doctest test.test_pdb.test_pdb_breakpoints[0]>:4 |
| 227 | (Pdb) condition 1 |
| 228 | Breakpoint 1 is now unconditional. |
| 229 | (Pdb) enable 1 |
| 230 | Enabled breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoints[0]>:3 |
| 231 | (Pdb) clear 1 |
| 232 | Deleted breakpoint 1 at <doctest test.test_pdb.test_pdb_breakpoints[0]>:3 |
| 233 | (Pdb) commands 2 |
| 234 | (com) print 42 |
| 235 | (com) end |
| 236 | (Pdb) continue |
| 237 | 1 |
| 238 | 42 |
| 239 | > <doctest test.test_pdb.test_pdb_breakpoints[0]>(4)test_function() |
| 240 | -> print(2) |
| 241 | (Pdb) continue |
| 242 | 2 |
| 243 | 3 |
| 244 | 4 |
| 245 | """ |
| 246 | |
| 247 | |
Georg Brandl | 46b9afc | 2010-07-30 09:14:20 +0000 | [diff] [blame] | 248 | def pdb_invoke(method, arg): |
| 249 | """Run pdb.method(arg).""" |
| 250 | import pdb; getattr(pdb, method)(arg) |
| 251 | |
| 252 | |
| 253 | def test_pdb_run_with_incorrect_argument(): |
| 254 | """Testing run and runeval with incorrect first argument. |
| 255 | |
| 256 | >>> pti = PdbTestInput(['continue',]) |
| 257 | >>> with pti: |
| 258 | ... pdb_invoke('run', lambda x: x) |
| 259 | Traceback (most recent call last): |
| 260 | TypeError: exec() arg 1 must be a string, bytes or code object |
| 261 | |
| 262 | >>> with pti: |
| 263 | ... pdb_invoke('runeval', lambda x: x) |
| 264 | Traceback (most recent call last): |
| 265 | TypeError: eval() arg 1 must be a string, bytes or code object |
| 266 | """ |
| 267 | |
| 268 | |
| 269 | def test_pdb_run_with_code_object(): |
| 270 | """Testing run and runeval with code object as a first argument. |
| 271 | |
| 272 | >>> with PdbTestInput(['step','x', 'continue']): |
| 273 | ... pdb_invoke('run', compile('x=1', '<string>', 'exec')) |
| 274 | > <string>(1)<module>() |
| 275 | (Pdb) step |
| 276 | --Return-- |
| 277 | > <string>(1)<module>()->None |
| 278 | (Pdb) x |
| 279 | 1 |
| 280 | (Pdb) continue |
| 281 | |
| 282 | >>> with PdbTestInput(['x', 'continue']): |
| 283 | ... x=0 |
| 284 | ... pdb_invoke('runeval', compile('x+1', '<string>', 'eval')) |
| 285 | > <string>(1)<module>()->None |
| 286 | (Pdb) x |
| 287 | 1 |
| 288 | (Pdb) continue |
| 289 | """ |
| 290 | |
| 291 | |
Georg Brandl | 6cccb86 | 2010-07-30 14:16:43 +0000 | [diff] [blame] | 292 | class PdbTestCase(unittest.TestCase): |
| 293 | |
| 294 | def test_issue7964(self): |
| 295 | # open the file as binary so we can force \r\n newline |
| 296 | with open(support.TESTFN, 'wb') as f: |
| 297 | f.write(b'print("testing my pdb")\r\n') |
| 298 | cmd = [sys.executable, '-m', 'pdb', support.TESTFN] |
| 299 | proc = subprocess.Popen(cmd, |
| 300 | stdout=subprocess.PIPE, |
| 301 | stdin=subprocess.PIPE, |
| 302 | stderr=subprocess.STDOUT, |
| 303 | ) |
| 304 | stdout, stderr = proc.communicate(b'quit\n') |
| 305 | self.assertNotIn(b'SyntaxError', stdout, |
| 306 | "Got a syntax error running test script under PDB") |
| 307 | |
| 308 | def tearDown(self): |
| 309 | support.unlink(support.TESTFN) |
| 310 | |
| 311 | |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 312 | def test_main(): |
| 313 | from test import test_pdb |
| 314 | support.run_doctest(test_pdb, verbosity=True) |
Georg Brandl | 6cccb86 | 2010-07-30 14:16:43 +0000 | [diff] [blame] | 315 | support.run_unittest(PdbTestCase) |
Georg Brandl | 243ad66 | 2009-05-05 09:00:19 +0000 | [diff] [blame] | 316 | |
| 317 | |
| 318 | if __name__ == '__main__': |
| 319 | test_main() |