From 608590097a6542768099dd371b84d8b37a1990da Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sat, 27 Jan 2024 16:48:01 +0000 Subject: [PATCH] [8.0.x] fix: avoid rounding microsecond to `1_000_000` (#11863) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Dương Quốc Khánh --- changelog/11861.bugfix.rst | 1 + src/_pytest/logging.py | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 changelog/11861.bugfix.rst diff --git a/changelog/11861.bugfix.rst b/changelog/11861.bugfix.rst new file mode 100644 index 000000000..a2734d799 --- /dev/null +++ b/changelog/11861.bugfix.rst @@ -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. diff --git a/src/_pytest/logging.py b/src/_pytest/logging.py index 5426c3513..d7e498d55 100644 --- a/src/_pytest/logging.py +++ b/src/_pytest/logging.py @@ -71,7 +71,8 @@ class DatetimeFormatter(logging.Formatter): tz = timezone(timedelta(seconds=ct.tm_gmtoff), ct.tm_zone) # Construct `datetime.datetime` object from `struct_time` # 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) # Use `logging.Formatter` for non-microsecond formats return super().formatTime(record, datefmt)