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

Skip to content

Commit e3bcbd2

Browse files
Issue #25108: Backported tests for traceback functions print_stack(),
format_stack(), and extract_stack() called without arguments.
1 parent 2a5f9da commit e3bcbd2

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

Lib/test/test_traceback.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,31 @@ def test_stack_format(self):
242242

243243
self.assertEqual(ststderr.getvalue(), "".join(stfmt))
244244

245+
def test_print_stack(self):
246+
def prn():
247+
traceback.print_stack()
248+
with captured_output("stderr") as stderr:
249+
prn()
250+
lineno = prn.__code__.co_firstlineno
251+
self.assertEqual(stderr.getvalue().splitlines()[-4:], [
252+
' File "%s", line %d, in test_print_stack' % (__file__, lineno+3),
253+
' prn()',
254+
' File "%s", line %d, in prn' % (__file__, lineno+1),
255+
' traceback.print_stack()',
256+
])
257+
258+
def test_format_stack(self):
259+
def fmt():
260+
return traceback.format_stack()
261+
result = fmt()
262+
lineno = fmt.__code__.co_firstlineno
263+
self.assertEqual(result[-2:], [
264+
' File "%s", line %d, in test_format_stack\n'
265+
' result = fmt()\n' % (__file__, lineno+2),
266+
' File "%s", line %d, in fmt\n'
267+
' return traceback.format_stack()\n' % (__file__, lineno+1),
268+
])
269+
245270

246271
cause_message = (
247272
"\nThe above exception was the direct cause "
@@ -443,6 +468,16 @@ def inner():
443468
# Local variable dict should now be empty.
444469
self.assertEqual(len(inner_frame.f_locals), 0)
445470

471+
def test_extract_stack(self):
472+
def extract():
473+
return traceback.extract_stack()
474+
result = extract()
475+
lineno = extract.__code__.co_firstlineno
476+
self.assertEqual(result[-2:], [
477+
(__file__, lineno+2, 'test_extract_stack', 'result = extract()'),
478+
(__file__, lineno+1, 'extract', 'return traceback.extract_stack()'),
479+
])
480+
446481

447482
def test_main():
448483
run_unittest(__name__)

0 commit comments

Comments
 (0)