Skip to content

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

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

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

Conversation

@gpshead
Copy link
Copy Markdown
Member

@gpshead gpshead commented Jan 27, 2025

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

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 (Cambridge 2025-09) 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 changed the title gh-132502: Implements PEP-829 adding timestamps to tracebacks gh-132502: Implements PEP-830 adding timestamps to tracebacks Apr 12, 2026
gpshead added 2 commits April 12, 2026 06:17
Resolve conflicts in Lib/subprocess.py (adopt generic sorted xoptions
loop) and Objects/exceptions.c (integrate _PyTuple_FromPair with
timestamp-aware __reduce__ methods).
@gpshead
Copy link
Copy Markdown
Member Author

gpshead commented Apr 12, 2026

Hmm, I'll leave this as "Draft" while the PEP discussion kicks off: https://discuss.python.org/t/pep-830-add-timestamps-to-exceptions-and-tracebacks/106942

@gpshead gpshead marked this pull request as draft April 12, 2026 20:19
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