https://github.com/python/cpython/commit/145bc2d638370cb6d3da361c6dc05c5bc29f0d11
commit: 145bc2d638370cb6d3da361c6dc05c5bc29f0d11
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2024-02-20T23:31:30Z
summary:

gh-110850: Use public PyTime functions (#115746)

Replace private _PyTime functions with public PyTime functions.

random_seed_time_pid() now reports errors to its caller.

files:
M Include/internal/pycore_time.h
M Modules/_datetimemodule.c
M Modules/_queuemodule.c
M Modules/_randommodule.c
M Modules/_testinternalcapi/pytime.c
M Modules/_testsinglephase.c
M Modules/_threadmodule.c
M Modules/selectmodule.c
M Modules/socketmodule.c
M Modules/timemodule.c
M Python/gc.c
M Python/gc_free_threading.c

diff --git a/Include/internal/pycore_time.h b/Include/internal/pycore_time.h
index 9692bbc89711a5..682aee2170bdae 100644
--- a/Include/internal/pycore_time.h
+++ b/Include/internal/pycore_time.h
@@ -321,7 +321,6 @@ extern int _PyTime_PerfCounterWithInfo(
 // Alias for backward compatibility
 #define _PyTime_MIN PyTime_MIN
 #define _PyTime_MAX PyTime_MAX
-#define _PyTime_AsSecondsDouble PyTime_AsSecondsDouble
 
 
 // --- _PyDeadline -----------------------------------------------------------
diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c
index 9fd59c34de6cef..a626bda2ea9be9 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/_queuemodule.c b/Modules/_queuemodule.c
index 5ef1cea24dce68..5db9b645849fcd 100644
--- a/Modules/_queuemodule.c
+++ b/Modules/_queuemodule.c
@@ -6,7 +6,7 @@
 #include "pycore_ceval.h"         // Py_MakePendingCalls()
 #include "pycore_moduleobject.h"  // _PyModule_GetState()
 #include "pycore_parking_lot.h"
-#include "pycore_time.h"          // PyTime_t
+#include "pycore_time.h"          // _PyTime_FromSecondsObject()
 
 #include <stdbool.h>
 #include <stddef.h>               // offsetof()
diff --git a/Modules/_randommodule.c b/Modules/_randommodule.c
index 920645b453536a..56b891dfe0f85f 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 <unistd.h>             // 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/_testinternalcapi/pytime.c 
b/Modules/_testinternalcapi/pytime.c
index 11a02413b8c114..2abe5c2b725713 100644
--- a/Modules/_testinternalcapi/pytime.c
+++ b/Modules/_testinternalcapi/pytime.c
@@ -2,10 +2,10 @@
 
 #include "parts.h"
 
-#include "pycore_time.h"
+#include "pycore_time.h"          // _PyTime_FromSeconds()
 
 #ifdef MS_WINDOWS
-#  include <winsock2.h>          // struct timeval
+#  include <winsock2.h>           // struct timeval
 #endif
 
 
diff --git a/Modules/_testsinglephase.c b/Modules/_testsinglephase.c
index 58d22e2d5dbe56..092673a9ea43e1 100644
--- a/Modules/_testsinglephase.c
+++ b/Modules/_testsinglephase.c
@@ -8,7 +8,6 @@
 //#include <time.h>
 #include "Python.h"
 #include "pycore_namespace.h"     // _PyNamespace_New()
-#include "pycore_time.h"          // PyTime_t
 
 
 typedef struct {
@@ -71,13 +70,13 @@ _set_initialized(PyTime_t *initialized)
 {
     /* We go strictly monotonic to ensure each time is unique. */
     PyTime_t prev;
-    if (_PyTime_MonotonicWithInfo(&prev, NULL) != 0) {
+    if (PyTime_Monotonic(&prev) != 0) {
         return -1;
     }
     /* We do a busy sleep since the interval should be super short. */
     PyTime_t t;
     do {
-        if (_PyTime_MonotonicWithInfo(&t, NULL) != 0) {
+        if (PyTime_Monotonic(&t) != 0) {
             return -1;
         }
     } while (t == prev);
@@ -136,7 +135,7 @@ init_module(PyObject *module, module_state *state)
         return -1;
     }
 
-    double d = _PyTime_AsSecondsDouble(state->initialized);
+    double d = PyTime_AsSecondsDouble(state->initialized);
     if (PyModule_Add(module, "_module_initialized", PyFloat_FromDouble(d)) < 
0) {
         return -1;
     }
@@ -157,7 +156,7 @@ common_state_initialized(PyObject *self, PyObject 
*Py_UNUSED(ignored))
     if (state == NULL) {
         Py_RETURN_NONE;
     }
-    double d = _PyTime_AsSecondsDouble(state->initialized);
+    double d = PyTime_AsSecondsDouble(state->initialized);
     return PyFloat_FromDouble(d);
 }
 
diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c
index addaafb4f86039..25e10027879055 100644
--- a/Modules/_threadmodule.c
+++ b/Modules/_threadmodule.c
@@ -1948,7 +1948,7 @@ thread_module_exec(PyObject *module)
 
     // TIMEOUT_MAX
     double timeout_max = (double)PY_TIMEOUT_MAX * 1e-6;
-    double time_max = _PyTime_AsSecondsDouble(_PyTime_MAX);
+    double time_max = PyTime_AsSecondsDouble(_PyTime_MAX);
     timeout_max = Py_MIN(timeout_max, time_max);
     // Round towards minus infinity
     timeout_max = floor(timeout_max);
diff --git a/Modules/selectmodule.c b/Modules/selectmodule.c
index 57d55a5611f121..f16173aafa7d3c 100644
--- a/Modules/selectmodule.c
+++ b/Modules/selectmodule.c
@@ -15,7 +15,7 @@
 #include "Python.h"
 #include "pycore_fileutils.h"     // _Py_set_inheritable()
 #include "pycore_import.h"        // _PyImport_GetModuleAttrString()
-#include "pycore_time.h"          // PyTime_t
+#include "pycore_time.h"          // _PyTime_FromSecondsObject()
 
 #include <stdbool.h>
 #include <stddef.h>               // offsetof()
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index 836cf6c05b3196..cd9a803648be71 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -3113,7 +3113,7 @@ sock_gettimeout(PySocketSockObject *s, PyObject 
*Py_UNUSED(ignored))
         Py_RETURN_NONE;
     }
     else {
-        double seconds = _PyTime_AsSecondsDouble(s->sock_timeout);
+        double seconds = PyTime_AsSecondsDouble(s->sock_timeout);
         return PyFloat_FromDouble(seconds);
     }
 }
@@ -6917,7 +6917,7 @@ socket_getdefaulttimeout(PyObject *self, PyObject 
*Py_UNUSED(ignored))
         Py_RETURN_NONE;
     }
     else {
-        double seconds = _PyTime_AsSecondsDouble(state->defaulttimeout);
+        double seconds = PyTime_AsSecondsDouble(state->defaulttimeout);
         return PyFloat_FromDouble(seconds);
     }
 }
