Commit 75c609cb authored by Lucas Felix's avatar Lucas Felix
Browse files

Handle __repr__ exceptions while capturing traceback's locals

parent 6046196a
......@@ -29,7 +29,7 @@ import platform
import importlib
from collections import deque
from datetime import datetime
from textwrap import indent
import ptpython.layout
from prompt_toolkit.patch_stdout import patch_stdout as patch_stdout_context
......@@ -99,11 +99,24 @@ class PrettyTraceback:
self._timestamp = datetime.now()
self._exc_type = exc_type
self._exc_value = exc_value
self._locals_capture_exc = ""
# convert traceback to StackSummary to stringify references and avoid memory leak
self._stack_summary = traceback.StackSummary.extract(
traceback.walk_tb(tb), lookup_lines=True, capture_locals=True
)
try:
self._stack_summary = traceback.StackSummary.extract(
traceback.walk_tb(tb), lookup_lines=True, capture_locals=True
)
except Exception as e:
# Capture_locals option fails as soon as one local's __repr__ fails.
# This will be fixed in python 3.11 with the addition of format_locals option,
# see https://github.com/python/cpython/pull/29299
# For the moment we can only disable capture_locals
self._locals_capture_exc = (
traceback.format_tb(sys.exc_info()[2], -1)[0] + f" {e}"
)
self._stack_summary = traceback.StackSummary.extract(
traceback.walk_tb(tb), lookup_lines=True, capture_locals=False
)
def _is_blacklisted(self, filename):
for black_path in PrettyTraceback._blacklist:
......@@ -119,6 +132,13 @@ class PrettyTraceback:
)
]
if self._locals_capture_exc:
msg = (
"Can't display local variables along stack trace, error occured during recovery:\n"
f"{self._locals_capture_exc}\n\n"
)
header.append((Token.Comment.Preproc, indent(msg, "* ")))
# Stack trace formatting
stack = []
for f in self._stack_summary:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment