Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0b58e86

Browse files
authored
bpo-45075: distinguish between frame and FrameSummary in traceback mo… (GH-28112)
1 parent 6f8bc46 commit 0b58e86

4 files changed

Lines changed: 48 additions & 39 deletions

File tree

Doc/library/traceback.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -353,12 +353,12 @@ capture data for later printing in a lightweight fashion.
353353
.. versionchanged:: 3.6
354354
Long sequences of repeated frames are now abbreviated.
355355

356-
.. method:: format_frame(frame)
356+
.. method:: format_frame_summary(frame_summary)
357357

358358
Returns a string for printing one of the frames involved in the stack.
359-
This method gets called for each frame object to be printed in the
360-
:class:`StackSummary`. If it returns ``None``, the frame is omitted
361-
from the output.
359+
This method is called for each :class:`FrameSummary` object to be
360+
printed by :meth:`StackSummary.format`. If it returns ``None``, the
361+
frame is omitted from the output.
362362

363363
.. versionadded:: 3.11
364364

@@ -368,7 +368,7 @@ capture data for later printing in a lightweight fashion.
368368

369369
.. versionadded:: 3.5
370370

371-
:class:`FrameSummary` objects represent a single frame in a traceback.
371+
A :class:`FrameSummary` object represents a single frame in a traceback.
372372

373373
.. class:: FrameSummary(filename, lineno, name, lookup_line=True, locals=None, line=None)
374374

Lib/test/test_traceback.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1515,8 +1515,8 @@ def some_inner(k, v):
15151515

15161516
def test_custom_format_frame(self):
15171517
class CustomStackSummary(traceback.StackSummary):
1518-
def format_frame(self, frame):
1519-
return f'{frame.filename}:{frame.lineno}'
1518+
def format_frame_summary(self, frame_summary):
1519+
return f'{frame_summary.filename}:{frame_summary.lineno}'
15201520

15211521
def some_inner():
15221522
return CustomStackSummary.extract(
@@ -1540,10 +1540,10 @@ def g():
15401540
exc_info = g()
15411541

15421542
class Skip_G(traceback.StackSummary):
1543-
def format_frame(self, frame):
1544-
if frame.name == 'g':
1543+
def format_frame_summary(self, frame_summary):
1544+
if frame_summary.name == 'g':
15451545
return None
1546-
return super().format_frame(frame)
1546+
return super().format_frame_summary(frame_summary)
15471547

15481548
stack = Skip_G.extract(
15491549
traceback.walk_tb(exc_info[2])).format()

Lib/traceback.py

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ def clear_frames(tb):
240240

241241

242242
class FrameSummary:
243-
"""A single frame from a traceback.
243+
"""Information about a single frame from a traceback.
244244
245245
- :attr:`filename` The filename for the frame.
246246
- :attr:`lineno` The line within filename for the frame that was
@@ -365,15 +365,15 @@ def _get_code_position(code, instruction_index):
365365
_RECURSIVE_CUTOFF = 3 # Also hardcoded in traceback.c.
366366

367367
class StackSummary(list):
368-
"""A stack of frames."""
368+
"""A list of FrameSummary objects, representing a stack of frames."""
369369

370370
@classmethod
371371
def extract(klass, frame_gen, *, limit=None, lookup_lines=True,
372372
capture_locals=False):
373373
"""Create a StackSummary from a traceback or stack object.
374374
375-
:param frame_gen: A generator that yields (frame, lineno) tuples to
376-
include in the stack.
375+
:param frame_gen: A generator that yields (frame, lineno) tuples
376+
whose summaries are to be included in the stack.
377377
:param limit: None to include all frames or the number of frames to
378378
include.
379379
:param lookup_lines: If True, lookup lines for each frame immediately,
@@ -394,7 +394,7 @@ def _extract_from_extended_frame_gen(klass, frame_gen, *, limit=None,
394394
lookup_lines=True, capture_locals=False):
395395
# Same as extract but operates on a frame generator that yields
396396
# (frame, (lineno, end_lineno, colno, end_colno)) in the stack.
397-
# Only lineno is required, the remaining fields can be empty if the
397+
# Only lineno is required, the remaining fields can be None if the
398398
# information is not available.
399399
if limit is None:
400400
limit = getattr(sys, 'tracebacklimit', None)
@@ -450,34 +450,38 @@ def from_list(klass, a_list):
450450
result.append(FrameSummary(filename, lineno, name, line=line))
451451
return result
452452

453-
def format_frame(self, frame):
454-
"""Format the lines for a single frame.
453+
def format_frame_summary(self, frame_summary):
454+
"""Format the lines for a single FrameSummary.
455455
456456
Returns a string representing one frame involved in the stack. This
457457
gets called for every frame to be printed in the stack summary.
458458
"""
459459
row = []
460460
row.append(' File "{}", line {}, in {}\n'.format(
461-
frame.filename, frame.lineno, frame.name))
462-
if frame.line:
463-
row.append(' {}\n'.format(frame.line.strip()))
461+
frame_summary.filename, frame_summary.lineno, frame_summary.name))
462+
if frame_summary.line:
463+
row.append(' {}\n'.format(frame_summary.line.strip()))
464464

