blob: 27fccd672514d3ce8992260ac5d98f4ac1e0d203 [file] [log] [blame]
Benjamin Peterson6a6666a2010-04-11 21:49:28 +00001# Verify that gdb can pretty-print the various PyObject* types
2#
3# The code for testing gdb was adapted from similar work in Unladen Swallow's
4# Lib/test/test_jit_gdb.py
5
6import os
7import re
8import subprocess
9import sys
Vinay Sajipf1b34ee2012-05-06 12:03:05 +010010import sysconfig
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000011import unittest
Victor Stinner150016f2010-05-19 23:04:56 +000012import locale
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000013
David Malcolm8d37ffa2012-06-27 14:15:34 -040014# Is this Python configured to support threads?
15try:
16 import _thread
17except ImportError:
18 _thread = None
19
Benjamin Peterson65c66ab2010-10-29 21:31:35 +000020from test.support import run_unittest, findfile, python_is_optimized
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000021
22try:
23 gdb_version, _ = subprocess.Popen(["gdb", "--version"],
24 stdout=subprocess.PIPE).communicate()
25except OSError:
26 # This is what "no gdb" looks like. There may, however, be other
27 # errors that manifest this way too.
28 raise unittest.SkipTest("Couldn't find gdb on the path")
29gdb_version_number = re.search(b"^GNU gdb [^\d]*(\d+)\.", gdb_version)
30if int(gdb_version_number.group(1)) < 7:
31 raise unittest.SkipTest("gdb versions before 7.0 didn't support python embedding"
Antoine Pitrou6a45e9d2010-04-11 22:47:34 +000032 " Saw:\n" + gdb_version.decode('ascii', 'replace'))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000033
Vinay Sajipf1b34ee2012-05-06 12:03:05 +010034if not sysconfig.is_python_build():
35 raise unittest.SkipTest("test_gdb only works on source builds at the moment.")
36
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000037# Verify that "gdb" was built with the embedded python support enabled:
38cmd = "--eval-command=python import sys; print sys.version_info"
39p = subprocess.Popen(["gdb", "--batch", cmd],
40 stdout=subprocess.PIPE)
41gdbpy_version, _ = p.communicate()
Benjamin Peterson9faa7ec2010-04-11 23:51:24 +000042if gdbpy_version == b'':
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000043 raise unittest.SkipTest("gdb not built with embedded python support")
44
Nick Coghlanbe4e4b52012-06-17 18:57:20 +100045# Verify that "gdb" can load our custom hooks
46p = subprocess.Popen(["gdb", "--batch", cmd,
47 "--args", sys.executable],
48 stdout=subprocess.PIPE, stderr=subprocess.PIPE)
49__, gdbpy_errors = p.communicate()
50if b"auto-loading has been declined" in gdbpy_errors:
51 msg = "gdb security settings prevent use of custom hooks: %s"
52 raise unittest.SkipTest(msg % gdbpy_errors)
53
Victor Stinner50eb60e2010-04-20 22:32:07 +000054def gdb_has_frame_select():
55 # Does this build of gdb have gdb.Frame.select ?
56 cmd = "--eval-command=python print(dir(gdb.Frame))"
57 p = subprocess.Popen(["gdb", "--batch", cmd],
58 stdout=subprocess.PIPE)
59 stdout, _ = p.communicate()
60 m = re.match(br'.*\[(.*)\].*', stdout)
61 if not m:
62 raise unittest.SkipTest("Unable to parse output from gdb.Frame.select test")
63 gdb_frame_dir = m.group(1).split(b', ')
64 return b"'select'" in gdb_frame_dir
65
66HAS_PYUP_PYDOWN = gdb_has_frame_select()
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000067
Martin v. Löwis5ae68102010-04-21 22:38:42 +000068BREAKPOINT_FN='builtin_id'
69
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000070class DebuggerTests(unittest.TestCase):
71
72 """Test that the debugger can debug Python."""
73
Georg Brandl09a7c722012-02-20 21:31:46 +010074 def run_gdb(self, *args, **env_vars):
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000075 """Runs gdb with the command line given by *args.
76
77 Returns its stdout, stderr
78 """
Georg Brandl09a7c722012-02-20 21:31:46 +010079 if env_vars:
80 env = os.environ.copy()
81 env.update(env_vars)
82 else:
83 env = None
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000084 out, err = subprocess.Popen(
Georg Brandl09a7c722012-02-20 21:31:46 +010085 args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env,
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000086 ).communicate()
Victor Stinner534db4e2010-04-23 20:33:55 +000087 return out.decode('utf-8', 'replace'), err.decode('utf-8', 'replace')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000088
89 def get_stack_trace(self, source=None, script=None,
Martin v. Löwis5ae68102010-04-21 22:38:42 +000090 breakpoint=BREAKPOINT_FN,
Benjamin Peterson6a6666a2010-04-11 21:49:28 +000091 cmds_after_breakpoint=None,
92 import_site=False):
93 '''
94 Run 'python -c SOURCE' under gdb with a breakpoint.
95
96 Support injecting commands after the breakpoint is reached
97
98 Returns the stdout from gdb
99
100 cmds_after_breakpoint: if provided, a list of strings: gdb commands
101 '''
102 # We use "set breakpoint pending yes" to avoid blocking with a:
103 # Function "foo" not defined.
104 # Make breakpoint pending on future shared library load? (y or [n])
105 # error, which typically happens python is dynamically linked (the
106 # breakpoints of interest are to be found in the shared library)
107 # When this happens, we still get:
Victor Stinner67df3a42010-04-21 13:53:05 +0000108 # Function "textiowrapper_write" not defined.
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000109 # emitted to stderr each time, alas.
110
111 # Initially I had "--eval-command=continue" here, but removed it to
112 # avoid repeated print breakpoints when traversing hierarchical data
113 # structures
114
115 # Generate a list of commands in gdb's language:
116 commands = ['set breakpoint pending yes',
117 'break %s' % breakpoint,
118 'run']
119 if cmds_after_breakpoint:
120 commands += cmds_after_breakpoint
121 else:
122 commands += ['backtrace']
123
124 # print commands
125
126 # Use "commands" to generate the arguments with which to invoke "gdb":
127 args = ["gdb", "--batch"]
128 args += ['--eval-command=%s' % cmd for cmd in commands]
129 args += ["--args",
130 sys.executable]
131
132 if not import_site:
133 # -S suppresses the default 'import site'
134 args += ["-S"]
135
136 if source:
137 args += ["-c", source]
138 elif script:
139 args += [script]
140
141 # print args
142 # print ' '.join(args)
143
144 # Use "args" to invoke gdb, capturing stdout, stderr:
Georg Brandl09a7c722012-02-20 21:31:46 +0100145 out, err = self.run_gdb(*args, PYTHONHASHSEED='0')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000146
147 # Ignore some noise on stderr due to the pending breakpoint:
148 err = err.replace('Function "%s" not defined.\n' % breakpoint, '')
Antoine Pitroua045f192010-05-05 18:30:22 +0000149 # Ignore some other noise on stderr (http://bugs.python.org/issue8600)
150 err = err.replace("warning: Unable to find libthread_db matching"
151 " inferior's thread library, thread debugging will"
152 " not be available.\n",
153 '')
Jesus Ceacee36552011-03-16 01:33:16 +0100154 err = err.replace("warning: Cannot initialize thread debugging"
155 " library: Debugger service failed\n",
156 '')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000157
158 # Ensure no unexpected error messages:
Ezio Melottib3aedd42010-11-20 19:04:17 +0000159 self.assertEqual(err, '')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000160 return out
161
162 def get_gdb_repr(self, source,
163 cmds_after_breakpoint=None,
164 import_site=False):
165 # Given an input python source representation of data,
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000166 # run "python -c'id(DATA)'" under gdb with a breakpoint on
167 # builtin_id and scrape out gdb's representation of the "op"
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000168 # parameter, and verify that the gdb displays the same string
169 #
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000170 # Verify that the gdb displays the expected string
171 #
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000172 # For a nested structure, the first time we hit the breakpoint will
173 # give us the top-level structure
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000174 gdb_output = self.get_stack_trace(source, breakpoint=BREAKPOINT_FN,
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000175 cmds_after_breakpoint=cmds_after_breakpoint,
176 import_site=import_site)
177 # gdb can insert additional '\n' and space characters in various places
178 # in its output, depending on the width of the terminal it's connected
179 # to (using its "wrap_here" function)
David Malcolm8d37ffa2012-06-27 14:15:34 -0400180 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 +0000181 gdb_output, re.DOTALL)
182 if not m:
183 self.fail('Unexpected gdb output: %r\n%s' % (gdb_output, gdb_output))
184 return m.group(1), gdb_output
185
186 def assertEndsWith(self, actual, exp_end):
187 '''Ensure that the given "actual" string ends with "exp_end"'''
Ezio Melottib3aedd42010-11-20 19:04:17 +0000188 self.assertTrue(actual.endswith(exp_end),
189 msg='%r did not end with %r' % (actual, exp_end))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000190
191 def assertMultilineMatches(self, actual, pattern):
192 m = re.match(pattern, actual, re.DOTALL)
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000193 if not m:
194 self.fail(msg='%r did not match %r' % (actual, pattern))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000195
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000196 def get_sample_script(self):
197 return findfile('gdb_sample.py')
198
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000199class PrettyPrintTests(DebuggerTests):
200 def test_getting_backtrace(self):
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000201 gdb_output = self.get_stack_trace('id(42)')
202 self.assertTrue(BREAKPOINT_FN in gdb_output)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000203
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000204 def assertGdbRepr(self, val, exp_repr=None, cmds_after_breakpoint=None):
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000205 # Ensure that gdb's rendering of the value in a debugged process
206 # matches repr(value) in this process:
Victor Stinner150016f2010-05-19 23:04:56 +0000207 gdb_repr, gdb_output = self.get_gdb_repr('id(' + ascii(val) + ')',
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000208 cmds_after_breakpoint)
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000209 if not exp_repr:
210 exp_repr = repr(val)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000211 self.assertEqual(gdb_repr, exp_repr,
212 ('%r did not equal expected %r; full output was:\n%s'
213 % (gdb_repr, exp_repr, gdb_output)))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000214
215 def test_int(self):
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000216 'Verify the pretty-printing of various "int"/long values'
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000217 self.assertGdbRepr(42)
218 self.assertGdbRepr(0)
219 self.assertGdbRepr(-7)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000220 self.assertGdbRepr(1000000000000)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000221 self.assertGdbRepr(-1000000000000000)
222
223 def test_singletons(self):
224 'Verify the pretty-printing of True, False and None'
225 self.assertGdbRepr(True)
226 self.assertGdbRepr(False)
227 self.assertGdbRepr(None)
228
229 def test_dicts(self):
230 'Verify the pretty-printing of dictionaries'
231 self.assertGdbRepr({})
232 self.assertGdbRepr({'foo': 'bar'})
Georg Brandl09a7c722012-02-20 21:31:46 +0100233 self.assertGdbRepr({'foo': 'bar', 'douglas': 42},
234 "{'foo': 'bar', 'douglas': 42}")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000235
236 def test_lists(self):
237 'Verify the pretty-printing of lists'
238 self.assertGdbRepr([])
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000239 self.assertGdbRepr(list(range(5)))
240
241 def test_bytes(self):
242 'Verify the pretty-printing of bytes'
243 self.assertGdbRepr(b'')
244 self.assertGdbRepr(b'And now for something hopefully the same')
245 self.assertGdbRepr(b'string with embedded NUL here \0 and then some more text')
246 self.assertGdbRepr(b'this is a tab:\t'
247 b' this is a slash-N:\n'
248 b' this is a slash-R:\r'
249 )
250
251 self.assertGdbRepr(b'this is byte 255:\xff and byte 128:\x80')
252
253 self.assertGdbRepr(bytes([b for b in range(255)]))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000254
255 def test_strings(self):
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000256 'Verify the pretty-printing of unicode strings'
Victor Stinner150016f2010-05-19 23:04:56 +0000257 encoding = locale.getpreferredencoding()
258 def check_repr(text):
259 try:
260 text.encode(encoding)
261 printable = True
262 except UnicodeEncodeError:
Antoine Pitrou4c7c4212010-09-09 20:40:28 +0000263 self.assertGdbRepr(text, ascii(text))
Victor Stinner150016f2010-05-19 23:04:56 +0000264 else:
265 self.assertGdbRepr(text)
266
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000267 self.assertGdbRepr('')
268 self.assertGdbRepr('And now for something hopefully the same')
269 self.assertGdbRepr('string with embedded NUL here \0 and then some more text')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000270
271 # Test printing a single character:
272 # U+2620 SKULL AND CROSSBONES
Victor Stinner150016f2010-05-19 23:04:56 +0000273 check_repr('\u2620')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000274
275 # Test printing a Japanese unicode string
276 # (I believe this reads "mojibake", using 3 characters from the CJK
277 # Unified Ideographs area, followed by U+3051 HIRAGANA LETTER KE)
Victor Stinner150016f2010-05-19 23:04:56 +0000278 check_repr('\u6587\u5b57\u5316\u3051')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000279
280 # Test a character outside the BMP:
281 # U+1D121 MUSICAL SYMBOL C CLEF
282 # This is:
283 # UTF-8: 0xF0 0x9D 0x84 0xA1
284 # UTF-16: 0xD834 0xDD21
Victor Stinner150016f2010-05-19 23:04:56 +0000285 check_repr(chr(0x1D121))
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000286
287 def test_tuples(self):
288 'Verify the pretty-printing of tuples'
289 self.assertGdbRepr(tuple())
290 self.assertGdbRepr((1,), '(1,)')
291 self.assertGdbRepr(('foo', 'bar', 'baz'))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000292
293 def test_sets(self):
294 'Verify the pretty-printing of sets'
295 self.assertGdbRepr(set())
Georg Brandl09a7c722012-02-20 21:31:46 +0100296 self.assertGdbRepr(set(['a', 'b']), "{'a', 'b'}")
297 self.assertGdbRepr(set([4, 5, 6]), "{4, 5, 6}")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000298
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000299 # Ensure that we handle sets containing the "dummy" key value,
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000300 # which happens on deletion:
301 gdb_repr, gdb_output = self.get_gdb_repr('''s = set(['a','b'])
302s.pop()
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000303id(s)''')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000304 self.assertEqual(gdb_repr, "{'b'}")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000305
306 def test_frozensets(self):
307 'Verify the pretty-printing of frozensets'
308 self.assertGdbRepr(frozenset())
Georg Brandl09a7c722012-02-20 21:31:46 +0100309 self.assertGdbRepr(frozenset(['a', 'b']), "frozenset({'a', 'b'})")
310 self.assertGdbRepr(frozenset([4, 5, 6]), "frozenset({4, 5, 6})")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000311
312 def test_exceptions(self):
313 # Test a RuntimeError
314 gdb_repr, gdb_output = self.get_gdb_repr('''
315try:
316 raise RuntimeError("I am an error")
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000317except RuntimeError as e:
318 id(e)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000319''')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000320 self.assertEqual(gdb_repr,
321 "RuntimeError('I am an error',)")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000322
323
324 # Test division by zero:
325 gdb_repr, gdb_output = self.get_gdb_repr('''
326try:
327 a = 1 / 0
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000328except ZeroDivisionError as e:
329 id(e)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000330''')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000331 self.assertEqual(gdb_repr,
332 "ZeroDivisionError('division by zero',)")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000333
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000334 def test_modern_class(self):
335 'Verify the pretty-printing of new-style class instances'
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000336 gdb_repr, gdb_output = self.get_gdb_repr('''
337class Foo:
338 pass
339foo = Foo()
340foo.an_int = 42
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000341id(foo)''')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100342 m = re.match(r'<Foo\(an_int=42\) at remote 0x-?[0-9a-f]+>', gdb_repr)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000343 self.assertTrue(m,
344 msg='Unexpected new-style class rendering %r' % gdb_repr)
345
346 def test_subclassing_list(self):
347 'Verify the pretty-printing of an instance of a list subclass'
348 gdb_repr, gdb_output = self.get_gdb_repr('''
349class Foo(list):
350 pass
351foo = Foo()
352foo += [1, 2, 3]
353foo.an_int = 42
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000354id(foo)''')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100355 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 +0000356
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_tuple(self):
361 'Verify the pretty-printing of an instance of a tuple subclass'
362 # This should exercise the negative tp_dictoffset code in the
363 # new-style class support
364 gdb_repr, gdb_output = self.get_gdb_repr('''
365class Foo(tuple):
366 pass
367foo = Foo((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
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000375 def assertSane(self, source, corruption, exprepr=None):
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000376 '''Run Python under gdb, corrupting variables in the inferior process
377 immediately before taking a backtrace.
378
379 Verify that the variable's representation is the expected failsafe
380 representation'''
381 if corruption:
382 cmds_after_breakpoint=[corruption, 'backtrace']
383 else:
384 cmds_after_breakpoint=['backtrace']
385
386 gdb_repr, gdb_output = \
387 self.get_gdb_repr(source,
388 cmds_after_breakpoint=cmds_after_breakpoint)
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000389 if exprepr:
390 if gdb_repr == exprepr:
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000391 # gdb managed to print the value in spite of the corruption;
392 # this is good (see http://bugs.python.org/issue8330)
393 return
394
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000395 # Match anything for the type name; 0xDEADBEEF could point to
396 # something arbitrary (see http://bugs.python.org/issue8330)
Antoine Pitrou4d098732011-11-26 01:42:03 +0100397 pattern = '<.* at remote 0x-?[0-9a-f]+>'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000398
399 m = re.match(pattern, gdb_repr)
400 if not m:
401 self.fail('Unexpected gdb representation: %r\n%s' % \
402 (gdb_repr, gdb_output))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000403
404 def test_NULL_ptr(self):
405 'Ensure that a NULL PyObject* is handled gracefully'
406 gdb_repr, gdb_output = (
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000407 self.get_gdb_repr('id(42)',
408 cmds_after_breakpoint=['set variable v=0',
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000409 'backtrace'])
410 )
411
Ezio Melottib3aedd42010-11-20 19:04:17 +0000412 self.assertEqual(gdb_repr, '0x0')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000413
414 def test_NULL_ob_type(self):
415 'Ensure that a PyObject* with NULL ob_type is handled gracefully'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000416 self.assertSane('id(42)',
417 'set v->ob_type=0')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000418
419 def test_corrupt_ob_type(self):
420 'Ensure that a PyObject* with a corrupt ob_type is handled gracefully'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000421 self.assertSane('id(42)',
422 'set v->ob_type=0xDEADBEEF',
423 exprepr='42')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000424
425 def test_corrupt_tp_flags(self):
426 'Ensure that a PyObject* with a type with corrupt tp_flags is handled'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000427 self.assertSane('id(42)',
428 'set v->ob_type->tp_flags=0x0',
429 exprepr='42')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000430
431 def test_corrupt_tp_name(self):
432 'Ensure that a PyObject* with a type with corrupt tp_name is handled'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000433 self.assertSane('id(42)',
434 'set v->ob_type->tp_name=0xDEADBEEF',
435 exprepr='42')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000436
437 def test_builtins_help(self):
438 'Ensure that the new-style class _Helper in site.py can be handled'
439 # (this was the issue causing tracebacks in
440 # http://bugs.python.org/issue8032#msg100537 )
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000441 gdb_repr, gdb_output = self.get_gdb_repr('id(__builtins__.help)', import_site=True)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000442
Antoine Pitrou4d098732011-11-26 01:42:03 +0100443 m = re.match(r'<_Helper at remote 0x-?[0-9a-f]+>', gdb_repr)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000444 self.assertTrue(m,
445 msg='Unexpected rendering %r' % gdb_repr)
446
447 def test_selfreferential_list(self):
448 '''Ensure that a reference loop involving a list doesn't lead proxyval
449 into an infinite loop:'''
450 gdb_repr, gdb_output = \
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000451 self.get_gdb_repr("a = [3, 4, 5] ; a.append(a) ; id(a)")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000452 self.assertEqual(gdb_repr, '[3, 4, 5, [...]]')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000453
454 gdb_repr, gdb_output = \
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000455 self.get_gdb_repr("a = [3, 4, 5] ; b = [a] ; a.append(b) ; id(a)")
Ezio Melottib3aedd42010-11-20 19:04:17 +0000456 self.assertEqual(gdb_repr, '[3, 4, 5, [[...]]]')
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000457
458 def test_selfreferential_dict(self):
459 '''Ensure that a reference loop involving a dict doesn't lead proxyval
460 into an infinite loop:'''
461 gdb_repr, gdb_output = \
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000462 self.get_gdb_repr("a = {} ; b = {'bar':a} ; a['foo'] = b ; id(a)")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000463
Ezio Melottib3aedd42010-11-20 19:04:17 +0000464 self.assertEqual(gdb_repr, "{'foo': {'bar': {...}}}")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000465
466 def test_selfreferential_old_style_instance(self):
467 gdb_repr, gdb_output = \
468 self.get_gdb_repr('''
469class Foo:
470 pass
471foo = Foo()
472foo.an_attr = foo
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000473id(foo)''')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100474 self.assertTrue(re.match('<Foo\(an_attr=<\.\.\.>\) at remote 0x-?[0-9a-f]+>',
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000475 gdb_repr),
476 'Unexpected gdb representation: %r\n%s' % \
477 (gdb_repr, gdb_output))
478
479 def test_selfreferential_new_style_instance(self):
480 gdb_repr, gdb_output = \
481 self.get_gdb_repr('''
482class Foo(object):
483 pass
484foo = Foo()
485foo.an_attr = foo
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000486id(foo)''')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100487 self.assertTrue(re.match('<Foo\(an_attr=<\.\.\.>\) at remote 0x-?[0-9a-f]+>',
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000488 gdb_repr),
489 'Unexpected gdb representation: %r\n%s' % \
490 (gdb_repr, gdb_output))
491
492 gdb_repr, gdb_output = \
493 self.get_gdb_repr('''
494class Foo(object):
495 pass
496a = Foo()
497b = Foo()
498a.an_attr = b
499b.an_attr = a
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000500id(a)''')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100501 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 +0000502 gdb_repr),
503 'Unexpected gdb representation: %r\n%s' % \
504 (gdb_repr, gdb_output))
505
506 def test_truncation(self):
507 'Verify that very long output is truncated'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000508 gdb_repr, gdb_output = self.get_gdb_repr('id(list(range(1000)))')
Ezio Melottib3aedd42010-11-20 19:04:17 +0000509 self.assertEqual(gdb_repr,
510 "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, "
511 "14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, "
512 "27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, "
513 "40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, "
514 "53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, "
515 "66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, "
516 "79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, "
517 "92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, "
518 "104, 105, 106, 107, 108, 109, 110, 111, 112, 113, "
519 "114, 115, 116, 117, 118, 119, 120, 121, 122, 123, "
520 "124, 125, 126, 127, 128, 129, 130, 131, 132, 133, "
521 "134, 135, 136, 137, 138, 139, 140, 141, 142, 143, "
522 "144, 145, 146, 147, 148, 149, 150, 151, 152, 153, "
523 "154, 155, 156, 157, 158, 159, 160, 161, 162, 163, "
524 "164, 165, 166, 167, 168, 169, 170, 171, 172, 173, "
525 "174, 175, 176, 177, 178, 179, 180, 181, 182, 183, "
526 "184, 185, 186, 187, 188, 189, 190, 191, 192, 193, "
527 "194, 195, 196, 197, 198, 199, 200, 201, 202, 203, "
528 "204, 205, 206, 207, 208, 209, 210, 211, 212, 213, "
529 "214, 215, 216, 217, 218, 219, 220, 221, 222, 223, "
530 "224, 225, 226...(truncated)")
531 self.assertEqual(len(gdb_repr),
532 1024 + len('...(truncated)'))
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000533
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000534 def test_builtin_method(self):
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000535 gdb_repr, gdb_output = self.get_gdb_repr('import sys; id(sys.stdout.readlines)')
Antoine Pitrou4d098732011-11-26 01:42:03 +0100536 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 +0000537 gdb_repr),
538 'Unexpected gdb representation: %r\n%s' % \
539 (gdb_repr, gdb_output))
540
541 def test_frames(self):
542 gdb_output = self.get_stack_trace('''
543def foo(a, b, c):
544 pass
545
546foo(3, 4, 5)
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000547id(foo.__code__)''',
548 breakpoint='builtin_id',
549 cmds_after_breakpoint=['print (PyFrameObject*)(((PyCodeObject*)v)->co_zombieframe)']
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000550 )
Antoine Pitrou4d098732011-11-26 01:42:03 +0100551 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 +0000552 gdb_output,
553 re.DOTALL),
554 'Unexpected gdb representation: %r\n%s' % (gdb_output, gdb_output))
555
Victor Stinnerd2084162011-12-19 13:42:24 +0100556@unittest.skipIf(python_is_optimized(),
557 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000558class PyListTests(DebuggerTests):
559 def assertListing(self, expected, actual):
560 self.assertEndsWith(actual, expected)
561
562 def test_basic_command(self):
563 'Verify that the "py-list" command works'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000564 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000565 cmds_after_breakpoint=['py-list'])
566
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000567 self.assertListing(' 5 \n'
568 ' 6 def bar(a, b, c):\n'
569 ' 7 baz(a, b, c)\n'
570 ' 8 \n'
571 ' 9 def baz(*args):\n'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000572 ' >10 id(42)\n'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000573 ' 11 \n'
574 ' 12 foo(1, 2, 3)\n',
575 bt)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000576
577 def test_one_abs_arg(self):
578 'Verify the "py-list" command with one absolute argument'
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 9'])
581
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000582 self.assertListing(' 9 def baz(*args):\n'
Martin v. Löwis5ae68102010-04-21 22:38:42 +0000583 ' >10 id(42)\n'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000584 ' 11 \n'
585 ' 12 foo(1, 2, 3)\n',
586 bt)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000587
588 def test_two_abs_args(self):
589 'Verify the "py-list" command with two absolute arguments'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000590 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000591 cmds_after_breakpoint=['py-list 1,3'])
592
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000593 self.assertListing(' 1 # Sample script for use by test_gdb.py\n'
594 ' 2 \n'
595 ' 3 def foo(a, b, c):\n',
596 bt)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000597
598class StackNavigationTests(DebuggerTests):
Victor Stinner50eb60e2010-04-20 22:32:07 +0000599 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Victor Stinnerd2084162011-12-19 13:42:24 +0100600 @unittest.skipIf(python_is_optimized(),
601 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000602 def test_pyup_command(self):
603 'Verify that the "py-up" command works'
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-up'])
606 self.assertMultilineMatches(bt,
607 r'''^.*
Antoine Pitrou4d098732011-11-26 01:42:03 +0100608#[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 +0000609 baz\(a, b, c\)
610$''')
611
Victor Stinner50eb60e2010-04-20 22:32:07 +0000612 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000613 def test_down_at_bottom(self):
614 'Verify handling of "py-down" at the bottom of the stack'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000615 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000616 cmds_after_breakpoint=['py-down'])
617 self.assertEndsWith(bt,
618 'Unable to find a newer python frame\n')
619
Victor Stinner50eb60e2010-04-20 22:32:07 +0000620 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000621 def test_up_at_top(self):
622 'Verify handling of "py-up" at the top of the stack'
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'] * 4)
625 self.assertEndsWith(bt,
626 'Unable to find an older python frame\n')
627
Victor Stinner50eb60e2010-04-20 22:32:07 +0000628 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Victor Stinnerd2084162011-12-19 13:42:24 +0100629 @unittest.skipIf(python_is_optimized(),
630 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000631 def test_up_then_down(self):
632 'Verify "py-up" followed by "py-down"'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000633 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000634 cmds_after_breakpoint=['py-up', 'py-down'])
635 self.assertMultilineMatches(bt,
636 r'''^.*
Antoine Pitrou4d098732011-11-26 01:42:03 +0100637#[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 +0000638 baz\(a, b, c\)
Antoine Pitrou4d098732011-11-26 01:42:03 +0100639#[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 +0000640 id\(42\)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000641$''')
642
643class PyBtTests(DebuggerTests):
Victor Stinnerd2084162011-12-19 13:42:24 +0100644 @unittest.skipIf(python_is_optimized(),
645 "Python was compiled with optimizations")
Victor Stinnere670c882011-05-13 17:40:15 +0200646 def test_bt(self):
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000647 'Verify that the "py-bt" command works'
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-bt'])
650 self.assertMultilineMatches(bt,
651 r'''^.*
Victor Stinnere670c882011-05-13 17:40:15 +0200652Traceback \(most recent call first\):
653 File ".*gdb_sample.py", line 10, in baz
654 id\(42\)
655 File ".*gdb_sample.py", line 7, in bar
656 baz\(a, b, c\)
657 File ".*gdb_sample.py", line 4, in foo
658 bar\(a, b, c\)
659 File ".*gdb_sample.py", line 12, in <module>
660 foo\(1, 2, 3\)
661''')
662
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_full(self):
666 'Verify that the "py-bt-full" command works'
667 bt = self.get_stack_trace(script=self.get_sample_script(),
668 cmds_after_breakpoint=['py-bt-full'])
669 self.assertMultilineMatches(bt,
670 r'''^.*
Antoine Pitrou4d098732011-11-26 01:42:03 +0100671#[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 +0000672 baz\(a, b, c\)
Antoine Pitrou4d098732011-11-26 01:42:03 +0100673#[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 +0000674 bar\(a, b, c\)
Antoine Pitrou4d098732011-11-26 01:42:03 +0100675#[0-9]+ Frame 0x-?[0-9a-f]+, for file .*gdb_sample.py, line 12, in <module> \(\)
Victor Stinnerd2084162011-12-19 13:42:24 +0100676 foo\(1, 2, 3\)
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000677''')
678
David Malcolm8d37ffa2012-06-27 14:15:34 -0400679 @unittest.skipUnless(_thread,
680 "Python was compiled without thread support")
681 def test_threads(self):
682 'Verify that "py-bt" indicates threads that are waiting for the GIL'
683 cmd = '''
684from threading import Thread
685
686class TestThread(Thread):
687 # These threads would run forever, but we'll interrupt things with the
688 # debugger
689 def run(self):
690 i = 0
691 while 1:
692 i += 1
693
694t = {}
695for i in range(4):
696 t[i] = TestThread()
697 t[i].start()
698
699# Trigger a breakpoint on the main thread
700id(42)
701
702'''
703 # Verify with "py-bt":
704 gdb_output = self.get_stack_trace(cmd,
705 cmds_after_breakpoint=['thread apply all py-bt'])
706 self.assertIn('Waiting for the GIL', gdb_output)
707
708 # Verify with "py-bt-full":
709 gdb_output = self.get_stack_trace(cmd,
710 cmds_after_breakpoint=['thread apply all py-bt-full'])
711 self.assertIn('Waiting for the GIL', gdb_output)
712
713 @unittest.skipIf(python_is_optimized(),
714 "Python was compiled with optimizations")
715 # Some older versions of gdb will fail with
716 # "Cannot find new threads: generic error"
717 # unless we add LD_PRELOAD=PATH-TO-libpthread.so.1 as a workaround
718 @unittest.skipUnless(_thread,
719 "Python was compiled without thread support")
720 def test_gc(self):
721 'Verify that "py-bt" indicates if a thread is garbage-collecting'
722 cmd = ('from gc import collect\n'
723 'id(42)\n'
724 'def foo():\n'
725 ' collect()\n'
726 'def bar():\n'
727 ' foo()\n'
728 'bar()\n')
729 # Verify with "py-bt":
730 gdb_output = self.get_stack_trace(cmd,
731 cmds_after_breakpoint=['break update_refs', 'continue', 'py-bt'],
732 )
733 self.assertIn('Garbage-collecting', gdb_output)
734
735 # Verify with "py-bt-full":
736 gdb_output = self.get_stack_trace(cmd,
737 cmds_after_breakpoint=['break update_refs', 'continue', 'py-bt-full'],
738 )
739 self.assertIn('Garbage-collecting', gdb_output)
740
741 @unittest.skipIf(python_is_optimized(),
742 "Python was compiled with optimizations")
743 # Some older versions of gdb will fail with
744 # "Cannot find new threads: generic error"
745 # unless we add LD_PRELOAD=PATH-TO-libpthread.so.1 as a workaround
746 @unittest.skipUnless(_thread,
747 "Python was compiled without thread support")
748 def test_pycfunction(self):
749 'Verify that "py-bt" displays invocations of PyCFunction instances'
750 cmd = ('from time import sleep\n'
751 'def foo():\n'
752 ' sleep(1)\n'
753 'def bar():\n'
754 ' foo()\n'
755 'bar()\n')
756 # Verify with "py-bt":
757 gdb_output = self.get_stack_trace(cmd,
758 breakpoint='time_sleep',
759 cmds_after_breakpoint=['bt', 'py-bt'],
760 )
761 self.assertIn('<built-in method sleep', gdb_output)
762
763 # Verify with "py-bt-full":
764 gdb_output = self.get_stack_trace(cmd,
765 breakpoint='time_sleep',
766 cmds_after_breakpoint=['py-bt-full'],
767 )
768 self.assertIn('#0 <built-in method sleep', gdb_output)
769
770
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000771class PyPrintTests(DebuggerTests):
Victor Stinnerd2084162011-12-19 13:42:24 +0100772 @unittest.skipIf(python_is_optimized(),
773 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000774 def test_basic_command(self):
775 'Verify that the "py-print" command works'
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000776 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000777 cmds_after_breakpoint=['py-print args'])
778 self.assertMultilineMatches(bt,
779 r".*\nlocal 'args' = \(1, 2, 3\)\n.*")
780
Vinay Sajip2549f872012-01-04 12:07:30 +0000781 @unittest.skipIf(python_is_optimized(),
782 "Python was compiled with optimizations")
Victor Stinner50eb60e2010-04-20 22:32:07 +0000783 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000784 def test_print_after_up(self):
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000785 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000786 cmds_after_breakpoint=['py-up', 'py-print c', 'py-print b', 'py-print a'])
787 self.assertMultilineMatches(bt,
788 r".*\nlocal 'c' = 3\nlocal 'b' = 2\nlocal 'a' = 1\n.*")
789
Victor Stinnerd2084162011-12-19 13:42:24 +0100790 @unittest.skipIf(python_is_optimized(),
791 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000792 def test_printing_global(self):
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000793 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000794 cmds_after_breakpoint=['py-print __name__'])
795 self.assertMultilineMatches(bt,
796 r".*\nglobal '__name__' = '__main__'\n.*")
797
Victor Stinnerd2084162011-12-19 13:42:24 +0100798 @unittest.skipIf(python_is_optimized(),
799 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000800 def test_printing_builtin(self):
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000801 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000802 cmds_after_breakpoint=['py-print len'])
803 self.assertMultilineMatches(bt,
Antoine Pitrou4d098732011-11-26 01:42:03 +0100804 r".*\nbuiltin 'len' = <built-in method len of module object at remote 0x-?[0-9a-f]+>\n.*")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000805
806class PyLocalsTests(DebuggerTests):
Victor Stinnerd2084162011-12-19 13:42:24 +0100807 @unittest.skipIf(python_is_optimized(),
808 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000809 def test_basic_command(self):
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000810 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000811 cmds_after_breakpoint=['py-locals'])
812 self.assertMultilineMatches(bt,
813 r".*\nargs = \(1, 2, 3\)\n.*")
814
Victor Stinner50eb60e2010-04-20 22:32:07 +0000815 @unittest.skipUnless(HAS_PYUP_PYDOWN, "test requires py-up/py-down commands")
Vinay Sajip2549f872012-01-04 12:07:30 +0000816 @unittest.skipIf(python_is_optimized(),
817 "Python was compiled with optimizations")
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000818 def test_locals_after_up(self):
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000819 bt = self.get_stack_trace(script=self.get_sample_script(),
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000820 cmds_after_breakpoint=['py-up', 'py-locals'])
821 self.assertMultilineMatches(bt,
822 r".*\na = 1\nb = 2\nc = 3\n.*")
823
824def test_main():
Martin v. Löwis5226fd62010-04-21 06:05:58 +0000825 run_unittest(PrettyPrintTests,
826 PyListTests,
827 StackNavigationTests,
828 PyBtTests,
829 PyPrintTests,
830 PyLocalsTests
Benjamin Peterson6a6666a2010-04-11 21:49:28 +0000831 )
832
833if __name__ == "__main__":
834 test_main()