@@ -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
178206cause_message = (
179207 "\n The above exception was the direct cause "
0 commit comments