10000 gh-110850: Use public PyTime functions (#115746) · python/cpython@145bc2d · GitHub
[go: up one dir, main page]

Skip to content

Commit 145bc2d

Browse files
authored
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.
1 parent 52d1477 commit 145bc2d

File tree

12 files changed

+39
-55
lines changed

12 files changed

+39
-55
lines changed

Include/internal/pycore_time.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,6 @@ extern int _PyTime_PerfCounterWithInfo(
321321
// Alias for backward compatibility
322322
#define _PyTime_MIN PyTime_MIN
323323
#define _PyTime_MAX PyTime_MAX
324-
#define _PyTime_AsSecondsDouble PyTime_AsSecondsDouble
325324

326325

327326
// --- _PyDeadline -----------------------------------------------------------

Modules/_datetimemodule.c

Lines changed: 5 additions & 1 deletion
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/_queuemodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "pycore_ceval.h" // Py_MakePendingCalls()
77
#include "pycore_moduleobject.h" // _PyModule_GetState()
88
#include "pycore_parking_lot.h"
9-
#include "pycore_time.h" // PyTime_t
9+
#include "pycore_time.h" // _PyTime_FromSecondsObject()
1010

1111
#include <stdbool.h>
1212
#include <stddef.h> // offsetof()

Modules/_randommodule.c

Lines changed: 12 additions & 6 deletions
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/_testinternalcapi/pytime.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
#include "parts.h"
44

5-
#include "pycore_time.h"
5+
#include "pycore_time.h" // _PyTime_FromSeconds()
66

77
#ifdef MS_WINDOWS
8-
# include <winsock2.h> // struct timeval
8+
# include <winsock2.h> // struct timeval
99
#endif
1010

1111

Modules/_testsinglephase.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
//#include <time.h>
99
#include "Python.h"
1010
#include "pycore_namespace.h" // _PyNamespace_New()
11-
#include "pycore_time.h" // PyTime_t
1211

1312

1413
typedef struct {
@@ -71,13 +70,13 @@ _set_initialized(PyTime_t *initialized)
7170
{
7271
/* We go strictly monotonic to ensure each time is unique. */
7372
PyTime_t prev;
74-
if (_PyTime_MonotonicWithInfo(&prev, NULL) != 0) {
73+
if (PyTime_Monotonic(&prev) != 0) {
7574
return -1;
7675
}
7776
/* We do a busy sleep since the interval should be super short. */
7877
PyTime_t t;
7978
do {
80-
if (_PyTime_MonotonicWithInfo(&t, NULL) != 0) {
79+
if (PyTime_Monotonic(&t) != 0) {
8180
return -1;
8281
}
8382
} while (t == prev);
@@ -136,7 +135,7 @@ init_module(PyObject *module, module_state *state)
136135
return -1;
137136
}
138137

139-
double d = _PyTime_AsSecondsDouble(state->initialized);
138+
double d = PyTime_AsSecondsDouble(state->initialized);
140139
if (PyModule_Add(module, "_module_initialized", PyFloat_FromDouble(d)) < 0) {
141140
return -1;
142141
}
@@ -157,7 +156,7 @@ common_state_initialized(PyObject *self, PyObject *Py_UNUSED(ignored))
157156
if (state == NULL) {
158157
Py_RETURN_NONE;
159158
}
160-
double d = _PyTime_AsSecondsDouble(state->initialized);
159+
double d = PyTime_AsSecondsDouble(state->initialized);
161160
return PyFloat_FromDouble(d);
162161
}
163162

Modules/_threadmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1948,7 +1948,7 @@ thread_module_exec(PyObject *module)
19481948

19491949
// TIMEOUT_MAX
19501950
double timeout_max = (double)PY_TIMEOUT_MAX * 1e-6;
1951-
double time_max = _PyTime_AsSecondsDouble(_PyTime_MAX);
1951+
double time_max = PyTime_AsSecondsDouble(_PyTime_MAX);
19521952
timeout_max = Py_MIN(timeout_max, time_max);
19531953
// Round towards minus infinity
19541954
timeout_max = floor(timeout_max);

Modules/selectmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include "Python.h"
1616
#include "pycore_fileutils.h" // _Py_set_inheritable()
1717
#include "pycore_import.h" // _PyImport_GetModuleAttrString()
18-
#include "pycore_time.h" // PyTime_t
18+
#include "pycore_time.h" // _PyTime_FromSecondsObject()
1919

2020
#include <stdbool.h>
2121< 741A div class="diff-text-inner">#include <stddef.h> // offsetof()

Modules/socketmodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3113,7 +3113,7 @@ sock_gettimeout(PySocketSockObject *s, PyObject *Py_UNUSED(ignored))
31133113
Py_RETURN_NONE;
31143114
}
31153115
else {
3116-
double seconds = _PyTime_AsSecondsDouble(s->sock_timeout);
3116+
double seconds = PyTime_AsSecondsDouble(s->sock_timeout);
31173117
return PyFloat_FromDouble(seconds);
31183118
}
31193119
}
@@ -6917,7 +6917,7 @@ socket_getdefaulttimeout(PyObject *self, PyObject *Py_UNUSED(ignored))
69176917
Py_RETURN_NONE;
69186918
}
69196919
else {
6920-
double seconds = _PyTime_AsSecondsDouble(state->defaulttimeout);
6920+
double seconds = PyTime_AsSecondsDouble(state->defaulttimeout);
69216921
return PyFloat_FromDouble(seconds);
69226922
}
69236923
}

