◐ Shell
clean mode source ↗

gh-132502: Implements PEP-830 adding timestamps to tracebacks by gpshead · Pull Request #129337 · python/cpython

added 24 commits

January 27, 2025 00:27
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.
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.
… other processes in tests as well

@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
Resolve conflicts in Lib/subprocess.py (adopt generic sorted xoptions
loop) and Objects/exceptions.c (integrate _PyTuple_FromPair with
timestamp-aware __reduce__ methods).

@gpshead gpshead marked this pull request as ready for review

April 12, 2026 20:18
Updates the implementation to match the PEP revisions in
python/peps#4928

* Drop the ``us`` display format; ``-X traceback_timestamps`` with no
  value or ``=1`` now selects ``ns``.
* ``ns`` format now renders as seconds with nine fractional digits
  (``<@1776017178.687320256>``) instead of a raw integer with an ``ns``
  suffix.  Integer divmod is used to preserve full precision.
* The ``no_timestamp`` boolean on the traceback formatting APIs is
  replaced by a tri-state ``timestamps`` keyword: ``None`` follows the
  global config, ``False`` suppresses, ``True`` forces display of any
  non-zero ``__timestamp_ns__``.  The setting now propagates through
  ``__cause__``, ``__context__`` and exception-group members.
* MemoryError instances handed out from the free list receive a fresh
  timestamp at hand-out time; only the last-resort static singleton
  remains at 0.