summaryrefslogtreecommitdiffstats
path: root/Lib/traceback.py
diff options
context:
space:
mode:
authorPablo Galindo Salgado <Pablogsal@gmail.com>2023-10-12 14:52:14 (GMT)
committerGitHub <noreply@github.com>2023-10-12 14:52:14 (GMT)
commite7331365b488382d906ce6733ab1349ded49c928 (patch)
treebecf226032f4abe846e3a46e07f666cedb55cbc9 /Lib/traceback.py
parent8c6c14b91bf95e04018151c53bce6e27e0e22447 (diff)
downloadcpython-e7331365b488382d906ce6733ab1349ded49c928.zip
cpython-e7331365b488382d906ce6733ab1349ded49c928.tar.gz
cpython-e7331365b488382d906ce6733ab1349ded49c928.tar.bz2
gh-110721: Use the traceback module for PyErr_Display() and fallback to the C implementation (#110702)
Diffstat (limited to 'Lib/traceback.py')
-rw-r--r--Lib/traceback.py26
1 files changed, 21 insertions, 5 deletions
diff --git a/Lib/traceback.py b/Lib/traceback.py
index 67941ff..12fcdad 100644
--- a/Lib/traceback.py
+++ b/Lib/traceback.py
@@ -125,6 +125,14 @@ def print_exception(exc, /, value=_sentinel, tb=_sentinel, limit=None, \
te.print(file=file, chain=chain)
+BUILTIN_EXCEPTION_LIMIT = object()
+
+
+def _print_exception_bltin(exc, /):
+ file = sys.stderr if sys.stderr is not None else sys.__stderr__
+ return print_exception(exc, limit=BUILTIN_EXCEPTION_LIMIT, file=file)
+
+
def format_exception(exc, /, value=_sentinel, tb=_sentinel, limit=None, \
chain=True):
"""Format a stack trace and the exception information.
@@ -406,12 +414,16 @@ class StackSummary(list):
# (frame, (lineno, end_lineno, colno, end_colno)) in the stack.
# Only lineno is required, the remaining fields can be None if the
# information is not available.
- if limit is None:
+ builtin_limit = limit is BUILTIN_EXCEPTION_LIMIT
+ if limit is None or builtin_limit:
limit = getattr(sys, 'tracebacklimit', None)
if limit is not None and limit < 0:
limit = 0
if limit is not None:
- if limit >= 0:
+ if builtin_limit:
+ frame_gen = tuple(frame_gen)
+ frame_gen = frame_gen[len(frame_gen) - limit:]
+ elif limit >= 0:
frame_gen = itertools.islice(frame_gen, limit)
else:
frame_gen = collections.deque(frame_gen, maxlen=-limit)
@@ -741,9 +753,9 @@ class TracebackException:
wrong_name = getattr(exc_value, "name", None)
if wrong_name is not None and wrong_name in sys.stdlib_module_names:
if suggestion:
- self._str += f" Or did you forget to import '{wrong_name}'"
+ self._str += f" Or did you forget to import '{wrong_name}'?"
else:
- self._str += f". Did you forget to import '{wrong_name}'"
+ self._str += f". Did you forget to import '{wrong_name}'?"
if lookup_lines:
self._load_lines()
self.__suppress_context__ = \
@@ -904,7 +916,11 @@ class TracebackException:
if self.offset is not None:
offset = self.offset
end_offset = self.end_offset if self.end_offset not in {None, 0} else offset
- if offset == end_offset or end_offset == -1:
+ if self.text and offset > len(self.text):
+ offset = len(self.text) + 1
+ if self.text and end_offset > len(self.text):
+ end_offset = len(self.text) + 1
+ if offset >= end_offset or end_offset < 0:
end_offset = offset + 1
# Convert 1-based column offset to 0-based index into stripped text