Skip to content

Commit b1c5328

Browse files
committed
gh-110850: Use public PyTime functions
Replace private _PyTime functions with public PyTime functions. random_seed_time_pid() now reports errors to its caller.
1 parent 52d1477 commit b1c5328

File tree

3 files changed

+23
-37
lines changed

3 files changed

+23
-37
lines changed

Modules/_datetimemodule.c

+5-1
Original file line numberDiff line numberDiff line change
@@ -5133,7 +5133,11 @@ datetime_from_timestamp(PyObject *cls, TM_FUNC f, PyObject *timestamp,
51335133
static PyObject *
51345134
datetime_best_possible(PyObject *cls, TM_FUNC f, PyObject *tzinfo)
51355135
{
5136-
PyTime_t ts = _PyTime_TimeUnchecked();
5136+
PyTime_t ts;
5137+
if (PyTime_Time(&ts) < 0) {
5138+
return NULL;
5139+
}
5140+
51375141
time_t secs;
51385142
int us;
51395143

Modules/_randommodule.c

+12-6
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@
7575
#include "pycore_modsupport.h" // _PyArg_NoKeywords()
7676
#include "pycore_moduleobject.h" // _PyModule_GetState()
7777
#include "pycore_pylifecycle.h" // _PyOS_URandomNonblock()
78-
#include "pycore_time.h" // _PyTime_TimeUnchecked()
7978

8079
#ifdef HAVE_UNISTD_H
8180
# include <unistd.h> // getpid()
@@ -260,13 +259,15 @@ random_seed_urandom(RandomObject *self)
260259
return 0;
261260
}
262261

263-
static void
262+
static int
264263
random_seed_time_pid(RandomObject *self)
265264
{
266265
PyTime_t now;
267-
uint32_t key[5];
266+
if (PyTime_Time(&now) < 0) {
267+
return -1;
268+
}
268269

269-
now = _PyTime_TimeUnchecked();
270+
uint32_t key[5];
270271
key[0] = (uint32_t)(now & 0xffffffffU);
271272
key[1] = (uint32_t)(now >> 32);
272273

@@ -278,11 +279,14 @@ random_seed_time_pid(RandomObject *self)
278279
key[2] = 0;
279280
#endif
280281

281-
now = _PyTime_MonotonicUnchecked();
282+
if (PyTime_Monotonic(&now) < 0) {
283+
return -1;
284+
}
282285
key[3] = (uint32_t)(now & 0xffffffffU);
283286
key[4] = (uint32_t)(now >> 32);
284287

285288
init_by_array(self, key, Py_ARRAY_LENGTH(key));
289+
return 0;
286290
}
287291

288292
static int
@@ -300,7 +304,9 @@ random_seed(RandomObject *self, PyObject *arg)
300304

301305
/* Reading system entropy failed, fall back on the worst entropy:
302306
use the current time and process identifier. */
303-
random_seed_time_pid(self);
307+
if (random_seed_time_pid(self) < 0) {
308+
return -1;
309+
}
304310
}
305311
return 0;
306312
}

Modules/timemodule.c

+6-30
Original file line numberDiff line numberDiff line change
@@ -103,19 +103,11 @@ _PyFloat_FromPyTime(PyTime_t t)
103103
}
104104

105105

106-
static int
107-
get_system_time(PyTime_t *t)
108-
{
109-
// Avoid _PyTime_TimeUnchecked() which silently ignores errors.
110-
return _PyTime_TimeWithInfo(t, NULL);
111-
}
112-
113-
114106
static PyObject *
115107
time_time(PyObject *self, PyObject *unused)
116108
{
117109
PyTime_t t;
118-
if (get_system_time(&t) < 0) {
110+
if (PyTime_Time(&t) < 0) {
119111
return NULL;
120112
}
121113
return _PyFloat_FromPyTime(t);
@@ -132,7 +124,7 @@ static PyObject *
132124
time_time_ns(PyObject *self, PyObject *unused)
133125
{
134126
PyTime_t t;
135-
if (get_system_time(&t) < 0) {
127+
if (PyTime_Time(&t) < 0) {
136128
return NULL;
137129
}
138130
return _PyTime_AsNanosecondsObject(t);
@@ -1156,19 +1148,11 @@ should not be relied on.");
11561148
#endif /* HAVE_WORKING_TZSET */
11571149

11581150

1159-
static int
1160-
get_monotonic(PyTime_t *t)
1161-
{
1162-
// Avoid _PyTime_MonotonicUnchecked() which silently ignores errors.
1163-
return _PyTime_MonotonicWithInfo(t, NULL);
1164-
}
1165-
1166-
11671151
static PyObject *
11681152
time_monotonic(PyObject *self, PyObject *unused)
11691153
{
11701154
PyTime_t t;
1171-
if (get_monotonic(&t) < 0) {
1155+
if (PyTime_Monotonic(&t) < 0) {
11721156
return NULL;
11731157
}
11741158
return _PyFloat_FromPyTime(t);
@@ -1183,7 +1167,7 @@ static PyObject *
11831167
time_monotonic_ns(PyObject *self, PyObject *unused)
11841168
{
11851169
PyTime_t t;
1186-
if (get_monotonic(&t) < 0) {
1170+
if (PyTime_Monotonic(&t) < 0) {
11871171
return NULL;
11881172
}
11891173
return _PyTime_AsNanosecondsObject(t);
@@ -1195,19 +1179,11 @@ PyDoc_STRVAR(monotonic_ns_doc,
11951179
Monotonic clock, cannot go backward, as nanoseconds.");
11961180

11971181

1198-
static int
1199-
get_perf_counter(PyTime_t *t)
1200-
{
1201-
// Avoid _PyTime_PerfCounterUnchecked() which silently ignores errors.
1202-
return _PyTime_PerfCounterWithInfo(t, NULL);
1203-
}
1204-
1205-
12061182
static PyObject *
12071183
time_perf_counter(PyObject *self, PyObject *unused)
12081184
{
12091185
PyTime_t t;
1210-
if (get_perf_counter(&t) < 0) {
1186+
if (PyTime_PerfCounter(&t) < 0) {
12111187
return NULL;
12121188
}
12131189
return _PyFloat_FromPyTime(t);
@@ -1223,7 +1199,7 @@ static PyObject *
12231199
time_perf_counter_ns(PyObject *self, PyObject *unused)
12241200
{
12251201
PyTime_t t;
1226-
if (get_perf_counter(&t) < 0) {
1202+
if (PyTime_PerfCounter(&t) < 0) {
12271203
return NULL;
12281204
}
12291205
return _PyTime_AsNanosecondsObject(t);

0 commit comments

Comments
 (0)