Skip to content

gh-132502: Prototyping adding timestamps to tracebacks. #129337

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

Draft
wants to merge 47 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
c5a9ccd
Proof of concept: Add timestamps to tracebacks.
gpshead Jan 27, 2025
3dc00c2
`PYTHON_TRACEBACK_TIMESTAMPS=1` required to enable their display.
gpshead Jan 27, 2025
a05766f
skip timestamp on StopIteration - not an error
gpshead Jan 28, 2025
cdb67f0
include the timestamp in exception pickles.
gpshead Jan 28, 2025
99ffc8a
os.environ not os.environb
gpshead Jan 30, 2025
c119a02
Cleaner struct layout.
gpshead Feb 2, 2025
bcc720b
Timestamp format configurability.
gpshead Feb 2, 2025
b065394
Plumb into exception subtypes; including pickling.
gpshead Feb 2, 2025
09a547a
minor cleanups
gpshead Feb 2, 2025
daa752d
initial pass at documentation.
gpshead Feb 2, 2025
e7fab86
Fix doc references?
gpshead Feb 2, 2025
e8a6297
proper refcount cleanup in error cases.
gpshead Feb 2, 2025
6809426
Merge branch 'main' into traceback-timestamps
gpshead Feb 9, 2025
0d83447
Allow `PyErr_Display*` to emit timestamps as well.
gpshead Feb 10, 2025
7c83ebf
Make the testsuite pass with timestamps turned on.
gpshead Feb 10, 2025
beadfb8
Docs docs docs docs docs
gpshead Feb 10, 2025
354c5f0
docs make check
gpshead Feb 10, 2025
33d20dd
docs typo
gpshead Feb 10, 2025
2f72323
GIT
gpshead Feb 10, 2025
7f7357d
docs: reword exceptions
gpshead Feb 10, 2025
d9d2b1f
vi PEBKAC reformatted traceback.rst? undo
gpshead Feb 10, 2025
98b4593
more formatting messup undo
gpshead Feb 10, 2025
c9ad56d
more undo
gpshead Feb 10, 2025
53b5500
reword some docs, add examples
gpshead Feb 10, 2025
8043b80
REDO BEFORE MERGE: enable on some CI builds
gpshead Feb 10, 2025
75072cb
Go full on -X traceback_timestamps command line flag.
gpshead Feb 26, 2025
eebec1d
Disable time collection when display is disabled.
gpshead Mar 2, 2025
9ff3234
remove errant Doc temp db file
gpshead Mar 2, 2025
f30c74d
WIP: optional config string
gpshead Mar 7, 2025
85496cf
WIP: no dict key for reduce when 0
gpshead Mar 7, 2025
9c8eda4
Merge branch 'main' into traceback-timestamps
gpshead Apr 13, 2025
6949cba
simplify `iso` format to not use datetime
gpshead Apr 14, 2025
b564e63
minor cleanups
gpshead Apr 14, 2025
e6199e3
post merge fixups
gpshead Apr 14, 2025
4c031a6
fix stripping tests
gpshead Apr 14, 2025
23e2538
test fixing when timestamps enabled
gpshead Apr 14, 2025
98ae94f
lets run CI on more platforms with it enabled
gpshead Apr 14, 2025
7d45424
duh why did my compiler allow that?
gpshead Apr 14, 2025
dde0f39
fix windows errors?
gpshead Apr 14, 2025
5f7b930
force_not_colorized
gpshead Apr 14, 2025
964bdd3
force utf8 for the entire test class
gpshead Apr 14, 2025
5554b67
waste time dealing with bad unicode test env. life
gpshead Apr 14, 2025
0f9bb1b
alignment SCIENCE
gpshead Apr 14, 2025
a55c3b1
errors="ignore" chicanery decode
gpshead Apr 14, 2025
d2d7583
Fix traceback_timestamps tests for cross-platform line endings
gpshead Apr 14, 2025
84989bb
(╯°□°)╯︵ ┻━┻ Windows
gpshead Apr 14, 2025
b268c82
pedantic-NotIn-too
gpshead Apr 14, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ jobs:
bolt-optimizations: ${{ matrix.bolt }}
free-threading: ${{ matrix.free-threading }}
os: ${{ matrix.os }}
traceback_timestamps: ${{ matrix.os == 'ubuntu-22.04-arm' && matrix.bolt == false && 'ns' || '' }}

