Kaydet (Commit) c3f417dc authored tarafından Berker Peksag's avatar Berker Peksag

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.
üst c19bb327
...@@ -209,6 +209,13 @@ class SyntaxTracebackCases(unittest.TestCase): ...@@ -209,6 +209,13 @@ class SyntaxTracebackCases(unittest.TestCase):
b'ZeroDivisionError: division by zero'] b'ZeroDivisionError: division by zero']
self.assertEqual(stderr.splitlines(), expected) 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): class TracebackFormatTests(unittest.TestCase):
...@@ -848,6 +855,12 @@ class TestTracebackException(unittest.TestCase): ...@@ -848,6 +855,12 @@ class TestTracebackException(unittest.TestCase):
exc = traceback.TracebackException(Exception, e, tb) exc = traceback.TracebackException(Exception, e, tb)
self.assertEqual(exc.stack[0].locals, None) 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): class MiscTest(unittest.TestCase):
......
...@@ -453,6 +453,7 @@ class TracebackException: ...@@ -453,6 +453,7 @@ class TracebackException:
_seen=_seen) _seen=_seen)
else: else:
context = None context = None
self.exc_traceback = exc_traceback
self.__cause__ = cause self.__cause__ = cause
self.__context__ = context self.__context__ = context
self.__suppress_context__ = \ self.__suppress_context__ = \
...@@ -561,6 +562,7 @@ class TracebackException: ...@@ -561,6 +562,7 @@ class TracebackException:
not self.__suppress_context__): not self.__suppress_context__):
yield from self.__context__.format(chain=chain) yield from self.__context__.format(chain=chain)
yield _context_message 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.stack.format()
yield from self.format_exception_only() yield from self.format_exception_only()
...@@ -28,6 +28,10 @@ Core and Builtins ...@@ -28,6 +28,10 @@ Core and Builtins
Library 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 #24620: Random.setstate() now validates the value of state last element.
- Issue #22485: Fixed an issue that caused `inspect.getsource` to return incorrect - Issue #22485: Fixed an issue that caused `inspect.getsource` to return incorrect
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment