Skip to content

Commit 53b5500

Browse files
committed
reword some docs, add examples
1 parent c9ad56d commit 53b5500

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

Doc/library/exceptions.rst

+19-4
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,31 @@ The following exceptions are used mostly as base classes for other exceptions.
161161

162162
.. attribute:: __timestamp_ns__
163163

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

175+
With ``PYTHON_TRACEBACK_TIMESTAMPS=iso`` in the environment ::
176+
177+
Traceback (most recent call last):
178+
File "<python-input-0>", line 1, in <module>
179+
raise RuntimeError("example")
180+
RuntimeError: example <@2025-02-08T01:21:28.675309>
181+
182+
With ``PYTHON_TRACEBACK_TIMESTAMPS=ns`` in the environment ::
183+
184+
Traceback (most recent call last):
185+
File "<python-input-0>", line 1, in <module>
186+
raise RuntimeError("example")
187+
RuntimeError: example <@1739172733527638530ns>
188+
174189
.. versionadded:: next
175190

176191

Doc/using/cmdline.rst

+13-15
Original file line numberDiff line numberDiff line change
@@ -1223,21 +1223,19 @@ conflict.
12231223

12241224
.. envvar:: PYTHON_TRACEBACK_TIMESTAMPS
12251225

1226-
If this variable is set to any of the following values, tracebacks displayed
1227-
by the :mod:`traceback` module will be annotated with the timestamp of each
1228-
exception. The values control the format of the timestamp. ``us`` or ``1``
1229-
displays decimal timestamps with microsecond precision, ``ns`` displays the
1230-
nanosecond timestamp as :func:`time.time_ns` would produce, ``iso`` enables
1231-
display formatted by :meth:`~datetime.datetime.isoformat`. The time is not
1232-
recorded on the :exc:`StopIteration` family of exceptions for performance
1233-
reasons as those are used for control flow rather than errors. If unset,
1234-
empty or other values this feature is disabled.
1235-
1236-
Timestamps are collected as nanoseconds internally when exceptions are
1237-
instantiated and are available via a :attr:`~BaseException.__timestamp_ns__`
1238-
attribute. Optional formatting of the timestamps only happens during
1239-
:mod:`traceback` rendering. The ``iso`` format is presumed slower to
1240-
display due to the complexity of the code involved.
1226+
If this variable is set to any of the following values, tracebacks printed
1227+
by the runtime will be annotated with the timestamp of each exception. The
1228+
values control the format of the timestamp. ``us`` or ``1`` prints decimal
1229+
timestamps with microsecond precision, ``ns`` prints the raw timestamp in
1230+
nanoseconds, ``iso`` prints the timestamp formatted by
1231+
:meth:`~datetime.datetime.isoformat` which is also microsecond precision.
1232+
The time is not recorded on the :exc:`StopIteration` family of exceptions
1233+
for performance reasons as those are used for control flow rather than
1234+
errors. If unset, empty, or other values this feature remains disabled.
1235+
1236+
Formatting of the timestamps only happens at printing time. The ``iso``
1237+
format may be slower due to the complexity of the code involved but is much
1238+
more readable.
12411239

12421240
.. versionadded:: next
12431241

0 commit comments

Comments
 (0)