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