Skip to content

Commit daa752d

Browse files
committed
initial pass at documentation.
1 parent 09a547a commit daa752d

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

Doc/library/exceptions.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,19 @@ The following exceptions are used mostly as base classes for other exceptions.
159159

160160
.. versionadded:: 3.11
161161

162+
.. attribute:: __timestamp_ns__
163+
164+
The time at which the exception instance was instantiated (usually when
165+
it was raised) in nanoseconds in :func:`time.time_ns` units. Display of
166+
this in tracebacks can be controlled using the
167+
:envvar:`PYTHON_TRACEBACK_TIMESTAMPS` environment variable. In
168+
applications with complicated exception chains and exception groups it be
169+
used to help visualize what happened when. The value will be 0 if a time
170+
was not recorded as is the case on :exc:`StopIteration` and
171+
:exc:`AsyncStopIteration`.
172+
173+
.. versionadded:: next
174+
162175

163176
.. exception:: Exception
164177

Doc/using/cmdline.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,6 +1221,27 @@ conflict.
12211221

12221222
.. versionadded:: 3.13
12231223

1224+
.. envvar:: PYTHON_TRACEBACK_TIMESTAMPS
1225+
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 :func:`datetime.isoformat`. The time is not recorded
1232+
on the :exc:`StopIteration` family of exceptions for performance reasons as
1233+
those are used for control flow rather than errors. If unset, empty or other
1234+
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.
1241+
1242+
.. versionadded:: next
1243+
1244+
12241245
Debug-mode variables
12251246
~~~~~~~~~~~~~~~~~~~~
12261247

0 commit comments

Comments
 (0)