Skip to content

Commit 3dc00c2

Browse files
committed
PYTHON_TRACEBACK_TIMESTAMPS=1 required to enable their display.
1 parent c5a9ccd commit 3dc00c2

3 files changed

Lines changed: 27 additions & 15 deletions

File tree

Lib/test/test_sys.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,13 +1595,13 @@ def inner():
15951595
class C(object): pass
15961596
check(C.__dict__, size('P'))
15971597
# BaseException
1598-
check(BaseException(), size('6Pb'))
1598+
check(BaseException(), size('3Pq3Pb'))
15991599
# UnicodeEncodeError
1600-
check(UnicodeEncodeError("", "", 0, 0, ""), size('6Pb 2P2nP'))
1600+
check(UnicodeEncodeError("", "", 0, 0, ""), size('3Pq3Pb 2P2nP'))
16011601
# UnicodeDecodeError
1602-
check(UnicodeDecodeError("", b"", 0, 0, ""), size('6Pb 2P2nP'))
1602+
check(UnicodeDecodeError("", b"", 0, 0, ""), size('3Pq3Pb 2P2nP'))
16031603
# UnicodeTranslateError
1604-
check(UnicodeTranslateError("", 0, 1, ""), size('6Pb 2P2nP'))
1604+
check(UnicodeTranslateError("", 0, 1, ""), size('3Pq3Pb 2P2nP'))
16051605
# ellipses
16061606
check(Ellipsis, size(''))
16071607
# EncodingMap

Lib/traceback.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import collections.abc
44
import itertools
55
import linecache
6+
import os
67
import sys
78
import textwrap
89
import warnings
@@ -17,6 +18,10 @@
1718
'FrameSummary', 'StackSummary', 'TracebackException',
1819
'walk_stack', 'walk_tb', 'print_list']
1920

21+
22+
ENABLE_TRACEBACK_TIMESTAMPS = os.environb.get(b"PYTHON_TRACEBACK_TIMESTAMPS", b"") == b"1"
23+
24+
2025
#
2126
# Formatting and printing lists of traceback lines.
2227
#
@@ -182,7 +187,7 @@ def format_exception_only(exc, /, value=_sentinel, *, show_group=False, **kwargs
182187
def _format_final_exc_line(etype, value, *, insert_final_newline=True, colorize=False, timestamp=0):
183188
valuestr = _safe_string(value, 'exception')
184189
end_char = "\n" if insert_final_newline else ""
185-
ts = f" <@t={timestamp:.6f}>" if timestamp else ""
190+
ts = f" <@T={timestamp:.6f}>" if timestamp else ""
186191
if colorize:
187192
timestamp = f"{ANSIColors.GREY}{ts}{ANSIColors.RESET}" if timestamp else ""
188193
if value is None or not valuestr:
@@ -1007,7 +1012,7 @@ class TracebackException:
10071012
- :attr:`__cause__` A TracebackException of the original *__cause__*.
10081013
- :attr:`__context__` A TracebackException of the original *__context__*.
10091014
- :attr:`__notes__` A reference to the original *__notes__* list.
1010-
- :attr:`timestamp` When the original exception was created (seconds).
1015+
- :attr:`_timestamp` When the exception was created (seconds), if enabled.
10111016
- :attr:`exceptions` For exception groups - a list of TracebackException
10121017
instances for the nested *exceptions*. ``None`` for other exceptions.
10131018
- :attr:`__suppress_context__` The *__suppress_context__* value from the
@@ -1061,7 +1066,10 @@ def __init__(self, exc_type, exc_value, exc_traceback, *, limit=None,
10611066
self.__notes__ = [
10621067
f'Ignored error getting __notes__: {_safe_string(e, '__notes__', repr)}']
10631068

1064-
self.timestamp = exc_value.__timestamp_ns__ / 1_000_000_000
1069+
if ENABLE_TRACEBACK_TIMESTAMPS:
1070+
self._timestamp = exc_value.__timestamp_ns__ / 1_000_000_000
1071+
else:
1072+
self._timestamp = 0
10651073

10661074
self._is_syntax_error = False
10671075
self._have_exc_type = exc_type is not None
@@ -1234,22 +1242,22 @@ def format_exception_only(self, *, show_group=False, _depth=0, **kwargs):
12341242

12351243
indent = 3 * _depth * ' '
12361244
if not self._have_exc_type:
1237-
yield indent + _format_final_exc_line(None, self._str, colorize=colorize, timestamp=self.timestamp)
1245+
yield indent + _format_final_exc_line(None, self._str, colorize=colorize, timestamp=self._timestamp)
12381246
return
12391247

12401248
stype = self.exc_type_str
12411249
if not self._is_syntax_error:
12421250
if _depth > 0:
12431251
# Nested exceptions needs correct handling of multiline messages.
12441252
formatted = _format_final_exc_line(
1245-
stype, self._str, insert_final_newline=False, colorize=colorize, timestamp=self.timestamp
1253+
stype, self._str, insert_final_newline=False, colorize=colorize, timestamp=self._timestamp
12461254
).split('\n')
12471255
yield from [
12481256
indent + l + '\n'
12491257
for l in formatted
12501258
]
12511259
else:
1252-
yield _format_final_exc_line(stype, self._str, colorize=colorize, timestamp=self.timestamp)
1260+
yield _format_final_exc_line(stype, self._str, colorize=colorize, timestamp=self._timestamp)
12531261
else:
12541262
yield from [indent + l for l in self._format_syntax_error(stype, colorize=colorize)]
12551263

Objects/exceptions.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,18 @@ BaseException_repr(PyBaseExceptionObject *self)
186186
PyObject *res;
187187
Py_BEGIN_CRITICAL_SECTION(self);
188188
const char *name = _PyType_Name(Py_TYPE(self));
189+
// TODO: check the env var at startup and control timestamp inclusion here.
189190
if (PyTuple_GET_SIZE(self->args) == 1) {
190-
res = PyUnicode_FromFormat("%s(%R) [@t=%lldns]", name,
191-
PyTuple_GET_ITEM(self->args, 0),
192-
self->timestamp_ns);
191+
// res = PyUnicode_FromFormat("%s(%R) [@t=%lldns]", name,
192+
// PyTuple_GET_ITEM(self->args, 0),
193+
// self->timestamp_ns);
194+
res = PyUnicode_FromFormat("%s(%R)", name,
195+
PyTuple_GET_ITEM(self->args, 0));
193196
}
194197
else {
195-
res = PyUnicode_FromFormat("%s%R [@t=%lldns]", name, self->args,
196-
self->timestamp_ns);
198+
// res = PyUnicode_FromFormat("%s%R [@t=%lldns]", name, self->args,
199+
// self->timestamp_ns);
200+
res = PyUnicode_FromFormat("%s%R", name, self->args);
197201
}
198202
Py_END_CRITICAL_SECTION();
199203
return res;

0 commit comments

Comments
 (0)