mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
bpo-44569: Decouple frame formatting in traceback.py (GH-27038)
This commit is contained in:
parent
a283ef116b
commit
8ce3008585
4 changed files with 69 additions and 33 deletions
|
@ -449,6 +449,48 @@ class StackSummary(list):
|
|||
result.append(FrameSummary(filename, lineno, name, line=line))
|
||||
return result
|
||||
|
||||
def format_frame(self, frame):
|
||||
"""Format the lines for a single frame.
|
||||
|
||||
Returns a string representing one frame involved in the stack. This
|
||||
gets called for every frame to be printed in the stack summary.
|
||||
"""
|
||||
row = []
|
||||
row.append(' File "{}", line {}, in {}\n'.format(
|
||||
frame.filename, frame.lineno, frame.name))
|
||||
if frame.line:
|
||||
row.append(' {}\n'.format(frame.line.strip()))
|
||||
|
||||
stripped_characters = len(frame._original_line) - len(frame.line.lstrip())
|
||||
if frame.end_lineno == frame.lineno and frame.end_colno != 0:
|
||||
colno = _byte_offset_to_character_offset(frame._original_line, frame.colno)
|
||||
end_colno = _byte_offset_to_character_offset(frame._original_line, frame.end_colno)
|
||||
|
||||
try:
|
||||
anchors = _extract_caret_anchors_from_line_segment(
|
||||
frame._original_line[colno - 1:end_colno - 1]
|
||||
)
|
||||
except Exception:
|
||||
anchors = None
|
||||
|
||||
row.append(' ')
|
||||
row.append(' ' * (colno - stripped_characters))
|
||||
|
||||
if anchors:
|
||||
row.append(anchors.primary_char * (anchors.left_end_offset))
|
||||
row.append(anchors.secondary_char * (anchors.right_start_offset - anchors.left_end_offset))
|
||||
row.append(anchors.primary_char * (end_colno - colno - anchors.right_start_offset))
|
||||
else:
|
||||
row.append('^' * (end_colno - colno))
|
||||
|
||||
row.append('\n')
|
||||
|
||||
if frame.locals:
|
||||
for name, value in sorted(frame.locals.items()):
|
||||
row.append(' {name} = {value}\n'.format(name=name, value=value))
|
||||
|
||||
return ''.join(row)
|
||||
|
||||
def format(self):
|
||||
"""Format the stack ready for printing.
|
||||
|
||||
|
@ -483,40 +525,8 @@ class StackSummary(list):
|
|||
count += 1
|
||||
if count > _RECURSIVE_CUTOFF:
|
||||
continue
|
||||
row = []
|
||||
row.append(' File "{}", line {}, in {}\n'.format(
|
||||
frame.filename, frame.lineno, frame.name))
|
||||
if frame.line:
|
||||
row.append(' {}\n'.format(frame.line.strip()))
|
||||
result.append(self.format_frame(frame))
|
||||
|
||||
stripped_characters = len(frame._original_line) - len(frame.line.lstrip())
|
||||
if frame.end_lineno == frame.lineno and frame.end_colno != 0:
|
||||
colno = _byte_offset_to_character_offset(frame._original_line, frame.colno)
|
||||
end_colno = _byte_offset_to_character_offset(frame._original_line, frame.end_colno)
|
||||
|
||||
try:
|
||||
anchors = _extract_caret_anchors_from_line_segment(
|
||||
frame._original_line[colno - 1:end_colno - 1]
|
||||
)
|
||||
except Exception:
|
||||
anchors = None
|
||||
|
||||
row.append(' ')
|
||||
row.append(' ' * (colno - stripped_characters))
|
||||
|
||||
if anchors:
|
||||
row.append(anchors.primary_char * (anchors.left_end_offset))
|
||||
row.append(anchors.secondary_char * (anchors.right_start_offset - anchors.left_end_offset))
|
||||
row.append(anchors.primary_char * (end_colno - colno - anchors.right_start_offset))
|
||||
else:
|
||||
row.append('^' * (end_colno - colno))
|
||||
|
||||
row.append('\n')
|
||||
|
||||
if frame.locals:
|
||||
for name, value in sorted(frame.locals.items()):
|
||||
row.append(' {name} = {value}\n'.format(name=name, value=value))
|
||||
result.append(''.join(row))
|
||||
if count > _RECURSIVE_CUTOFF:
|
||||
count -= _RECURSIVE_CUTOFF
|
||||
result.append(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue