blob: abcb23e7a6459ea434bf029b7f31f455320fda10 [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
R David Murrayf358eaf2012-10-27 13:26:14 -040066# Verify that "gdb" can load our custom hooks. In theory this should never fail.
R David Murrayf9333022012-10-27 13:22:41 -040067cmd = ['--args', sys.executable]
68_, gdbpy_errors = run_gdb('--args', sys.executable)
69if "auto-loading has been declined" in gdbpy_errors:
70 msg = "gdb security settings prevent use of custom hooks: "
71 raise unittest.SkipTest(msg + gdbpy_errors.rstrip())
Nick Coghlanbe4e4b52012-06-17 18:57:20 +100072
Victor Stinner50eb60e2010-04-20 22:32:07 +000073def gdb_has_frame_select():
74 # Does this build of gdb have gdb.Frame.select ?
R David Murrayf9333022012-10-27 13:22:41 -040075 stdout, _ = run_gdb("--eval-command=python print(dir(gdb.Frame))")
76 m = re.match(r'.*\[(.*)\].*', stdout)
Victor Stinner50eb60e2010-04-20 22:32:07 +000077 if not m:
78 raise unittest.SkipTest("Unable to parse output from gdb.Frame.select test")
R David Murrayf9333022012-10-27 13:22:41 -040079 gdb_frame_dir = m.group(1).split(', ')
80 return "'select'" in gdb_frame_dir
Victor Stinner50eb60e2010-04-20 22:32:07 +000081
82HAS_PYUP_PYDOWN = gdb_has_frame_select()
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000083
Martin v. Löwis5ae68102010-04-21 22:38:42 +000084BREAKPOINT_FN='builtin_id'
85
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000086class DebuggerTests(unittest.TestCase):
87
88 """Test that the debugger can debug Python."""
89
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000090 def get_stack_trace(self, source=None, script=None,
Martin v. Löwis5ae68102010-04-21 22:38:42 +000091 breakpoint=BREAKPOINT_FN,
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000092 cmds_after_breakpoint=None,
93 import_site=False):
94 '''
95 Run 'python -c SOURCE' under gdb with a breakpoint.
96
97 Support injecting commands after the breakpoint is reached
98
99 Returns the stdout from gdb
100
101 cmds_after_breakpoint: if provided, a list of strings: gdb commands
102 '''
103 # We use "set breakpoint pending yes" to avoid blocking with a:
104 # Function "foo" not defined.
105 # Make breakpoint pending on future shared library load? (y or [n])
106 # error, which typically happens python is dynamically linked (the
107 # breakpoints of interest are to be found in the shared library)
108 # When this happens, we still get:
Victor Stinner67df3a42010-04-21 13:53:05 +0000109 # Function "textiowrapper_write" not defined.
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000110 # emitted to stderr each time, alas.
111
112 # Initially I had "--eval-command=continue" here, but removed it to
113 # avoid repeated print breakpoints when traversing hierarchical data
114 # structures
115
116 # Generate a list of commands in gdb's language:
117 commands = ['set breakpoint pending yes',
118 'break %s' % breakpoint,
119 'run']
120 if cmds_after_breakpoint:
121 commands += cmds_after_breakpoint
122 else:
123 commands += ['backtrace']
124
125 # print commands
126
127 # Use "commands" to generate the arguments with which to invoke "gdb":
128 args = ["gdb", "--batch"]
129 args += ['--eval-command=%s' % cmd for cmd in commands]
130 args += ["--args",
131 sys.executable]
132
133 if not import_site:
134 # -S suppresses the default 'import site'
135 args += ["-S"]
136
137 if source:
138 args += ["-c", source]
139 elif script:
140 args += [script]
141
142 # print args
143 # print ' '.join(args)
144
145 # Use "args" to invoke gdb, capturing stdout, stderr:
R David Murrayf9333022012-10-27 13:22:41 -0400146 out, err = run_gdb(*args, PYTHONHASHSEED='0')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000147
Antoine Pitrou81641d62013-05-01 00:15:44 +0200148 errlines = err.splitlines()
149 unexpected_errlines = []
150
151 # Ignore some benign messages on stderr.
152 ignore_patterns = (
153 'Function "%s" not defined.' % breakpoint,
154 "warning: no loadable sections found in added symbol-file"
155 " system-supplied DSO",
156 "warning: Unable to find libthread_db matching"
157 " inferior's thread library, thread debugging will"
158 " not be available.",
159 "warning: Cannot initialize thread debugging"
160 " library: Debugger service failed",
161 'warning: Could not load shared library symbols for '
162 'linux-vdso.so',
163 'warning: Could not load shared library symbols for '
164 'linux-gate.so',
165 'Do you need "set solib-search-path" or '
166 '"set sysroot"?',
167 )
168 for line in errlines:
169 if not line.startswith(ignore_patterns):
170 unexpected_errlines.append(line)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000171
172 # Ensure no unexpected error messages:
Antoine Pitrou81641d62013-05-01 00:15:44 +0200173 self.assertEqual(unexpected_errlines, [])
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000174 return out
175
176 def get_gdb_repr(self, source,
177 cmds_after_breakpoint=None,
178 import_site=False):
179 # Given an input python source representation of data,
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000180 # run "python -c'id(DATA)'" under gdb with a breakpoint on
181 # builtin_id and scrape out gdb's representation of the "op"
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000182 # parameter, and verify that the gdb displays the same string
183 #
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000184 # Verify that the gdb displays the expected string
185 #
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000186 # For a nested structure, the first time we hit the breakpoint will
187 # give us the top-level structure
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000188 gdb_output = self.get_stack_trace(source, breakpoint=BREAKPOINT_FN,
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000189 cmds_after_breakpoint=cmds_after_breakpoint,
190 import_site=import_site)
191 # gdb can insert additional '\n' and space characters in various places
192 # in its output, depending on the width of the terminal it's connected
193 # to (using its "wrap_here" function)
David Malcolm8d37ffa2012-06-27 14:15:34 -0400194 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 +0000195 gdb_output, re.DOTALL)
196 if not m:
197 self.fail('Unexpected gdb output: %r\n%s' % (gdb_output, gdb_output))
198 return m.group(1), gdb_output
199
200 def assertEndsWith(self, actual, exp_end):
201 '''Ensure that the given "actual" string ends with "exp_end"'''
Ezio Melottib3aedd42010-11-20 19:04:17 +0000202 self.assertTrue(actual.endswith(exp_end),
203 msg='%r did not end with %r' % (actual, exp_end))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000204
205 def assertMultilineMatches(self, actual, pattern):
206 m = re.match(pattern, actual, re.DOTALL)
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000207 if not m:
208 self.fail(msg='%r did not match %r' % (actual, pattern))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000209
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000210 def get_sample_script(self):
211 return findfile('gdb_sample.py')
212
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000213class PrettyPrintTests(DebuggerTests):
214 def test_getting_backtrace(self):
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000215 gdb_output = self.get_stack_trace('id(42)')
216 self.assertTrue(BREAKPOINT_FN in gdb_output)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000217
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000218 def assertGdbRepr(self, val, exp_repr=None, cmds_after_breakpoint=None):
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000219 # Ensure that gdb's rendering of the value in a debugged process
220 # matches repr(value) in this process:
Victor Stinner150016f2010-05-19 23:04:56 +0000221 gdb_repr, gdb_output = self.get_gdb_repr('id(' + ascii(val) + ')',
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000222 cmds_after_breakpoint)
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000223 if not exp_repr:
224 exp_repr = repr(val)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000225 self.assertEqual(gdb_repr, exp_repr,
226 ('%r did not equal expected %r; full output was:\n%s'
227 % (gdb_repr, exp_repr, gdb_output)))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000228
229 def test_int(self):
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000230 'Verify the pretty-printing of various "int"/long values'
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000231 self.assertGdbRepr(42)
232 self.assertGdbRepr(0)
233 self.assertGdbRepr(-7)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000234 self.assertGdbRepr(1000000000000)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000235 self.assertGdbRepr(-1000000000000000)
236
237 def test_singletons(self):
238 'Verify the pretty-printing of True, False and None'
239 self.assertGdbRepr(True)
240 self.assertGdbRepr(False)
241 self.assertGdbRepr(None)
242
243 def test_dicts(self):
244 'Verify the pretty-printing of dictionaries'
245 self.assertGdbRepr({})
246 self.assertGdbRepr({'foo': 'bar'})
Georg Brandl09a7c722012-02-20 21:31:46 +0100247 self.assertGdbRepr({'foo': 'bar', 'douglas': 42},
248 "{'foo': 'bar', 'douglas': 42}")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000249
250 def test_lists(self):
251 'Verify the pretty-printing of lists'
252 self.assertGdbRepr([])
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000253 self.assertGdbRepr(list(range(5)))
254
255 def test_bytes(self):
256 'Verify the pretty-printing of bytes'
257 self.assertGdbRepr(b'')
258 self.assertGdbRepr(b'And now for something hopefully the same')
259 self.assertGdbRepr(b'string with embedded NUL here \0 and then some more text')
260 self.assertGdbRepr(b'this is a tab:\t'
261 b' this is a slash-N:\n'
262 b' this is a slash-R:\r'
263 )
264
265 self.assertGdbRepr(b'this is byte 255:\xff and byte 128:\x80')
266
267 self.assertGdbRepr(bytes([b for b in range(255)]))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000268
269 def test_strings(self):
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000270 'Verify the pretty-printing of unicode strings'
Victor Stinner150016f2010-05-19 23:04:56 +0000271 encoding = locale.getpreferredencoding()
272 def check_repr(text):
273 try:
274 text.encode(encoding)
275 printable = True
276 except UnicodeEncodeError:
Antoine Pitrou4c7c4212010-09-09 20:40:28 +0000277 self.assertGdbRepr(text, ascii(text))
Victor Stinner150016f2010-05-19 23:04:56 +0000278 else:
279 self.assertGdbRepr(text)
280
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000281 self.assertGdbRepr('')
282 self.assertGdbRepr('And now for something hopefully the same')
283 self.assertGdbRepr('string with embedded NUL here \0 and then some more text')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000284
285 # Test printing a single character:
286 # U+2620 SKULL AND CROSSBONES
Victor Stinner150016f2010-05-19 23:04:56 +0000287 check_repr('\u2620')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000288
289 # Test printing a Japanese unicode string
290 # (I believe this reads "mojibake", using 3 characters from the CJK
291 # Unified Ideographs area, followed by U+3051 HIRAGANA LETTER KE)
Victor Stinner150016f2010-05-19 23:04:56 +0000292 check_repr('\u6587\u5b57\u5316\u3051')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000293
294 # Test a character outside the BMP:
295 # U+1D121 MUSICAL SYMBOL C CLEF
296 # This is:
297 # UTF-8: 0xF0 0x9D 0x84 0xA1
298 # UTF-16: 0xD834 0xDD21
Victor Stinner150016f2010-05-19 23:04:56 +0000299 check_repr(chr(0x1D121))
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000300
301 def test_tuples(self):
302 'Verify the pretty-printing of tuples'
303 self.assertGdbRepr(tuple())
304 self.assertGdbRepr((1,), '(1,)')
305 self.assertGdbRepr(('foo', 'bar', 'baz'))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000306
307 def test_sets(self):
308 'Verify the pretty-printing of sets'
309 self.assertGdbRepr(set())
Georg Brandl09a7c722012-02-20 21:31:46 +0100310 self.assertGdbRepr(set(['a', 'b']), "{'a', 'b'}")
311 self.assertGdbRepr(set([4, 5, 6]), "{4, 5, 6}")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000312
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000313 # Ensure that we handle sets containing the "dummy" key value,
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000314 # which happens on deletion:
315 gdb_repr, gdb_output = self.get_gdb_repr('''s = set(['a','b'])
316s.pop()
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000317id(s)''')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000318 self.assertEqual(gdb_repr, "{'b'}")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000319
320 def test_frozensets(self):
321 'Verify the pretty-printing of frozensets'
322 self.assertGdbRepr(frozenset())
Georg Brandl09a7c722012-02-20 21:31:46 +0100323 self.assertGdbRepr(frozenset(['a', 'b']), "frozenset({'a', 'b'})")
324 self.assertGdbRepr(frozenset([4, 5, 6]), "frozenset({4, 5, 6})")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000325
326 def test_exceptions(self):
327 # Test a RuntimeError
328 gdb_repr, gdb_output = self.get_gdb_repr('''
329try:
330 raise RuntimeError("I am an error")
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000331except RuntimeError as e:
332 id(e)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000333''')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000334 self.assertEqual(gdb_repr,
335 "RuntimeError('I am an error',)")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000336
337
338 # Test division by zero:
339 gdb_repr, gdb_output = self.get_gdb_repr('''
340try:
341 a = 1 / 0
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000342except ZeroDivisionError as e:
343 id(e)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000344''')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000345 self.assertEqual(gdb_repr,
346 "ZeroDivisionError('division by zero',)")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000347
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000348 def test_modern_class(self):
349 'Verify the pretty-printing of new-style class instances'
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000350 gdb_repr, gdb_output = self.get_gdb_repr('''
351class Foo:
352 pass
353foo = Foo()
354foo.an_int = 42
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000355id(foo)''')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100356 m = re.match(r'<Foo\(an_int=42\) at remote 0x-?[0-9a-f]+>', gdb_repr)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000357 self.assertTrue(m,
358 msg='Unexpected new-style class rendering %r' % gdb_repr)
359
360 def test_subclassing_list(self):
361 'Verify the pretty-printing of an instance of a list subclass'
362 gdb_repr, gdb_output = self.get_gdb_repr('''
363class Foo(list):
364 pass
365foo = Foo()
366foo += [1, 2, 3]
367foo.an_int = 42
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000368id(foo)''')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100369 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 +0000370
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000371 self.assertTrue(m,
372 msg='Unexpected new-style class rendering %r' % gdb_repr)
373
374 def test_subclassing_tuple(self):
375 'Verify the pretty-printing of an instance of a tuple subclass'
376 # This should exercise the negative tp_dictoffset code in the
377 # new-style class support
378 gdb_repr, gdb_output = self.get_gdb_repr('''
379class Foo(tuple):
380 pass
381foo = Foo((1, 2, 3))
382foo.an_int = 42
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000383id(foo)''')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100384 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 +0000385
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000386 self.assertTrue(m,
387 msg='Unexpected new-style class rendering %r' % gdb_repr)
388
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000389 def assertSane(self, source, corruption, exprepr=None):
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000390 '''Run Python under gdb, corrupting variables in the inferior process
391 immediately before taking a backtrace.
392
393 Verify that the variable's representation is the expected failsafe
394 representation'''
395 if corruption:
396 cmds_after_breakpoint=[corruption, 'backtrace']
397 else:
398 cmds_after_breakpoint=['backtrace']
399
400 gdb_repr, gdb_output = \
401 self.get_gdb_repr(source,
402 cmds_after_breakpoint=cmds_after_breakpoint)
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000403 if exprepr:
404 if gdb_repr == exprepr:
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000405 # gdb managed to print the value in spite of the corruption;
406 # this is good (see http://bugs.python.org/issue8330)
407 return
408
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000409 # Match anything for the type name; 0xDEADBEEF could point to
410 # something arbitrary (see http://bugs.python.org/issue8330)
Antoine Pitrou4d098732011-11-26 01:42:03 +0100411 pattern = '<.* at remote 0x-?[0-9a-f]+>'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000412
413 m = re.match(pattern, gdb_repr)
414 if not m:
415 self.fail('Unexpected gdb representation: %r\n%s' % \
416 (gdb_repr, gdb_output))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000417
418 def test_NULL_ptr(self):
419 'Ensure that a NULL PyObject* is handled gracefully'
420 gdb_repr, gdb_output = (
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000421 self.get_gdb_repr('id(42)',
422 cmds_after_breakpoint=['set variable v=0',
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000423 'backtrace'])
424 )
425
Ezio Melottib3aedd42010-11-20 19:04:17 +0000426 self.assertEqual(gdb_repr, '0x0')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000427
428 def test_NULL_ob_type(self):
429 'Ensure that a PyObject* with NULL ob_type is handled gracefully'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000430 self.assertSane('id(42)',
431 'set v->ob_type=0')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000432
433 def test_corrupt_ob_type(self):
434 'Ensure that a PyObject* with a corrupt ob_type is handled gracefully'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000435 self.assertSane('id(42)',
436 'set v->ob_type=0xDEADBEEF',
437 exprepr='42')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000438
439 def test_corrupt_tp_flags(self):
440 'Ensure that a PyObject* with a type with corrupt tp_flags is handled'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000441 self.assertSane('id(42)',
442 'set v->ob_type->tp_flags=0x0',
443 exprepr='42')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000444
445 def test_corrupt_tp_name(self):
446 'Ensure that a PyObject* with a type with corrupt tp_name is handled'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000447 self.assertSane('id(42)',
448 'set v->ob_type->tp_name=0xDEADBEEF',
449 exprepr='42')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000450
451 def test_builtins_help(self):
452 'Ensure that the new-style class _Helper in site.py can be handled'
453 # (this was the issue causing tracebacks in
454 # http://bugs.python.org/issue8032#msg100537 )
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000455 gdb_repr, gdb_output = self.get_gdb_repr('id(__builtins__.help)', import_site=True)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000456
Antoine Pitrou4d098732011-11-26 01:42:03 +0100457 m = re.match(r'<_Helper at remote 0x-?[0-9a-f]+>', gdb_repr)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000458 self.assertTrue(m,
459 msg='Unexpected rendering %r' % gdb_repr)
460
461 def test_selfreferential_list(self):
462 '''Ensure that a reference loop involving a list doesn't lead proxyval
463 into an infinite loop:'''
464 gdb_repr, gdb_output = \
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000465 self.get_gdb_repr("a = [3, 4, 5] ; a.append(a) ; id(a)")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000466 self.assertEqual(gdb_repr, '[3, 4, 5, [...]]')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000467
468 gdb_repr, gdb_output = \
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000469 self.get_gdb_repr("a = [3, 4, 5] ; b = [a] ; a.append(b) ; id(a)")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000470 self.assertEqual(gdb_repr, '[3, 4, 5, [[...]]]')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000471
472 def test_selfreferential_dict(self):
473 '''Ensure that a reference loop involving a dict doesn't lead proxyval
474 into an infinite loop:'''
475 gdb_repr, gdb_output = \
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000476 self.get_gdb_repr("a = {} ; b = {'bar':a} ; a['foo'] = b ; id(a)")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000477
Ezio Melottib3aedd42010-11-20 19:04:17 +0000478 self.assertEqual(gdb_repr, "{'foo': {'bar': {...}}}")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000479
480 def test_selfreferential_old_style_instance(self):
481 gdb_repr, gdb_output = \
482 self.get_gdb_repr('''
483class Foo:
484 pass
485foo = Foo()
486foo.an_attr = foo
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000487id(foo)''')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100488 self.assertTrue(re.match('<Foo\(an_attr=<\.\.\.>\) at remote 0x-?[0-9a-f]+>',
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000489 gdb_repr),
490 'Unexpected gdb representation: %r\n%s' % \
491 (gdb_repr, gdb_output))
492
493 def test_selfreferential_new_style_instance(self):
494 gdb_repr, gdb_output = \
495 self.get_gdb_repr('''
496class Foo(object):
497 pass
498foo = Foo()
499foo.an_attr = foo
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000500id(foo)''')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100501 self.assertTrue(re.match('<Foo\(an_attr=<\.\.\.>\) at remote 0x-?[0-9a-f]+>',
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000502 gdb_repr),
503 'Unexpected gdb representation: %r\n%s' % \
504 (gdb_repr, gdb_output))
505
506 gdb_repr, gdb_output = \
507 self.get_gdb_repr('''
508class Foo(object):
509 pass
510a = Foo()
511b = Foo()
512a.an_attr = b
513b.an_attr = a
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000514id(a)''')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100515 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 +0000516 gdb_repr),
517 'Unexpected gdb representation: %r\n%s' % \
518 (gdb_repr, gdb_output))
519
520 def test_truncation(self):
521 'Verify that very long output is truncated'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000522 gdb_repr, gdb_output = self.get_gdb_repr('id(list(range(1000)))')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000523 self.assertEqual(gdb_repr,
524 "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, "
525 "14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, "
526 "27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, "
527 "40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, "
528 "53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, "
529 "66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, "
530 "79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, "
531 "92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, "
532 "104, 105, 106, 107, 108, 109, 110, 111, 112, 113, "
533 "114, 115, 116, 117, 118, 119, 120, 121, 122, 123, "
534 "124, 125, 126, 127, 128, 129, 130, 131, 132, 133, "
535 "134, 135, 136, 137, 138, 139, 140, 141, 142, 143, "
536 "144, 145, 146, 147, 148, 149, 150, 151, 152, 153, "
537 "154, 155, 156, 157, 158, 159, 160, 161, 162, 163, "
538 "164, 165, 166, 167, 168, 169, 170, 171, 172, 173, "
539 "174, 175, 176, 177, 178, 179, 180, 181, 182, 183, "
540 "184, 185, 186, 187, 188, 189, 190, 191, 192, 193, "
541 "194, 195, 196, 197, 198, 199, 200, 201, 202, 203, "
542 "204, 205, 206, 207, 208, 209, 210, 211, 212, 213, "
543 "214, 215, 216, 217, 218, 219, 220, 221, 222, 223, "
544 "224, 225, 226...(truncated)")
545 self.assertEqual(len(gdb_repr),
546 1024 + len('...(truncated)'))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000547
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000548 def test_builtin_method(self):
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000549 gdb_repr, gdb_output = self.get_gdb_repr('import sys; id(sys.stdout.readlines)')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100550 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 +0000551 gdb_repr),
552 'Unexpected gdb representation: %r\n%s' % \
553 (gdb_repr, gdb_output))
554
555 def test_frames(self):
556 gdb_output = self.get_stack_trace('''
557def foo(a, b, c):
558 pass
559
560foo(3, 4, 5)
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000561id(foo.__code__)''',
562 breakpoint='builtin_id',
563 cmds_after_breakpoint=['print (PyFrameObject*)(((PyCodeObject*)v)->co_zombieframe)']
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000564 )
Antoine Pitrou4d098732011-11-26 01:42:03 +0100565 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 +0000566 gdb_output,
567 re.DOTALL),
568 'Unexpected gdb representation: %r\n%s' % (gdb_output, gdb_output))
569
Victor Stinnerd2084162011-12-19 13:42:24 +0100570@unittest.skipIf(python_is_optimized(),
571 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000572class PyListTests(DebuggerTests):
573 def assertListing(self, expected, actual):
574 self.assertEndsWith(actual, expected)
575
576 def test_basic_command(self):
577 'Verify that the "py-list" command works'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000578 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000579 cmds_after_breakpoint=['py-list'])
580
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000581 self.assertListing(' 5 \n'
582 ' 6 def bar(a, b, c):\n'
583 ' 7 baz(a, b, c)\n'
584 ' 8 \n'
585 ' 9 def baz(*args):\n'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000586 ' >10 id(42)\n'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000587 ' 11 \n'
588 ' 12 foo(1, 2, 3)\n',
589 bt)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000590
591 def test_one_abs_arg(self):
592 'Verify the "py-list" command with one absolute argument'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000593 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000594 cmds_after_breakpoint=['py-list 9'])
595
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000596 self.assertListing(' 9 def baz(*args):\n'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000597 ' >10 id(42)\n'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000598 ' 11 \n'
599 ' 12 foo(1, 2, 3)\n',
600 bt)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000601
602 def test_two_abs_args(self):
603 'Verify the "py-list" command with two absolute arguments'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000604 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000605 cmds_after_breakpoint=['py-list 1,3'])
606
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000607 self.assertListing(' 1 # Sample script for use by test_gdb.py\n'
608 ' 2 \n'
609 ' 3 def foo(a, b, c):\n',
610 bt)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000611
612class StackNavigationTests(DebuggerTests):
Victor Stinner50eb60e2010-04-20 22:32:07 +0000613 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Victor Stinnerd2084162011-12-19 13:42:24 +0100614 @unittest.skipIf(python_is_optimized(),
615 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000616 def test_pyup_command(self):
617 'Verify that the "py-up" command works'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000618 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000619 cmds_after_breakpoint=['py-up'])
620 self.assertMultilineMatches(bt,
621 r'''^.*
Antoine Pitrou4d098732011-11-26 01:42:03 +0100622#[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 +0000623 baz\(a, b, c\)
624$''')
625
Victor Stinner50eb60e2010-04-20 22:32:07 +0000626 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000627 def test_down_at_bottom(self):
628 'Verify handling of "py-down" at the bottom of the stack'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000629 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000630 cmds_after_breakpoint=['py-down'])
631 self.assertEndsWith(bt,
632 'Unable to find a newer python frame\n')
633
Victor Stinner50eb60e2010-04-20 22:32:07 +0000634 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000635 def test_up_at_top(self):
636 'Verify handling of "py-up" at the top of the stack'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000637 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000638 cmds_after_breakpoint=['py-up'] * 4)
639 self.assertEndsWith(bt,
640 'Unable to find an older python frame\n')
641
Victor Stinner50eb60e2010-04-20 22:32:07 +0000642 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Victor Stinnerd2084162011-12-19 13:42:24 +0100643 @unittest.skipIf(python_is_optimized(),
644 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000645 def test_up_then_down(self):
646 'Verify "py-up" followed by "py-down"'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000647 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000648 cmds_after_breakpoint=['py-up', 'py-down'])
649 self.assertMultilineMatches(bt,
650 r'''^.*
Antoine Pitrou4d098732011-11-26 01:42:03 +0100651#[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 +0000652 baz\(a, b, c\)
Antoine Pitrou4d098732011-11-26 01:42:03 +0100653#[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 +0000654 id\(42\)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000655$''')
656
657class PyBtTests(DebuggerTests):
Victor Stinnerd2084162011-12-19 13:42:24 +0100658 @unittest.skipIf(python_is_optimized(),
659 "Python was compiled with optimizations")
Victor Stinnere670c882011-05-13 17:40:15 +0200660 def test_bt(self):
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000661 'Verify that the "py-bt" command works'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000662 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000663 cmds_after_breakpoint=['py-bt'])
664 self.assertMultilineMatches(bt,
665 r'''^.*
Victor Stinnere670c882011-05-13 17:40:15 +0200666Traceback \(most recent call first\):
667 File ".*gdb_sample.py", line 10, in baz
668 id\(42\)
669 File ".*gdb_sample.py", line 7, in bar
670 baz\(a, b, c\)
671 File ".*gdb_sample.py", line 4, in foo
672 bar\(a, b, c\)
673 File ".*gdb_sample.py", line 12, in <module>
674 foo\(1, 2, 3\)
675''')
676
Victor Stinnerd2084162011-12-19 13:42:24 +0100677 @unittest.skipIf(python_is_optimized(),
678 "Python was compiled with optimizations")
Victor Stinnere670c882011-05-13 17:40:15 +0200679 def test_bt_full(self):
680 'Verify that the "py-bt-full" command works'
681 bt = self.get_stack_trace(script=self.get_sample_script(),
682 cmds_after_breakpoint=['py-bt-full'])
683 self.assertMultilineMatches(bt,
684 r'''^.*
Antoine Pitrou4d098732011-11-26 01:42:03 +0100685#[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 +0000686 baz\(a, b, c\)
Antoine Pitrou4d098732011-11-26 01:42:03 +0100687#[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 +0000688 bar\(a, b, c\)
Antoine Pitrou4d098732011-11-26 01:42:03 +0100689#[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 12, in <module> \(\)
Victor Stinnerd2084162011-12-19 13:42:24 +0100690 foo\(1, 2, 3\)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000691''')
692
David Malcolm8d37ffa2012-06-27 14:15:34 -0400693 @unittest.skipUnless(_thread,
694 "Python was compiled without thread support")
695 def test_threads(self):
696 'Verify that "py-bt" indicates threads that are waiting for the GIL'
697 cmd = '''
698from threading import Thread
699
700class TestThread(Thread):
701 # These threads would run forever, but we'll interrupt things with the
702 # debugger
703 def run(self):
704 i = 0
705 while 1:
706 i += 1
707
708t = {}
709for i in range(4):
710 t[i] = TestThread()
711 t[i].start()
712
713# Trigger a breakpoint on the main thread
714id(42)
715
716'''
717 # Verify with "py-bt":
718 gdb_output = self.get_stack_trace(cmd,
719 cmds_after_breakpoint=['thread apply all py-bt'])
720 self.assertIn('Waiting for the GIL', gdb_output)
721
722 # Verify with "py-bt-full":
723 gdb_output = self.get_stack_trace(cmd,
724 cmds_after_breakpoint=['thread apply all py-bt-full'])
725 self.assertIn('Waiting for the GIL', gdb_output)
726
727 @unittest.skipIf(python_is_optimized(),
728 "Python was compiled with optimizations")
729 # Some older versions of gdb will fail with
730 # "Cannot find new threads: generic error"
731 # unless we add LD_PRELOAD=PATH-TO-libpthread.so.1 as a workaround
732 @unittest.skipUnless(_thread,
733 "Python was compiled without thread support")
734 def test_gc(self):
735 'Verify that "py-bt" indicates if a thread is garbage-collecting'
736 cmd = ('from gc import collect\n'
737 'id(42)\n'
738 'def foo():\n'
739 ' collect()\n'
740 'def bar():\n'
741 ' foo()\n'
742 'bar()\n')
743 # Verify with "py-bt":
744 gdb_output = self.get_stack_trace(cmd,
745 cmds_after_breakpoint=['break update_refs', 'continue', 'py-bt'],
746 )
747 self.assertIn('Garbage-collecting', gdb_output)
748
749 # Verify with "py-bt-full":
750 gdb_output = self.get_stack_trace(cmd,
751 cmds_after_breakpoint=['break update_refs', 'continue', 'py-bt-full'],
752 )
753 self.assertIn('Garbage-collecting', gdb_output)
754
755 @unittest.skipIf(python_is_optimized(),
756 "Python was compiled with optimizations")
757 # Some older versions of gdb will fail with
758 # "Cannot find new threads: generic error"
759 # unless we add LD_PRELOAD=PATH-TO-libpthread.so.1 as a workaround
760 @unittest.skipUnless(_thread,
761 "Python was compiled without thread support")
762 def test_pycfunction(self):
763 'Verify that "py-bt" displays invocations of PyCFunction instances'
764 cmd = ('from time import sleep\n'
765 'def foo():\n'
766 ' sleep(1)\n'
767 'def bar():\n'
768 ' foo()\n'
769 'bar()\n')
770 # Verify with "py-bt":
771 gdb_output = self.get_stack_trace(cmd,
772 breakpoint='time_sleep',
773 cmds_after_breakpoint=['bt', 'py-bt'],
774 )
775 self.assertIn('<built-in method sleep', gdb_output)
776
777 # Verify with "py-bt-full":
778 gdb_output = self.get_stack_trace(cmd,
779 breakpoint='time_sleep',
780 cmds_after_breakpoint=['py-bt-full'],
781 )
782 self.assertIn('#0 <built-in method sleep', gdb_output)
783
784
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000785class PyPrintTests(DebuggerTests):
Victor Stinnerd2084162011-12-19 13:42:24 +0100786 @unittest.skipIf(python_is_optimized(),
787 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000788 def test_basic_command(self):
789 'Verify that the "py-print" command works'
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-print args'])
792 self.assertMultilineMatches(bt,
793 r".*\nlocal 'args' = \(1, 2, 3\)\n.*")
794
Vinay Sajip2549f872012-01-04 12:07:30 +0000795 @unittest.skipIf(python_is_optimized(),
796 "Python was compiled with optimizations")
Victor Stinner50eb60e2010-04-20 22:32:07 +0000797 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000798 def test_print_after_up(self):
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000799 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000800 cmds_after_breakpoint=['py-up', 'py-print c', 'py-print b', 'py-print a'])
801 self.assertMultilineMatches(bt,
802 r".*\nlocal 'c' = 3\nlocal 'b' = 2\nlocal 'a' = 1\n.*")
803
Victor Stinnerd2084162011-12-19 13:42:24 +0100804 @unittest.skipIf(python_is_optimized(),
805 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000806 def test_printing_global(self):
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000807 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000808 cmds_after_breakpoint=['py-print __name__'])
809 self.assertMultilineMatches(bt,
810 r".*\nglobal '__name__' = '__main__'\n.*")
811
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_printing_builtin(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-print len'])
817 self.assertMultilineMatches(bt,
Antoine Pitrou4d098732011-11-26 01:42:03 +0100818 r".*\nbuiltin 'len' = <built-in method len of module object at remote 0x-?[0-9a-f]+>\n.*")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000819
820class PyLocalsTests(DebuggerTests):
Victor Stinnerd2084162011-12-19 13:42:24 +0100821 @unittest.skipIf(python_is_optimized(),
822 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000823 def test_basic_command(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-locals'])
826 self.assertMultilineMatches(bt,
827 r".*\nargs = \(1, 2, 3\)\n.*")
828
Victor Stinner50eb60e2010-04-20 22:32:07 +0000829 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Vinay Sajip2549f872012-01-04 12:07:30 +0000830 @unittest.skipIf(python_is_optimized(),
831 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000832 def test_locals_after_up(self):
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000833 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000834 cmds_after_breakpoint=['py-up', 'py-locals'])
835 self.assertMultilineMatches(bt,
836 r".*\na = 1\nb = 2\nc = 3\n.*")
837
838def test_main():
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000839 run_unittest(PrettyPrintTests,
840 PyListTests,
841 StackNavigationTests,
842 PyBtTests,
843 PyPrintTests,
844 PyLocalsTests
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000845 )
846
847if __name__ == "__main__":
848 test_main()