Modules/timemodule.c

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -98,24 +98,16 @@ get_time_state(PyObject *module)
9898
static PyObject*
9999
_PyFloat_FromPyTime(PyTime_t t)
100100
{
101-
double d = _PyTime_AsSecondsDouble(t);
101+
double d = PyTime_AsSecondsDouble(t);
102102
return PyFloat_FromDouble(d);
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);
@@ -2190,7 +2166,7 @@ pysleep(PyTime_t timeout)
21902166
PyTime_t deadline, monotonic;
21912167
int err = 0;
21922168

2193-
if (get_monotonic(&monotonic) < 0) {
2169+
if (PyTime_Monotonic(&monotonic) < 0) {
21942170
return -1;
21952171
}
21962172
deadline = monotonic + timeout;
@@ -2243,7 +2219,7 @@ pysleep(PyTime_t timeout)
22432219
}
22442220

22452221
#ifndef HAVE_CLOCK_NANOSLEEP
2246-
if (get_monotonic(&monotonic) < 0) {
2222+
if (PyTime_Monotonic(&monotonic) < 0) {
22472223
return -1;
22482224
}
22492225
timeout = deadline - monotonic;

Python/gc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1428,7 +1428,7 @@ gc_collect_main(PyThreadState *tstate, int generation, _PyGC_Reason reason)
14281428
debug_cycle("uncollectable", FROM_GC(gc));
14291429
}
14301430
if (gcstate->debug & _PyGC_DEBUG_STATS) {
1431-
double d = _PyTime_AsSecondsDouble(_PyTime_PerfCounterUnchecked() - t1);
1431+
double d = PyTime_AsSecondsDouble(_PyTime_PerfCounterUnchecked() - t1);
14321432
PySys_WriteStderr(
14331433
"gc: done, %zd unreachable, %zd uncollectable, %.4fs elapsed\n",
14341434
n+m, n, d);

Python/gc_free_threading.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1136,7 +1136,7 @@ gc_collect_main(PyThreadState *tstate, int generation, _PyGC_Reason reason)
11361136
n = state.uncollectable;
11371137

11381138
if (gcstate->debug & _PyGC_DEBUG_STATS) {
1139-
double d = _PyTime_AsSecondsDouble(_PyTime_PerfCounterUnchecked() - t1);
1139+
double d = PyTime_AsSecondsDouble(_PyTime_PerfCounterUnchecked() - t1);
11401140
PySys_WriteStderr(
11411141
"gc: done, %zd unreachable, %zd uncollectable, %.4fs elapsed\n",
11421142
n+m, n, d);

0 commit comments

Comments
 (0)
0