Skip to content

Commit

Permalink
pythongh-110850: Use public PyTime functions
Browse files Browse the repository at this point in the history
Replace private _PyTime functions with public PyTime functions.

random_seed_time_pid() now reports errors to its caller.
  • Loading branch information
vstinner committed Feb 20, 2024
1 parent 52d1477 commit b1c5328
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 37 deletions.
6 changes: 5 additions & 1 deletion Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
18 changes: 12 additions & 6 deletions Modules/_randommodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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);

Expand All @@ -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
Expand All @@ -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;
}
Expand Down
36 changes: 6 additions & 30 deletions Modules/timemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit b1c5328

Please sign in to comment.