Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d9fec15

Browse files
committed
refactor traceback.py to reduce code duplication (closes #17646)
Patch by Martin Morrison.
1 parent 0bb83f8 commit d9fec15

2 files changed

Lines changed: 132 additions & 143 deletions

File tree

Lib/test/test_traceback.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,26 @@ def test_traceback_format(self):
160160
file_ = StringIO()
161161
traceback_print(tb, file_)
162162
python_fmt = file_.getvalue()
163+
# Call all _tb and _exc functions
164+
with captured_output("stderr") as tbstderr:
165+
traceback.print_tb(tb)
166+
tbfile = StringIO()
167+
traceback.print_tb(tb, file=tbfile)
168+
with captured_output("stderr") as excstderr:
169+
traceback.print_exc()
170+
excfmt = traceback.format_exc()
171+
excfile = StringIO()
172+
traceback.print_exc(file=excfile)
163173
else:
164174
raise Error("unable to create test traceback string")
165175

166176
# Make sure that Python and the traceback module format the same thing
167177
self.assertEqual(traceback_fmt, python_fmt)
178+
# Now verify the _tb func output
179+
self.assertEqual(tbstderr.getvalue(), tbfile.getvalue())
180+
# Now verify the _exc func output
181+
self.assertEqual(excstderr.getvalue(), excfile.getvalue())
182+
self.assertEqual(excfmt, excfile.getvalue())
168183

169184
# Make sure that the traceback is properly indented.
170185
tb_lines = python_fmt.splitlines()
@@ -174,6 +189,19 @@ def test_traceback_format(self):
174189
self.assertTrue(location.startswith(' File'))
175190
self.assertTrue(source_line.startswith(' raise'))
176191

192+
def test_stack_format(self):
193+
# Verify _stack functions. Note we have to use _getframe(1) to
194+
# compare them without this frame appearing in the output
195+
with captured_output("stderr") as ststderr:
196+
traceback.print_stack(sys._getframe(1))
197+
stfile = StringIO()
198+
traceback.print_stack(sys._getframe(1), file=stfile)
199+
self.assertEqual(ststderr.getvalue(), stfile.getvalue())
200+
201+
stfmt = traceback.format_stack(sys._getframe(1))
202+
203+
self.assertEqual(ststderr.getvalue(), "".join(stfmt))
204+
177205

178206
cause_message = (
179207
"\nThe above exception was the direct cause "

0 commit comments

Comments
 (0)