Skip to content

Commit

Permalink
pythongh-112332: save snapshot of exc_type in TracebackException. Add…
Browse files Browse the repository at this point in the history
… option to not save the exc_type itself.
  • Loading branch information
iritkatriel committed Nov 23, 2023
1 parent 9e56eed commit aad7828
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ class TracebackException:

def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
lookup_lines=True, capture_locals=False, compact=False,
max_group_width=15, max_group_depth=10, _seen=None):
max_group_width=15, max_group_depth=10, save_exc_type=True, _seen=None):
# NB: we need to accept exc_traceback, exc_value, exc_traceback to
# permit backwards compat with the existing API, otherwise we
# need stub thunk objects just to glue it together.
Expand All @@ -754,12 +754,23 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
_walk_tb_with_full_positions(exc_traceback),
limit=limit, lookup_lines=lookup_lines,
capture_locals=capture_locals)
self.exc_type = exc_type

self.exc_type = exc_type if save_exc_type else None

# Capture now to permit freeing resources: only complication is in the
# unofficial API _format_final_exc_line
self._str = _safe_string(exc_value, 'exception')
self.__notes__ = getattr(exc_value, '__notes__', None)

self._is_syntax_error = False
self._have_exc_type = exc_type is not None
if exc_type is not None:
self._exc_type_qualname = exc_type.__qualname__
self._exc_type_module = exc_type.__module__
else:
self._exc_type_qualname = None
self._exc_type_module = None

if exc_type and issubclass(exc_type, SyntaxError):
# Handle SyntaxError's specially
self.filename = exc_value.filename
Expand All @@ -771,6 +782,7 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
self.offset = exc_value.offset
self.end_offset = exc_value.end_offset
self.msg = exc_value.msg
self._is_syntax_error = True
elif exc_type and issubclass(exc_type, ImportError) and \
getattr(exc_value, "name_from", None) is not None:
wrong_name = getattr(exc_value, "name_from", None)
Expand Down Expand Up @@ -901,18 +913,18 @@ def format_exception_only(self, *, show_group=False, _depth=0):
"""

indent = 3 * _depth * ' '
if self.exc_type is None:
if not self._have_exc_type:
yield indent + _format_final_exc_line(None, self._str)
return

stype = self.exc_type.__qualname__
smod = self.exc_type.__module__
stype = self._exc_type_qualname
smod = self._exc_type_module
if smod not in ("__main__", "builtins"):
if not isinstance(smod, str):
smod = "<unknown>"
stype = smod + '.' + stype

if not issubclass(self.exc_type, SyntaxError):
if not self._is_syntax_error:
if _depth > 0:
# Nested exceptions needs correct handling of multiline messages.
formatted = _format_final_exc_line(
Expand Down

0 comments on commit aad7828

Please sign in to comment.