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