-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
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
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this 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
andrecent_first
across module-level functions andTracebackException
/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 bothextract_tb()
andextract_stack()
, e.g., "Print the list of tuples as returned byextract_tb()
orextract_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 toTrue
." 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
Co-authored-by: Copilot <[email protected]>
If *recent_first* is true, the most recent stack trace entries are printed | ||
first, otherwise the oldest entries are printed first. The default is false. | ||
|
||
.. note:: | ||
``recent_first=True`` is useful for showing stack traces in places where | ||
people see the top of the stack trace first, such as in a web browser. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this needs the loudness of a .. note
block. Merging this into the previous paragraph would be sufficient IMO
|
||
.. versionchanged:: next | ||
Added *show_lines* parameter. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: some of new version changed directives use the style "Added X parameter" while others use "Added the X parameter". Both styles are used elsewhere in the docs, but it would probably be better to use one consistently here
Doc/library/traceback.rst
Outdated
The keyword argument *show_lines*, if ``False``, prevents source code | ||
lines from being included in the output. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
There was a problem hiding this comment.
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.
There was a problem hiding this 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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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]>
Co-authored-by: Brian Schubert <[email protected]> Co-authored-by: Hugo van Kemenade <[email protected]>
recent_first
andshow_lines
parameters #135751📚 Documentation preview 📚: https://cpython-previews--135752.org.readthedocs.build/