Review request: gh-135751: traceback: add recent_first and show_lines parameter

I am proposing to add recent_first and show_lines parameters to traceback APIs.
Please review its design and implementation.

Current traceback format (recent call last, show each lines) is optimized for console.
It is not good for observability backends. For example, this is the list of stacktrace representations in Open Telemetry conventions. All languages except Python uses “recent call first, no source code lines” format.

My design decisions:

  • Fullpath of source code looks too long, but this PR focuses only order and source lines.
  • Default values of both options are same to existing behavior.
  • I chose the name recent_first=False because reverse nor reversed_order are not descriptive. Since we see Traceback (most recent call last) everyday, “recent call (first | last)” would more descriptive. I removed “call” for shorter name. recent_last=True would also work, and I don’t have a strong preference between the two.
  • I chose the name show_lines because of consistency with:
    • FrameSummary.line
    • StackSummary.extract(..., lookup_lines, ...)
    • format_exception_only(..., show_group, ...)
8 Likes

Big +1 from me. This feature will be used.

Note that faulthandler already uses recent_first=True equivalent behavior (and show_lines=False because it isn’t possible to do source line lookup in a signal handler crashing context…)

1 Like