Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-87822: Make traceback module robust to exceptions from repr() of local values #94691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Doc/library/traceback.rst
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,10 @@ capture data for later printing in a lightweight fashion.
local variables in each :class:`FrameSummary` are captured as object
representations.

.. versionchanged:: 3.12
Exceptions raised from :func:`repr` on a local variable (when
*capture_locals* is ``True``) are no longer propagated to the caller.

.. classmethod:: from_list(a_list)

Construct a :class:`StackSummary` object from a supplied list of
Expand Down
8 changes: 6 additions & 2 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -2247,6 +2247,9 @@ def format_frame_summary(self, frame_summary):
f' File "{__file__}", line {lno}, in f\n 1/0\n'
)

class Unrepresentable:
def __repr__(self) -> str:
raise Exception("Unrepresentable")

class TestTracebackException(unittest.TestCase):

Expand Down Expand Up @@ -2514,12 +2517,13 @@ def test_locals(self):
linecache.updatecache('/foo.py', globals())
e = Exception("uh oh")
c = test_code('/foo.py', 'method')
f = test_frame(c, globals(), {'something': 1, 'other': 'string'})
f = test_frame(c, globals(), {'something': 1, 'other': 'string', 'unrepresentable': Unrepresentable()})
tb = test_tb(f, 6, None, 0)
exc = traceback.TracebackException(
Exception, e, tb, capture_locals=True)
self.assertEqual(
exc.stack[0].locals, {'something': '1', 'other': "'string'"})
exc.stack[0].locals,
{'something': '1', 'other': "'string'", 'unrepresentable': '<local repr() failed>'})

def test_no_locals(self):
linecache.updatecache('/foo.py', globals())
Expand Down
3 changes: 2 additions & 1 deletion Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ 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
self.locals = {k: _safe_string(v, 'local', func=repr)
for k, v in locals.items()} if locals else None
self.end_lineno = end_lineno
self.colno = colno
self.end_colno = end_colno
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1590,6 +1590,7 @@ Ed Schouten
Scott Schram
Robin Schreiber
Chad J. Schroeder
Simon-Martin Schroeder
Christian Schubert
Sam Schulenburg
Andreas Schwab
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
When called with ``capture_locals=True``, the :mod:`traceback` module functions swallow exceptions raised from calls to ``repr()`` on local variables of frames. This is in order to prioritize the original exception over rendering errors. An indication of the failure is printed in place of the missing value. (Patch by Simon-Martin Schroeder).