New submission from daniel hahler <[email protected]>:
Exceptions within `__repr__` methods of captured locals
(e.g. via the `capture_locals` argument of `TracebackException`) are not
handled:
```
import traceback
class CrashingRepr:
def __repr__(self):
raise RuntimeError("crash")
traceback.FrameSummary("fname", 1, "name", locals={"crash": CrashingRepr()})
```
Result:
```
Traceback (most recent call last):
File "test_framesummary_repr.py", line 9, in <module>
traceback.FrameSummary("fname", 1, "name", locals={"crash": CrashingRepr()})
File "…/pyenv/3.8.0/lib/python3.8/traceback.py", line 260, in __init__
self.locals = {k: repr(v) for k, v in locals.items()} if locals else None
File "…/pyenv/3.8.0/lib/python3.8/traceback.py", line 260, in <dictcomp>
self.locals = {k: repr(v) for k, v in locals.items()} if locals else None
File "test_framesummary_repr.py", line 6, in __repr__
raise RuntimeError("crash")
RuntimeError: crash
```
The following patch would fix this:
```diff
diff --git i/Lib/traceback.py w/Lib/traceback.py
index 7a4c8e19f9..eed7082db4 100644
--- i/Lib/traceback.py
+++ w/Lib/traceback.py
class FrameSummary:
"""A single frame from a traceback.
@@ -257,7 +265,17 @@ def __init__(self, filename, lineno, name, *,
lookup_line=True,
self._line = line
if lookup_line:
self.line
- self.locals = {k: repr(v) for k, v in locals.items()} if locals else
None
+ if locals:
+ self.locals = {}
+ for k, v in locals.items():
+ try:
+ self.locals[k] = repr(v)
+ except (KeyboardInterrupt, SystemExit):
+ raise
+ except BaseException as exc:
+ self.locals[k] = f"<unrepresentable repr ({exc})>"
+ else:
+ self.locals = None
def __eq__(self, other):
if isinstance(other, FrameSummary):
```
----------
components: Library (Lib)
messages: 359400
nosy: blueyed
priority: normal
severity: normal
status: open
title: traceback.FrameSummary does not handle exceptions from `repr()`
type: behavior
versions: Python 3.5, Python 3.6, Python 3.7, Python 3.8, Python 3.9
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue39228>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com