mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
gh-99180: Remove traceback anchors in return and assign statements that cover all the displayed range (#112670)
This commit is contained in:
parent
c1bf4874c1
commit
4a08a75cf4
3 changed files with 240 additions and 33 deletions
|
@ -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.
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue