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