Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Closed
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
8 changes: 6 additions & 2 deletions 8 Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
test_frame = namedtuple('frame', ['f_code', 'f_globals', 'f_locals'])
test_tb = namedtuple('tb', ['tb_frame', 'tb_lineno', 'tb_next'])

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


class TracebackCases(unittest.TestCase):
# For now, a very minimal set of tests. I want to be sure that
Expand Down Expand Up @@ -995,9 +999,9 @@ def test_format_smoke(self):
def test_locals(self):
linecache.updatecache('/foo.py', globals())
c = test_code('/foo.py', 'method')
f = test_frame(c, globals(), {'something': 1})
f = test_frame(c, globals(), {'something': 1, "unrepresentable": Unrepresentable()})
s = traceback.StackSummary.extract(iter([(f, 6)]), capture_locals=True)
self.assertEqual(s[0].locals, {'something': '1'})
self.assertEqual(s[0].locals, {'something': '1', "unrepresentable": "[Unrepresentable: Unrepresentable]"})

def test_no_locals(self):
linecache.updatecache('/foo.py', globals())
Expand Down
7 changes: 6 additions & 1 deletion 7 Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ def clear_frames(tb):
pass
tb = tb.tb_next

def _try_repr(x):
try:
return repr(x)
except Exception as exc:
return "[Unrepresentable: {}]".format(exc)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if exc is also not representable ?


class FrameSummary:
"""A single frame from a traceback.
Expand Down Expand Up @@ -257,7 +262,7 @@ 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: _try_repr(v) for k, v in locals.items()} if locals else None

def __eq__(self, other):
if isinstance(other, FrameSummary):
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.