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.
This commit is contained in:
Berker Peksag 2015-07-24 17:36:21 +03:00
parent 37c5f154f0
commit 2218e9cf14
3 changed files with 10 additions and 1 deletions

View File

@ -1 +1,2 @@
Robert Collins <rbtcollins@hp.com>
Berker Peksag <berker.peksag@gmail.com>

View File

@ -465,6 +465,7 @@ class TracebackException:
_seen=_seen)
else:
context = None
self.exc_traceback = exc_traceback
self.__cause__ = cause
self.__context__ = context
self.__suppress_context__ = \
@ -590,6 +591,7 @@ class TracebackException:
for line in self.__context__.format(chain=chain):
yield line
yield _context_message
if self.exc_traceback is not None:
yield u('Traceback (most recent call last):\n')
for line in self.stack.format():
yield line

View File

@ -793,6 +793,12 @@ class TestTracebackException(unittest.TestCase):
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()), [u("Exception: haven\n")])
def test_syntax_no_extras(self):
linecache.updatecache('/foo.py', fake_module)
e = SyntaxError("uh oh")