Skip to content

gh-132502: Implements PEP-829 adding timestamps to tracebacks#129337

Draft
gpshead wants to merge 77 commits intopython:mainfrom
gpshead:traceback-timestamps
Draft

gh-132502: Implements PEP-829 adding timestamps to tracebacks#129337
gpshead wants to merge 77 commits intopython:mainfrom
gpshead:traceback-timestamps

Conversation

@gpshead
Copy link
Member

@gpshead gpshead commented Jan 27, 2025

PEP coming together in python/peps#4592 - that's a much better description. Go read the PEP.

Updates: [edits]

I've done performance testing. It now appears to be a no-op (no meaningful regression) as desired performance wise on Linux. For that to be true, I had to add the one special case I suspected might matter: Don't collect the timestamp on StopIteration and AsyncStopIteration as those are not infrequent error exceptions, but a normal part of application control flow. Without that, one of the async pyperformance benchmarks showed a significant performance hit of over 10%.

I've changed the year ago early original to not emit the timestamps by default and after discussing at our sprint, to not even collect the data when display is disabled. If you set the PYTHON_TRACEBACK_TIMESTAMPS= environment variable to one of either us or 1, ns, or iso, timestamps will be enabled at exception instantiation time and in traceback module display output. See the documentation in the PR & see the PEP.

This came up at work as a suggestion to make debugging what happened in
big async servers with lots of exception groups and exceptions easier.
Timestamps when emitting exception groups containing tracebacks often
with their own nested causes would allow some semblance of order to be
understood.

This is a demo.  If we want such a feature, we should settle on
semantics in a Discuss thread and write it up as a PEP.  This should be
simpler than exception notes (PEP-678) was.  One thought was just to
store the timestamp as a note; but that'd involve string and list
creation on every exception.

Performance testing needs to be done. This is the kind of thing that is
visually distracting, so not all applications want to _see_ the
timestamps.  A knob to turn that on for those who do seems more useful
rather than making that the default.  But the performance impact of
merely collecting the timestamps is worth knowing.
Avoids a 15% regression in the pyperformance async_generators suite.
Tested with `PYTHON_TRACEBACK_TIMESTAMPS=ns` set.

First pass.  Further review could rework some of these changes.

Explicit tests for the new feature have yet to be added.
@gpshead gpshead force-pushed the traceback-timestamps branch from 6c85054 to 53b5500 Compare February 10, 2025 08:04
Better docs, improved tests.

Claude Code using Sonnet 3.7 helped with this, but that was a bit of a battle
as our CPython code context size for this type of change is huge.
TODO: performance testing - this increases the conditional load on every
BaseException instantiation with that interp->config.field && field[0] check.

If needed, we could cache the "collect or not" bool in a static global as it is
fair to say this is a process wide setting rather than per interpreter, but ugh.
@gpshead gpshead added the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Jun 8, 2025
@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by @gpshead for commit 77ffb5a 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F129337%2Fmerge

If you want to schedule another build, you need to add the 🔨 test-with-refleak-buildbots label again.

@bedevere-bot bedevere-bot removed the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Jun 8, 2025
gpshead and others added 2 commits June 8, 2025 12:30
- Add cross-references between command line flags and environment variables
- Clarify default behavior when PYTHON_TRACEBACK_TIMESTAMPS is unset
- Simplify traceback.rst wording to use "canonical output"
- Fix alphabetical ordering in test_config.py
- Improve comment formatting in test_sys.py for better readability

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
The functionality is already provided by force_color(False) and
force_not_colorized() which were added since this branch started.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@rocky-d

This comment was marked as spam.

@gpshead gpshead force-pushed the traceback-timestamps branch from 82c6246 to 7e6d542 Compare March 15, 2026 06:56
gpshead and others added 5 commits March 15, 2026 07:23
Resolved conflicts:
- .github/workflows/build.yml: keep both traceback_timestamps and test-opts
- .github/workflows/reusable-ubuntu.yml: keep both input parameters
- Doc/using/cmdline.rst: add traceback_timestamps after new main entries
- Include/cpython/initconfig.h: add traceback_timestamps after pymalloc_hugepages
- Python/sysmodule.c: add traceback_timestamps after lazy_imports in flags

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…PR and mention that in a comment to avoid it recurring
@gpshead gpshead force-pushed the traceback-timestamps branch from 962d209 to 7d2a4d3 Compare March 15, 2026 07:24
@gpshead gpshead changed the title gh-132502: Prototyping adding timestamps to tracebacks. gh-132502: Implements PEP-829 adding timestamps to tracebacks Mar 15, 2026
gpshead and others added 2 commits March 15, 2026 19:18
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
gpshead and others added 9 commits March 15, 2026 20:38
Resolved conflicts in sysmodule.c (keep 3.13 in comment) and test_sys.py
(adopt main's split of indexable vs name-only sys.flags attributes,
add traceback_timestamps to the name-only section).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Resolved conflict in initconfig.c --help-env: main cleaned up duplicate
entries; keep only our PYTHON_TRACEBACK_TIMESTAMPS addition.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
"us" is what "1" means, not what you get when no value is provided.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants