blob: a872e68250c3c6ee53319ae44cad43b7a1157b6d [file] [log] [blame]
Benjamin Peterson6a6666a2010-04-11 21:49:28 +00001# 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
6import os
7import re
8import subprocess
9import sys
Vinay Sajipf1b34ee2012-05-06 12:03:05 +010010import sysconfig
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000011import unittest
Victor Stinner150016f2010-05-19 23:04:56 +000012import locale
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000013
David Malcolm8d37ffa2012-06-27 14:15:34 -040014# Is this Python configured to support threads?
15try:
16 import _thread
17except ImportError:
18 _thread = None
19
Benjamin Peterson65c66ab2010-10-29 21:31:35 +000020from test.support import run_unittest, findfile, python_is_optimized
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000021
22try:
23 gdb_version, _ = subprocess.Popen(["gdb", "--version"],
24 stdout=subprocess.PIPE).communicate()
25except OSError:
26 # This is what "no gdb" looks like. There may, however, be other
27 # errors that manifest this way too.
28 raise unittest.SkipTest("Couldn't find gdb on the path")
29gdb_version_number = re.search(b"^GNU gdb [^\d]*(\d+)\.", gdb_version)
30if int(gdb_version_number.group(1)) < 7:
31 raise unittest.SkipTest("gdb versions before 7.0 didn't support python embedding"
Antoine Pitrou6a45e9d2010-04-11 22:47:34 +000032 " Saw:\n" + gdb_version.decode('ascii', 'replace'))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000033
Vinay Sajipf1b34ee2012-05-06 12:03:05 +010034if not sysconfig.is_python_build():
35 raise unittest.SkipTest("test_gdb only works on source builds at the moment.")
36
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000037# Verify that "gdb" was built with the embedded python support enabled:
38cmd = "--eval-command=python import sys; print sys.version_info"
39p = subprocess.Popen(["gdb", "--batch", cmd],
40 stdout=subprocess.PIPE)
41gdbpy_version, _ = p.communicate()
Benjamin Peterson9faa7ec2010-04-11 23:51:24 +000042if gdbpy_version == b'':
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000043 raise unittest.SkipTest("gdb not built with embedded python support")
44
Nick Coghlanbe4e4b52012-06-17 18:57:20 +100045# Verify that "gdb" can load our custom hooks
46p = subprocess.Popen(["gdb", "--batch", cmd,
47 "--args", sys.executable],
48 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
49__, gdbpy_errors = p.communicate()
50if b"auto-loading has been declined" in gdbpy_errors:
51 msg = "gdb security settings prevent use of custom hooks: %s"
52 raise unittest.SkipTest(msg % gdbpy_errors)
53
Victor Stinner50eb60e2010-04-20 22:32:07 +000054def gdb_has_frame_select():
55 # Does this build of gdb have gdb.Frame.select ?
56 cmd = "--eval-command=python print(dir(gdb.Frame))"
57 p = subprocess.Popen(["gdb", "--batch", cmd],
58 stdout=subprocess.PIPE)
59 stdout, _ = p.communicate()
60 m = re.match(br'.*\[(.*)\].*', stdout)
61 if not m:
62 raise unittest.SkipTest("Unable to parse output from gdb.Frame.select test")
63 gdb_frame_dir = m.group(1).split(b', ')
64 return b"'select'" in gdb_frame_dir
65
66HAS_PYUP_PYDOWN = gdb_has_frame_select()
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000067
Martin v. Löwis5ae68102010-04-21 22:38:42 +000068BREAKPOINT_FN='builtin_id'
69
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000070class DebuggerTests(unittest.TestCase):
71
72 """Test that the debugger can debug Python."""
73
Georg Brandl09a7c722012-02-20 21:31:46 +010074 def run_gdb(self, *args, **env_vars):
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000075 """Runs gdb with the command line given by *args.
76
77 Returns its stdout, stderr
78 """
Georg Brandl09a7c722012-02-20 21:31:46 +010079 if env_vars:
80 env = os.environ.copy()
81 env.update(env_vars)
82 else:
83 env = None
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000084 out, err = subprocess.Popen(
Georg Brandl09a7c722012-02-20 21:31:46 +010085 args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env,
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000086 ).communicate()
Victor Stinner534db4e2010-04-23 20:33:55 +000087 return out.decode('utf-8', 'replace'), err.decode('utf-8', 'replace')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000088
89 def get_stack_trace(self, source=None, script=None,
Martin v. Löwis5ae68102010-04-21 22:38:42 +000090 breakpoint=BREAKPOINT_FN,
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000091 cmds_after_breakpoint=None,
92 import_site=False):
93 '''
94 Run 'python -c SOURCE' under gdb with a breakpoint.
95
96 Support injecting commands after the breakpoint is reached
97
98 Returns the stdout from gdb
99
100 cmds_after_breakpoint: if provided, a list of strings: gdb commands
101 '''
102 # We use "set breakpoint pending yes" to avoid blocking with a:
103 # Function "foo" not defined.
104 # Make breakpoint pending on future shared library load? (y or [n])
105 # error, which typically happens python is dynamically linked (the
106 # breakpoints of interest are to be found in the shared library)
107 # When this happens, we still get:
Victor Stinner67df3a42010-04-21 13:53:05 +0000108 # Function "textiowrapper_write" not defined.
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000109 # emitted to stderr each time, alas.
110
111 # Initially I had "--eval-command=continue" here, but removed it to
112 # avoid repeated print breakpoints when traversing hierarchical data
113 # structures
114
115 # Generate a list of commands in gdb's language:
116 commands = ['set breakpoint pending yes',
117 'break %s' % breakpoint,
118 'run']
119 if cmds_after_breakpoint:
120 commands += cmds_after_breakpoint
121 else:
122 commands += ['backtrace']
123
124 # print commands
125
126 # Use "commands" to generate the arguments with which to invoke "gdb":
127 args = ["gdb", "--batch"]
128 args += ['--eval-command=%s' % cmd for cmd in commands]
129 args += ["--args",
130 sys.executable]
131
132 if not import_site:
133 # -S suppresses the default 'import site'
134 args += ["-S"]
135
136 if source:
137 args += ["-c", source]
138 elif script:
139 args += [script]
140
141 # print args
142 # print ' '.join(args)
143
144 # Use "args" to invoke gdb, capturing stdout, stderr:
Georg Brandl09a7c722012-02-20 21:31:46 +0100145 out, err = self.run_gdb(*args, PYTHONHASHSEED='0')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000146
147 # Ignore some noise on stderr due to the pending breakpoint:
148 err = err.replace('Function "%s" not defined.\n' % breakpoint, '')
Antoine Pitroua045f192010-05-05 18:30:22 +0000149 # Ignore some other noise on stderr (http://bugs.python.org/issue8600)
150 err = err.replace("warning: Unable to find libthread_db matching"
151 " inferior's thread library, thread debugging will"
152 " not be available.\n",
153 '')
Jesus Ceacee36552011-03-16 01:33:16 +0100154 err = err.replace("warning: Cannot initialize thread debugging"
155 " library: Debugger service failed\n",
156 '')
Benjamin Petersonf8a9a832012-09-20 23:48:23 -0400157 err = err.replace('warning: Could not load shared library symbols for '
158 'linux-vdso.so.1.\n'
159 'Do you need "set solib-search-path" or '
160 '"set sysroot"?\n',
161 '')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000162
163 # Ensure no unexpected error messages:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000164 self.assertEqual(err, '')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000165 return out
166
167 def get_gdb_repr(self, source,
168 cmds_after_breakpoint=None,
169 import_site=False):
170 # Given an input python source representation of data,
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000171 # run "python -c'id(DATA)'" under gdb with a breakpoint on
172 # builtin_id and scrape out gdb's representation of the "op"
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000173 # parameter, and verify that the gdb displays the same string
174 #
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000175 # Verify that the gdb displays the expected string
176 #
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000177 # For a nested structure, the first time we hit the breakpoint will
178 # give us the top-level structure
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000179 gdb_output = self.get_stack_trace(source, breakpoint=BREAKPOINT_FN,
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000180 cmds_after_breakpoint=cmds_after_breakpoint,
181 import_site=import_site)
182 # gdb can insert additional '\n' and space characters in various places
183 # in its output, depending on the width of the terminal it's connected
184 # to (using its "wrap_here" function)
David Malcolm8d37ffa2012-06-27 14:15:34 -0400185 m = re.match('.*#0\s+builtin_id\s+\(self\=.*,\s+v=\s*(.*?)\)\s+at\s+\S*Python/bltinmodule.c.*',
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000186 gdb_output, re.DOTALL)
187 if not m:
188 self.fail('Unexpected gdb output: %r\n%s' % (gdb_output, gdb_output))
189 return m.group(1), gdb_output
190
191 def assertEndsWith(self, actual, exp_end):
192 '''Ensure that the given "actual" string ends with "exp_end"'''
Ezio Melottib3aedd42010-11-20 19:04:17 +0000193 self.assertTrue(actual.endswith(exp_end),
194 msg='%r did not end with %r' % (actual, exp_end))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000195
196 def assertMultilineMatches(self, actual, pattern):
197 m = re.match(pattern, actual, re.DOTALL)
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000198 if not m:
199 self.fail(msg='%r did not match %r' % (actual, pattern))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000200
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000201 def get_sample_script(self):
202 return findfile('gdb_sample.py')
203
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000204class PrettyPrintTests(DebuggerTests):
205 def test_getting_backtrace(self):
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000206 gdb_output = self.get_stack_trace('id(42)')
207 self.assertTrue(BREAKPOINT_FN in gdb_output)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000208
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000209 def assertGdbRepr(self, val, exp_repr=None, cmds_after_breakpoint=None):
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000210 # Ensure that gdb's rendering of the value in a debugged process
211 # matches repr(value) in this process:
Victor Stinner150016f2010-05-19 23:04:56 +0000212 gdb_repr, gdb_output = self.get_gdb_repr('id(' + ascii(val) + ')',
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000213 cmds_after_breakpoint)
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000214 if not exp_repr:
215 exp_repr = repr(val)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000216 self.assertEqual(gdb_repr, exp_repr,
217 ('%r did not equal expected %r; full output was:\n%s'
218 % (gdb_repr, exp_repr, gdb_output)))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000219
220 def test_int(self):
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000221 'Verify the pretty-printing of various "int"/long values'
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000222 self.assertGdbRepr(42)
223 self.assertGdbRepr(0)
224 self.assertGdbRepr(-7)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000225 self.assertGdbRepr(1000000000000)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000226 self.assertGdbRepr(-1000000000000000)
227
228 def test_singletons(self):
229 'Verify the pretty-printing of True, False and None'
230 self.assertGdbRepr(True)
231 self.assertGdbRepr(False)
232 self.assertGdbRepr(None)
233
234 def test_dicts(self):
235 'Verify the pretty-printing of dictionaries'
236 self.assertGdbRepr({})
237 self.assertGdbRepr({'foo': 'bar'})
Georg Brandl09a7c722012-02-20 21:31:46 +0100238 self.assertGdbRepr({'foo': 'bar', 'douglas': 42},
239 "{'foo': 'bar', 'douglas': 42}")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000240
241 def test_lists(self):
242 'Verify the pretty-printing of lists'
243 self.assertGdbRepr([])
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000244 self.assertGdbRepr(list(range(5)))
245
246 def test_bytes(self):
247 'Verify the pretty-printing of bytes'
248 self.assertGdbRepr(b'')
249 self.assertGdbRepr(b'And now for something hopefully the same')
250 self.assertGdbRepr(b'string with embedded NUL here \0 and then some more text')
251 self.assertGdbRepr(b'this is a tab:\t'
252 b' this is a slash-N:\n'
253 b' this is a slash-R:\r'
254 )
255
256 self.assertGdbRepr(b'this is byte 255:\xff and byte 128:\x80')
257
258 self.assertGdbRepr(bytes([b for b in range(255)]))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000259
260 def test_strings(self):
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000261 'Verify the pretty-printing of unicode strings'
Victor Stinner150016f2010-05-19 23:04:56 +0000262 encoding = locale.getpreferredencoding()
263 def check_repr(text):
264 try:
265 text.encode(encoding)
266 printable = True
267 except UnicodeEncodeError:
Antoine Pitrou4c7c4212010-09-09 20:40:28 +0000268 self.assertGdbRepr(text, ascii(text))
Victor Stinner150016f2010-05-19 23:04:56 +0000269 else:
270 self.assertGdbRepr(text)
271
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000272 self.assertGdbRepr('')
273 self.assertGdbRepr('And now for something hopefully the same')
274 self.assertGdbRepr('string with embedded NUL here \0 and then some more text')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000275
276 # Test printing a single character:
277 # U+2620 SKULL AND CROSSBONES
Victor Stinner150016f2010-05-19 23:04:56 +0000278 check_repr('\u2620')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000279
280 # Test printing a Japanese unicode string
281 # (I believe this reads "mojibake", using 3 characters from the CJK
282 # Unified Ideographs area, followed by U+3051 HIRAGANA LETTER KE)
Victor Stinner150016f2010-05-19 23:04:56 +0000283 check_repr('\u6587\u5b57\u5316\u3051')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000284
285 # Test a character outside the BMP:
286 # U+1D121 MUSICAL SYMBOL C CLEF
287 # This is:
288 # UTF-8: 0xF0 0x9D 0x84 0xA1
289 # UTF-16: 0xD834 0xDD21
Victor Stinner150016f2010-05-19 23:04:56 +0000290 check_repr(chr(0x1D121))
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000291
292 def test_tuples(self):
293 'Verify the pretty-printing of tuples'
294 self.assertGdbRepr(tuple())
295 self.assertGdbRepr((1,), '(1,)')
296 self.assertGdbRepr(('foo', 'bar', 'baz'))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000297
298 def test_sets(self):
299 'Verify the pretty-printing of sets'
300 self.assertGdbRepr(set())
Georg Brandl09a7c722012-02-20 21:31:46 +0100301 self.assertGdbRepr(set(['a', 'b']), "{'a', 'b'}")
302 self.assertGdbRepr(set([4, 5, 6]), "{4, 5, 6}")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000303
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000304 # Ensure that we handle sets containing the "dummy" key value,
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000305 # which happens on deletion:
306 gdb_repr, gdb_output = self.get_gdb_repr('''s = set(['a','b'])
307s.pop()
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000308id(s)''')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000309 self.assertEqual(gdb_repr, "{'b'}")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000310
311 def test_frozensets(self):
312 'Verify the pretty-printing of frozensets'
313 self.assertGdbRepr(frozenset())
Georg Brandl09a7c722012-02-20 21:31:46 +0100314 self.assertGdbRepr(frozenset(['a', 'b']), "frozenset({'a', 'b'})")
315 self.assertGdbRepr(frozenset([4, 5, 6]), "frozenset({4, 5, 6})")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000316
317 def test_exceptions(self):
318 # Test a RuntimeError
319 gdb_repr, gdb_output = self.get_gdb_repr('''
320try:
321 raise RuntimeError("I am an error")
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000322except RuntimeError as e:
323 id(e)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000324''')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000325 self.assertEqual(gdb_repr,
326 "RuntimeError('I am an error',)")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000327
328
329 # Test division by zero:
330 gdb_repr, gdb_output = self.get_gdb_repr('''
331try:
332 a = 1 / 0
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000333except ZeroDivisionError as e:
334 id(e)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000335''')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000336 self.assertEqual(gdb_repr,
337 "ZeroDivisionError('division by zero',)")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000338
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000339 def test_modern_class(self):
340 'Verify the pretty-printing of new-style class instances'
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000341 gdb_repr, gdb_output = self.get_gdb_repr('''
342class Foo:
343 pass
344foo = Foo()
345foo.an_int = 42
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000346id(foo)''')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100347 m = re.match(r'<Foo\(an_int=42\) at remote 0x-?[0-9a-f]+>', gdb_repr)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000348 self.assertTrue(m,
349 msg='Unexpected new-style class rendering %r' % gdb_repr)
350
351 def test_subclassing_list(self):
352 'Verify the pretty-printing of an instance of a list subclass'
353 gdb_repr, gdb_output = self.get_gdb_repr('''
354class Foo(list):
355 pass
356foo = Foo()
357foo += [1, 2, 3]
358foo.an_int = 42
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000359id(foo)''')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100360 m = re.match(r'<Foo\(an_int=42\) at remote 0x-?[0-9a-f]+>', gdb_repr)
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000361
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000362 self.assertTrue(m,
363 msg='Unexpected new-style class rendering %r' % gdb_repr)
364
365 def test_subclassing_tuple(self):
366 'Verify the pretty-printing of an instance of a tuple subclass'
367 # This should exercise the negative tp_dictoffset code in the
368 # new-style class support
369 gdb_repr, gdb_output = self.get_gdb_repr('''
370class Foo(tuple):
371 pass
372foo = Foo((1, 2, 3))
373foo.an_int = 42
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000374id(foo)''')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100375 m = re.match(r'<Foo\(an_int=42\) at remote 0x-?[0-9a-f]+>', gdb_repr)
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000376
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000377 self.assertTrue(m,
378 msg='Unexpected new-style class rendering %r' % gdb_repr)
379
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000380 def assertSane(self, source, corruption, exprepr=None):
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000381 '''Run Python under gdb, corrupting variables in the inferior process
382 immediately before taking a backtrace.
383
384 Verify that the variable's representation is the expected failsafe
385 representation'''
386 if corruption:
387 cmds_after_breakpoint=[corruption, 'backtrace']
388 else:
389 cmds_after_breakpoint=['backtrace']
390
391 gdb_repr, gdb_output = \
392 self.get_gdb_repr(source,
393 cmds_after_breakpoint=cmds_after_breakpoint)
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000394 if exprepr:
395 if gdb_repr == exprepr:
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000396 # gdb managed to print the value in spite of the corruption;
397 # this is good (see http://bugs.python.org/issue8330)
398 return
399
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000400 # Match anything for the type name; 0xDEADBEEF could point to
401 # something arbitrary (see http://bugs.python.org/issue8330)
Antoine Pitrou4d098732011-11-26 01:42:03 +0100402 pattern = '<.* at remote 0x-?[0-9a-f]+>'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000403
404 m = re.match(pattern, gdb_repr)
405 if not m:
406 self.fail('Unexpected gdb representation: %r\n%s' % \
407 (gdb_repr, gdb_output))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000408
409 def test_NULL_ptr(self):
410 'Ensure that a NULL PyObject* is handled gracefully'
411 gdb_repr, gdb_output = (
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000412 self.get_gdb_repr('id(42)',
413 cmds_after_breakpoint=['set variable v=0',
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000414 'backtrace'])
415 )
416
Ezio Melottib3aedd42010-11-20 19:04:17 +0000417 self.assertEqual(gdb_repr, '0x0')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000418
419 def test_NULL_ob_type(self):
420 'Ensure that a PyObject* with NULL ob_type is handled gracefully'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000421 self.assertSane('id(42)',
422 'set v->ob_type=0')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000423
424 def test_corrupt_ob_type(self):
425 'Ensure that a PyObject* with a corrupt ob_type is handled gracefully'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000426 self.assertSane('id(42)',
427 'set v->ob_type=0xDEADBEEF',
428 exprepr='42')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000429
430 def test_corrupt_tp_flags(self):
431 'Ensure that a PyObject* with a type with corrupt tp_flags is handled'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000432 self.assertSane('id(42)',
433 'set v->ob_type->tp_flags=0x0',
434 exprepr='42')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000435
436 def test_corrupt_tp_name(self):
437 'Ensure that a PyObject* with a type with corrupt tp_name is handled'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000438 self.assertSane('id(42)',
439 'set v->ob_type->tp_name=0xDEADBEEF',
440 exprepr='42')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000441
442 def test_builtins_help(self):
443 'Ensure that the new-style class _Helper in site.py can be handled'
444 # (this was the issue causing tracebacks in
445 # http://bugs.python.org/issue8032#msg100537 )
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000446 gdb_repr, gdb_output = self.get_gdb_repr('id(__builtins__.help)', import_site=True)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000447
Antoine Pitrou4d098732011-11-26 01:42:03 +0100448 m = re.match(r'<_Helper at remote 0x-?[0-9a-f]+>', gdb_repr)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000449 self.assertTrue(m,
450 msg='Unexpected rendering %r' % gdb_repr)
451
452 def test_selfreferential_list(self):
453 '''Ensure that a reference loop involving a list doesn't lead proxyval
454 into an infinite loop:'''
455 gdb_repr, gdb_output = \
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000456 self.get_gdb_repr("a = [3, 4, 5] ; a.append(a) ; id(a)")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000457 self.assertEqual(gdb_repr, '[3, 4, 5, [...]]')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000458
459 gdb_repr, gdb_output = \
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000460 self.get_gdb_repr("a = [3, 4, 5] ; b = [a] ; a.append(b) ; id(a)")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000461 self.assertEqual(gdb_repr, '[3, 4, 5, [[...]]]')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000462
463 def test_selfreferential_dict(self):
464 '''Ensure that a reference loop involving a dict doesn't lead proxyval
465 into an infinite loop:'''
466 gdb_repr, gdb_output = \
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000467 self.get_gdb_repr("a = {} ; b = {'bar':a} ; a['foo'] = b ; id(a)")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000468
Ezio Melottib3aedd42010-11-20 19:04:17 +0000469 self.assertEqual(gdb_repr, "{'foo': {'bar': {...}}}")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000470
471 def test_selfreferential_old_style_instance(self):
472 gdb_repr, gdb_output = \
473 self.get_gdb_repr('''
474class Foo:
475 pass
476foo = Foo()
477foo.an_attr = foo
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000478id(foo)''')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100479 self.assertTrue(re.match('<Foo\(an_attr=<\.\.\.>\) at remote 0x-?[0-9a-f]+>',
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000480 gdb_repr),
481 'Unexpected gdb representation: %r\n%s' % \
482 (gdb_repr, gdb_output))
483
484 def test_selfreferential_new_style_instance(self):
485 gdb_repr, gdb_output = \
486 self.get_gdb_repr('''
487class Foo(object):
488 pass
489foo = Foo()
490foo.an_attr = foo
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000491id(foo)''')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100492 self.assertTrue(re.match('<Foo\(an_attr=<\.\.\.>\) at remote 0x-?[0-9a-f]+>',
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000493 gdb_repr),
494 'Unexpected gdb representation: %r\n%s' % \
495 (gdb_repr, gdb_output))
496
497 gdb_repr, gdb_output = \
498 self.get_gdb_repr('''
499class Foo(object):
500 pass
501a = Foo()
502b = Foo()
503a.an_attr = b
504b.an_attr = a
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000505id(a)''')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100506 self.assertTrue(re.match('<Foo\(an_attr=<Foo\(an_attr=<\.\.\.>\) at remote 0x-?[0-9a-f]+>\) at remote 0x-?[0-9a-f]+>',
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000507 gdb_repr),
508 'Unexpected gdb representation: %r\n%s' % \
509 (gdb_repr, gdb_output))
510
511 def test_truncation(self):
512 'Verify that very long output is truncated'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000513 gdb_repr, gdb_output = self.get_gdb_repr('id(list(range(1000)))')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000514 self.assertEqual(gdb_repr,
515 "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, "
516 "14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, "
517 "27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, "
518 "40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, "
519 "53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, "
520 "66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, "
521 "79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, "
522 "92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, "
523 "104, 105, 106, 107, 108, 109, 110, 111, 112, 113, "
524 "114, 115, 116, 117, 118, 119, 120, 121, 122, 123, "
525 "124, 125, 126, 127, 128, 129, 130, 131, 132, 133, "
526 "134, 135, 136, 137, 138, 139, 140, 141, 142, 143, "
527 "144, 145, 146, 147, 148, 149, 150, 151, 152, 153, "
528 "154, 155, 156, 157, 158, 159, 160, 161, 162, 163, "
529 "164, 165, 166, 167, 168, 169, 170, 171, 172, 173, "
530 "174, 175, 176, 177, 178, 179, 180, 181, 182, 183, "
531 "184, 185, 186, 187, 188, 189, 190, 191, 192, 193, "
532 "194, 195, 196, 197, 198, 199, 200, 201, 202, 203, "
533 "204, 205, 206, 207, 208, 209, 210, 211, 212, 213, "
534 "214, 215, 216, 217, 218, 219, 220, 221, 222, 223, "
535 "224, 225, 226...(truncated)")
536 self.assertEqual(len(gdb_repr),
537 1024 + len('...(truncated)'))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000538
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000539 def test_builtin_method(self):
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000540 gdb_repr, gdb_output = self.get_gdb_repr('import sys; id(sys.stdout.readlines)')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100541 self.assertTrue(re.match('<built-in method readlines of _io.TextIOWrapper object at remote 0x-?[0-9a-f]+>',
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000542 gdb_repr),
543 'Unexpected gdb representation: %r\n%s' % \
544 (gdb_repr, gdb_output))
545
546 def test_frames(self):
547 gdb_output = self.get_stack_trace('''
548def foo(a, b, c):
549 pass
550
551foo(3, 4, 5)
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000552id(foo.__code__)''',
553 breakpoint='builtin_id',
554 cmds_after_breakpoint=['print (PyFrameObject*)(((PyCodeObject*)v)->co_zombieframe)']
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000555 )
Antoine Pitrou4d098732011-11-26 01:42:03 +0100556 self.assertTrue(re.match('.*\s+\$1 =\s+Frame 0x-?[0-9a-f]+, for file <string>, line 3, in foo \(\)\s+.*',
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000557 gdb_output,
558 re.DOTALL),
559 'Unexpected gdb representation: %r\n%s' % (gdb_output, gdb_output))
560
Victor Stinnerd2084162011-12-19 13:42:24 +0100561@unittest.skipIf(python_is_optimized(),
562 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000563class PyListTests(DebuggerTests):
564 def assertListing(self, expected, actual):
565 self.assertEndsWith(actual, expected)
566
567 def test_basic_command(self):
568 'Verify that the "py-list" command works'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000569 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000570 cmds_after_breakpoint=['py-list'])
571
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000572 self.assertListing(' 5 \n'
573 ' 6 def bar(a, b, c):\n'
574 ' 7 baz(a, b, c)\n'
575 ' 8 \n'
576 ' 9 def baz(*args):\n'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000577 ' >10 id(42)\n'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000578 ' 11 \n'
579 ' 12 foo(1, 2, 3)\n',
580 bt)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000581
582 def test_one_abs_arg(self):
583 'Verify the "py-list" command with one absolute argument'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000584 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000585 cmds_after_breakpoint=['py-list 9'])
586
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000587 self.assertListing(' 9 def baz(*args):\n'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000588 ' >10 id(42)\n'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000589 ' 11 \n'
590 ' 12 foo(1, 2, 3)\n',
591 bt)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000592
593 def test_two_abs_args(self):
594 'Verify the "py-list" command with two absolute arguments'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000595 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000596 cmds_after_breakpoint=['py-list 1,3'])
597
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000598 self.assertListing(' 1 # Sample script for use by test_gdb.py\n'
599 ' 2 \n'
600 ' 3 def foo(a, b, c):\n',
601 bt)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000602
603class StackNavigationTests(DebuggerTests):
Victor Stinner50eb60e2010-04-20 22:32:07 +0000604 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Victor Stinnerd2084162011-12-19 13:42:24 +0100605 @unittest.skipIf(python_is_optimized(),
606 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000607 def test_pyup_command(self):
608 'Verify that the "py-up" command works'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000609 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000610 cmds_after_breakpoint=['py-up'])
611 self.assertMultilineMatches(bt,
612 r'''^.*
Antoine Pitrou4d098732011-11-26 01:42:03 +0100613#[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000614 baz\(a, b, c\)
615$''')
616
Victor Stinner50eb60e2010-04-20 22:32:07 +0000617 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000618 def test_down_at_bottom(self):
619 'Verify handling of "py-down" at the bottom of the stack'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000620 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000621 cmds_after_breakpoint=['py-down'])
622 self.assertEndsWith(bt,
623 'Unable to find a newer python frame\n')
624
Victor Stinner50eb60e2010-04-20 22:32:07 +0000625 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000626 def test_up_at_top(self):
627 'Verify handling of "py-up" at the top of the stack'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000628 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000629 cmds_after_breakpoint=['py-up'] * 4)
630 self.assertEndsWith(bt,
631 'Unable to find an older python frame\n')
632
Victor Stinner50eb60e2010-04-20 22:32:07 +0000633 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Victor Stinnerd2084162011-12-19 13:42:24 +0100634 @unittest.skipIf(python_is_optimized(),
635 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000636 def test_up_then_down(self):
637 'Verify "py-up" followed by "py-down"'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000638 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000639 cmds_after_breakpoint=['py-up', 'py-down'])
640 self.assertMultilineMatches(bt,
641 r'''^.*
Antoine Pitrou4d098732011-11-26 01:42:03 +0100642#[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000643 baz\(a, b, c\)
Antoine Pitrou4d098732011-11-26 01:42:03 +0100644#[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 10, in baz \(args=\(1, 2, 3\)\)
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000645 id\(42\)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000646$''')
647
648class PyBtTests(DebuggerTests):
Victor Stinnerd2084162011-12-19 13:42:24 +0100649 @unittest.skipIf(python_is_optimized(),
650 "Python was compiled with optimizations")
Victor Stinnere670c882011-05-13 17:40:15 +0200651 def test_bt(self):
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000652 'Verify that the "py-bt" command works'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000653 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000654 cmds_after_breakpoint=['py-bt'])
655 self.assertMultilineMatches(bt,
656 r'''^.*
Victor Stinnere670c882011-05-13 17:40:15 +0200657Traceback \(most recent call first\):
658 File ".*gdb_sample.py", line 10, in baz
659 id\(42\)
660 File ".*gdb_sample.py", line 7, in bar
661 baz\(a, b, c\)
662 File ".*gdb_sample.py", line 4, in foo
663 bar\(a, b, c\)
664 File ".*gdb_sample.py", line 12, in <module>
665 foo\(1, 2, 3\)
666''')
667
Victor Stinnerd2084162011-12-19 13:42:24 +0100668 @unittest.skipIf(python_is_optimized(),
669 "Python was compiled with optimizations")
Victor Stinnere670c882011-05-13 17:40:15 +0200670 def test_bt_full(self):
671 'Verify that the "py-bt-full" command works'
672 bt = self.get_stack_trace(script=self.get_sample_script(),
673 cmds_after_breakpoint=['py-bt-full'])
674 self.assertMultilineMatches(bt,
675 r'''^.*
Antoine Pitrou4d098732011-11-26 01:42:03 +0100676#[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000677 baz\(a, b, c\)
Antoine Pitrou4d098732011-11-26 01:42:03 +0100678#[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 4, in foo \(a=1, b=2, c=3\)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000679 bar\(a, b, c\)
Antoine Pitrou4d098732011-11-26 01:42:03 +0100680#[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 12, in <module> \(\)
Victor Stinnerd2084162011-12-19 13:42:24 +0100681 foo\(1, 2, 3\)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000682''')
683
David Malcolm8d37ffa2012-06-27 14:15:34 -0400684 @unittest.skipUnless(_thread,
685 "Python was compiled without thread support")
686 def test_threads(self):
687 'Verify that "py-bt" indicates threads that are waiting for the GIL'
688 cmd = '''
689from threading import Thread
690
691class TestThread(Thread):
692 # These threads would run forever, but we'll interrupt things with the
693 # debugger
694 def run(self):
695 i = 0
696 while 1:
697 i += 1
698
699t = {}
700for i in range(4):
701 t[i] = TestThread()
702 t[i].start()
703
704# Trigger a breakpoint on the main thread
705id(42)
706
707'''
708 # Verify with "py-bt":
709 gdb_output = self.get_stack_trace(cmd,
710 cmds_after_breakpoint=['thread apply all py-bt'])
711 self.assertIn('Waiting for the GIL', gdb_output)
712
713 # Verify with "py-bt-full":
714 gdb_output = self.get_stack_trace(cmd,
715 cmds_after_breakpoint=['thread apply all py-bt-full'])
716 self.assertIn('Waiting for the GIL', gdb_output)
717
718 @unittest.skipIf(python_is_optimized(),
719 "Python was compiled with optimizations")
720 # Some older versions of gdb will fail with
721 # "Cannot find new threads: generic error"
722 # unless we add LD_PRELOAD=PATH-TO-libpthread.so.1 as a workaround
723 @unittest.skipUnless(_thread,
724 "Python was compiled without thread support")
725 def test_gc(self):
726 'Verify that "py-bt" indicates if a thread is garbage-collecting'
727 cmd = ('from gc import collect\n'
728 'id(42)\n'
729 'def foo():\n'
730 ' collect()\n'
731 'def bar():\n'
732 ' foo()\n'
733 'bar()\n')
734 # Verify with "py-bt":
735 gdb_output = self.get_stack_trace(cmd,
736 cmds_after_breakpoint=['break update_refs', 'continue', 'py-bt'],
737 )
738 self.assertIn('Garbage-collecting', gdb_output)
739
740 # Verify with "py-bt-full":
741 gdb_output = self.get_stack_trace(cmd,
742 cmds_after_breakpoint=['break update_refs', 'continue', 'py-bt-full'],
743 )
744 self.assertIn('Garbage-collecting', gdb_output)
745
746 @unittest.skipIf(python_is_optimized(),
747 "Python was compiled with optimizations")
748 # Some older versions of gdb will fail with
749 # "Cannot find new threads: generic error"
750 # unless we add LD_PRELOAD=PATH-TO-libpthread.so.1 as a workaround
751 @unittest.skipUnless(_thread,
752 "Python was compiled without thread support")
753 def test_pycfunction(self):
754 'Verify that "py-bt" displays invocations of PyCFunction instances'
755 cmd = ('from time import sleep\n'
756 'def foo():\n'
757 ' sleep(1)\n'
758 'def bar():\n'
759 ' foo()\n'
760 'bar()\n')
761 # Verify with "py-bt":
762 gdb_output = self.get_stack_trace(cmd,
763 breakpoint='time_sleep',
764 cmds_after_breakpoint=['bt', 'py-bt'],
765 )
766 self.assertIn('<built-in method sleep', gdb_output)
767
768 # Verify with "py-bt-full":
769 gdb_output = self.get_stack_trace(cmd,
770 breakpoint='time_sleep',
771 cmds_after_breakpoint=['py-bt-full'],
772 )
773 self.assertIn('#0 <built-in method sleep', gdb_output)
774
775
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000776class PyPrintTests(DebuggerTests):
Victor Stinnerd2084162011-12-19 13:42:24 +0100777 @unittest.skipIf(python_is_optimized(),
778 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000779 def test_basic_command(self):
780 'Verify that the "py-print" command works'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000781 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000782 cmds_after_breakpoint=['py-print args'])
783 self.assertMultilineMatches(bt,
784 r".*\nlocal 'args' = \(1, 2, 3\)\n.*")
785
Vinay Sajip2549f872012-01-04 12:07:30 +0000786 @unittest.skipIf(python_is_optimized(),
787 "Python was compiled with optimizations")
Victor Stinner50eb60e2010-04-20 22:32:07 +0000788 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000789 def test_print_after_up(self):
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000790 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000791 cmds_after_breakpoint=['py-up', 'py-print c', 'py-print b', 'py-print a'])
792 self.assertMultilineMatches(bt,
793 r".*\nlocal 'c' = 3\nlocal 'b' = 2\nlocal 'a' = 1\n.*")
794
Victor Stinnerd2084162011-12-19 13:42:24 +0100795 @unittest.skipIf(python_is_optimized(),
796 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000797 def test_printing_global(self):
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000798 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000799 cmds_after_breakpoint=['py-print __name__'])
800 self.assertMultilineMatches(bt,
801 r".*\nglobal '__name__' = '__main__'\n.*")
802
Victor Stinnerd2084162011-12-19 13:42:24 +0100803 @unittest.skipIf(python_is_optimized(),
804 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000805 def test_printing_builtin(self):
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000806 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000807 cmds_after_breakpoint=['py-print len'])
808 self.assertMultilineMatches(bt,
Antoine Pitrou4d098732011-11-26 01:42:03 +0100809 r".*\nbuiltin 'len' = <built-in method len of module object at remote 0x-?[0-9a-f]+>\n.*")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000810
811class PyLocalsTests(DebuggerTests):
Victor Stinnerd2084162011-12-19 13:42:24 +0100812 @unittest.skipIf(python_is_optimized(),
813 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000814 def test_basic_command(self):
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000815 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000816 cmds_after_breakpoint=['py-locals'])
817 self.assertMultilineMatches(bt,
818 r".*\nargs = \(1, 2, 3\)\n.*")
819
Victor Stinner50eb60e2010-04-20 22:32:07 +0000820 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Vinay Sajip2549f872012-01-04 12:07:30 +0000821 @unittest.skipIf(python_is_optimized(),
822 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000823 def test_locals_after_up(self):
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000824 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000825 cmds_after_breakpoint=['py-up', 'py-locals'])
826 self.assertMultilineMatches(bt,
827 r".*\na = 1\nb = 2\nc = 3\n.*")
828
829def test_main():
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000830 run_unittest(PrettyPrintTests,
831 PyListTests,
832 StackNavigationTests,
833 PyBtTests,
834 PyPrintTests,
835 PyLocalsTests
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000836 )
837
838if __name__ == "__main__":
839 test_main()