Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 1 | # Verify that gdb can pretty-print the various PyObject* types |
| 2 | # |
| 3 | # The code for testing gdb was adapted from similar work in Unladen Swallow's |
| 4 | # Lib/test/test_jit_gdb.py |
| 5 | |
| 6 | import os |
| 7 | import re |
| 8 | import subprocess |
| 9 | import sys |
| 10 | import unittest |
| 11 | |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 12 | from test.support import run_unittest, findfile |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 13 | |
| 14 | try: |
| 15 | gdb_version, _ = subprocess.Popen(["gdb", "--version"], |
| 16 | stdout=subprocess.PIPE).communicate() |
| 17 | except OSError: |
| 18 | # This is what "no gdb" looks like. There may, however, be other |
| 19 | # errors that manifest this way too. |
| 20 | raise unittest.SkipTest("Couldn't find gdb on the path") |
| 21 | gdb_version_number = re.search(b"^GNU gdb [^\d]*(\d+)\.", gdb_version) |
| 22 | if int(gdb_version_number.group(1)) < 7: |
| 23 | raise unittest.SkipTest("gdb versions before 7.0 didn't support python embedding" |
Antoine Pitrou | 6a45e9d | 2010-04-11 22:47:34 +0000 | [diff] [blame] | 24 | " Saw:\n" + gdb_version.decode('ascii', 'replace')) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 25 | |
| 26 | # Verify that "gdb" was built with the embedded python support enabled: |
| 27 | cmd = "--eval-command=python import sys; print sys.version_info" |
| 28 | p = subprocess.Popen(["gdb", "--batch", cmd], |
| 29 | stdout=subprocess.PIPE) |
| 30 | gdbpy_version, _ = p.communicate() |
Benjamin Peterson | 9faa7ec | 2010-04-11 23:51:24 +0000 | [diff] [blame] | 31 | if gdbpy_version == b'': |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 32 | raise unittest.SkipTest("gdb not built with embedded python support") |
| 33 | |
Victor Stinner | 50eb60e | 2010-04-20 22:32:07 +0000 | [diff] [blame] | 34 | def gdb_has_frame_select(): |
| 35 | # Does this build of gdb have gdb.Frame.select ? |
| 36 | cmd = "--eval-command=python print(dir(gdb.Frame))" |
| 37 | p = subprocess.Popen(["gdb", "--batch", cmd], |
| 38 | stdout=subprocess.PIPE) |
| 39 | stdout, _ = p.communicate() |
| 40 | m = re.match(br'.*\[(.*)\].*', stdout) |
| 41 | if not m: |
| 42 | raise unittest.SkipTest("Unable to parse output from gdb.Frame.select test") |
| 43 | gdb_frame_dir = m.group(1).split(b', ') |
| 44 | return b"'select'" in gdb_frame_dir |
| 45 | |
| 46 | HAS_PYUP_PYDOWN = gdb_has_frame_select() |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 47 | |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 48 | BREAKPOINT_FN='builtin_id' |
| 49 | |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 50 | class DebuggerTests(unittest.TestCase): |
| 51 | |
| 52 | """Test that the debugger can debug Python.""" |
| 53 | |
| 54 | def run_gdb(self, *args): |
| 55 | """Runs gdb with the command line given by *args. |
| 56 | |
| 57 | Returns its stdout, stderr |
| 58 | """ |
| 59 | out, err = subprocess.Popen( |
| 60 | args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 61 | ).communicate() |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 62 | return out.decode('utf-8'), err.decode('utf-8') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 63 | |
| 64 | def get_stack_trace(self, source=None, script=None, |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 65 | breakpoint=BREAKPOINT_FN, |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 66 | cmds_after_breakpoint=None, |
| 67 | import_site=False): |
| 68 | ''' |
| 69 | Run 'python -c SOURCE' under gdb with a breakpoint. |
| 70 | |
| 71 | Support injecting commands after the breakpoint is reached |
| 72 | |
| 73 | Returns the stdout from gdb |
| 74 | |
| 75 | cmds_after_breakpoint: if provided, a list of strings: gdb commands |
| 76 | ''' |
| 77 | # We use "set breakpoint pending yes" to avoid blocking with a: |
| 78 | # Function "foo" not defined. |
| 79 | # Make breakpoint pending on future shared library load? (y or [n]) |
| 80 | # error, which typically happens python is dynamically linked (the |
| 81 | # breakpoints of interest are to be found in the shared library) |
| 82 | # When this happens, we still get: |
Victor Stinner | 67df3a4 | 2010-04-21 13:53:05 +0000 | [diff] [blame] | 83 | # Function "textiowrapper_write" not defined. |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 84 | # emitted to stderr each time, alas. |
| 85 | |
| 86 | # Initially I had "--eval-command=continue" here, but removed it to |
| 87 | # avoid repeated print breakpoints when traversing hierarchical data |
| 88 | # structures |
| 89 | |
| 90 | # Generate a list of commands in gdb's language: |
| 91 | commands = ['set breakpoint pending yes', |
| 92 | 'break %s' % breakpoint, |
| 93 | 'run'] |
| 94 | if cmds_after_breakpoint: |
| 95 | commands += cmds_after_breakpoint |
| 96 | else: |
| 97 | commands += ['backtrace'] |
| 98 | |
| 99 | # print commands |
| 100 | |
| 101 | # Use "commands" to generate the arguments with which to invoke "gdb": |
| 102 | args = ["gdb", "--batch"] |
| 103 | args += ['--eval-command=%s' % cmd for cmd in commands] |
| 104 | args += ["--args", |
| 105 | sys.executable] |
| 106 | |
| 107 | if not import_site: |
| 108 | # -S suppresses the default 'import site' |
| 109 | args += ["-S"] |
| 110 | |
| 111 | if source: |
| 112 | args += ["-c", source] |
| 113 | elif script: |
| 114 | args += [script] |
| 115 | |
| 116 | # print args |
| 117 | # print ' '.join(args) |
| 118 | |
| 119 | # Use "args" to invoke gdb, capturing stdout, stderr: |
| 120 | out, err = self.run_gdb(*args) |
| 121 | |
| 122 | # Ignore some noise on stderr due to the pending breakpoint: |
| 123 | err = err.replace('Function "%s" not defined.\n' % breakpoint, '') |
| 124 | |
| 125 | # Ensure no unexpected error messages: |
| 126 | self.assertEquals(err, '') |
| 127 | |
| 128 | return out |
| 129 | |
| 130 | def get_gdb_repr(self, source, |
| 131 | cmds_after_breakpoint=None, |
| 132 | import_site=False): |
| 133 | # Given an input python source representation of data, |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 134 | # run "python -c'id(DATA)'" under gdb with a breakpoint on |
| 135 | # builtin_id and scrape out gdb's representation of the "op" |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 136 | # parameter, and verify that the gdb displays the same string |
| 137 | # |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 138 | # Verify that the gdb displays the expected string |
| 139 | # |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 140 | # For a nested structure, the first time we hit the breakpoint will |
| 141 | # give us the top-level structure |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 142 | gdb_output = self.get_stack_trace(source, breakpoint=BREAKPOINT_FN, |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 143 | cmds_after_breakpoint=cmds_after_breakpoint, |
| 144 | import_site=import_site) |
| 145 | # gdb can insert additional '\n' and space characters in various places |
| 146 | # in its output, depending on the width of the terminal it's connected |
| 147 | # to (using its "wrap_here" function) |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 148 | m = re.match('.*#0\s+builtin_id\s+\(self\=.*,\s+v=\s*(.*?)\)\s+at\s+Python/bltinmodule.c.*', |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 149 | gdb_output, re.DOTALL) |
| 150 | if not m: |
| 151 | self.fail('Unexpected gdb output: %r\n%s' % (gdb_output, gdb_output)) |
| 152 | return m.group(1), gdb_output |
| 153 | |
| 154 | def assertEndsWith(self, actual, exp_end): |
| 155 | '''Ensure that the given "actual" string ends with "exp_end"''' |
| 156 | self.assert_(actual.endswith(exp_end), |
| 157 | msg='%r did not end with %r' % (actual, exp_end)) |
| 158 | |
| 159 | def assertMultilineMatches(self, actual, pattern): |
| 160 | m = re.match(pattern, actual, re.DOTALL) |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 161 | if not m: |
| 162 | self.fail(msg='%r did not match %r' % (actual, pattern)) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 163 | |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 164 | def get_sample_script(self): |
| 165 | return findfile('gdb_sample.py') |
| 166 | |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 167 | class PrettyPrintTests(DebuggerTests): |
| 168 | def test_getting_backtrace(self): |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 169 | gdb_output = self.get_stack_trace('id(42)') |
| 170 | self.assertTrue(BREAKPOINT_FN in gdb_output) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 171 | |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 172 | def assertGdbRepr(self, val, exp_repr=None, cmds_after_breakpoint=None): |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 173 | # Ensure that gdb's rendering of the value in a debugged process |
| 174 | # matches repr(value) in this process: |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 175 | gdb_repr, gdb_output = self.get_gdb_repr('id(' + repr(val) + ')', |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 176 | cmds_after_breakpoint) |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 177 | if not exp_repr: |
| 178 | exp_repr = repr(val) |
| 179 | self.assertEquals(gdb_repr, exp_repr, |
| 180 | ('%r did not equal expected %r; full output was:\n%s' |
| 181 | % (gdb_repr, exp_repr, gdb_output))) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 182 | |
| 183 | def test_int(self): |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 184 | 'Verify the pretty-printing of various "int"/long values' |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 185 | self.assertGdbRepr(42) |
| 186 | self.assertGdbRepr(0) |
| 187 | self.assertGdbRepr(-7) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 188 | self.assertGdbRepr(1000000000000) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 189 | self.assertGdbRepr(-1000000000000000) |
| 190 | |
| 191 | def test_singletons(self): |
| 192 | 'Verify the pretty-printing of True, False and None' |
| 193 | self.assertGdbRepr(True) |
| 194 | self.assertGdbRepr(False) |
| 195 | self.assertGdbRepr(None) |
| 196 | |
| 197 | def test_dicts(self): |
| 198 | 'Verify the pretty-printing of dictionaries' |
| 199 | self.assertGdbRepr({}) |
| 200 | self.assertGdbRepr({'foo': 'bar'}) |
| 201 | self.assertGdbRepr({'foo': 'bar', 'douglas':42}) |
| 202 | |
| 203 | def test_lists(self): |
| 204 | 'Verify the pretty-printing of lists' |
| 205 | self.assertGdbRepr([]) |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 206 | self.assertGdbRepr(list(range(5))) |
| 207 | |
| 208 | def test_bytes(self): |
| 209 | 'Verify the pretty-printing of bytes' |
| 210 | self.assertGdbRepr(b'') |
| 211 | self.assertGdbRepr(b'And now for something hopefully the same') |
| 212 | self.assertGdbRepr(b'string with embedded NUL here \0 and then some more text') |
| 213 | self.assertGdbRepr(b'this is a tab:\t' |
| 214 | b' this is a slash-N:\n' |
| 215 | b' this is a slash-R:\r' |
| 216 | ) |
| 217 | |
| 218 | self.assertGdbRepr(b'this is byte 255:\xff and byte 128:\x80') |
| 219 | |
| 220 | self.assertGdbRepr(bytes([b for b in range(255)])) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 221 | |
| 222 | def test_strings(self): |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 223 | 'Verify the pretty-printing of unicode strings' |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 224 | self.assertGdbRepr('') |
| 225 | self.assertGdbRepr('And now for something hopefully the same') |
| 226 | self.assertGdbRepr('string with embedded NUL here \0 and then some more text') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 227 | |
| 228 | # Test printing a single character: |
| 229 | # U+2620 SKULL AND CROSSBONES |
| 230 | self.assertGdbRepr('\u2620') |
| 231 | |
| 232 | # Test printing a Japanese unicode string |
| 233 | # (I believe this reads "mojibake", using 3 characters from the CJK |
| 234 | # Unified Ideographs area, followed by U+3051 HIRAGANA LETTER KE) |
| 235 | self.assertGdbRepr('\u6587\u5b57\u5316\u3051') |
| 236 | |
| 237 | # Test a character outside the BMP: |
| 238 | # U+1D121 MUSICAL SYMBOL C CLEF |
| 239 | # This is: |
| 240 | # UTF-8: 0xF0 0x9D 0x84 0xA1 |
| 241 | # UTF-16: 0xD834 0xDD21 |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 242 | if sys.maxunicode == 0x10FFFF: |
| 243 | # wide unicode: |
| 244 | self.assertGdbRepr(chr(0x1D121)) |
| 245 | else: |
| 246 | # narrow unicode: |
| 247 | self.assertGdbRepr(chr(0x1D121), |
| 248 | "'\\U0000d834\\U0000dd21'") |
| 249 | |
| 250 | def test_tuples(self): |
| 251 | 'Verify the pretty-printing of tuples' |
| 252 | self.assertGdbRepr(tuple()) |
| 253 | self.assertGdbRepr((1,), '(1,)') |
| 254 | self.assertGdbRepr(('foo', 'bar', 'baz')) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 255 | |
| 256 | def test_sets(self): |
| 257 | 'Verify the pretty-printing of sets' |
| 258 | self.assertGdbRepr(set()) |
| 259 | self.assertGdbRepr(set(['a', 'b'])) |
| 260 | self.assertGdbRepr(set([4, 5, 6])) |
| 261 | |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 262 | # Ensure that we handle sets containing the "dummy" key value, |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 263 | # which happens on deletion: |
| 264 | gdb_repr, gdb_output = self.get_gdb_repr('''s = set(['a','b']) |
| 265 | s.pop() |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 266 | id(s)''') |
| 267 | self.assertEquals(gdb_repr, "{'b'}") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 268 | |
| 269 | def test_frozensets(self): |
| 270 | 'Verify the pretty-printing of frozensets' |
| 271 | self.assertGdbRepr(frozenset()) |
| 272 | self.assertGdbRepr(frozenset(['a', 'b'])) |
| 273 | self.assertGdbRepr(frozenset([4, 5, 6])) |
| 274 | |
| 275 | def test_exceptions(self): |
| 276 | # Test a RuntimeError |
| 277 | gdb_repr, gdb_output = self.get_gdb_repr(''' |
| 278 | try: |
| 279 | raise RuntimeError("I am an error") |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 280 | except RuntimeError as e: |
| 281 | id(e) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 282 | ''') |
| 283 | self.assertEquals(gdb_repr, |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 284 | "RuntimeError('I am an error',)") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 285 | |
| 286 | |
| 287 | # Test division by zero: |
| 288 | gdb_repr, gdb_output = self.get_gdb_repr(''' |
| 289 | try: |
| 290 | a = 1 / 0 |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 291 | except ZeroDivisionError as e: |
| 292 | id(e) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 293 | ''') |
| 294 | self.assertEquals(gdb_repr, |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 295 | "ZeroDivisionError('division by zero',)") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 296 | |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 297 | def test_modern_class(self): |
| 298 | 'Verify the pretty-printing of new-style class instances' |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 299 | gdb_repr, gdb_output = self.get_gdb_repr(''' |
| 300 | class Foo: |
| 301 | pass |
| 302 | foo = Foo() |
| 303 | foo.an_int = 42 |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 304 | id(foo)''') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 305 | m = re.match(r'<Foo\(an_int=42\) at remote 0x[0-9a-f]+>', gdb_repr) |
| 306 | self.assertTrue(m, |
| 307 | msg='Unexpected new-style class rendering %r' % gdb_repr) |
| 308 | |
| 309 | def test_subclassing_list(self): |
| 310 | 'Verify the pretty-printing of an instance of a list subclass' |
| 311 | gdb_repr, gdb_output = self.get_gdb_repr(''' |
| 312 | class Foo(list): |
| 313 | pass |
| 314 | foo = Foo() |
| 315 | foo += [1, 2, 3] |
| 316 | foo.an_int = 42 |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 317 | id(foo)''') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 318 | m = re.match(r'<Foo\(an_int=42\) at remote 0x[0-9a-f]+>', gdb_repr) |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 319 | |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 320 | self.assertTrue(m, |
| 321 | msg='Unexpected new-style class rendering %r' % gdb_repr) |
| 322 | |
| 323 | def test_subclassing_tuple(self): |
| 324 | 'Verify the pretty-printing of an instance of a tuple subclass' |
| 325 | # This should exercise the negative tp_dictoffset code in the |
| 326 | # new-style class support |
| 327 | gdb_repr, gdb_output = self.get_gdb_repr(''' |
| 328 | class Foo(tuple): |
| 329 | pass |
| 330 | foo = Foo((1, 2, 3)) |
| 331 | foo.an_int = 42 |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 332 | id(foo)''') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 333 | m = re.match(r'<Foo\(an_int=42\) at remote 0x[0-9a-f]+>', gdb_repr) |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 334 | |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 335 | self.assertTrue(m, |
| 336 | msg='Unexpected new-style class rendering %r' % gdb_repr) |
| 337 | |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 338 | def assertSane(self, source, corruption, exprepr=None): |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 339 | '''Run Python under gdb, corrupting variables in the inferior process |
| 340 | immediately before taking a backtrace. |
| 341 | |
| 342 | Verify that the variable's representation is the expected failsafe |
| 343 | representation''' |
| 344 | if corruption: |
| 345 | cmds_after_breakpoint=[corruption, 'backtrace'] |
| 346 | else: |
| 347 | cmds_after_breakpoint=['backtrace'] |
| 348 | |
| 349 | gdb_repr, gdb_output = \ |
| 350 | self.get_gdb_repr(source, |
| 351 | cmds_after_breakpoint=cmds_after_breakpoint) |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 352 | if exprepr: |
| 353 | if gdb_repr == exprepr: |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 354 | # gdb managed to print the value in spite of the corruption; |
| 355 | # this is good (see http://bugs.python.org/issue8330) |
| 356 | return |
| 357 | |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 358 | # Match anything for the type name; 0xDEADBEEF could point to |
| 359 | # something arbitrary (see http://bugs.python.org/issue8330) |
| 360 | pattern = '<.* at remote 0x[0-9a-f]+>' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 361 | |
| 362 | m = re.match(pattern, gdb_repr) |
| 363 | if not m: |
| 364 | self.fail('Unexpected gdb representation: %r\n%s' % \ |
| 365 | (gdb_repr, gdb_output)) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 366 | |
| 367 | def test_NULL_ptr(self): |
| 368 | 'Ensure that a NULL PyObject* is handled gracefully' |
| 369 | gdb_repr, gdb_output = ( |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 370 | self.get_gdb_repr('id(42)', |
| 371 | cmds_after_breakpoint=['set variable v=0', |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 372 | 'backtrace']) |
| 373 | ) |
| 374 | |
| 375 | self.assertEquals(gdb_repr, '0x0') |
| 376 | |
| 377 | def test_NULL_ob_type(self): |
| 378 | 'Ensure that a PyObject* with NULL ob_type is handled gracefully' |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 379 | self.assertSane('id(42)', |
| 380 | 'set v->ob_type=0') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 381 | |
| 382 | def test_corrupt_ob_type(self): |
| 383 | 'Ensure that a PyObject* with a corrupt ob_type is handled gracefully' |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 384 | self.assertSane('id(42)', |
| 385 | 'set v->ob_type=0xDEADBEEF', |
| 386 | exprepr='42') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 387 | |
| 388 | def test_corrupt_tp_flags(self): |
| 389 | 'Ensure that a PyObject* with a type with corrupt tp_flags is handled' |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 390 | self.assertSane('id(42)', |
| 391 | 'set v->ob_type->tp_flags=0x0', |
| 392 | exprepr='42') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 393 | |
| 394 | def test_corrupt_tp_name(self): |
| 395 | 'Ensure that a PyObject* with a type with corrupt tp_name is handled' |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 396 | self.assertSane('id(42)', |
| 397 | 'set v->ob_type->tp_name=0xDEADBEEF', |
| 398 | exprepr='42') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 399 | |
| 400 | def test_builtins_help(self): |
| 401 | 'Ensure that the new-style class _Helper in site.py can be handled' |
| 402 | # (this was the issue causing tracebacks in |
| 403 | # http://bugs.python.org/issue8032#msg100537 ) |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 404 | gdb_repr, gdb_output = self.get_gdb_repr('id(__builtins__.help)', import_site=True) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 405 | |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 406 | m = re.match(r'<_Helper at remote 0x[0-9a-f]+>', gdb_repr) |
| 407 | self.assertTrue(m, |
| 408 | msg='Unexpected rendering %r' % gdb_repr) |
| 409 | |
| 410 | def test_selfreferential_list(self): |
| 411 | '''Ensure that a reference loop involving a list doesn't lead proxyval |
| 412 | into an infinite loop:''' |
| 413 | gdb_repr, gdb_output = \ |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 414 | self.get_gdb_repr("a = [3, 4, 5] ; a.append(a) ; id(a)") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 415 | self.assertEquals(gdb_repr, '[3, 4, 5, [...]]') |
| 416 | |
| 417 | gdb_repr, gdb_output = \ |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 418 | self.get_gdb_repr("a = [3, 4, 5] ; b = [a] ; a.append(b) ; id(a)") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 419 | self.assertEquals(gdb_repr, '[3, 4, 5, [[...]]]') |
| 420 | |
| 421 | def test_selfreferential_dict(self): |
| 422 | '''Ensure that a reference loop involving a dict doesn't lead proxyval |
| 423 | into an infinite loop:''' |
| 424 | gdb_repr, gdb_output = \ |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 425 | self.get_gdb_repr("a = {} ; b = {'bar':a} ; a['foo'] = b ; id(a)") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 426 | |
| 427 | self.assertEquals(gdb_repr, "{'foo': {'bar': {...}}}") |
| 428 | |
| 429 | def test_selfreferential_old_style_instance(self): |
| 430 | gdb_repr, gdb_output = \ |
| 431 | self.get_gdb_repr(''' |
| 432 | class Foo: |
| 433 | pass |
| 434 | foo = Foo() |
| 435 | foo.an_attr = foo |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 436 | id(foo)''') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 437 | self.assertTrue(re.match('<Foo\(an_attr=<\.\.\.>\) at remote 0x[0-9a-f]+>', |
| 438 | gdb_repr), |
| 439 | 'Unexpected gdb representation: %r\n%s' % \ |
| 440 | (gdb_repr, gdb_output)) |
| 441 | |
| 442 | def test_selfreferential_new_style_instance(self): |
| 443 | gdb_repr, gdb_output = \ |
| 444 | self.get_gdb_repr(''' |
| 445 | class Foo(object): |
| 446 | pass |
| 447 | foo = Foo() |
| 448 | foo.an_attr = foo |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 449 | id(foo)''') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 450 | self.assertTrue(re.match('<Foo\(an_attr=<\.\.\.>\) at remote 0x[0-9a-f]+>', |
| 451 | gdb_repr), |
| 452 | 'Unexpected gdb representation: %r\n%s' % \ |
| 453 | (gdb_repr, gdb_output)) |
| 454 | |
| 455 | gdb_repr, gdb_output = \ |
| 456 | self.get_gdb_repr(''' |
| 457 | class Foo(object): |
| 458 | pass |
| 459 | a = Foo() |
| 460 | b = Foo() |
| 461 | a.an_attr = b |
| 462 | b.an_attr = a |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 463 | id(a)''') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 464 | self.assertTrue(re.match('<Foo\(an_attr=<Foo\(an_attr=<\.\.\.>\) at remote 0x[0-9a-f]+>\) at remote 0x[0-9a-f]+>', |
| 465 | gdb_repr), |
| 466 | 'Unexpected gdb representation: %r\n%s' % \ |
| 467 | (gdb_repr, gdb_output)) |
| 468 | |
| 469 | def test_truncation(self): |
| 470 | 'Verify that very long output is truncated' |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 471 | gdb_repr, gdb_output = self.get_gdb_repr('id(list(range(1000)))') |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 472 | self.assertEquals(gdb_repr, |
| 473 | "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, " |
| 474 | "14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, " |
| 475 | "27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, " |
| 476 | "40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, " |
| 477 | "53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, " |
| 478 | "66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, " |
| 479 | "79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, " |
| 480 | "92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, " |
| 481 | "104, 105, 106, 107, 108, 109, 110, 111, 112, 113, " |
| 482 | "114, 115, 116, 117, 118, 119, 120, 121, 122, 123, " |
| 483 | "124, 125, 126, 127, 128, 129, 130, 131, 132, 133, " |
| 484 | "134, 135, 136, 137, 138, 139, 140, 141, 142, 143, " |
| 485 | "144, 145, 146, 147, 148, 149, 150, 151, 152, 153, " |
| 486 | "154, 155, 156, 157, 158, 159, 160, 161, 162, 163, " |
| 487 | "164, 165, 166, 167, 168, 169, 170, 171, 172, 173, " |
| 488 | "174, 175, 176, 177, 178, 179, 180, 181, 182, 183, " |
| 489 | "184, 185, 186, 187, 188, 189, 190, 191, 192, 193, " |
| 490 | "194, 195, 196, 197, 198, 199, 200, 201, 202, 203, " |
| 491 | "204, 205, 206, 207, 208, 209, 210, 211, 212, 213, " |
| 492 | "214, 215, 216, 217, 218, 219, 220, 221, 222, 223, " |
| 493 | "224, 225, 226...(truncated)") |
| 494 | self.assertEquals(len(gdb_repr), |
| 495 | 1024 + len('...(truncated)')) |
| 496 | |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 497 | def test_builtin_method(self): |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 498 | gdb_repr, gdb_output = self.get_gdb_repr('import sys; id(sys.stdout.readlines)') |
| 499 | self.assertTrue(re.match('<built-in method readlines of _io.TextIOWrapper object at remote 0x[0-9a-f]+>', |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 500 | gdb_repr), |
| 501 | 'Unexpected gdb representation: %r\n%s' % \ |
| 502 | (gdb_repr, gdb_output)) |
| 503 | |
| 504 | def test_frames(self): |
| 505 | gdb_output = self.get_stack_trace(''' |
| 506 | def foo(a, b, c): |
| 507 | pass |
| 508 | |
| 509 | foo(3, 4, 5) |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 510 | id(foo.__code__)''', |
| 511 | breakpoint='builtin_id', |
| 512 | cmds_after_breakpoint=['print (PyFrameObject*)(((PyCodeObject*)v)->co_zombieframe)'] |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 513 | ) |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 514 | self.assertTrue(re.match('.*\s+\$1 =\s+Frame 0x[0-9a-f]+, for file <string>, line 3, in foo \(\)\s+.*', |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 515 | gdb_output, |
| 516 | re.DOTALL), |
| 517 | 'Unexpected gdb representation: %r\n%s' % (gdb_output, gdb_output)) |
| 518 | |
| 519 | class PyListTests(DebuggerTests): |
| 520 | def assertListing(self, expected, actual): |
| 521 | self.assertEndsWith(actual, expected) |
| 522 | |
| 523 | def test_basic_command(self): |
| 524 | 'Verify that the "py-list" command works' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 525 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 526 | cmds_after_breakpoint=['py-list']) |
| 527 | |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 528 | self.assertListing(' 5 \n' |
| 529 | ' 6 def bar(a, b, c):\n' |
| 530 | ' 7 baz(a, b, c)\n' |
| 531 | ' 8 \n' |
| 532 | ' 9 def baz(*args):\n' |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 533 | ' >10 id(42)\n' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 534 | ' 11 \n' |
| 535 | ' 12 foo(1, 2, 3)\n', |
| 536 | bt) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 537 | |
| 538 | def test_one_abs_arg(self): |
| 539 | 'Verify the "py-list" command with one absolute argument' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 540 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 541 | cmds_after_breakpoint=['py-list 9']) |
| 542 | |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 543 | self.assertListing(' 9 def baz(*args):\n' |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 544 | ' >10 id(42)\n' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 545 | ' 11 \n' |
| 546 | ' 12 foo(1, 2, 3)\n', |
| 547 | bt) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 548 | |
| 549 | def test_two_abs_args(self): |
| 550 | 'Verify the "py-list" command with two absolute arguments' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 551 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 552 | cmds_after_breakpoint=['py-list 1,3']) |
| 553 | |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 554 | self.assertListing(' 1 # Sample script for use by test_gdb.py\n' |
| 555 | ' 2 \n' |
| 556 | ' 3 def foo(a, b, c):\n', |
| 557 | bt) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 558 | |
| 559 | class StackNavigationTests(DebuggerTests): |
Victor Stinner | 50eb60e | 2010-04-20 22:32:07 +0000 | [diff] [blame] | 560 | @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 561 | def test_pyup_command(self): |
| 562 | 'Verify that the "py-up" command works' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 563 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 564 | cmds_after_breakpoint=['py-up']) |
| 565 | self.assertMultilineMatches(bt, |
| 566 | r'''^.* |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 567 | #[0-9]+ Frame 0x[0-9a-f]+, for file .*gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 568 | baz\(a, b, c\) |
| 569 | $''') |
| 570 | |
Victor Stinner | 50eb60e | 2010-04-20 22:32:07 +0000 | [diff] [blame] | 571 | @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 572 | def test_down_at_bottom(self): |
| 573 | 'Verify handling of "py-down" at the bottom of the stack' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 574 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 575 | cmds_after_breakpoint=['py-down']) |
| 576 | self.assertEndsWith(bt, |
| 577 | 'Unable to find a newer python frame\n') |
| 578 | |
Victor Stinner | 50eb60e | 2010-04-20 22:32:07 +0000 | [diff] [blame] | 579 | @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 580 | def test_up_at_top(self): |
| 581 | 'Verify handling of "py-up" at the top of the stack' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 582 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 583 | cmds_after_breakpoint=['py-up'] * 4) |
| 584 | self.assertEndsWith(bt, |
| 585 | 'Unable to find an older python frame\n') |
| 586 | |
Victor Stinner | 50eb60e | 2010-04-20 22:32:07 +0000 | [diff] [blame] | 587 | @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 588 | def test_up_then_down(self): |
| 589 | 'Verify "py-up" followed by "py-down"' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 590 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 591 | cmds_after_breakpoint=['py-up', 'py-down']) |
| 592 | self.assertMultilineMatches(bt, |
| 593 | r'''^.* |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 594 | #[0-9]+ Frame 0x[0-9a-f]+, for file .*gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 595 | baz\(a, b, c\) |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 596 | #[0-9]+ Frame 0x[0-9a-f]+, for file .*gdb_sample.py, line 10, in baz \(args=\(1, 2, 3\)\) |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 597 | id\(42\) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 598 | $''') |
| 599 | |
| 600 | class PyBtTests(DebuggerTests): |
| 601 | def test_basic_command(self): |
| 602 | 'Verify that the "py-bt" command works' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 603 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 604 | cmds_after_breakpoint=['py-bt']) |
| 605 | self.assertMultilineMatches(bt, |
| 606 | r'''^.* |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 607 | #[0-9]+ Frame 0x[0-9a-f]+, for file .*gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 608 | baz\(a, b, c\) |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 609 | #[0-9]+ Frame 0x[0-9a-f]+, for file .*gdb_sample.py, line 4, in foo \(a=1, b=2, c=3\) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 610 | bar\(a, b, c\) |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 611 | #[0-9]+ Frame 0x[0-9a-f]+, for file .*gdb_sample.py, line 12, in <module> \(\) |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 612 | foo\(1, 2, 3\) |
| 613 | ''') |
| 614 | |
| 615 | class PyPrintTests(DebuggerTests): |
| 616 | def test_basic_command(self): |
| 617 | 'Verify that the "py-print" command works' |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 618 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 619 | cmds_after_breakpoint=['py-print args']) |
| 620 | self.assertMultilineMatches(bt, |
| 621 | r".*\nlocal 'args' = \(1, 2, 3\)\n.*") |
| 622 | |
Victor Stinner | 50eb60e | 2010-04-20 22:32:07 +0000 | [diff] [blame] | 623 | @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 624 | def test_print_after_up(self): |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 625 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 626 | cmds_after_breakpoint=['py-up', 'py-print c', 'py-print b', 'py-print a']) |
| 627 | self.assertMultilineMatches(bt, |
| 628 | r".*\nlocal 'c' = 3\nlocal 'b' = 2\nlocal 'a' = 1\n.*") |
| 629 | |
| 630 | def test_printing_global(self): |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 631 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 632 | cmds_after_breakpoint=['py-print __name__']) |
| 633 | self.assertMultilineMatches(bt, |
| 634 | r".*\nglobal '__name__' = '__main__'\n.*") |
| 635 | |
| 636 | def test_printing_builtin(self): |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 637 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 638 | cmds_after_breakpoint=['py-print len']) |
| 639 | self.assertMultilineMatches(bt, |
Martin v. Löwis | 5ae6810 | 2010-04-21 22:38:42 +0000 | [diff] [blame^] | 640 | r".*\nbuiltin 'len' = <built-in method len of module object at remote 0x[0-9a-f]+>\n.*") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 641 | |
| 642 | class PyLocalsTests(DebuggerTests): |
| 643 | def test_basic_command(self): |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 644 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 645 | cmds_after_breakpoint=['py-locals']) |
| 646 | self.assertMultilineMatches(bt, |
| 647 | r".*\nargs = \(1, 2, 3\)\n.*") |
| 648 | |
Victor Stinner | 50eb60e | 2010-04-20 22:32:07 +0000 | [diff] [blame] | 649 | @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands") |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 650 | def test_locals_after_up(self): |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 651 | bt = self.get_stack_trace(script=self.get_sample_script(), |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 652 | cmds_after_breakpoint=['py-up', 'py-locals']) |
| 653 | self.assertMultilineMatches(bt, |
| 654 | r".*\na = 1\nb = 2\nc = 3\n.*") |
| 655 | |
| 656 | def test_main(): |
Martin v. Löwis | 5226fd6 | 2010-04-21 06:05:58 +0000 | [diff] [blame] | 657 | run_unittest(PrettyPrintTests, |
| 658 | PyListTests, |
| 659 | StackNavigationTests, |
| 660 | PyBtTests, |
| 661 | PyPrintTests, |
| 662 | PyLocalsTests |
Benjamin Peterson | 6a6666a | 2010-04-11 21:49:28 +0000 | [diff] [blame] | 663 | ) |
| 664 | |
| 665 | if __name__ == "__main__": |
| 666 | test_main() |