logging: avoid rounding microsecond to `1_000_000` (#11861)

Rounding microsecond might cause it to reach `1_000_000`, which raises a TypeError.
This commit is contained in:
Dương Quốc Khánh 2024-01-27 22:40:31 +09:00 committed by GitHub
parent ac2cd72e5f
commit a164ed6400
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 3 additions and 1 deletions

View File

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

View File

@ -71,7 +71,8 @@ class DatetimeFormatter(logging.Formatter):
tz = timezone(timedelta(seconds=ct.tm_gmtoff), ct.tm_zone) tz = timezone(timedelta(seconds=ct.tm_gmtoff), ct.tm_zone)
# Construct `datetime.datetime` object from `struct_time` # Construct `datetime.datetime` object from `struct_time`
# and msecs information from `record` # and msecs information from `record`
dt = datetime(*ct[0:6], microsecond=round(record.msecs * 1000), tzinfo=tz) # Using int() instead of round() to avoid it exceeding 1_000_000 and causing a ValueError (#11861).
dt = datetime(*ct[0:6], microsecond=int(record.msecs * 1000), tzinfo=tz)
return dt.strftime(datefmt) return dt.strftime(datefmt)
# Use `logging.Formatter` for non-microsecond formats # Use `logging.Formatter` for non-microsecond formats
return super().formatTime(record, datefmt) return super().formatTime(record, datefmt)