summaryrefslogtreecommitdiffstats
path: root/Lib/traceback.py
diff options
context:
space:
mode:
authorPablo Galindo Salgado <Pablogsal@gmail.com>2024-05-01 13:42:10 (GMT)
committerGitHub <noreply@github.com>2024-05-01 13:42:10 (GMT)
commit4a08a75cf4c490f7c43ede69bdf6e5a79c6a3af3 (patch)
tree3f33ef13df1da9660e6f54227b873b9ac0aff42e /Lib/traceback.py
parentc1bf4874c1e9db2beda1d62c8c241229783c789b (diff)
downloadcpython-4a08a75cf4c490f7c43ede69bdf6e5a79c6a3af3.zip
cpython-4a08a75cf4c490f7c43ede69bdf6e5a79c6a3af3.tar.gz
cpython-4a08a75cf4c490f7c43ede69bdf6e5a79c6a3af3.tar.bz2
gh-99180: Remove traceback anchors in return and assign statements that cover all the displayed range (#112670)
Diffstat (limited to 'Lib/traceback.py')
-rw-r--r--Lib/traceback.py38
1 files changed, 33 insertions, 5 deletions
diff --git a/Lib/traceback.py b/Lib/traceback.py
index fccec0c..6ce745f 100644
--- a/Lib/traceback.py
+++ b/Lib/traceback.py
@@ -617,13 +617,10 @@ class StackSummary(list):
# attempt to parse for anchors
anchors = None
+ show_carets = False
with suppress(Exception):
anchors = _extract_caret_anchors_from_line_segment(segment)
-
- # only use carets if there are anchors or the carets do not span all lines
- show_carets = False
- if anchors or all_lines[0][:start_offset].lstrip() or all_lines[-1][end_offset:].rstrip():
- show_carets = True
+ show_carets = self.should_show_carets(start_offset, end_offset, all_lines, anchors)
result = []
@@ -737,6 +734,37 @@ class StackSummary(list):
return ''.join(row)
+ def should_show_carets(self, start_offset, end_offset, all_lines, anchors):
+ with suppress(SyntaxError, ImportError):
+ import ast
+ tree = ast.parse('\n'.join(all_lines))
+ statement = tree.body[0]
+ value = None
+ def _spawns_full_line(value):
+ return (
+ value.lineno == 1
+ and value.end_lineno == len(all_lines)
+ and value.col_offset == start_offset
+ and value.end_col_offset == end_offset
+ )
+ match statement:
+ case ast.Return(value=ast.Call()):
+ if isinstance(statement.value.func, ast.Name):
+ value = statement.value
+ case ast.Assign(value=ast.Call()):
+ if (
+ len(statement.targets) == 1 and
+ isinstance(statement.targets[0], ast.Name)
+ ):
+ value = statement.value
+ if value is not None and _spawns_full_line(value):
+ return False
+ if anchors:
+ return True
+ if all_lines[0][:start_offset].lstrip() or all_lines[-1][end_offset:].rstrip():
+ return True
+ return False
+
def format(self, **kwargs):
"""Format the stack ready for printing.