From 62f0ffa86136f06c84cee139f29aa3b9ad0385a3 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Fri, 18 Sep 2015 10:09:29 +0300 Subject: Issue #25108: Backported tests for traceback functions print_stack(), format_stack(), and extract_stack() called without arguments. --- Lib/test/test_traceback.py | 46 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py index 8b0322b..88e8b7b 100644 --- a/Lib/test/test_traceback.py +++ b/Lib/test/test_traceback.py @@ -4,7 +4,8 @@ from StringIO import StringIO import sys import unittest from imp import reload -from test.test_support import run_unittest, is_jython, Error, cpython_only +from test.test_support import (run_unittest, is_jython, Error, cpython_only, + captured_output) import traceback @@ -206,9 +207,50 @@ class TracebackFormatTests(unittest.TestCase): self.assertTrue(location.startswith(' File')) self.assertTrue(source_line.startswith(' raise')) + def test_print_stack(self): + def prn(): + traceback.print_stack() + with captured_output("stderr") as stderr: + prn() + lineno = prn.__code__.co_firstlineno + self.assertEqual(stderr.getvalue().splitlines()[-4:], [ + ' File "%s", line %d, in test_print_stack' % (__file__, lineno+3), + ' prn()', + ' File "%s", line %d, in prn' % (__file__, lineno+1), + ' traceback.print_stack()', + ]) + + def test_format_stack(self): + def fmt(): + return traceback.format_stack() + result = fmt() + lineno = fmt.__code__.co_firstlineno + self.assertEqual(result[-2:], [ + ' File "%s", line %d, in test_format_stack\n' + ' result = fmt()\n' % (__file__, lineno+2), + ' File "%s", line %d, in fmt\n' + ' return traceback.format_stack()\n' % (__file__, lineno+1), + ]) + + +class MiscTracebackCases(unittest.TestCase): + # + # Check non-printing functions in traceback module + # + + def test_extract_stack(self): + def extract(): + return traceback.extract_stack() + result = extract() + lineno = extract.__code__.co_firstlineno + self.assertEqual(result[-2:], [ + (__file__, lineno+2, 'test_extract_stack', 'result = extract()'), + (__file__, lineno+1, 'extract', 'return traceback.extract_stack()'), + ]) + def test_main(): - run_unittest(TracebackCases, TracebackFormatTests) + run_unittest(TracebackCases, TracebackFormatTests, MiscTracebackCases) if __name__ == "__main__": -- cgit v0.12