From a69b7407e337c2b3be0ed61567b4883f589997fc Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 1 Dec 2026 02:50:52 +0100 Subject: [PATCH] gh-67795: Remove _PyTime_FromSecondsDouble() function os.timerfd_settime() now raises ValueError instead of OverflowError if initial or interval is not-a-number (NaN). * os.timerfd_settime() now uses _PyTime_FromSecondsObject() instead of _PyTime_FromSecondsDouble(): it is more precise for fractions.Fraction and decimal.Decimal types. * Remove internal function _PyTime_FromSecondsDouble(): it's no longer used. --- Include/internal/pycore_time.h | 6 ------ Lib/test/test_os/test_os.py | 9 +++++++++ Modules/clinic/posixmodule.c.h | 32 +++++++------------------------- Modules/posixmodule.c | 26 +++++++++++++++----------- Python/pytime.c | 7 ------- 5 files changed, 31 insertions(+), 49 deletions(-) diff --git a/Include/internal/pycore_time.h b/Include/internal/pycore_time.h index 12940cdce8abbd..c84111b7972056 100644 --- a/Include/internal/pycore_time.h +++ b/Include/internal/pycore_time.h @@ -132,12 +132,6 @@ PyAPI_FUNC(int) _PyTime_ObjectToTimespec( // Export for '_socket' shared extension. PyAPI_FUNC(PyTime_t) _PyTime_FromSeconds(int seconds); -// Create a timestamp from a number of seconds in double. -extern int _PyTime_FromSecondsDouble( - double seconds, - _PyTime_round_t round, - PyTime_t *result); - // Macro to create a timestamp from a number of seconds, no integer overflow. // Only use the macro for small values, prefer _PyTime_FromSeconds(). #define _PYTIME_FROMSECONDS(seconds) \ diff --git a/Lib/test/test_os/test_os.py b/Lib/test/test_os/test_os.py index 4c80285a50a5f7..72266a281e4183 100644 --- a/Lib/test/test_os/test_os.py +++ b/Lib/test/test_os/test_os.py @@ -4555,6 +4555,15 @@ def test_timerfd_ns_select(self): total_time_ns = initial_expiration_ns + interval_ns * (count - 1) self.assertGreater(t, total_time_ns - self.CLOCK_RES_NS) + def test_timerfd_settime_nan(self): + fd = self.timerfd_create(time.CLOCK_REALTIME) + + with self.assertRaises(ValueError): + os.timerfd_settime(fd, initial=float("nan")) + with self.assertRaises(ValueError): + os.timerfd_settime(fd, interval=float("nan")) + + class OSErrorTests(unittest.TestCase): def setUp(self): class Str(str): diff --git a/Modules/clinic/posixmodule.c.h b/Modules/clinic/posixmodule.c.h index c2f183f7037836..9d26ce60cb2090 100644 --- a/Modules/clinic/posixmodule.c.h +++ b/Modules/clinic/posixmodule.c.h @@ -6963,7 +6963,7 @@ PyDoc_STRVAR(os_timerfd_settime__doc__, static PyObject * os_timerfd_settime_impl(PyObject *module, int fd, int flags, - double initial_double, double interval_double); + PyObject *initial_obj, PyObject *interval_obj); static PyObject * os_timerfd_settime(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) @@ -7000,8 +7000,8 @@ os_timerfd_settime(PyObject *module, PyObject *const *args, Py_ssize_t nargs, Py Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1; int fd; int flags = 0; - double initial_double = 0.0; - double interval_double = 0.0; + PyObject *initial_obj = NULL; + PyObject *interval_obj = NULL; args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, /*minpos*/ 1, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf); @@ -7025,32 +7025,14 @@ os_timerfd_settime(PyObject *module, PyObject *const *args, Py_ssize_t nargs, Py } } if (args[2]) { - if (PyFloat_CheckExact(args[2])) { - initial_double = PyFloat_AS_DOUBLE(args[2]); - } - else - { - initial_double = PyFloat_AsDouble(args[2]); - if (initial_double == -1.0 && PyErr_Occurred()) { - goto exit; - } - } + initial_obj = args[2]; if (!--noptargs) { goto skip_optional_kwonly; } } - if (PyFloat_CheckExact(args[3])) { - interval_double = PyFloat_AS_DOUBLE(args[3]); - } - else - { - interval_double = PyFloat_AsDouble(args[3]); - if (interval_double == -1.0 && PyErr_Occurred()) { - goto exit; - } - } + interval_obj = args[3]; skip_optional_kwonly: - return_value = os_timerfd_settime_impl(module, fd, flags, initial_double, interval_double); + return_value = os_timerfd_settime_impl(module, fd, flags, initial_obj, interval_obj); exit: return return_value; @@ -13763,4 +13745,4 @@ os__emscripten_log(PyObject *module, PyObject *const *args, Py_ssize_t nargs, Py #ifndef OS__EMSCRIPTEN_LOG_METHODDEF #define OS__EMSCRIPTEN_LOG_METHODDEF #endif /* !defined(OS__EMSCRIPTEN_LOG_METHODDEF) */ -/*[clinic end generated code: output=077c57e5f93bd790 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=5a277059c453f1d1 input=a9049054013a1b77]*/ diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 619154309b40e3..78a6ed7804e1dc 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -11560,9 +11560,9 @@ os.timerfd_settime * flags: int = 0 0 or a bit mask of TFD_TIMER_ABSTIME or TFD_TIMER_CANCEL_ON_SET. - initial as initial_double: double = 0.0 + initial as initial_obj: object(c_default="NULL") = 0.0 The initial expiration time, in seconds. - interval as interval_double: double = 0.0 + interval as interval_obj: object(c_default="NULL") = 0.0 The timer's interval, in seconds. Alter a timer file descriptor's internal timer in seconds. @@ -11570,17 +11570,21 @@ Alter a timer file descriptor's internal timer in seconds. static PyObject * os_timerfd_settime_impl(PyObject *module, int fd, int flags, - double initial_double, double interval_double) -/*[clinic end generated code: output=df4c1bce6859224e input=81d2c0d7e936e8a7]*/ + PyObject *initial_obj, PyObject *interval_obj) +/*[clinic end generated code: output=1b72baf09ba2c79c input=48be44fd2372a8b2]*/ { - PyTime_t initial, interval; - if (_PyTime_FromSecondsDouble(initial_double, _PyTime_ROUND_FLOOR, - &initial) < 0) { - return NULL; + PyTime_t initial = 0, interval = 0; + if (initial_obj != NULL) { + if (_PyTime_FromSecondsObject(&initial, initial_obj, + _PyTime_ROUND_FLOOR) < 0) { + return NULL; + } } - if (_PyTime_FromSecondsDouble(interval_double, _PyTime_ROUND_FLOOR, - &interval) < 0) { - return NULL; + if (interval_obj != NULL) { + if (_PyTime_FromSecondsObject(&interval, interval_obj, + _PyTime_ROUND_FLOOR) < 0) { + return NULL; + } } struct itimerspec new_value, old_value; diff --git a/Python/pytime.c b/Python/pytime.c index d0f6c3372846be..9a28643a19cc7f 100644 --- a/Python/pytime.c +++ b/Python/pytime.c @@ -1038,13 +1038,6 @@ PyTime_AsSecondsDouble(PyTime_t ns) } -int -_PyTime_FromSecondsDouble(double seconds, _PyTime_round_t round, PyTime_t *result) -{ - return pytime_from_double(result, seconds, round, SEC_TO_NS); -} - - static PyTime_t pytime_divide_round_up(const PyTime_t t, const PyTime_t k) {