summaryrefslogtreecommitdiff
path: root/Lib/test
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2013-04-29 16:09:39 -0400
committerBenjamin Peterson <benjamin@python.org>2013-04-29 16:09:39 -0400
commit984bb3552a570a3c59c0529c6d83d3248e56e461 (patch)
tree8defff7a1ca8b0b5eca8d31d96272789851397d6 /Lib/test
parent9f4d539f55e6d1b1e6ebf96554e0dc72c46670fd (diff)
downloadcpython-984bb3552a570a3c59c0529c6d83d3248e56e461.tar.gz
refactor traceback.py to reduce code duplication (closes #17646)
Patch by Martin Morrison.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_traceback.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py
index 5bce2af68a..24753a8321 100644
--- a/Lib/test/test_traceback.py
+++ b/Lib/test/test_traceback.py
@@ -160,11 +160,26 @@ class TracebackFormatTests(unittest.TestCase):
file_ = StringIO()
traceback_print(tb, file_)
python_fmt = file_.getvalue()
+ # Call all _tb and _exc functions
+ with captured_output("stderr") as tbstderr:
+ traceback.print_tb(tb)
+ tbfile = StringIO()
+ traceback.print_tb(tb, file=tbfile)
+ with captured_output("stderr") as excstderr:
+ traceback.print_exc()
+ excfmt = traceback.format_exc()
+ excfile = StringIO()
+ traceback.print_exc(file=excfile)
else:
raise Error("unable to create test traceback string")
# Make sure that Python and the traceback module format the same thing
self.assertEqual(traceback_fmt, python_fmt)
+ # Now verify the _tb func output
+ self.assertEqual(tbstderr.getvalue(), tbfile.getvalue())
+ # Now verify the _exc func output
+ self.assertEqual(excstderr.getvalue(), excfile.getvalue())
+ self.assertEqual(excfmt, excfile.getvalue())
# Make sure that the traceback is properly indented.
tb_lines = python_fmt.splitlines()
@@ -174,6 +189,19 @@ class TracebackFormatTests(unittest.TestCase):
self.assertTrue(location.startswith(' File'))
self.assertTrue(source_line.startswith(' raise'))
+ def test_stack_format(self):
+ # Verify _stack functions. Note we have to use _getframe(1) to
+ # compare them without this frame appearing in the output
+ with captured_output("stderr") as ststderr:
+ traceback.print_stack(sys._getframe(1))
+ stfile = StringIO()
+ traceback.print_stack(sys._getframe(1), file=stfile)
+ self.assertEqual(ststderr.getvalue(), stfile.getvalue())
+
+ stfmt = traceback.format_stack(sys._getframe(1))
+
+ self.assertEqual(ststderr.getvalue(), "".join(stfmt))
+
cause_message = (
"\nThe above exception was the direct cause "