Skip to content

Commit a164ed6

Browse files
authored
logging: avoid rounding microsecond to 1_000_000 (#11861)
Rounding microsecond might cause it to reach `1_000_000`, which raises a TypeError.
1 parent ac2cd72 commit a164ed6

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

changelog/11861.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Avoid microsecond exceeds ``1_000_000`` when using ``log-date-format`` with ``%f`` specifier, which might cause the test suite to crash.

src/_pytest/logging.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ def formatTime(self, record: LogRecord, datefmt: Optional[str] = None) -> str:
7171
tz = timezone(timedelta(seconds=ct.tm_gmtoff), ct.tm_zone)
7272
# Construct `datetime.datetime` object from `struct_time`
7373
# and msecs information from `record`
74-
dt = datetime(*ct[0:6], microsecond=round(record.msecs * 1000), tzinfo=tz)
74+
# Using int() instead of round() to avoid it exceeding 1_000_000 and causing a ValueError (#11861).
75+
dt = datetime(*ct[0:6], microsecond=int(record.msecs * 1000), tzinfo=tz)
7576
return dt.strftime(datefmt)
7677
# Use `logging.Formatter` for non-microsecond formats
7778
return super().formatTime(record, datefmt)

0 commit comments

Comments
 (0)