build-ubuntu-ssltests:
name: 'Ubuntu SSL tests with OpenSSL'
Expand Down Expand Up @@ -686,3 +687,4 @@ jobs:
|| ''
}}
jobs: ${{ toJSON(needs) }}
traceback_timestamps: 'iso'
5 changes: 5 additions & 0 deletions .github/workflows/reusable-macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ on:
description: OS to run the job
required: true
type: string
traceback_timestamps:
description: Set to us|1|ns|iso to enable printing timestamps on exceptions in tracebacks (for feature coverage)
required: false
type: string

env:
FORCE_COLOR: 1
Expand All @@ -29,6 +33,7 @@ jobs:
HOMEBREW_NO_INSTALL_CLEANUP: 1
HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK: 1
PYTHONSTRICTEXTENSIONBUILD: 1
PYTHON_TRACEBACK_TIMESTAMPS: ${{ inputs.traceback_timestamps }}
TERM: linux
steps:
- uses: actions/checkout@v4
Expand Down
11 changes: 8 additions & 3 deletions .github/workflows/reusable-ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ on:
type: boolean
default: false
os:
description: OS to run the job
required: true
type: string
description: OS to run the job
required: true
type: string
traceback_timestamps:
description: Set to us|1|ns|iso to enable printing timestamps on exceptions in tracebacks (for feature coverage)
required: false
type: string

env:
FORCE_COLOR: 1
Expand All @@ -33,6 +37,7 @@ jobs:
OPENSSL_VER: 3.0.15
PYTHONSTRICTEXTENSIONBUILD: 1
TERM: linux
PYTHON_TRACEBACK_TIMESTAMPS: ${{ inputs.traceback_timestamps }}
steps:
- uses: actions/checkout@v4
with:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/reusable-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
timeout-minutes: 60
env:
ARCH: ${{ inputs.arch }}
PYTHON_TRACEBACK_TIMESTAMPS: 'ns'
steps:
- uses: actions/checkout@v4
with:
Expand Down
23 changes: 23 additions & 0 deletions Doc/c-api/init_config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,10 @@ Configuration Options
- :c:member:`tracemalloc <PyConfig.tracemalloc>`
- ``int``
- Read-only
* - ``"traceback_timestamps"``
- :c:member:`traceback_timestamps <PyConfig.traceback_timestamps>`
- ``str``
- Read-only
* - ``"use_environment"``
- :c:member:`use_environment <PyConfig.use_environment>`
- ``bool``
Expand Down Expand Up @@ -1883,6 +1887,25 @@ PyConfig

Default: ``-1`` in Python mode, ``0`` in isolated mode.

.. c:member:: wchar_t* traceback_timestamps

Format of timestamps shown in tracebacks.

If not ``NULL`` or an empty string, timestamps of exceptions are collected
and will be displayed in the configured format. Acceptable values are:

* ``"us"``: Display timestamps in microseconds
* ``"ns"``: Display timestamps in nanoseconds
* ``"iso"``: Display timestamps in ISO-8601 format
* ``""``: Collection and display is disabled.

Set by the :option:`-X traceback_timestamps=FORMAT <-X>` command line
option or the :envvar:`PYTHON_TRACEBACK_TIMESTAMPS` environment variable.

Default: ``NULL`` (timestamps disabled).

.. versionadded:: next

.. c:member:: int perf_profiling

