Skip to content

gh-135751: traceback: add recent_first and show_lines parameter #135752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

methane
Copy link
Member

@methane methane commented Jun 20, 2025

@methane methane requested a review from Copilot June 20, 2025 12:30
@methane methane added type-feature A feature request or enhancement stdlib Python modules in the Lib dir labels Jun 20, 2025
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Adds two new keyword-only parameters, show_lines and recent_first, to the traceback module’s formatting and printing APIs, allowing callers to suppress source lines or invert traceback order.

  • Introduce show_lines and recent_first across module-level functions and TracebackException/StackSummary methods
  • Update internal printing/formatting logic to pass these flags through
  • Extend tests and documentation to cover new behavior

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
Misc/NEWS.d/next/Library/2025-06-20-21-16-32…rst Announce new parameters in the NEWS file
Lib/traceback.py Core implementation: API signatures and logic updates
Lib/test/test_traceback.py New unit tests for show_lines and recent_first flags
Doc/whatsnew/3.15.rst Document what’s new section for show_lines/recent_first
Doc/library/traceback.rst Update function/method docstrings to include new flags
Comments suppressed due to low confidence (4)

Lib/traceback.py:31

  • The first line of the docstring was truncated and now only mentions extract_stack(). It should refer to both extract_tb() and extract_stack(), e.g., "Print the list of tuples as returned by extract_tb() or extract_stack() as a formatted stack trace to the given file."
    extract_stack() as a formatted stack trace to the given file.

Doc/whatsnew/3.15.rst:181

  • [nitpick] Grammar in release notes: replace "It is default to True." with "Defaults to True." for clarity.
  It is default to ``True``.

Doc/library/traceback.rst:125

  • [nitpick] This sentence is duplicated immediately above; remove one instance to avoid redundancy.
   If *show_lines* is false, source code lines are not included in the output.

Doc/library/traceback.rst:175

  • [nitpick] Using "next" as a version string may be confusing. Consider using the actual upcoming version number or a placeholder like "3.x" for clarity.
   .. versionchanged:: next

Comment on lines 588 to 589
The keyword argument *show_lines*, if ``False``, prevents source code
lines from being included in the output.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The keyword argument *show_lines*, if ``False``, prevents source code
lines from being included in the output.
If *show_lines* is set to ``False``, source code lines will not be
included in the output.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or flip to remove double negative.

Copy link
Member

@iritkatriel iritkatriel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think show_lines should be renamed to something like show_source.

@@ -1566,12 +1623,13 @@ def format(self, *, chain=True, _ctx=None, **kwargs):
_ctx.exception_group_depth = 0


def print(self, *, file=None, chain=True, **kwargs):
def print(self, *, file=None, chain=True, show_lines=True, recent_first=False, **kwargs):
"""Print the result of self.format(chain=chain) to 'file'."""
colorize = kwargs.get("colorize", False)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not add the new args via **kwargs as was done for colorize? It would save having to update all the signatures.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think colorize is in kwargs because of its hidden parameter. Callars determine use COLOR automatically by isatty() and env variables and pass it to here. colorize is not documented.

On the other hand, show_lines and recent_first are explicit parameter.
User need to determine and specify them manually.

Co-authored-by: Hugo van Kemenade <[email protected]>
methane and others added 2 commits June 21, 2025 08:57
Co-authored-by: Brian Schubert <[email protected]>
Co-authored-by: Hugo van Kemenade <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting core review stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants