changeset: 97043:2825c87d3f72 parent: 97040:f6e399ae670f parent: 97042:b45077269aaa user: Berker Peksag date: Fri Jul 24 17:37:27 2015 +0300 files: Misc/NEWS description: Issue #24695: Fix a regression in traceback.print_exception() If exc_traceback is None we shouldn't print a traceback header like described in the documentation. diff -r f6e399ae670f -r 2825c87d3f72 Lib/test/test_traceback.py --- a/Lib/test/test_traceback.py Fri Jul 24 09:07:12 2015 +0300 +++ b/Lib/test/test_traceback.py Fri Jul 24 17:37:27 2015 +0300 @@ -209,6 +209,13 @@ b'ZeroDivisionError: division by zero'] self.assertEqual(stderr.splitlines(), expected) + def test_print_exception(self): + output = StringIO() + traceback.print_exception( + Exception, Exception("projector"), None, file=output + ) + self.assertEqual(output.getvalue(), "Exception: projector\n") + class TracebackFormatTests(unittest.TestCase): @@ -848,6 +855,12 @@ exc = traceback.TracebackException(Exception, e, tb) self.assertEqual(exc.stack[0].locals, None) + def test_traceback_header(self): + # do not print a traceback header if exc_traceback is None + # see issue #24695 + exc = traceback.TracebackException(Exception, Exception("haven"), None) + self.assertEqual(list(exc.format()), ["Exception: haven\n"]) + class MiscTest(unittest.TestCase): diff -r f6e399ae670f -r 2825c87d3f72 Lib/traceback.py --- a/Lib/traceback.py Fri Jul 24 09:07:12 2015 +0300 +++ b/Lib/traceback.py Fri Jul 24 17:37:27 2015 +0300 @@ -453,6 +453,7 @@ _seen=_seen) else: context = None + self.exc_traceback = exc_traceback self.__cause__ = cause self.__context__ = context self.__suppress_context__ = \ @@ -561,6 +562,7 @@ not self.__suppress_context__): yield from self.__context__.format(chain=chain) yield _context_message - yield 'Traceback (most recent call last):\n' + if self.exc_traceback is not None: + yield 'Traceback (most recent call last):\n' yield from self.stack.format() yield from self.format_exception_only() diff -r f6e399ae670f -r 2825c87d3f72 Misc/NEWS --- a/Misc/NEWS Fri Jul 24 09:07:12 2015 +0300 +++ b/Misc/NEWS Fri Jul 24 17:37:27 2015 +0300 @@ -57,6 +57,10 @@ Library ------- +- Issue #24695: Fix a regression in traceback.print_exception(). If + exc_traceback is None we shouldn't print a traceback header like described + in the documentation. + - Issue #24620: Random.setstate() now validates the value of state last element. - Issue #22485: Fixed an issue that caused `inspect.getsource` to return incorrect