Enable the Linux ``perf`` profiler support?
Expand Down
21 changes: 21 additions & 0 deletions Doc/library/doctest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,11 @@ Some details you should read once, but won't need to remember:
everything following the leftmost colon and any module information in the
exception name is ignored.

* When the :const:`IGNORE_EXCEPTION_TIMESTAMPS` doctest option is specified,
timestamps of the formats that :envvar:`PYTHON_TRACEBACK_TIMESTAMPS`
environment variable could append to exception messages are stripped from
tracebacks generated by the code under test before comparison.

* The interactive shell omits the traceback header line for some
:exc:`SyntaxError`\ s. But doctest uses the traceback header line to
distinguish exceptions from non-exceptions. So in the rare case where you need
Expand Down Expand Up @@ -652,6 +657,22 @@ doctest decides whether actual output matches an example's expected output:
to the module containing the exception under test.


.. data:: IGNORE_EXCEPTION_TIMESTAMPS

When the :envvar:`PYTHON_TRACEBACK_TIMESTAMPS` environment variable is set,
exception and warning messages rendered in tracebacks may have a timestamp
value appended after the message. This strips all possible formats of that
timestamp tag that may be appended to an exception message in generated
tracebacks before comparing.

:const:`ELLIPSIS` and :const:`IGNORE_EXCEPTION_DETAIL` could also be used to
avoid matching those. This can be cleaner when you need to test specific
details of exception messages.

.. versionadded:: next
:const:`IGNORE_EXCEPTION_TIMESTAMPS` was added.


.. data:: SKIP

When specified, do not run the example at all. This can be useful in contexts
Expand Down
29 changes: 29 additions & 0 deletions Doc/library/exceptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,35 @@ The following exceptions are used mostly as base classes for other exceptions.

.. versionadded:: 3.11

.. attribute:: __timestamp_ns__

The absolute time in nanoseconds at which this exception was instantiated
(usually: when it was raised); the same accuracy as :func:`time.time_ns`.
Display of these timestamps after the exception message in tracebacks is
off by default but can be configured using the
:envvar:`PYTHON_TRACEBACK_TIMESTAMPS` environment variable. In
applications with complicated exception chains and exception groups it
may be useful to help understand what happened when. The value will be
``0`` if a timestamp was not recorded. :exc:`StopIteration` and
:exc:`StopAsyncIteration` never record timestamps as those are primarily
for control flow.

With ``PYTHON_TRACEBACK_TIMESTAMPS=iso`` in the environment ::

Traceback (most recent call last):
File "<python-input-0>", line 1, in <module>
raise RuntimeError("example")
RuntimeError: example <@2025-02-08T01:21:28.675309Z>

With ``PYTHON_TRACEBACK_TIMESTAMPS=ns`` in the environment ::

Traceback (most recent call last):
File "<python-input-0>", line 1, in <module>
raise RuntimeError("example")
RuntimeError: example <@1739172733527638530ns>

.. versionadded:: next


.. exception:: Exception

Expand Down
7 changes: 7 additions & 0 deletions Doc/library/sys.rst
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,10 @@ always available. Unless explicitly noted otherwise, all variables are read-only
- :option:`-X context_aware_warnings <-X>` and
:envvar:`PYTHON_CONTEXT_AWARE_WARNINGS`

* - .. attribute:: flags.traceback_timestamps
- :option:`-X traceback_timestamps <-X>`. This is a string containing
the selected format (``us``, ``ns``, ``iso``), or an empty string
when disabled.

.. versionchanged:: 3.2
Added ``quiet`` attribute for the new :option:`-q` flag.
Expand Down Expand Up @@ -642,6 +646,9 @@ always available. Unless explicitly noted otherwise, all variables are read-only
.. versionchanged:: 3.14
Added the ``context_aware_warnings`` attribute.

.. versionchanged:: next
Added the ``traceback_timestamps`` attribute.


.. data:: float_info

Expand Down
Loading
Loading