blob: 846422b8377187c21232fe9a72ed354f02b27c48 [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")
R David Murrayf9333022012-10-27 13:22:41 -040029gdb_version_number = re.search(b"^GNU gdb [^\d]*(\d+)\.(\d)", gdb_version)
30gdb_major_version = int(gdb_version_number.group(1))
31gdb_minor_version = int(gdb_version_number.group(2))
32if gdb_major_version < 7:
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000033 raise unittest.SkipTest("gdb versions before 7.0 didn't support python embedding"
Antoine Pitrou6a45e9d2010-04-11 22:47:34 +000034 " Saw:\n" + gdb_version.decode('ascii', 'replace'))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000035
Vinay Sajipf1b34ee2012-05-06 12:03:05 +010036if not sysconfig.is_python_build():
37 raise unittest.SkipTest("test_gdb only works on source builds at the moment.")
38
R David Murrayf9333022012-10-27 13:22:41 -040039# Location of custom hooks file in a repository checkout.
40checkout_hook_path = os.path.join(os.path.dirname(sys.executable),
41 'python-gdb.py')
42
43def run_gdb(*args, **env_vars):
44 """Runs gdb in --batch mode with the additional arguments given by *args.
45
46 Returns its (stdout, stderr) decoded from utf-8 using the replace handler.
47 """
48 if env_vars:
49 env = os.environ.copy()
50 env.update(env_vars)
51 else:
52 env = None
53 base_cmd = ('gdb', '--batch')
54 if (gdb_major_version, gdb_minor_version) >= (7, 4):
55 base_cmd += ('-iex', 'add-auto-load-safe-path ' + checkout_hook_path)
56 out, err = subprocess.Popen(base_cmd + args,
57 stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env,
58 ).communicate()
59 return out.decode('utf-8', 'replace'), err.decode('utf-8', 'replace')
60
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000061# Verify that "gdb" was built with the embedded python support enabled:
Antoine Pitroub17d2aa2013-11-23 17:40:36 +010062gdbpy_version, _ = run_gdb("--eval-command=python import sys; print(sys.version_info)")
R David Murrayf9333022012-10-27 13:22:41 -040063if not gdbpy_version:
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000064 raise unittest.SkipTest("gdb not built with embedded python support")
65
Nick Coghlance346872013-09-22 19:38:16 +100066# Verify that "gdb" can load our custom hooks, as OS security settings may
67# disallow this without a customised .gdbinit.
R David Murrayf9333022012-10-27 13:22:41 -040068cmd = ['--args', sys.executable]
69_, gdbpy_errors = run_gdb('--args', sys.executable)
70if "auto-loading has been declined" in gdbpy_errors:
71 msg = "gdb security settings prevent use of custom hooks: "
72 raise unittest.SkipTest(msg + gdbpy_errors.rstrip())
Nick Coghlanbe4e4b52012-06-17 18:57:20 +100073
Victor Stinner50eb60e2010-04-20 22:32:07 +000074def gdb_has_frame_select():
75 # Does this build of gdb have gdb.Frame.select ?
R David Murrayf9333022012-10-27 13:22:41 -040076 stdout, _ = run_gdb("--eval-command=python print(dir(gdb.Frame))")
77 m = re.match(r'.*\[(.*)\].*', stdout)
Victor Stinner50eb60e2010-04-20 22:32:07 +000078 if not m:
79 raise unittest.SkipTest("Unable to parse output from gdb.Frame.select test")
R David Murrayf9333022012-10-27 13:22:41 -040080 gdb_frame_dir = m.group(1).split(', ')
81 return "'select'" in gdb_frame_dir
Victor Stinner50eb60e2010-04-20 22:32:07 +000082
83HAS_PYUP_PYDOWN = gdb_has_frame_select()
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000084
Martin v. Löwis5ae68102010-04-21 22:38:42 +000085BREAKPOINT_FN='builtin_id'
86
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000087class DebuggerTests(unittest.TestCase):
88
89 """Test that the debugger can debug Python."""
90
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000091 def get_stack_trace(self, source=None, script=None,
Martin v. Löwis5ae68102010-04-21 22:38:42 +000092 breakpoint=BREAKPOINT_FN,
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000093 cmds_after_breakpoint=None,
94 import_site=False):
95 '''
96 Run 'python -c SOURCE' under gdb with a breakpoint.
97
98 Support injecting commands after the breakpoint is reached
99
100 Returns the stdout from gdb
101
102 cmds_after_breakpoint: if provided, a list of strings: gdb commands
103 '''
104 # We use "set breakpoint pending yes" to avoid blocking with a:
105 # Function "foo" not defined.
106 # Make breakpoint pending on future shared library load? (y or [n])
107 # error, which typically happens python is dynamically linked (the
108 # breakpoints of interest are to be found in the shared library)
109 # When this happens, we still get:
Victor Stinner67df3a42010-04-21 13:53:05 +0000110 # Function "textiowrapper_write" not defined.
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000111 # emitted to stderr each time, alas.
112
113 # Initially I had "--eval-command=continue" here, but removed it to
114 # avoid repeated print breakpoints when traversing hierarchical data
115 # structures
116
117 # Generate a list of commands in gdb's language:
118 commands = ['set breakpoint pending yes',
119 'break %s' % breakpoint,
120 'run']
121 if cmds_after_breakpoint:
122 commands += cmds_after_breakpoint
123 else:
124 commands += ['backtrace']
125
126 # print commands
127
128 # Use "commands" to generate the arguments with which to invoke "gdb":
129 args = ["gdb", "--batch"]
130 args += ['--eval-command=%s' % cmd for cmd in commands]
131 args += ["--args",
132 sys.executable]
133
134 if not import_site:
135 # -S suppresses the default 'import site'
136 args += ["-S"]
137
138 if source:
139 args += ["-c", source]
140 elif script:
141 args += [script]
142
143 # print args
Antoine Pitroua8892a12013-11-24 14:58:17 +0100144 # print (' '.join(args))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000145
146 # Use "args" to invoke gdb, capturing stdout, stderr:
R David Murrayf9333022012-10-27 13:22:41 -0400147 out, err = run_gdb(*args, PYTHONHASHSEED='0')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000148
Antoine Pitrou81641d62013-05-01 00:15:44 +0200149 errlines = err.splitlines()
150 unexpected_errlines = []
151
152 # Ignore some benign messages on stderr.
153 ignore_patterns = (
154 'Function "%s" not defined.' % breakpoint,
155 "warning: no loadable sections found in added symbol-file"
156 " system-supplied DSO",
157 "warning: Unable to find libthread_db matching"
158 " inferior's thread library, thread debugging will"
159 " not be available.",
160 "warning: Cannot initialize thread debugging"
161 " library: Debugger service failed",
162 'warning: Could not load shared library symbols for '
163 'linux-vdso.so',
164 'warning: Could not load shared library symbols for '
165 'linux-gate.so',
166 'Do you need "set solib-search-path" or '
167 '"set sysroot"?',
168 )
169 for line in errlines:
170 if not line.startswith(ignore_patterns):
171 unexpected_errlines.append(line)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000172
173 # Ensure no unexpected error messages:
Antoine Pitrou81641d62013-05-01 00:15:44 +0200174 self.assertEqual(unexpected_errlines, [])
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000175 return out
176
177 def get_gdb_repr(self, source,
178 cmds_after_breakpoint=None,
179 import_site=False):
180 # Given an input python source representation of data,
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000181 # run "python -c'id(DATA)'" under gdb with a breakpoint on
182 # builtin_id and scrape out gdb's representation of the "op"
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000183 # parameter, and verify that the gdb displays the same string
184 #
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000185 # Verify that the gdb displays the expected string
186 #
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000187 # For a nested structure, the first time we hit the breakpoint will
188 # give us the top-level structure
Antoine Pitroua8892a12013-11-24 14:58:17 +0100189
190 # NOTE: avoid decoding too much of the traceback as some
191 # undecodable characters may lurk there in optimized mode
192 # (issue #19743).
193 cmds_after_breakpoint = cmds_after_breakpoint or ["backtrace 1"]
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000194 gdb_output = self.get_stack_trace(source, breakpoint=BREAKPOINT_FN,
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000195 cmds_after_breakpoint=cmds_after_breakpoint,
196 import_site=import_site)
197 # gdb can insert additional '\n' and space characters in various places
198 # in its output, depending on the width of the terminal it's connected
199 # to (using its "wrap_here" function)
David Malcolm8d37ffa2012-06-27 14:15:34 -0400200 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 +0000201 gdb_output, re.DOTALL)
202 if not m:
203 self.fail('Unexpected gdb output: %r\n%s' % (gdb_output, gdb_output))
204 return m.group(1), gdb_output
205
206 def assertEndsWith(self, actual, exp_end):
207 '''Ensure that the given "actual" string ends with "exp_end"'''
Ezio Melottib3aedd42010-11-20 19:04:17 +0000208 self.assertTrue(actual.endswith(exp_end),
209 msg='%r did not end with %r' % (actual, exp_end))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000210
211 def assertMultilineMatches(self, actual, pattern):
212 m = re.match(pattern, actual, re.DOTALL)
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000213 if not m:
214 self.fail(msg='%r did not match %r' % (actual, pattern))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000215
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000216 def get_sample_script(self):
217 return findfile('gdb_sample.py')
218
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000219class PrettyPrintTests(DebuggerTests):
220 def test_getting_backtrace(self):
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000221 gdb_output = self.get_stack_trace('id(42)')
222 self.assertTrue(BREAKPOINT_FN in gdb_output)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000223
Antoine Pitroua8892a12013-11-24 14:58:17 +0100224 def assertGdbRepr(self, val, exp_repr=None):
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000225 # Ensure that gdb's rendering of the value in a debugged process
226 # matches repr(value) in this process:
Antoine Pitroua8892a12013-11-24 14:58:17 +0100227 gdb_repr, gdb_output = self.get_gdb_repr('id(' + ascii(val) + ')')
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000228 if not exp_repr:
229 exp_repr = repr(val)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000230 self.assertEqual(gdb_repr, exp_repr,
231 ('%r did not equal expected %r; full output was:\n%s'
232 % (gdb_repr, exp_repr, gdb_output)))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000233
234 def test_int(self):
Serhiy Storchaka95949422013-08-27 19:40:23 +0300235 'Verify the pretty-printing of various int values'
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000236 self.assertGdbRepr(42)
237 self.assertGdbRepr(0)
238 self.assertGdbRepr(-7)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000239 self.assertGdbRepr(1000000000000)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000240 self.assertGdbRepr(-1000000000000000)
241
242 def test_singletons(self):
243 'Verify the pretty-printing of True, False and None'
244 self.assertGdbRepr(True)
245 self.assertGdbRepr(False)
246 self.assertGdbRepr(None)
247
248 def test_dicts(self):
249 'Verify the pretty-printing of dictionaries'
250 self.assertGdbRepr({})
251 self.assertGdbRepr({'foo': 'bar'})
Georg Brandl09a7c722012-02-20 21:31:46 +0100252 self.assertGdbRepr({'foo': 'bar', 'douglas': 42},
253 "{'foo': 'bar', 'douglas': 42}")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000254
255 def test_lists(self):
256 'Verify the pretty-printing of lists'
257 self.assertGdbRepr([])
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000258 self.assertGdbRepr(list(range(5)))
259
260 def test_bytes(self):
261 'Verify the pretty-printing of bytes'
262 self.assertGdbRepr(b'')
263 self.assertGdbRepr(b'And now for something hopefully the same')
264 self.assertGdbRepr(b'string with embedded NUL here \0 and then some more text')
265 self.assertGdbRepr(b'this is a tab:\t'
266 b' this is a slash-N:\n'
267 b' this is a slash-R:\r'
268 )
269
270 self.assertGdbRepr(b'this is byte 255:\xff and byte 128:\x80')
271
272 self.assertGdbRepr(bytes([b for b in range(255)]))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000273
274 def test_strings(self):
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000275 'Verify the pretty-printing of unicode strings'
Victor Stinner150016f2010-05-19 23:04:56 +0000276 encoding = locale.getpreferredencoding()
277 def check_repr(text):
278 try:
279 text.encode(encoding)
280 printable = True
281 except UnicodeEncodeError:
Antoine Pitrou4c7c4212010-09-09 20:40:28 +0000282 self.assertGdbRepr(text, ascii(text))
Victor Stinner150016f2010-05-19 23:04:56 +0000283 else:
284 self.assertGdbRepr(text)
285
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000286 self.assertGdbRepr('')
287 self.assertGdbRepr('And now for something hopefully the same')
288 self.assertGdbRepr('string with embedded NUL here \0 and then some more text')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000289
290 # Test printing a single character:
291 # U+2620 SKULL AND CROSSBONES
Victor Stinner150016f2010-05-19 23:04:56 +0000292 check_repr('\u2620')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000293
294 # Test printing a Japanese unicode string
295 # (I believe this reads "mojibake", using 3 characters from the CJK
296 # Unified Ideographs area, followed by U+3051 HIRAGANA LETTER KE)
Victor Stinner150016f2010-05-19 23:04:56 +0000297 check_repr('\u6587\u5b57\u5316\u3051')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000298
299 # Test a character outside the BMP:
300 # U+1D121 MUSICAL SYMBOL C CLEF
301 # This is:
302 # UTF-8: 0xF0 0x9D 0x84 0xA1
303 # UTF-16: 0xD834 0xDD21
Victor Stinner150016f2010-05-19 23:04:56 +0000304 check_repr(chr(0x1D121))
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000305
306 def test_tuples(self):
307 'Verify the pretty-printing of tuples'
308 self.assertGdbRepr(tuple())
309 self.assertGdbRepr((1,), '(1,)')
310 self.assertGdbRepr(('foo', 'bar', 'baz'))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000311
312 def test_sets(self):
313 'Verify the pretty-printing of sets'
314 self.assertGdbRepr(set())
Georg Brandl09a7c722012-02-20 21:31:46 +0100315 self.assertGdbRepr(set(['a', 'b']), "{'a', 'b'}")
316 self.assertGdbRepr(set([4, 5, 6]), "{4, 5, 6}")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000317
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000318 # Ensure that we handle sets containing the "dummy" key value,
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000319 # which happens on deletion:
320 gdb_repr, gdb_output = self.get_gdb_repr('''s = set(['a','b'])
321s.pop()
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000322id(s)''')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000323 self.assertEqual(gdb_repr, "{'b'}")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000324
325 def test_frozensets(self):
326 'Verify the pretty-printing of frozensets'
327 self.assertGdbRepr(frozenset())
Georg Brandl09a7c722012-02-20 21:31:46 +0100328 self.assertGdbRepr(frozenset(['a', 'b']), "frozenset({'a', 'b'})")
329 self.assertGdbRepr(frozenset([4, 5, 6]), "frozenset({4, 5, 6})")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000330
331 def test_exceptions(self):
332 # Test a RuntimeError
333 gdb_repr, gdb_output = self.get_gdb_repr('''
334try:
335 raise RuntimeError("I am an error")
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000336except RuntimeError as e:
337 id(e)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000338''')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000339 self.assertEqual(gdb_repr,
340 "RuntimeError('I am an error',)")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000341
342
343 # Test division by zero:
344 gdb_repr, gdb_output = self.get_gdb_repr('''
345try:
346 a = 1 / 0
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000347except ZeroDivisionError as e:
348 id(e)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000349''')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000350 self.assertEqual(gdb_repr,
351 "ZeroDivisionError('division by zero',)")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000352
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000353 def test_modern_class(self):
354 'Verify the pretty-printing of new-style class instances'
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000355 gdb_repr, gdb_output = self.get_gdb_repr('''
356class Foo:
357 pass
358foo = Foo()
359foo.an_int = 42
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000360id(foo)''')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100361 m = re.match(r'<Foo\(an_int=42\) at remote 0x-?[0-9a-f]+>', gdb_repr)
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_list(self):
366 'Verify the pretty-printing of an instance of a list subclass'
367 gdb_repr, gdb_output = self.get_gdb_repr('''
368class Foo(list):
369 pass
370foo = Foo()
371foo += [1, 2, 3]
372foo.an_int = 42
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000373id(foo)''')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100374 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 +0000375
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000376 self.assertTrue(m,
377 msg='Unexpected new-style class rendering %r' % gdb_repr)
378
379 def test_subclassing_tuple(self):
380 'Verify the pretty-printing of an instance of a tuple subclass'
381 # This should exercise the negative tp_dictoffset code in the
382 # new-style class support
383 gdb_repr, gdb_output = self.get_gdb_repr('''
384class Foo(tuple):
385 pass
386foo = Foo((1, 2, 3))
387foo.an_int = 42
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000388id(foo)''')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100389 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 +0000390
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000391 self.assertTrue(m,
392 msg='Unexpected new-style class rendering %r' % gdb_repr)
393
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000394 def assertSane(self, source, corruption, exprepr=None):
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000395 '''Run Python under gdb, corrupting variables in the inferior process
396 immediately before taking a backtrace.
397
398 Verify that the variable's representation is the expected failsafe
399 representation'''
400 if corruption:
401 cmds_after_breakpoint=[corruption, 'backtrace']
402 else:
403 cmds_after_breakpoint=['backtrace']
404
405 gdb_repr, gdb_output = \
406 self.get_gdb_repr(source,
407 cmds_after_breakpoint=cmds_after_breakpoint)
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000408 if exprepr:
409 if gdb_repr == exprepr:
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000410 # gdb managed to print the value in spite of the corruption;
411 # this is good (see http://bugs.python.org/issue8330)
412 return
413
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000414 # Match anything for the type name; 0xDEADBEEF could point to
415 # something arbitrary (see http://bugs.python.org/issue8330)
Antoine Pitrou4d098732011-11-26 01:42:03 +0100416 pattern = '<.* at remote 0x-?[0-9a-f]+>'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000417
418 m = re.match(pattern, gdb_repr)
419 if not m:
420 self.fail('Unexpected gdb representation: %r\n%s' % \
421 (gdb_repr, gdb_output))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000422
423 def test_NULL_ptr(self):
424 'Ensure that a NULL PyObject* is handled gracefully'
425 gdb_repr, gdb_output = (
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000426 self.get_gdb_repr('id(42)',
427 cmds_after_breakpoint=['set variable v=0',
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000428 'backtrace'])
429 )
430
Ezio Melottib3aedd42010-11-20 19:04:17 +0000431 self.assertEqual(gdb_repr, '0x0')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000432
433 def test_NULL_ob_type(self):
434 'Ensure that a PyObject* with NULL ob_type is handled gracefully'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000435 self.assertSane('id(42)',
436 'set v->ob_type=0')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000437
438 def test_corrupt_ob_type(self):
439 'Ensure that a PyObject* with a corrupt ob_type is handled gracefully'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000440 self.assertSane('id(42)',
441 'set v->ob_type=0xDEADBEEF',
442 exprepr='42')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000443
444 def test_corrupt_tp_flags(self):
445 'Ensure that a PyObject* with a type with corrupt tp_flags is handled'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000446 self.assertSane('id(42)',
447 'set v->ob_type->tp_flags=0x0',
448 exprepr='42')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000449
450 def test_corrupt_tp_name(self):
451 'Ensure that a PyObject* with a type with corrupt tp_name is handled'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000452 self.assertSane('id(42)',
453 'set v->ob_type->tp_name=0xDEADBEEF',
454 exprepr='42')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000455
456 def test_builtins_help(self):
457 'Ensure that the new-style class _Helper in site.py can be handled'
458 # (this was the issue causing tracebacks in
459 # http://bugs.python.org/issue8032#msg100537 )
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000460 gdb_repr, gdb_output = self.get_gdb_repr('id(__builtins__.help)', import_site=True)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000461
Antoine Pitrou4d098732011-11-26 01:42:03 +0100462 m = re.match(r'<_Helper at remote 0x-?[0-9a-f]+>', gdb_repr)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000463 self.assertTrue(m,
464 msg='Unexpected rendering %r' % gdb_repr)
465
466 def test_selfreferential_list(self):
467 '''Ensure that a reference loop involving a list doesn't lead proxyval
468 into an infinite loop:'''
469 gdb_repr, gdb_output = \
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000470 self.get_gdb_repr("a = [3, 4, 5] ; a.append(a) ; id(a)")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000471 self.assertEqual(gdb_repr, '[3, 4, 5, [...]]')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000472
473 gdb_repr, gdb_output = \
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000474 self.get_gdb_repr("a = [3, 4, 5] ; b = [a] ; a.append(b) ; id(a)")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000475 self.assertEqual(gdb_repr, '[3, 4, 5, [[...]]]')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000476
477 def test_selfreferential_dict(self):
478 '''Ensure that a reference loop involving a dict doesn't lead proxyval
479 into an infinite loop:'''
480 gdb_repr, gdb_output = \
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000481 self.get_gdb_repr("a = {} ; b = {'bar':a} ; a['foo'] = b ; id(a)")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000482
Ezio Melottib3aedd42010-11-20 19:04:17 +0000483 self.assertEqual(gdb_repr, "{'foo': {'bar': {...}}}")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000484
485 def test_selfreferential_old_style_instance(self):
486 gdb_repr, gdb_output = \
487 self.get_gdb_repr('''
488class Foo:
489 pass
490foo = Foo()
491foo.an_attr = foo
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000492id(foo)''')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100493 self.assertTrue(re.match('<Foo\(an_attr=<\.\.\.>\) at remote 0x-?[0-9a-f]+>',
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000494 gdb_repr),
495 'Unexpected gdb representation: %r\n%s' % \
496 (gdb_repr, gdb_output))
497
498 def test_selfreferential_new_style_instance(self):
499 gdb_repr, gdb_output = \
500 self.get_gdb_repr('''
501class Foo(object):
502 pass
503foo = Foo()
504foo.an_attr = foo
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000505id(foo)''')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100506 self.assertTrue(re.match('<Foo\(an_attr=<\.\.\.>\) 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 gdb_repr, gdb_output = \
512 self.get_gdb_repr('''
513class Foo(object):
514 pass
515a = Foo()
516b = Foo()
517a.an_attr = b
518b.an_attr = a
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000519id(a)''')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100520 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 +0000521 gdb_repr),
522 'Unexpected gdb representation: %r\n%s' % \
523 (gdb_repr, gdb_output))
524
525 def test_truncation(self):
526 'Verify that very long output is truncated'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000527 gdb_repr, gdb_output = self.get_gdb_repr('id(list(range(1000)))')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000528 self.assertEqual(gdb_repr,
529 "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, "
530 "14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, "
531 "27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, "
532 "40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, "
533 "53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, "
534 "66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, "
535 "79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, "
536 "92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, "
537 "104, 105, 106, 107, 108, 109, 110, 111, 112, 113, "
538 "114, 115, 116, 117, 118, 119, 120, 121, 122, 123, "
539 "124, 125, 126, 127, 128, 129, 130, 131, 132, 133, "
540 "134, 135, 136, 137, 138, 139, 140, 141, 142, 143, "
541 "144, 145, 146, 147, 148, 149, 150, 151, 152, 153, "
542 "154, 155, 156, 157, 158, 159, 160, 161, 162, 163, "
543 "164, 165, 166, 167, 168, 169, 170, 171, 172, 173, "
544 "174, 175, 176, 177, 178, 179, 180, 181, 182, 183, "
545 "184, 185, 186, 187, 188, 189, 190, 191, 192, 193, "
546 "194, 195, 196, 197, 198, 199, 200, 201, 202, 203, "
547 "204, 205, 206, 207, 208, 209, 210, 211, 212, 213, "
548 "214, 215, 216, 217, 218, 219, 220, 221, 222, 223, "
549 "224, 225, 226...(truncated)")
550 self.assertEqual(len(gdb_repr),
551 1024 + len('...(truncated)'))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000552
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000553 def test_builtin_method(self):
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000554 gdb_repr, gdb_output = self.get_gdb_repr('import sys; id(sys.stdout.readlines)')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100555 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 +0000556 gdb_repr),
557 'Unexpected gdb representation: %r\n%s' % \
558 (gdb_repr, gdb_output))
559
560 def test_frames(self):
561 gdb_output = self.get_stack_trace('''
562def foo(a, b, c):
563 pass
564
565foo(3, 4, 5)
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000566id(foo.__code__)''',
567 breakpoint='builtin_id',
568 cmds_after_breakpoint=['print (PyFrameObject*)(((PyCodeObject*)v)->co_zombieframe)']
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000569 )
Antoine Pitrou4d098732011-11-26 01:42:03 +0100570 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 +0000571 gdb_output,
572 re.DOTALL),
573 'Unexpected gdb representation: %r\n%s' % (gdb_output, gdb_output))
574
Victor Stinnerd2084162011-12-19 13:42:24 +0100575@unittest.skipIf(python_is_optimized(),
576 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000577class PyListTests(DebuggerTests):
578 def assertListing(self, expected, actual):
579 self.assertEndsWith(actual, expected)
580
581 def test_basic_command(self):
582 'Verify that the "py-list" command works'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000583 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000584 cmds_after_breakpoint=['py-list'])
585
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000586 self.assertListing(' 5 \n'
587 ' 6 def bar(a, b, c):\n'
588 ' 7 baz(a, b, c)\n'
589 ' 8 \n'
590 ' 9 def baz(*args):\n'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000591 ' >10 id(42)\n'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000592 ' 11 \n'
593 ' 12 foo(1, 2, 3)\n',
594 bt)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000595
596 def test_one_abs_arg(self):
597 'Verify the "py-list" command with one absolute argument'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000598 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000599 cmds_after_breakpoint=['py-list 9'])
600
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000601 self.assertListing(' 9 def baz(*args):\n'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000602 ' >10 id(42)\n'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000603 ' 11 \n'
604 ' 12 foo(1, 2, 3)\n',
605 bt)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000606
607 def test_two_abs_args(self):
608 'Verify the "py-list" command with two absolute arguments'
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-list 1,3'])
611
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000612 self.assertListing(' 1 # Sample script for use by test_gdb.py\n'
613 ' 2 \n'
614 ' 3 def foo(a, b, c):\n',
615 bt)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000616
617class StackNavigationTests(DebuggerTests):
Victor Stinner50eb60e2010-04-20 22:32:07 +0000618 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Victor Stinnerd2084162011-12-19 13:42:24 +0100619 @unittest.skipIf(python_is_optimized(),
620 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000621 def test_pyup_command(self):
622 'Verify that the "py-up" command works'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000623 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000624 cmds_after_breakpoint=['py-up'])
625 self.assertMultilineMatches(bt,
626 r'''^.*
Antoine Pitrou4d098732011-11-26 01:42:03 +0100627#[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 +0000628 baz\(a, b, c\)
629$''')
630
Victor Stinner50eb60e2010-04-20 22:32:07 +0000631 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000632 def test_down_at_bottom(self):
633 'Verify handling of "py-down" at the bottom of the stack'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000634 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000635 cmds_after_breakpoint=['py-down'])
636 self.assertEndsWith(bt,
637 'Unable to find a newer python frame\n')
638
Victor Stinner50eb60e2010-04-20 22:32:07 +0000639 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000640 def test_up_at_top(self):
641 'Verify handling of "py-up" at the top of the stack'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000642 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000643 cmds_after_breakpoint=['py-up'] * 4)
644 self.assertEndsWith(bt,
645 'Unable to find an older python frame\n')
646
Victor Stinner50eb60e2010-04-20 22:32:07 +0000647 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Victor Stinnerd2084162011-12-19 13:42:24 +0100648 @unittest.skipIf(python_is_optimized(),
649 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000650 def test_up_then_down(self):
651 'Verify "py-up" followed by "py-down"'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000652 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000653 cmds_after_breakpoint=['py-up', 'py-down'])
654 self.assertMultilineMatches(bt,
655 r'''^.*
Antoine Pitrou4d098732011-11-26 01:42:03 +0100656#[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 +0000657 baz\(a, b, c\)
Antoine Pitrou4d098732011-11-26 01:42:03 +0100658#[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 +0000659 id\(42\)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000660$''')
661
662class PyBtTests(DebuggerTests):
Victor Stinnerd2084162011-12-19 13:42:24 +0100663 @unittest.skipIf(python_is_optimized(),
664 "Python was compiled with optimizations")
Victor Stinnere670c882011-05-13 17:40:15 +0200665 def test_bt(self):
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000666 'Verify that the "py-bt" command works'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000667 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000668 cmds_after_breakpoint=['py-bt'])
669 self.assertMultilineMatches(bt,
670 r'''^.*
Victor Stinnere670c882011-05-13 17:40:15 +0200671Traceback \(most recent call first\):
672 File ".*gdb_sample.py", line 10, in baz
673 id\(42\)
674 File ".*gdb_sample.py", line 7, in bar
675 baz\(a, b, c\)
676 File ".*gdb_sample.py", line 4, in foo
677 bar\(a, b, c\)
678 File ".*gdb_sample.py", line 12, in <module>
679 foo\(1, 2, 3\)
680''')
681
Victor Stinnerd2084162011-12-19 13:42:24 +0100682 @unittest.skipIf(python_is_optimized(),
683 "Python was compiled with optimizations")
Victor Stinnere670c882011-05-13 17:40:15 +0200684 def test_bt_full(self):
685 'Verify that the "py-bt-full" command works'
686 bt = self.get_stack_trace(script=self.get_sample_script(),
687 cmds_after_breakpoint=['py-bt-full'])
688 self.assertMultilineMatches(bt,
689 r'''^.*
Antoine Pitrou4d098732011-11-26 01:42:03 +0100690#[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 +0000691 baz\(a, b, c\)
Antoine Pitrou4d098732011-11-26 01:42:03 +0100692#[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 +0000693 bar\(a, b, c\)
Antoine Pitrou4d098732011-11-26 01:42:03 +0100694#[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 12, in <module> \(\)
Victor Stinnerd2084162011-12-19 13:42:24 +0100695 foo\(1, 2, 3\)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000696''')
697
David Malcolm8d37ffa2012-06-27 14:15:34 -0400698 @unittest.skipUnless(_thread,
699 "Python was compiled without thread support")
700 def test_threads(self):
701 'Verify that "py-bt" indicates threads that are waiting for the GIL'
702 cmd = '''
703from threading import Thread
704
705class TestThread(Thread):
706 # These threads would run forever, but we'll interrupt things with the
707 # debugger
708 def run(self):
709 i = 0
710 while 1:
711 i += 1
712
713t = {}
714for i in range(4):
715 t[i] = TestThread()
716 t[i].start()
717
718# Trigger a breakpoint on the main thread
719id(42)
720
721'''
722 # Verify with "py-bt":
723 gdb_output = self.get_stack_trace(cmd,
724 cmds_after_breakpoint=['thread apply all py-bt'])
725 self.assertIn('Waiting for the GIL', gdb_output)
726
727 # Verify with "py-bt-full":
728 gdb_output = self.get_stack_trace(cmd,
729 cmds_after_breakpoint=['thread apply all py-bt-full'])
730 self.assertIn('Waiting for the GIL', gdb_output)
731
732 @unittest.skipIf(python_is_optimized(),
733 "Python was compiled with optimizations")
734 # Some older versions of gdb will fail with
735 # "Cannot find new threads: generic error"
736 # unless we add LD_PRELOAD=PATH-TO-libpthread.so.1 as a workaround
737 @unittest.skipUnless(_thread,
738 "Python was compiled without thread support")
739 def test_gc(self):
740 'Verify that "py-bt" indicates if a thread is garbage-collecting'
741 cmd = ('from gc import collect\n'
742 'id(42)\n'
743 'def foo():\n'
744 ' collect()\n'
745 'def bar():\n'
746 ' foo()\n'
747 'bar()\n')
748 # Verify with "py-bt":
749 gdb_output = self.get_stack_trace(cmd,
750 cmds_after_breakpoint=['break update_refs', 'continue', 'py-bt'],
751 )
752 self.assertIn('Garbage-collecting', gdb_output)
753
754 # Verify with "py-bt-full":
755 gdb_output = self.get_stack_trace(cmd,
756 cmds_after_breakpoint=['break update_refs', 'continue', 'py-bt-full'],
757 )
758 self.assertIn('Garbage-collecting', gdb_output)
759
760 @unittest.skipIf(python_is_optimized(),
761 "Python was compiled with optimizations")
762 # Some older versions of gdb will fail with
763 # "Cannot find new threads: generic error"
764 # unless we add LD_PRELOAD=PATH-TO-libpthread.so.1 as a workaround
765 @unittest.skipUnless(_thread,
766 "Python was compiled without thread support")
767 def test_pycfunction(self):
768 'Verify that "py-bt" displays invocations of PyCFunction instances'
769 cmd = ('from time import sleep\n'
770 'def foo():\n'
771 ' sleep(1)\n'
772 'def bar():\n'
773 ' foo()\n'
774 'bar()\n')
775 # Verify with "py-bt":
776 gdb_output = self.get_stack_trace(cmd,
777 breakpoint='time_sleep',
778 cmds_after_breakpoint=['bt', 'py-bt'],
779 )
780 self.assertIn('<built-in method sleep', gdb_output)
781
782 # Verify with "py-bt-full":
783 gdb_output = self.get_stack_trace(cmd,
784 breakpoint='time_sleep',
785 cmds_after_breakpoint=['py-bt-full'],
786 )
787 self.assertIn('#0 <built-in method sleep', gdb_output)
788
789
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000790class PyPrintTests(DebuggerTests):
Victor Stinnerd2084162011-12-19 13:42:24 +0100791 @unittest.skipIf(python_is_optimized(),
792 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000793 def test_basic_command(self):
794 'Verify that the "py-print" command works'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000795 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000796 cmds_after_breakpoint=['py-print args'])
797 self.assertMultilineMatches(bt,
798 r".*\nlocal 'args' = \(1, 2, 3\)\n.*")
799
Vinay Sajip2549f872012-01-04 12:07:30 +0000800 @unittest.skipIf(python_is_optimized(),
801 "Python was compiled with optimizations")
Victor Stinner50eb60e2010-04-20 22:32:07 +0000802 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000803 def test_print_after_up(self):
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000804 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000805 cmds_after_breakpoint=['py-up', 'py-print c', 'py-print b', 'py-print a'])
806 self.assertMultilineMatches(bt,
807 r".*\nlocal 'c' = 3\nlocal 'b' = 2\nlocal 'a' = 1\n.*")
808
Victor Stinnerd2084162011-12-19 13:42:24 +0100809 @unittest.skipIf(python_is_optimized(),
810 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000811 def test_printing_global(self):
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000812 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000813 cmds_after_breakpoint=['py-print __name__'])
814 self.assertMultilineMatches(bt,
815 r".*\nglobal '__name__' = '__main__'\n.*")
816
Victor Stinnerd2084162011-12-19 13:42:24 +0100817 @unittest.skipIf(python_is_optimized(),
818 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000819 def test_printing_builtin(self):
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000820 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000821 cmds_after_breakpoint=['py-print len'])
822 self.assertMultilineMatches(bt,
Antoine Pitrou4d098732011-11-26 01:42:03 +0100823 r".*\nbuiltin 'len' = <built-in method len of module object at remote 0x-?[0-9a-f]+>\n.*")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000824
825class PyLocalsTests(DebuggerTests):
Victor Stinnerd2084162011-12-19 13:42:24 +0100826 @unittest.skipIf(python_is_optimized(),
827 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000828 def test_basic_command(self):
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000829 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000830 cmds_after_breakpoint=['py-locals'])
831 self.assertMultilineMatches(bt,
832 r".*\nargs = \(1, 2, 3\)\n.*")
833
Victor Stinner50eb60e2010-04-20 22:32:07 +0000834 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Vinay Sajip2549f872012-01-04 12:07:30 +0000835 @unittest.skipIf(python_is_optimized(),
836 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000837 def test_locals_after_up(self):
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000838 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000839 cmds_after_breakpoint=['py-up', 'py-locals'])
840 self.assertMultilineMatches(bt,
841 r".*\na = 1\nb = 2\nc = 3\n.*")
842
843def test_main():
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000844 run_unittest(PrettyPrintTests,
845 PyListTests,
846 StackNavigationTests,
847 PyBtTests,
848 PyPrintTests,
849 PyLocalsTests
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000850 )
851
852if __name__ == "__main__":
853 test_main()