blob: fe8682a3e64accacf273520c12850ad04b8d2ab4 [file] [log] [blame]
Martin v. Löwisbf0dfb32010-04-01 07:40:51 +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
10import unittest
11
12from test.test_support import run_unittest
13
14try:
15 gdb_version, _ = subprocess.Popen(["gdb", "--version"],
16 stdout=subprocess.PIPE).communicate()
17except OSError:
18 # This is what "no gdb" looks like. There may, however, be other
19 # errors that manifest this way too.
20 raise unittest.SkipTest("Couldn't find gdb on the path")
21gdb_version_number = re.search(r"^GNU gdb [^\d]*(\d+)\.", gdb_version)
22if int(gdb_version_number.group(1)) < 7:
23 raise unittest.SkipTest("gdb versions before 7.0 didn't support python embedding"
24 " Saw:\n" + gdb_version)
25
26# Verify that "gdb" was built with the embedded python support enabled:
27cmd = "--eval-command=python import sys; print sys.version_info"
28p = subprocess.Popen(["gdb", "--batch", cmd],
29 stdout=subprocess.PIPE)
30gdbpy_version, _ = p.communicate()
31if gdbpy_version == '':
32 raise unittest.SkipTest("gdb not built with embedded python support")
33
34
35class DebuggerTests(unittest.TestCase):
36
37 """Test that the debugger can debug Python."""
38
39 def run_gdb(self, *args):
40 """Runs gdb with the command line given by *args.
41
42 Returns its stdout, stderr
43 """
44 out, err = subprocess.Popen(
45 args, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
46 ).communicate()
47 return out, err
48
49 def get_stack_trace(self, source=None, script=None,
50 breakpoint='PyObject_Print',
51 cmds_after_breakpoint=None,
52 import_site=False):
53 '''
54 Run 'python -c SOURCE' under gdb with a breakpoint.
55
56 Support injecting commands after the breakpoint is reached
57
58 Returns the stdout from gdb
59
60 cmds_after_breakpoint: if provided, a list of strings: gdb commands
61 '''
62 # We use "set breakpoint pending yes" to avoid blocking with a:
63 # Function "foo" not defined.
64 # Make breakpoint pending on future shared library load? (y or [n])
65 # error, which typically happens python is dynamically linked (the
66 # breakpoints of interest are to be found in the shared library)
67 # When this happens, we still get:
68 # Function "PyObject_Print" not defined.
69 # emitted to stderr each time, alas.
70
71 # Initially I had "--eval-command=continue" here, but removed it to
72 # avoid repeated print breakpoints when traversing hierarchical data
73 # structures
74
75 # Generate a list of commands in gdb's language:
76 commands = ['set breakpoint pending yes',
77 'break %s' % breakpoint,
78 'run']
79 if cmds_after_breakpoint:
80 commands += cmds_after_breakpoint
81 else:
82 commands += ['backtrace']
83
84 # print commands
85
86 # Use "commands" to generate the arguments with which to invoke "gdb":
87 args = ["gdb", "--batch"]
88 args += ['--eval-command=%s' % cmd for cmd in commands]
89 args += ["--args",
90 sys.executable]
91
92 if not import_site:
93 # -S suppresses the default 'import site'
94 args += ["-S"]
95
96 if source:
97 args += ["-c", source]
98 elif script:
99 args += [script]
100
101 # print args
102 # print ' '.join(args)
103
104 # Use "args" to invoke gdb, capturing stdout, stderr:
105 out, err = self.run_gdb(*args)
106
107 # Ignore some noise on stderr due to the pending breakpoint:
108 err = err.replace('Function "%s" not defined.\n' % breakpoint, '')
109
110 # Ensure no unexpected error messages:
111 self.assertEquals(err, '')
112
113 return out
114
115 def get_gdb_repr(self, source,
116 cmds_after_breakpoint=None,
117 import_site=False):
118 # Given an input python source representation of data,
119 # run "python -c'print DATA'" under gdb with a breakpoint on
120 # PyObject_Print and scrape out gdb's representation of the "op"
121 # parameter, and verify that the gdb displays the same string
122 #
123 # For a nested structure, the first time we hit the breakpoint will
124 # give us the top-level structure
125 gdb_output = self.get_stack_trace(source, breakpoint='PyObject_Print',
126 cmds_after_breakpoint=cmds_after_breakpoint,
127 import_site=import_site)
128 m = re.match('.*#0 PyObject_Print \(op\=(.*?), fp=.*\).*',
129 gdb_output, re.DOTALL)
130 #print m.groups()
131 return m.group(1), gdb_output
132
133 def assertEndsWith(self, actual, exp_end):
134 '''Ensure that the given "actual" string ends with "exp_end"'''
135 self.assert_(actual.endswith(exp_end),
136 msg='%r did not end with %r' % (actual, exp_end))
137
138 def assertMultilineMatches(self, actual, pattern):
139 m = re.match(pattern, actual, re.DOTALL)
140 self.assert_(m,
141 msg='%r did not match %r' % (actual, pattern))
142
143class PrettyPrintTests(DebuggerTests):
144 def test_getting_backtrace(self):
145 gdb_output = self.get_stack_trace('print 42')
146 self.assertTrue('PyObject_Print' in gdb_output)
147
148 def assertGdbRepr(self, val, cmds_after_breakpoint=None):
149 # Ensure that gdb's rendering of the value in a debugged process
150 # matches repr(value) in this process:
151 gdb_repr, gdb_output = self.get_gdb_repr('print ' + repr(val),
152 cmds_after_breakpoint)
153 self.assertEquals(gdb_repr, repr(val), gdb_output)
154
155 def test_int(self):
156 'Verify the pretty-printing of various "int" values'
157 self.assertGdbRepr(42)
158 self.assertGdbRepr(0)
159 self.assertGdbRepr(-7)
160 self.assertGdbRepr(sys.maxint)
161 self.assertGdbRepr(-sys.maxint)
162
163 def test_long(self):
164 'Verify the pretty-printing of various "long" values'
165 self.assertGdbRepr(0L)
166 self.assertGdbRepr(1000000000000L)
167 self.assertGdbRepr(-1L)
168 self.assertGdbRepr(-1000000000000000L)
169
170 def test_singletons(self):
171 'Verify the pretty-printing of True, False and None'
172 self.assertGdbRepr(True)
173 self.assertGdbRepr(False)
174 self.assertGdbRepr(None)
175
176 def test_dicts(self):
177 'Verify the pretty-printing of dictionaries'
178 self.assertGdbRepr({})
179 self.assertGdbRepr({'foo': 'bar'})
180 self.assertGdbRepr({'foo': 'bar', 'douglas':42})
181
182 def test_lists(self):
183 'Verify the pretty-printing of lists'
184 self.assertGdbRepr([])
185 self.assertGdbRepr(range(5))
186
187 def test_strings(self):
188 'Verify the pretty-printing of strings'
189 self.assertGdbRepr('')
190 self.assertGdbRepr('And now for something hopefully the same')
191 self.assertGdbRepr('string with embedded NUL here \0 and then some more text')
192 self.assertGdbRepr('this is byte 255:\xff and byte 128:\x80')
193
194 def test_tuples(self):
195 'Verify the pretty-printing of tuples'
196 self.assertGdbRepr(tuple())
197 self.assertGdbRepr((1,))
198 self.assertGdbRepr(('foo', 'bar', 'baz'))
199
200 def test_unicode(self):
201 'Verify the pretty-printing of unicode values'
202 # Test the empty unicode string:
203 self.assertGdbRepr(u'')
204
205 self.assertGdbRepr(u'hello world')
206
207 # Test printing a single character:
208 # U+2620 SKULL AND CROSSBONES
209 self.assertGdbRepr(u'\u2620')
210
211 # Test printing a Japanese unicode string
212 # (I believe this reads "mojibake", using 3 characters from the CJK
213 # Unified Ideographs area, followed by U+3051 HIRAGANA LETTER KE)
214 self.assertGdbRepr(u'\u6587\u5b57\u5316\u3051')
215
216 # Test a character outside the BMP:
217 # U+1D121 MUSICAL SYMBOL C CLEF
218 # This is:
219 # UTF-8: 0xF0 0x9D 0x84 0xA1
220 # UTF-16: 0xD834 0xDD21
221 try:
222 # This will only work on wide-unicode builds:
223 self.assertGdbRepr(unichr(0x1D121))
224 except ValueError, e:
225 if e.message != 'unichr() arg not in range(0x10000) (narrow Python build)':
226 raise e
227
228 def test_sets(self):
229 'Verify the pretty-printing of sets'
230 self.assertGdbRepr(set())
231 self.assertGdbRepr(set(['a', 'b']))
232 self.assertGdbRepr(set([4, 5, 6]))
233
234 # Ensure that we handled sets containing the "dummy" key value,
235 # which happens on deletion:
236 gdb_repr, gdb_output = self.get_gdb_repr('''s = set(['a','b'])
237s.pop()
238print s''')
239 self.assertEquals(gdb_repr, "set(['b'])")
240
241 def test_frozensets(self):
242 'Verify the pretty-printing of frozensets'
243 self.assertGdbRepr(frozenset())
244 self.assertGdbRepr(frozenset(['a', 'b']))
245 self.assertGdbRepr(frozenset([4, 5, 6]))
246
247 def test_exceptions(self):
248 # Test a RuntimeError
249 gdb_repr, gdb_output = self.get_gdb_repr('''
250try:
251 raise RuntimeError("I am an error")
252except RuntimeError, e:
253 print e
254''')
255 self.assertEquals(gdb_repr,
256 "exceptions.RuntimeError('I am an error',)")
257
258
259 # Test division by zero:
260 gdb_repr, gdb_output = self.get_gdb_repr('''
261try:
262 a = 1 / 0
263except ZeroDivisionError, e:
264 print e
265''')
266 self.assertEquals(gdb_repr,
267 "exceptions.ZeroDivisionError('integer division or modulo by zero',)")
268
269 def test_classic_class(self):
270 'Verify the pretty-printing of classic class instances'
271 gdb_repr, gdb_output = self.get_gdb_repr('''
272class Foo:
273 pass
274foo = Foo()
275foo.an_int = 42
276print foo''')
277 m = re.match(r'<Foo\(an_int=42\) at remote 0x[0-9a-f]+>', gdb_repr)
278 self.assertTrue(m,
279 msg='Unexpected classic-class rendering %r' % gdb_repr)
280
281 def test_modern_class(self):
282 'Verify the pretty-printing of new-style class instances'
283 gdb_repr, gdb_output = self.get_gdb_repr('''
284class Foo(object):
285 pass
286foo = Foo()
287foo.an_int = 42
288print foo''')
289 m = re.match(r'<Foo\(an_int=42\) at remote 0x[0-9a-f]+>', gdb_repr)
290 self.assertTrue(m,
291 msg='Unexpected new-style class rendering %r' % gdb_repr)
292
293 def test_subclassing_list(self):
294 'Verify the pretty-printing of an instance of a list subclass'
295 gdb_repr, gdb_output = self.get_gdb_repr('''
296class Foo(list):
297 pass
298foo = Foo()
299foo += [1, 2, 3]
300foo.an_int = 42
301print foo''')
302 m = re.match(r'<Foo\(an_int=42\) at remote 0x[0-9a-f]+>', gdb_repr)
303 self.assertTrue(m,
304 msg='Unexpected new-style class rendering %r' % gdb_repr)
305
306 def test_subclassing_tuple(self):
307 'Verify the pretty-printing of an instance of a tuple subclass'
308 # This should exercise the negative tp_dictoffset code in the
309 # new-style class support
310 gdb_repr, gdb_output = self.get_gdb_repr('''
311class Foo(tuple):
312 pass
313foo = Foo((1, 2, 3))
314foo.an_int = 42
315print foo''')
316 m = re.match(r'<Foo\(an_int=42\) at remote 0x[0-9a-f]+>', gdb_repr)
317 self.assertTrue(m,
318 msg='Unexpected new-style class rendering %r' % gdb_repr)
319
320 def assertSane(self, source, corruption, exp_type='unknown'):
321 '''Run Python under gdb, corrupting variables in the inferior process
322 immediately before taking a backtrace.
323
324 Verify that the variable's representation is the expected failsafe
325 representation'''
326 if corruption:
327 cmds_after_breakpoint=[corruption, 'backtrace']
328 else:
329 cmds_after_breakpoint=['backtrace']
330
331 gdb_repr, gdb_output = \
332 self.get_gdb_repr(source,
333 cmds_after_breakpoint=cmds_after_breakpoint)
334 self.assertTrue(re.match('<%s at remote 0x[0-9a-f]+>' % exp_type,
335 gdb_repr),
336 'Unexpected gdb representation: %r\n%s' % \
337 (gdb_repr, gdb_output))
338
339 def test_NULL_ptr(self):
340 'Ensure that a NULL PyObject* is handled gracefully'
341 gdb_repr, gdb_output = (
342 self.get_gdb_repr('print 42',
343 cmds_after_breakpoint=['set variable op=0',
344 'backtrace'])
345 )
346
347 self.assertEquals(gdb_repr, '0x0')
348
349 def test_NULL_ob_type(self):
350 'Ensure that a PyObject* with NULL ob_type is handled gracefully'
351 self.assertSane('print 42',
352 'set op->ob_type=0')
353
354 def test_corrupt_ob_type(self):
355 'Ensure that a PyObject* with a corrupt ob_type is handled gracefully'
356 self.assertSane('print 42',
357 'set op->ob_type=0xDEADBEEF')
358
359 def test_corrupt_tp_flags(self):
360 'Ensure that a PyObject* with a type with corrupt tp_flags is handled'
361 self.assertSane('print 42',
362 'set op->ob_type->tp_flags=0x0',
363 exp_type='int')
364
365 def test_corrupt_tp_name(self):
366 'Ensure that a PyObject* with a type with corrupt tp_name is handled'
367 self.assertSane('print 42',
368 'set op->ob_type->tp_name=0xDEADBEEF')
369
370 def test_NULL_instance_dict(self):
371 'Ensure that a PyInstanceObject with with a NULL in_dict is handled'
372 self.assertSane('''
373class Foo:
374 pass
375foo = Foo()
376foo.an_int = 42
377print foo''',
378 'set ((PyInstanceObject*)op)->in_dict = 0',
379 exp_type='Foo')
380
381 def test_builtins_help(self):
382 'Ensure that the new-style class _Helper in site.py can be handled'
383 # (this was the issue causing tracebacks in
384 # http://bugs.python.org/issue8032#msg100537 )
385
386 gdb_repr, gdb_output = self.get_gdb_repr('print __builtins__.help', import_site=True)
387 m = re.match(r'<_Helper at remote 0x[0-9a-f]+>', gdb_repr)
388 self.assertTrue(m,
389 msg='Unexpected rendering %r' % gdb_repr)
390
391 def test_selfreferential_list(self):
392 '''Ensure that a reference loop involving a list doesn't lead proxyval
393 into an infinite loop:'''
394 gdb_repr, gdb_output = \
395 self.get_gdb_repr("a = [3, 4, 5] ; a.append(a) ; print a")
396
397 self.assertEquals(gdb_repr, '[3, 4, 5, [...]]')
398
399 gdb_repr, gdb_output = \
400 self.get_gdb_repr("a = [3, 4, 5] ; b = [a] ; a.append(b) ; print a")
401
402 self.assertEquals(gdb_repr, '[3, 4, 5, [[...]]]')
403
404 def test_selfreferential_dict(self):
405 '''Ensure that a reference loop involving a dict doesn't lead proxyval
406 into an infinite loop:'''
407 gdb_repr, gdb_output = \
408 self.get_gdb_repr("a = {} ; b = {'bar':a} ; a['foo'] = b ; print a")
409
410 self.assertEquals(gdb_repr, "{'foo': {'bar': {...}}}")
411
412 def test_selfreferential_old_style_instance(self):
413 gdb_repr, gdb_output = \
414 self.get_gdb_repr('''
415class Foo:
416 pass
417foo = Foo()
418foo.an_attr = foo
419print foo''')
420 self.assertTrue(re.match('<Foo\(an_attr=<\.\.\.>\) at remote 0x[0-9a-f]+>',
421 gdb_repr),
422 'Unexpected gdb representation: %r\n%s' % \
423 (gdb_repr, gdb_output))
424
425 def test_selfreferential_new_style_instance(self):
426 gdb_repr, gdb_output = \
427 self.get_gdb_repr('''
428class Foo(object):
429 pass
430foo = Foo()
431foo.an_attr = foo
432print foo''')
433 self.assertTrue(re.match('<Foo\(an_attr=<\.\.\.>\) at remote 0x[0-9a-f]+>',
434 gdb_repr),
435 'Unexpected gdb representation: %r\n%s' % \
436 (gdb_repr, gdb_output))
437
438 gdb_repr, gdb_output = \
439 self.get_gdb_repr('''
440class Foo(object):
441 pass
442a = Foo()
443b = Foo()
444a.an_attr = b
445b.an_attr = a
446print a''')
447 self.assertTrue(re.match('<Foo\(an_attr=<Foo\(an_attr=<\.\.\.>\) at remote 0x[0-9a-f]+>\) at remote 0x[0-9a-f]+>',
448 gdb_repr),
449 'Unexpected gdb representation: %r\n%s' % \
450 (gdb_repr, gdb_output))
451
452 def test_truncation(self):
453 'Verify that very long output is truncated'
454 gdb_repr, gdb_output = self.get_gdb_repr('print range(1000)')
455 self.assertEquals(gdb_repr,
456 "\n [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, "
457 "14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, "
458 "27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, "
459 "40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, "
460 "53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, "
461 "66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, "
462 "79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, "
463 "92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, "
464 "104, 105, 106, 107, 108, 109, 110, 111, 112, 113, "
465 "114, 115, 116, 117, 118, 119, 120, 121, 122, 123, "
466 "124, 125, 126, 127, 128, 129, 130, 131, 132, 133, "
467 "134, 135, 136, 137, 138, 139, 140, 141, 142, 143, "
468 "144, 145, 146, 147, 148, 149, 150, 151, 152, 153, "
469 "154, 155, 156, 157, 158, 159, 160, 161, 162, 163, "
470 "164, 165, 166, 167, 168, 169, 170, 171, 172, 173, "
471 "174, 175, 176, 177, 178, 179, 180, 181, 182, 183, "
472 "184, 185, 186, 187, 188, 189, 190, 191, 192, 193, "
473 "194, 195, 196, 197, 198, 199, 200, 201, 202, 203, "
474 "204, 205, 206, 207, 208, 209, 210, 211, 212, 213, "
475 "214, 215, 216, 217, 218, 219, 220, 221, 222, 223, "
476 "224, 225, 226...(truncated)")
477 self.assertEquals(len(gdb_repr),
478 len('\n ') + 1024 + len('...(truncated)'))
479
480 def test_builtin_function(self):
481 gdb_repr, gdb_output = self.get_gdb_repr('print len')
482 self.assertEquals(gdb_repr, '<built-in function len>')
483
484 def test_builtin_method(self):
485 gdb_repr, gdb_output = self.get_gdb_repr('import sys; print sys.stdout.readlines')
486 self.assertTrue(re.match('<built-in method readlines of file object at remote 0x[0-9a-f]+>',
487 gdb_repr),
488 'Unexpected gdb representation: %r\n%s' % \
489 (gdb_repr, gdb_output))
490
491 def test_frames(self):
492 gdb_output = self.get_stack_trace('''
493def foo(a, b, c):
494 pass
495
496foo(3, 4, 5)
497print foo.__code__''',
498 breakpoint='PyObject_Print',
499 cmds_after_breakpoint=['print (PyFrameObject*)(((PyCodeObject*)op)->co_zombieframe)']
500 )
501 for line in gdb_output.splitlines():
502 if line.startswith('$1'):
503 self.assertTrue(re.match(r'\$1 = Frame 0x[0-9a-f]+, for file <string>, line 3, in foo \(\)',
504 line),
505 'Unexpected gdb representation: %r\n%s' % (line, gdb_output))
506 return
507 self.fail('Did not find expected line beginning with $1')
508
509
510
511class PyListTests(DebuggerTests):
512 def assertListing(self, expected, actual):
513 self.assertEndsWith(actual, expected)
514
515 def test_basic_command(self):
516 'Verify that the "py-list" command works'
517 bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py',
518 cmds_after_breakpoint=['py-list'])
519
520 self.assertListing('''
521 5
522 6 def bar(a, b, c):
523 7 baz(a, b, c)
524 8
525 9 def baz(*args):
526 >10 print(42)
527 11
528 12 foo(1, 2, 3)
529''',
530 bt)
531
532 def test_one_abs_arg(self):
533 'Verify the "py-list" command with one absolute argument'
534 bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py',
535 cmds_after_breakpoint=['py-list 9'])
536
537 self.assertListing('''
538 9 def baz(*args):
539 >10 print(42)
540 11
541 12 foo(1, 2, 3)
542''',
543 bt)
544
545 def test_two_abs_args(self):
546 'Verify the "py-list" command with two absolute arguments'
547 bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py',
548 cmds_after_breakpoint=['py-list 1,3'])
549
550 self.assertListing('''
551 1 # Sample script for use by test_gdb.py
552 2
553 3 def foo(a, b, c):
554''',
555 bt)
556
557class StackNavigationTests(DebuggerTests):
558 def test_pyup_command(self):
559 'Verify that the "py-up" command works'
560 bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py',
561 cmds_after_breakpoint=['py-up'])
562 self.assertMultilineMatches(bt,
563 r'''^.*
564#[0-9]+ Frame 0x[0-9a-f]+, for file Lib/test/test_gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\)
565 baz\(a, b, c\)
566$''')
567
568 def test_down_at_bottom(self):
569 'Verify handling of "py-down" at the bottom of the stack'
570 bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py',
571 cmds_after_breakpoint=['py-down'])
572 self.assertEndsWith(bt,
573 'Unable to find a newer python frame\n')
574
575 def test_up_at_top(self):
576 'Verify handling of "py-up" at the top of the stack'
577 bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py',
578 cmds_after_breakpoint=['py-up'] * 4)
579 self.assertEndsWith(bt,
580 'Unable to find an older python frame\n')
581
582 def test_up_then_down(self):
583 'Verify "py-up" followed by "py-down"'
584 bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py',
585 cmds_after_breakpoint=['py-up', 'py-down'])
586 self.assertMultilineMatches(bt,
587 r'''^.*
588#[0-9]+ Frame 0x[0-9a-f]+, for file Lib/test/test_gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\)
589 baz\(a, b, c\)
590#[0-9]+ Frame 0x[0-9a-f]+, for file Lib/test/test_gdb_sample.py, line 10, in baz \(args=\(1, 2, 3\)\)
591 print\(42\)
592$''')
593
594class PyBtTests(DebuggerTests):
595 def test_basic_command(self):
596 'Verify that the "py-bt" command works'
597 bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py',
598 cmds_after_breakpoint=['py-bt'])
599 self.assertMultilineMatches(bt,
600 r'''^.*
601#[0-9]+ Frame 0x[0-9a-f]+, for file Lib/test/test_gdb_sample.py, line 7, in bar \(a=1, b=2, c=3\)
602 baz\(a, b, c\)
603#[0-9]+ Frame 0x[0-9a-f]+, for file Lib/test/test_gdb_sample.py, line 4, in foo \(a=1, b=2, c=3\)
604 bar\(a, b, c\)
605#[0-9]+ Frame 0x[0-9a-f]+, for file Lib/test/test_gdb_sample.py, line 12, in <module> \(\)
606foo\(1, 2, 3\)
607''')
608
609class PyPrintTests(DebuggerTests):
610 def test_basic_command(self):
611 'Verify that the "py-print" command works'
612 bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py',
613 cmds_after_breakpoint=['py-print args'])
614 self.assertMultilineMatches(bt,
615 r".*\nlocal 'args' = \(1, 2, 3\)\n.*")
616
617 def test_print_after_up(self):
618 bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py',
619 cmds_after_breakpoint=['py-up', 'py-print c', 'py-print b', 'py-print a'])
620 self.assertMultilineMatches(bt,
621 r".*\nlocal 'c' = 3\nlocal 'b' = 2\nlocal 'a' = 1\n.*")
622
623 def test_printing_global(self):
624 bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py',
625 cmds_after_breakpoint=['py-print __name__'])
626 self.assertMultilineMatches(bt,
627 r".*\nglobal '__name__' = '__main__'\n.*")
628
629 def test_printing_builtin(self):
630 bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py',
631 cmds_after_breakpoint=['py-print len'])
632 self.assertMultilineMatches(bt,
633 r".*\nbuiltin 'len' = <built-in function len>\n.*")
634
635class PyLocalsTests(DebuggerTests):
636 def test_basic_command(self):
637 bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py',
638 cmds_after_breakpoint=['py-locals'])
639 self.assertMultilineMatches(bt,
640 r".*\nargs = \(1, 2, 3\)\n.*")
641
642 def test_locals_after_up(self):
643 bt = self.get_stack_trace(script='Lib/test/test_gdb_sample.py',
644 cmds_after_breakpoint=['py-up', 'py-locals'])
645 self.assertMultilineMatches(bt,
646 r".*\na = 1\nb = 2\nc = 3\n.*")
647
648def test_main():
649 run_unittest(PrettyPrintTests,
650 #PyListTests,
651 #StackNavigationTests,
652 #PyBtTests,
653 #PyPrintTests,
654 #PyLocalsTests
655 )
656
657if __name__ == "__main__":
658 test_main()