blob: 284c8d8fd98a6ce834fc1244cfea6f40d7061f50 [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:
R David Murrayf9333022012-10-27 13:22:41 -040062gdbpy_version, _ = run_gdb("--eval-command=python import sys; print sys.version_info")
63if 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
144 # print ' '.join(args)
145
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
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000189 gdb_output = self.get_stack_trace(source, breakpoint=BREAKPOINT_FN,
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000190 cmds_after_breakpoint=cmds_after_breakpoint,
191 import_site=import_site)
192 # gdb can insert additional '\n' and space characters in various places
193 # in its output, depending on the width of the terminal it's connected
194 # to (using its "wrap_here" function)
David Malcolm8d37ffa2012-06-27 14:15:34 -0400195 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 +0000196 gdb_output, re.DOTALL)
197 if not m:
198 self.fail('Unexpected gdb output: %r\n%s' % (gdb_output, gdb_output))
199 return m.group(1), gdb_output
200
201 def assertEndsWith(self, actual, exp_end):
202 '''Ensure that the given "actual" string ends with "exp_end"'''
Ezio Melottib3aedd42010-11-20 19:04:17 +0000203 self.assertTrue(actual.endswith(exp_end),
204 msg='%r did not end with %r' % (actual, exp_end))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000205
206 def assertMultilineMatches(self, actual, pattern):
207 m = re.match(pattern, actual, re.DOTALL)
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000208 if not m:
209 self.fail(msg='%r did not match %r' % (actual, pattern))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000210
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000211 def get_sample_script(self):
212 return findfile('gdb_sample.py')
213
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000214class PrettyPrintTests(DebuggerTests):
215 def test_getting_backtrace(self):
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000216 gdb_output = self.get_stack_trace('id(42)')
217 self.assertTrue(BREAKPOINT_FN in gdb_output)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000218
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000219 def assertGdbRepr(self, val, exp_repr=None, cmds_after_breakpoint=None):
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000220 # Ensure that gdb's rendering of the value in a debugged process
221 # matches repr(value) in this process:
Victor Stinner150016f2010-05-19 23:04:56 +0000222 gdb_repr, gdb_output = self.get_gdb_repr('id(' + ascii(val) + ')',
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000223 cmds_after_breakpoint)
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000224 if not exp_repr:
225 exp_repr = repr(val)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000226 self.assertEqual(gdb_repr, exp_repr,
227 ('%r did not equal expected %r; full output was:\n%s'
228 % (gdb_repr, exp_repr, gdb_output)))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000229
230 def test_int(self):
Serhiy Storchaka95949422013-08-27 19:40:23 +0300231 'Verify the pretty-printing of various int values'
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000232 self.assertGdbRepr(42)
233 self.assertGdbRepr(0)
234 self.assertGdbRepr(-7)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000235 self.assertGdbRepr(1000000000000)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000236 self.assertGdbRepr(-1000000000000000)
237
238 def test_singletons(self):
239 'Verify the pretty-printing of True, False and None'
240 self.assertGdbRepr(True)
241 self.assertGdbRepr(False)
242 self.assertGdbRepr(None)
243
244 def test_dicts(self):
245 'Verify the pretty-printing of dictionaries'
246 self.assertGdbRepr({})
247 self.assertGdbRepr({'foo': 'bar'})
Georg Brandl09a7c722012-02-20 21:31:46 +0100248 self.assertGdbRepr({'foo': 'bar', 'douglas': 42},
249 "{'foo': 'bar', 'douglas': 42}")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000250
251 def test_lists(self):
252 'Verify the pretty-printing of lists'
253 self.assertGdbRepr([])
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000254 self.assertGdbRepr(list(range(5)))
255
256 def test_bytes(self):
257 'Verify the pretty-printing of bytes'
258 self.assertGdbRepr(b'')
259 self.assertGdbRepr(b'And now for something hopefully the same')
260 self.assertGdbRepr(b'string with embedded NUL here \0 and then some more text')
261 self.assertGdbRepr(b'this is a tab:\t'
262 b' this is a slash-N:\n'
263 b' this is a slash-R:\r'
264 )
265
266 self.assertGdbRepr(b'this is byte 255:\xff and byte 128:\x80')
267
268 self.assertGdbRepr(bytes([b for b in range(255)]))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000269
270 def test_strings(self):
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000271 'Verify the pretty-printing of unicode strings'
Victor Stinner150016f2010-05-19 23:04:56 +0000272 encoding = locale.getpreferredencoding()
273 def check_repr(text):
274 try:
275 text.encode(encoding)
276 printable = True
277 except UnicodeEncodeError:
Antoine Pitrou4c7c4212010-09-09 20:40:28 +0000278 self.assertGdbRepr(text, ascii(text))
Victor Stinner150016f2010-05-19 23:04:56 +0000279 else:
280 self.assertGdbRepr(text)
281
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000282 self.assertGdbRepr('')
283 self.assertGdbRepr('And now for something hopefully the same')
284 self.assertGdbRepr('string with embedded NUL here \0 and then some more text')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000285
286 # Test printing a single character:
287 # U+2620 SKULL AND CROSSBONES
Victor Stinner150016f2010-05-19 23:04:56 +0000288 check_repr('\u2620')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000289
290 # Test printing a Japanese unicode string
291 # (I believe this reads "mojibake", using 3 characters from the CJK
292 # Unified Ideographs area, followed by U+3051 HIRAGANA LETTER KE)
Victor Stinner150016f2010-05-19 23:04:56 +0000293 check_repr('\u6587\u5b57\u5316\u3051')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000294
295 # Test a character outside the BMP:
296 # U+1D121 MUSICAL SYMBOL C CLEF
297 # This is:
298 # UTF-8: 0xF0 0x9D 0x84 0xA1
299 # UTF-16: 0xD834 0xDD21
Victor Stinner150016f2010-05-19 23:04:56 +0000300 check_repr(chr(0x1D121))
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000301
302 def test_tuples(self):
303 'Verify the pretty-printing of tuples'
304 self.assertGdbRepr(tuple())
305 self.assertGdbRepr((1,), '(1,)')
306 self.assertGdbRepr(('foo', 'bar', 'baz'))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000307
308 def test_sets(self):
309 'Verify the pretty-printing of sets'
310 self.assertGdbRepr(set())
Georg Brandl09a7c722012-02-20 21:31:46 +0100311 self.assertGdbRepr(set(['a', 'b']), "{'a', 'b'}")
312 self.assertGdbRepr(set([4, 5, 6]), "{4, 5, 6}")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000313
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000314 # Ensure that we handle sets containing the "dummy" key value,
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000315 # which happens on deletion:
316 gdb_repr, gdb_output = self.get_gdb_repr('''s = set(['a','b'])
317s.pop()
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000318id(s)''')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000319 self.assertEqual(gdb_repr, "{'b'}")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000320
321 def test_frozensets(self):
322 'Verify the pretty-printing of frozensets'
323 self.assertGdbRepr(frozenset())
Georg Brandl09a7c722012-02-20 21:31:46 +0100324 self.assertGdbRepr(frozenset(['a', 'b']), "frozenset({'a', 'b'})")
325 self.assertGdbRepr(frozenset([4, 5, 6]), "frozenset({4, 5, 6})")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000326
327 def test_exceptions(self):
328 # Test a RuntimeError
329 gdb_repr, gdb_output = self.get_gdb_repr('''
330try:
331 raise RuntimeError("I am an error")
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000332except RuntimeError as e:
333 id(e)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000334''')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000335 self.assertEqual(gdb_repr,
336 "RuntimeError('I am an error',)")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000337
338
339 # Test division by zero:
340 gdb_repr, gdb_output = self.get_gdb_repr('''
341try:
342 a = 1 / 0
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000343except ZeroDivisionError as e:
344 id(e)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000345''')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000346 self.assertEqual(gdb_repr,
347 "ZeroDivisionError('division by zero',)")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000348
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000349 def test_modern_class(self):
350 'Verify the pretty-printing of new-style class instances'
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000351 gdb_repr, gdb_output = self.get_gdb_repr('''
352class Foo:
353 pass
354foo = Foo()
355foo.an_int = 42
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000356id(foo)''')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100357 m = re.match(r'<Foo\(an_int=42\) at remote 0x-?[0-9a-f]+>', gdb_repr)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000358 self.assertTrue(m,
359 msg='Unexpected new-style class rendering %r' % gdb_repr)
360
361 def test_subclassing_list(self):
362 'Verify the pretty-printing of an instance of a list subclass'
363 gdb_repr, gdb_output = self.get_gdb_repr('''
364class Foo(list):
365 pass
366foo = Foo()
367foo += [1, 2, 3]
368foo.an_int = 42
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000369id(foo)''')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100370 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 +0000371
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000372 self.assertTrue(m,
373 msg='Unexpected new-style class rendering %r' % gdb_repr)
374
375 def test_subclassing_tuple(self):
376 'Verify the pretty-printing of an instance of a tuple subclass'
377 # This should exercise the negative tp_dictoffset code in the
378 # new-style class support
379 gdb_repr, gdb_output = self.get_gdb_repr('''
380class Foo(tuple):
381 pass
382foo = Foo((1, 2, 3))
383foo.an_int = 42
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000384id(foo)''')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100385 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 +0000386
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000387 self.assertTrue(m,
388 msg='Unexpected new-style class rendering %r' % gdb_repr)
389
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000390 def assertSane(self, source, corruption, exprepr=None):
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000391 '''Run Python under gdb, corrupting variables in the inferior process
392 immediately before taking a backtrace.
393
394 Verify that the variable's representation is the expected failsafe
395 representation'''
396 if corruption:
397 cmds_after_breakpoint=[corruption, 'backtrace']
398 else:
399 cmds_after_breakpoint=['backtrace']
400
401 gdb_repr, gdb_output = \
402 self.get_gdb_repr(source,
403 cmds_after_breakpoint=cmds_after_breakpoint)
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000404 if exprepr:
405 if gdb_repr == exprepr:
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000406 # gdb managed to print the value in spite of the corruption;
407 # this is good (see http://bugs.python.org/issue8330)
408 return
409
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000410 # Match anything for the type name; 0xDEADBEEF could point to
411 # something arbitrary (see http://bugs.python.org/issue8330)
Antoine Pitrou4d098732011-11-26 01:42:03 +0100412 pattern = '<.* at remote 0x-?[0-9a-f]+>'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000413
414 m = re.match(pattern, gdb_repr)
415 if not m:
416 self.fail('Unexpected gdb representation: %r\n%s' % \
417 (gdb_repr, gdb_output))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000418
419 def test_NULL_ptr(self):
420 'Ensure that a NULL PyObject* is handled gracefully'
421 gdb_repr, gdb_output = (
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000422 self.get_gdb_repr('id(42)',
423 cmds_after_breakpoint=['set variable v=0',
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000424 'backtrace'])
425 )
426
Ezio Melottib3aedd42010-11-20 19:04:17 +0000427 self.assertEqual(gdb_repr, '0x0')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000428
429 def test_NULL_ob_type(self):
430 'Ensure that a PyObject* with NULL ob_type is handled gracefully'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000431 self.assertSane('id(42)',
432 'set v->ob_type=0')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000433
434 def test_corrupt_ob_type(self):
435 'Ensure that a PyObject* with a corrupt ob_type is handled gracefully'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000436 self.assertSane('id(42)',
437 'set v->ob_type=0xDEADBEEF',
438 exprepr='42')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000439
440 def test_corrupt_tp_flags(self):
441 'Ensure that a PyObject* with a type with corrupt tp_flags is handled'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000442 self.assertSane('id(42)',
443 'set v->ob_type->tp_flags=0x0',
444 exprepr='42')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000445
446 def test_corrupt_tp_name(self):
447 'Ensure that a PyObject* with a type with corrupt tp_name is handled'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000448 self.assertSane('id(42)',
449 'set v->ob_type->tp_name=0xDEADBEEF',
450 exprepr='42')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000451
452 def test_builtins_help(self):
453 'Ensure that the new-style class _Helper in site.py can be handled'
454 # (this was the issue causing tracebacks in
455 # http://bugs.python.org/issue8032#msg100537 )
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000456 gdb_repr, gdb_output = self.get_gdb_repr('id(__builtins__.help)', import_site=True)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000457
Antoine Pitrou4d098732011-11-26 01:42:03 +0100458 m = re.match(r'<_Helper at remote 0x-?[0-9a-f]+>', gdb_repr)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000459 self.assertTrue(m,
460 msg='Unexpected rendering %r' % gdb_repr)
461
462 def test_selfreferential_list(self):
463 '''Ensure that a reference loop involving a list doesn't lead proxyval
464 into an infinite loop:'''
465 gdb_repr, gdb_output = \
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000466 self.get_gdb_repr("a = [3, 4, 5] ; a.append(a) ; id(a)")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000467 self.assertEqual(gdb_repr, '[3, 4, 5, [...]]')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000468
469 gdb_repr, gdb_output = \
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000470 self.get_gdb_repr("a = [3, 4, 5] ; b = [a] ; a.append(b) ; 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 def test_selfreferential_dict(self):
474 '''Ensure that a reference loop involving a dict doesn't lead proxyval
475 into an infinite loop:'''
476 gdb_repr, gdb_output = \
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000477 self.get_gdb_repr("a = {} ; b = {'bar':a} ; a['foo'] = b ; id(a)")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000478
Ezio Melottib3aedd42010-11-20 19:04:17 +0000479 self.assertEqual(gdb_repr, "{'foo': {'bar': {...}}}")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000480
481 def test_selfreferential_old_style_instance(self):
482 gdb_repr, gdb_output = \
483 self.get_gdb_repr('''
484class Foo:
485 pass
486foo = Foo()
487foo.an_attr = foo
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000488id(foo)''')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100489 self.assertTrue(re.match('<Foo\(an_attr=<\.\.\.>\) at remote 0x-?[0-9a-f]+>',
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000490 gdb_repr),
491 'Unexpected gdb representation: %r\n%s' % \
492 (gdb_repr, gdb_output))
493
494 def test_selfreferential_new_style_instance(self):
495 gdb_repr, gdb_output = \
496 self.get_gdb_repr('''
497class Foo(object):
498 pass
499foo = Foo()
500foo.an_attr = foo
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000501id(foo)''')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100502 self.assertTrue(re.match('<Foo\(an_attr=<\.\.\.>\) at remote 0x-?[0-9a-f]+>',
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000503 gdb_repr),
504 'Unexpected gdb representation: %r\n%s' % \
505 (gdb_repr, gdb_output))
506
507 gdb_repr, gdb_output = \
508 self.get_gdb_repr('''
509class Foo(object):
510 pass
511a = Foo()
512b = Foo()
513a.an_attr = b
514b.an_attr = a
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000515id(a)''')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100516 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 +0000517 gdb_repr),
518 'Unexpected gdb representation: %r\n%s' % \
519 (gdb_repr, gdb_output))
520
521 def test_truncation(self):
522 'Verify that very long output is truncated'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000523 gdb_repr, gdb_output = self.get_gdb_repr('id(list(range(1000)))')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000524 self.assertEqual(gdb_repr,
525 "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, "
526 "14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, "
527 "27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, "
528 "40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, "
529 "53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, "
530 "66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, "
531 "79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, "
532 "92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, "
533 "104, 105, 106, 107, 108, 109, 110, 111, 112, 113, "
534 "114, 115, 116, 117, 118, 119, 120, 121, 122, 123, "
535 "124, 125, 126, 127, 128, 129, 130, 131, 132, 133, "
536 "134, 135, 136, 137, 138, 139, 140, 141, 142, 143, "
537 "144, 145, 146, 147, 148, 149, 150, 151, 152, 153, "
538 "154, 155, 156, 157, 158, 159, 160, 161, 162, 163, "
539 "164, 165, 166, 167, 168, 169, 170, 171, 172, 173, "
540 "174, 175, 176, 177, 178, 179, 180, 181, 182, 183, "
541 "184, 185, 186, 187, 188, 189, 190, 191, 192, 193, "
542 "194, 195, 196, 197, 198, 199, 200, 201, 202, 203, "
543 "204, 205, 206, 207, 208, 209, 210, 211, 212, 213, "
544 "214, 215, 216, 217, 218, 219, 220, 221, 222, 223, "
545 "224, 225, 226...(truncated)")
546 self.assertEqual(len(gdb_repr),
547 1024 + len('...(truncated)'))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000548
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000549 def test_builtin_method(self):
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000550 gdb_repr, gdb_output = self.get_gdb_repr('import sys; id(sys.stdout.readlines)')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100551 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 +0000552 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('''
558def foo(a, b, c):
559 pass
560
561foo(3, 4, 5)
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000562id(foo.__code__)''',
563 breakpoint='builtin_id',
564 cmds_after_breakpoint=['print (PyFrameObject*)(((PyCodeObject*)v)->co_zombieframe)']
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000565 )
Antoine Pitrou4d098732011-11-26 01:42:03 +0100566 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 +0000567 gdb_output,
568 re.DOTALL),
569 'Unexpected gdb representation: %r\n%s' % (gdb_output, gdb_output))
570
Victor Stinnerd2084162011-12-19 13:42:24 +0100571@unittest.skipIf(python_is_optimized(),
572 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000573class 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öwis5226fd62010-04-21 06:05:58 +0000579 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000580 cmds_after_breakpoint=['py-list'])
581
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000582 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'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000587 ' >10 id(42)\n'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000588 ' 11 \n'
589 ' 12 foo(1, 2, 3)\n',
590 bt)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000591
592 def test_one_abs_arg(self):
593 'Verify the "py-list" command with one absolute argument'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000594 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000595 cmds_after_breakpoint=['py-list 9'])
596
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000597 self.assertListing(' 9 def baz(*args):\n'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000598 ' >10 id(42)\n'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000599 ' 11 \n'
600 ' 12 foo(1, 2, 3)\n',
601 bt)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000602
603 def test_two_abs_args(self):
604 'Verify the "py-list" command with two absolute arguments'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000605 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000606 cmds_after_breakpoint=['py-list 1,3'])
607
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000608 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)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000612
613class StackNavigationTests(DebuggerTests):
Victor Stinner50eb60e2010-04-20 22:32:07 +0000614 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Victor Stinnerd2084162011-12-19 13:42:24 +0100615 @unittest.skipIf(python_is_optimized(),
616 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000617 def test_pyup_command(self):
618 'Verify that the "py-up" command works'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000619 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000620 cmds_after_breakpoint=['py-up'])
621 self.assertMultilineMatches(bt,
622 r'''^.*
Antoine Pitrou4d098732011-11-26 01:42:03 +0100623#[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 +0000624 baz\(a, b, c\)
625$''')
626
Victor Stinner50eb60e2010-04-20 22:32:07 +0000627 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000628 def test_down_at_bottom(self):
629 'Verify handling of "py-down" at the bottom of the stack'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000630 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000631 cmds_after_breakpoint=['py-down'])
632 self.assertEndsWith(bt,
633 'Unable to find a newer python frame\n')
634
Victor Stinner50eb60e2010-04-20 22:32:07 +0000635 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000636 def test_up_at_top(self):
637 'Verify handling of "py-up" at the top of the stack'
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'] * 4)
640 self.assertEndsWith(bt,
641 'Unable to find an older python frame\n')
642
Victor Stinner50eb60e2010-04-20 22:32:07 +0000643 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Victor Stinnerd2084162011-12-19 13:42:24 +0100644 @unittest.skipIf(python_is_optimized(),
645 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000646 def test_up_then_down(self):
647 'Verify "py-up" followed by "py-down"'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000648 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000649 cmds_after_breakpoint=['py-up', 'py-down'])
650 self.assertMultilineMatches(bt,
651 r'''^.*
Antoine Pitrou4d098732011-11-26 01:42:03 +0100652#[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 +0000653 baz\(a, b, c\)
Antoine Pitrou4d098732011-11-26 01:42:03 +0100654#[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 +0000655 id\(42\)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000656$''')
657
658class PyBtTests(DebuggerTests):
Victor Stinnerd2084162011-12-19 13:42:24 +0100659 @unittest.skipIf(python_is_optimized(),
660 "Python was compiled with optimizations")
Victor Stinnere670c882011-05-13 17:40:15 +0200661 def test_bt(self):
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000662 'Verify that the "py-bt" command works'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000663 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000664 cmds_after_breakpoint=['py-bt'])
665 self.assertMultilineMatches(bt,
666 r'''^.*
Victor Stinnere670c882011-05-13 17:40:15 +0200667Traceback \(most recent call first\):
668 File ".*gdb_sample.py", line 10, in baz
669 id\(42\)
670 File ".*gdb_sample.py", line 7, in bar
671 baz\(a, b, c\)
672 File ".*gdb_sample.py", line 4, in foo
673 bar\(a, b, c\)
674 File ".*gdb_sample.py", line 12, in <module>
675 foo\(1, 2, 3\)
676''')
677
Victor Stinnerd2084162011-12-19 13:42:24 +0100678 @unittest.skipIf(python_is_optimized(),
679 "Python was compiled with optimizations")
Victor Stinnere670c882011-05-13 17:40:15 +0200680 def test_bt_full(self):
681 'Verify that the "py-bt-full" command works'
682 bt = self.get_stack_trace(script=self.get_sample_script(),
683 cmds_after_breakpoint=['py-bt-full'])
684 self.assertMultilineMatches(bt,
685 r'''^.*
Antoine Pitrou4d098732011-11-26 01:42:03 +0100686#[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 +0000687 baz\(a, b, c\)
Antoine Pitrou4d098732011-11-26 01:42:03 +0100688#[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 +0000689 bar\(a, b, c\)
Antoine Pitrou4d098732011-11-26 01:42:03 +0100690#[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 12, in <module> \(\)
Victor Stinnerd2084162011-12-19 13:42:24 +0100691 foo\(1, 2, 3\)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000692''')
693
David Malcolm8d37ffa2012-06-27 14:15:34 -0400694 @unittest.skipUnless(_thread,
695 "Python was compiled without thread support")
696 def test_threads(self):
697 'Verify that "py-bt" indicates threads that are waiting for the GIL'
698 cmd = '''
699from threading import Thread
700
701class TestThread(Thread):
702 # These threads would run forever, but we'll interrupt things with the
703 # debugger
704 def run(self):
705 i = 0
706 while 1:
707 i += 1
708
709t = {}
710for i in range(4):
711 t[i] = TestThread()
712 t[i].start()
713
714# Trigger a breakpoint on the main thread
715id(42)
716
717'''
718 # Verify with "py-bt":
719 gdb_output = self.get_stack_trace(cmd,
720 cmds_after_breakpoint=['thread apply all py-bt'])
721 self.assertIn('Waiting for the GIL', gdb_output)
722
723 # Verify with "py-bt-full":
724 gdb_output = self.get_stack_trace(cmd,
725 cmds_after_breakpoint=['thread apply all py-bt-full'])
726 self.assertIn('Waiting for the GIL', gdb_output)
727
728 @unittest.skipIf(python_is_optimized(),
729 "Python was compiled with optimizations")
730 # Some older versions of gdb will fail with
731 # "Cannot find new threads: generic error"
732 # unless we add LD_PRELOAD=PATH-TO-libpthread.so.1 as a workaround
733 @unittest.skipUnless(_thread,
734 "Python was compiled without thread support")
735 def test_gc(self):
736 'Verify that "py-bt" indicates if a thread is garbage-collecting'
737 cmd = ('from gc import collect\n'
738 'id(42)\n'
739 'def foo():\n'
740 ' collect()\n'
741 'def bar():\n'
742 ' foo()\n'
743 'bar()\n')
744 # Verify with "py-bt":
745 gdb_output = self.get_stack_trace(cmd,
746 cmds_after_breakpoint=['break update_refs', 'continue', 'py-bt'],
747 )
748 self.assertIn('Garbage-collecting', gdb_output)
749
750 # Verify with "py-bt-full":
751 gdb_output = self.get_stack_trace(cmd,
752 cmds_after_breakpoint=['break update_refs', 'continue', 'py-bt-full'],
753 )
754 self.assertIn('Garbage-collecting', gdb_output)
755
756 @unittest.skipIf(python_is_optimized(),
757 "Python was compiled with optimizations")
758 # Some older versions of gdb will fail with
759 # "Cannot find new threads: generic error"
760 # unless we add LD_PRELOAD=PATH-TO-libpthread.so.1 as a workaround
761 @unittest.skipUnless(_thread,
762 "Python was compiled without thread support")
763 def test_pycfunction(self):
764 'Verify that "py-bt" displays invocations of PyCFunction instances'
765 cmd = ('from time import sleep\n'
766 'def foo():\n'
767 ' sleep(1)\n'
768 'def bar():\n'
769 ' foo()\n'
770 'bar()\n')
771 # Verify with "py-bt":
772 gdb_output = self.get_stack_trace(cmd,
773 breakpoint='time_sleep',
774 cmds_after_breakpoint=['bt', 'py-bt'],
775 )
776 self.assertIn('<built-in method sleep', gdb_output)
777
778 # Verify with "py-bt-full":
779 gdb_output = self.get_stack_trace(cmd,
780 breakpoint='time_sleep',
781 cmds_after_breakpoint=['py-bt-full'],
782 )
783 self.assertIn('#0 <built-in method sleep', gdb_output)
784
785
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000786class PyPrintTests(DebuggerTests):
Victor Stinnerd2084162011-12-19 13:42:24 +0100787 @unittest.skipIf(python_is_optimized(),
788 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000789 def test_basic_command(self):
790 'Verify that the "py-print" command works'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000791 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000792 cmds_after_breakpoint=['py-print args'])
793 self.assertMultilineMatches(bt,
794 r".*\nlocal 'args' = \(1, 2, 3\)\n.*")
795
Vinay Sajip2549f872012-01-04 12:07:30 +0000796 @unittest.skipIf(python_is_optimized(),
797 "Python was compiled with optimizations")
Victor Stinner50eb60e2010-04-20 22:32:07 +0000798 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000799 def test_print_after_up(self):
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000800 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000801 cmds_after_breakpoint=['py-up', 'py-print c', 'py-print b', 'py-print a'])
802 self.assertMultilineMatches(bt,
803 r".*\nlocal 'c' = 3\nlocal 'b' = 2\nlocal 'a' = 1\n.*")
804
Victor Stinnerd2084162011-12-19 13:42:24 +0100805 @unittest.skipIf(python_is_optimized(),
806 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000807 def test_printing_global(self):
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000808 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000809 cmds_after_breakpoint=['py-print __name__'])
810 self.assertMultilineMatches(bt,
811 r".*\nglobal '__name__' = '__main__'\n.*")
812
Victor Stinnerd2084162011-12-19 13:42:24 +0100813 @unittest.skipIf(python_is_optimized(),
814 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000815 def test_printing_builtin(self):
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000816 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000817 cmds_after_breakpoint=['py-print len'])
818 self.assertMultilineMatches(bt,
Antoine Pitrou4d098732011-11-26 01:42:03 +0100819 r".*\nbuiltin 'len' = <built-in method len of module object at remote 0x-?[0-9a-f]+>\n.*")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000820
821class PyLocalsTests(DebuggerTests):
Victor Stinnerd2084162011-12-19 13:42:24 +0100822 @unittest.skipIf(python_is_optimized(),
823 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000824 def test_basic_command(self):
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000825 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000826 cmds_after_breakpoint=['py-locals'])
827 self.assertMultilineMatches(bt,
828 r".*\nargs = \(1, 2, 3\)\n.*")
829
Victor Stinner50eb60e2010-04-20 22:32:07 +0000830 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Vinay Sajip2549f872012-01-04 12:07:30 +0000831 @unittest.skipIf(python_is_optimized(),
832 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000833 def test_locals_after_up(self):
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000834 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000835 cmds_after_breakpoint=['py-up', 'py-locals'])
836 self.assertMultilineMatches(bt,
837 r".*\na = 1\nb = 2\nc = 3\n.*")
838
839def test_main():
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000840 run_unittest(PrettyPrintTests,
841 PyListTests,
842 StackNavigationTests,
843 PyBtTests,
844 PyPrintTests,
845 PyLocalsTests
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000846 )
847
848if __name__ == "__main__":
849 test_main()