diff --git a/Modules/timemodule.c b/Modules/timemodule.c
index 28dba903d2b9e8..ac96ed40a9bb27 100644
--- a/Modules/timemodule.c
+++ b/Modules/timemodule.c
@@ -98,24 +98,16 @@ get_time_state(PyObject *module)
 static PyObject*
 _PyFloat_FromPyTime(PyTime_t t)
 {
-    double d = _PyTime_AsSecondsDouble(t);
+    double d = PyTime_AsSecondsDouble(t);
     return PyFloat_FromDouble(d);
 }
 
 
-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);
@@ -2190,7 +2166,7 @@ pysleep(PyTime_t timeout)
     PyTime_t deadline, monotonic;
     int err = 0;
 
-    if (get_monotonic(&monotonic) < 0) {
+    if (PyTime_Monotonic(&monotonic) < 0) {
         return -1;
     }
     deadline = monotonic + timeout;
@@ -2243,7 +2219,7 @@ pysleep(PyTime_t timeout)
         }
 
 #ifndef HAVE_CLOCK_NANOSLEEP
-        if (get_monotonic(&monotonic) < 0) {
+        if (PyTime_Monotonic(&monotonic) < 0) {
             return -1;
         }
         timeout = deadline - monotonic;
diff --git a/Python/gc.c b/Python/gc.c
index a031897d235dea..ea3b596d1713df 100644
--- a/Python/gc.c
+++ b/Python/gc.c
@@ -1428,7 +1428,7 @@ gc_collect_main(PyThreadState *tstate, int generation, 
_PyGC_Reason reason)
             debug_cycle("uncollectable", FROM_GC(gc));
     }
     if (gcstate->debug & _PyGC_DEBUG_STATS) {
-        double d = _PyTime_AsSecondsDouble(_PyTime_PerfCounterUnchecked() - 
t1);
+        double d = PyTime_AsSecondsDouble(_PyTime_PerfCounterUnchecked() - t1);
         PySys_WriteStderr(
             "gc: done, %zd unreachable, %zd uncollectable, %.4fs elapsed\n",
             n+m, n, d);
diff --git a/Python/gc_free_threading.c b/Python/gc_free_threading.c
index 4d886ee369db11..14790899825de1 100644
--- a/Python/gc_free_threading.c
+++ b/Python/gc_free_threading.c
@@ -1136,7 +1136,7 @@ gc_collect_main(PyThreadState *tstate, int generation, 
_PyGC_Reason reason)
     n = state.uncollectable;
 
     if (gcstate->debug & _PyGC_DEBUG_STATS) {
-        double d = _PyTime_AsSecondsDouble(_PyTime_PerfCounterUnchecked() - 
t1);
+        double d = PyTime_AsSecondsDouble(_PyTime_PerfCounterUnchecked() - t1);
         PySys_WriteStderr(
             "gc: done, %zd unreachable, %zd uncollectable, %.4fs elapsed\n",
             n+m, n, d);

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to