465-
stripped_characters = len(frame._original_line) - len(frame.line.lstrip())
465+
orig_line_len = len(frame_summary._original_line)
466+
frame_line_len = len(frame_summary.line.lstrip())
467+
stripped_characters = orig_line_len - frame_line_len
466468
if (
467-
frame.colno is not None
468-
and frame.end_colno is not None
469+
frame_summary.colno is not None
470+
and frame_summary.end_colno is not None
469471
):
470-
colno = _byte_offset_to_character_offset(frame._original_line, frame.colno)
471-
end_colno = _byte_offset_to_character_offset(frame._original_line, frame.end_colno)
472+
colno = _byte_offset_to_character_offset(
473+
frame_summary._original_line, frame_summary.colno)
474+
end_colno = _byte_offset_to_character_offset(
475+
frame_summary._original_line, frame_summary.end_colno)
472476

473477
anchors = None
474-
if frame.lineno == frame.end_lineno:
478+
if frame_summary.lineno == frame_summary.end_lineno:
475479
with suppress(Exception):
476480
anchors = _extract_caret_anchors_from_line_segment(
477-
frame._original_line[colno - 1:end_colno - 1]
481+
frame_summary._original_line[colno - 1:end_colno - 1]
478482
)
479483
else:
480-
end_colno = stripped_characters + len(frame.line.strip())
484+
end_colno = stripped_characters + len(frame_summary.line.strip())
481485

482486
row.append(' ')
483487
row.append(' ' * (colno - stripped_characters))
@@ -491,8 +495,8 @@ def format_frame(self, frame):
491495

492496
row.append('\n')
493497

494-
if frame.locals:
495-
for name, value in sorted(frame.locals.items()):
498+
if frame_summary.locals:
499+
for name, value in sorted(frame_summary.locals.items()):
496500
row.append(' {name} = {value}\n'.format(name=name, value=value))
497501

498502
return ''.join(row)
@@ -514,27 +518,27 @@ def format(self):
514518
last_line = None
515519
last_name = None
516520
count = 0
517-
for frame in self:
518-
formatted_frame = self.format_frame(frame)
521+
for frame_summary in self:
522+
formatted_frame = self.format_frame_summary(frame_summary)
519523
if formatted_frame is None:
520524
continue
521-
if (last_file is None or last_file != frame.filename or
522-
last_line is None or last_line != frame.lineno or
523-
last_name is None or last_name != frame.name):
525+
if (last_file is None or last_file != frame_summary.filename or
526+
last_line is None or last_line != frame_summary.lineno or
527+
last_name is None or last_name != frame_summary.name):
524528
if count > _RECURSIVE_CUTOFF:
525529
count -= _RECURSIVE_CUTOFF
526530
result.append(
527531
f' [Previous line repeated {count} more '
528532
f'time{"s" if count > 1 else ""}]\n'
529533
)
530-
last_file = frame.filename
531-
last_line = frame.lineno
532-
last_name = frame.name
534+
last_file = frame_summary.filename
535+
last_line = frame_summary.lineno
536+
last_name = frame_summary.name
533537
count = 0
534538
count += 1
535539
if count > _RECURSIVE_CUTOFF:
536540
continue
537-
result.append(self.format_frame(frame))
541+
result.append(formatted_frame)
538542

539543
if count > _RECURSIVE_CUTOFF:
540544
count -= _RECURSIVE_CUTOFF
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Rename :meth:`traceback.StackSummary.format_frame` to
2+
:meth:`traceback.StackSummary.format_frame_summary`. This method was added
3+
for 3.11 so it was not released yet.
4+
5+
Updated code and docs to better distinguish frame and FrameSummary.

0 commit comments

Comments
 (0)