From b1c5328be0bae9cc27bda8c7836c16bb4c5bf379 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 20 Feb 2024 18:45:21 +0100 Subject: [PATCH] gh-110850: Use public PyTime functions Replace private _PyTime functions with public PyTime functions. random_seed_time_pid() now reports errors to its caller. --- Modules/_datetimemodule.c | 6 +++++- Modules/_randommodule.c | 18 ++++++++++++------ Modules/timemodule.c | 36 ++++++------------------------------ 3 files changed, 23 insertions(+), 37 deletions(-) diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index 9fd59c34de6cefc..a626bda2ea9be9f 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -5133,7 +5133,11 @@ datetime_from_timestamp(PyObject *cls, TM_FUNC f, PyObject *timestamp, static PyObject * datetime_best_possible(PyObject *cls, TM_FUNC f, PyObject *tzinfo) { - PyTime_t ts = _PyTime_TimeUnchecked(); + PyTime_t ts; + if (PyTime_Time(&ts) < 0) { + return NULL; + } + time_t secs; int us; diff --git a/Modules/_randommodule.c b/Modules/_randommodule.c index 920645b453536ab..56b891dfe0f85f8 100644 --- a/Modules/_randommodule.c +++ b/Modules/_randommodule.c @@ -75,7 +75,6 @@ #include "pycore_modsupport.h" // _PyArg_NoKeywords() #include "pycore_moduleobject.h" // _PyModule_GetState() #include "pycore_pylifecycle.h" // _PyOS_URandomNonblock() -#include "pycore_time.h" // _PyTime_TimeUnchecked() #ifdef HAVE_UNISTD_H # include // getpid() @@ -260,13 +259,15 @@ random_seed_urandom(RandomObject *self) return 0; } -static void +static int random_seed_time_pid(RandomObject *self) { PyTime_t now; - uint32_t key[5]; + if (PyTime_Time(&now) < 0) { + return -1; + } - now = _PyTime_TimeUnchecked(); + uint32_t key[5]; key[0] = (uint32_t)(now & 0xffffffffU); key[1] = (uint32_t)(now >> 32); @@ -278,11 +279,14 @@ random_seed_time_pid(RandomObject *self) key[2] = 0; #endif - now = _PyTime_MonotonicUnchecked(); + if (PyTime_Monotonic(&now) < 0) { + return -1; + } key[3] = (uint32_t)(now & 0xffffffffU); key[4] = (uint32_t)(now >> 32); init_by_array(self, key, Py_ARRAY_LENGTH(key)); + return 0; } static int @@ -300,7 +304,9 @@ random_seed(RandomObject *self, PyObject *arg) /* Reading system entropy failed, fall back on the worst entropy: use the current time and process identifier. */ - random_seed_time_pid(self); + if (random_seed_time_pid(self) < 0) { + return -1; + } } return 0; } diff --git a/Modules/timemodule.c b/Modules/timemodule.c index 28dba903d2b9e87..b21ec89232a0d23 100644 --- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -103,19 +103,11 @@ _PyFloat_FromPyTime(PyTime_t t) } -static int -get_system_time(PyTime_t *t) -{ - // Avoid _PyTime_TimeUnchecked() which silently ignores errors. - return _PyTime_TimeWithInfo(t, NULL); -} - - static PyObject * time_time(PyObject *self, PyObject *unused) { PyTime_t t; - if (get_system_time(&t) < 0) { + if (PyTime_Time(&t) < 0) { return NULL; } return _PyFloat_FromPyTime(t); @@ -132,7 +124,7 @@ static PyObject * time_time_ns(PyObject *self, PyObject *unused) { PyTime_t t; - if (get_system_time(&t) < 0) { + if (PyTime_Time(&t) < 0) { return NULL; } return _PyTime_AsNanosecondsObject(t); @@ -1156,19 +1148,11 @@ should not be relied on."); #endif /* HAVE_WORKING_TZSET */ -static int -get_monotonic(PyTime_t *t) -{ - // Avoid _PyTime_MonotonicUnchecked() which silently ignores errors. - return _PyTime_MonotonicWithInfo(t, NULL); -} - - static PyObject * time_monotonic(PyObject *self, PyObject *unused) { PyTime_t t; - if (get_monotonic(&t) < 0) { + if (PyTime_Monotonic(&t) < 0) { return NULL; } return _PyFloat_FromPyTime(t); @@ -1183,7 +1167,7 @@ static PyObject * time_monotonic_ns(PyObject *self, PyObject *unused) { PyTime_t t; - if (get_monotonic(&t) < 0) { + if (PyTime_Monotonic(&t) < 0) { return NULL; } return _PyTime_AsNanosecondsObject(t); @@ -1195,19 +1179,11 @@ PyDoc_STRVAR(monotonic_ns_doc, Monotonic clock, cannot go backward, as nanoseconds."); -static int -get_perf_counter(PyTime_t *t) -{ - // Avoid _PyTime_PerfCounterUnchecked() which silently ignores errors. - return _PyTime_PerfCounterWithInfo(t, NULL); -} - - static PyObject * time_perf_counter(PyObject *self, PyObject *unused) { PyTime_t t; - if (get_perf_counter(&t) < 0) { + if (PyTime_PerfCounter(&t) < 0) { return NULL; } return _PyFloat_FromPyTime(t); @@ -1223,7 +1199,7 @@ static PyObject * time_perf_counter_ns(PyObject *self, PyObject *unused) { PyTime_t t; - if (get_perf_counter(&t) < 0) { + if (PyTime_PerfCounter(&t) < 0) { return NULL; } return _PyTime_AsNanosecondsObject(t);