From acc65fcc6119087560306f671895ba0ba87917f7 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 1 Dec 2026 16:05:12 +0100 Subject: [PATCH 1/4] gh-67795: Rewrite pytime_from_pyfloat() Convert the value to a number of nanoseconds using integers (PyTime_t), instead of doing the computation using doubles to avoid loss of precision. --- Lib/test/test_time.py | 8 +++++++- Python/pytime.c | 37 +++++++++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py index 7d838842a16418..cedb91601097f4 100644 --- a/Lib/test/test_time.py +++ b/Lib/test/test_time.py @@ -1141,9 +1141,15 @@ def test_FromSecondsObject(self): _PyTime_FromSecondsObject, lambda secs: secs * SEC_TO_NS) + def seconds_to_time_t(seconds): + floatpart, intpart = math.modf(seconds) + t = int(intpart) * 10 ** 9 + ns = self.decimal_round(floatpart * float(10**9)) + return t + ns + self.check_float_rounding( _PyTime_FromSecondsObject, - lambda ns: self.decimal_round(ns * SEC_TO_NS)) + seconds_to_time_t) self.check_ratio_rounding( _PyTime_FromSecondsObject, diff --git a/Python/pytime.c b/Python/pytime.c index 6b0efc3101fcda..13dd393b5d6e9a 100644 --- a/Python/pytime.c +++ b/Python/pytime.c @@ -917,23 +917,48 @@ pytime_from_pyfloat(PyTime_t *tp, PyObject *obj, _PyTime_round_t round, { double value = PyFloat_AsDouble(obj); if (value == -1 && PyErr_Occurred()) { + *tp = 0; return -1; } if (isnan(value)) { PyErr_SetString(PyExc_ValueError, "Invalid value NaN (not a number)"); + *tp = 0; return -1; } + double intpart; /* volatile avoids optimization changing how numbers are rounded */ - volatile double d; + volatile double floatpart; - /* convert to a number of nanoseconds */ - d = value; - d *= (double)unit_to_ns; - d = pytime_round(d, round); + // Multiply by unit_to_ns using integers (PyTime_t), instead of double + // to avoid loss of precision. + floatpart = modf(value, &intpart); + PyTime_t t; + if (pytime_from_double_cast(&t, intpart) < 0) { + *tp = 0; + return -1; // overflow + } + if (pytime_mul(&t, unit_to_ns) < 0) { + pytime_overflow(); + return -1; + } + + floatpart *= unit_to_ns; + floatpart = pytime_round(floatpart, round); + // It's safe to cast floatpart to PyTime_t, since its value is in + // the range [-unit_to_ns; unit_to_ns], where unit_to_ns maximum + // is SEC_TO_NS (for _PyTime_FromSecondsObject()). + assert(-unit_to_ns <= floatpart && floatpart <= unit_to_ns); - return pytime_from_double_cast(tp, d); + PyTime_t ns = (PyTime_t)floatpart; + if (pytime_add(&t, ns) < 0) { + pytime_overflow(); + return -1; + } + + *tp = t; + return 0; } From f2e505bf7ecc8f86cb3ab585e25fc23e530a1036 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 1 Dec 2026 18:44:08 +0100 Subject: [PATCH 2/4] Address Stan's review --- Lib/test/test_time.py | 2 +- Modules/_testinternalcapi/pytime.c | 2 +- Python/pytime.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py index cedb91601097f4..e045dcd3fdc3e1 100644 --- a/Lib/test/test_time.py +++ b/Lib/test/test_time.py @@ -1223,7 +1223,7 @@ def seconds_filter(secs): @unittest.skipUnless(hasattr(_testinternalcapi, '_PyTime_AsTimespec'), 'need _testinternalcapi._PyTime_AsTimespec') def test_AsTimespec(self): - # Test_PyTime_AsTimespec() + # Test _PyTime_AsTimespec() from _testinternalcapi import _PyTime_AsTimespec def timespec_converter(ns): diff --git a/Modules/_testinternalcapi/pytime.c b/Modules/_testinternalcapi/pytime.c index 4f2d1909e1ebee..49218c2ca1b705 100644 --- a/Modules/_testinternalcapi/pytime.c +++ b/Modules/_testinternalcapi/pytime.c @@ -82,7 +82,7 @@ test_PyTime_AsTimeval(PyObject *self, PyObject *args) return Py_BuildValue("Nl", seconds, (long)tv.tv_usec); } -// Test _PyTime_AsTimeval_clamup() +// Test _PyTime_AsTimeval_clamp() static PyObject * test_PyTime_AsTimeval_clamp(PyObject *self, PyObject *args) { diff --git a/Python/pytime.c b/Python/pytime.c index 13dd393b5d6e9a..07c253856decdb 100644 --- a/Python/pytime.c +++ b/Python/pytime.c @@ -442,7 +442,7 @@ pytime_double_to_time_t(time_t *sec, double value) // Convert C double to C PyTime_t. -// Set *sec and return 0 on success. +// Set *tp and return 0 on success. // Set OverflowError and return -1 on overflow. // // See also pytime_double_to_time_t(). From ffb8b2283f19ac316a07a6215f4853948332c2f3 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 1 Dec 2026 18:45:26 +0100 Subject: [PATCH 3/4] pytime_double_to_time_t(): remove comment on NaN The argument cannot be NaN. --- Python/pytime.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/Python/pytime.c b/Python/pytime.c index 07c253856decdb..e868c4b360d966 100644 --- a/Python/pytime.c +++ b/Python/pytime.c @@ -426,9 +426,6 @@ pytime_double_to_time_t(time_t *sec, double value) assumption that time_t is a two's complement integer type with no trap representation, and that `PY_TIME_T_MIN` is within the representable range of a C double. - - Note: we want the `if` condition below to be true for NaNs; therefore, - resist any temptation to simplify by applying De Morgan's laws. */ const double time_t_min = (double)PY_TIME_T_MIN; if (!(time_t_min <= value && value < -time_t_min)) { From a577844c1125026f3c6003bc8f6759cac0068ea0 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 3 Dec 2026 00:39:28 +0100 Subject: [PATCH 4/4] seconds_to_time_t => seconds_to_pytime --- Lib/test/test_time.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py index e045dcd3fdc3e1..684f4774d991d7 100644 --- a/Lib/test/test_time.py +++ b/Lib/test/test_time.py @@ -1141,7 +1141,7 @@ def test_FromSecondsObject(self): _PyTime_FromSecondsObject, lambda secs: secs * SEC_TO_NS) - def seconds_to_time_t(seconds): + def seconds_to_pytime(seconds): floatpart, intpart = math.modf(seconds) t = int(intpart) * 10 ** 9 ns = self.decimal_round(floatpart * float(10**9)) @@ -1149,7 +1149,7 @@ def seconds_to_time_t(seconds): self.check_float_rounding( _PyTime_FromSecondsObject, - seconds_to_time_t) + seconds_to_pytime) self.check_ratio_rounding( _PyTime_FromSecondsObject,