Skip to content

Commit 113053a

Browse files
authored
gh-110850: Fix _PyTime_FromSecondsDouble() API (#116606)
Return 0 on success. Set an exception and return -1 on error. Fix os.timerfd_settime(): properly report exceptions on _PyTime_FromSecondsDouble() failure. No longer export _PyTime_FromSecondsDouble().
1 parent 2731913 commit 113053a

File tree

4 files changed

+40
-31
lines changed

4 files changed

+40
-31
lines changed

Include/internal/pycore_time.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,10 @@ PyAPI_FUNC(int) _PyTime_ObjectToTimespec(
132132
PyAPI_FUNC(PyTime_t) _PyTime_FromSeconds(int seconds);
133133

134134
// Create a timestamp from a number of seconds in double.
135-
// Export for '_socket' shared extension.
136-
PyAPI_FUNC(PyTime_t) _PyTime_FromSecondsDouble(double seconds, _PyTime_round_t round);
135+
extern int _PyTime_FromSecondsDouble(
136+
double seconds,
137+
_PyTime_round_t round,
138+
PyTime_t *result);
137139

138140
// Macro to create a timestamp from a number of seconds, no integer overflow.
139141
// Only use the macro for small values, prefer _PyTime_FromSeconds().

Modules/clinic/posixmodule.c.h

+12-12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/posixmodule.c

+20-10
Original file line numberDiff line numberDiff line change
@@ -10483,30 +10483,40 @@ os.timerfd_settime
1048310483
*
1048410484
flags: int = 0
1048510485
0 or a bit mask of TFD_TIMER_ABSTIME or TFD_TIMER_CANCEL_ON_SET.
10486-
initial: double = 0.0
10486+
initial as initial_double: double = 0.0
1048710487
The initial expiration time, in seconds.
10488-
interval: double = 0.0
10488+
interval as interval_double: double = 0.0
1048910489
The timer's interval, in seconds.
1049010490
1049110491
Alter a timer file descriptor's internal timer in seconds.
1049210492
[clinic start generated code]*/
1049310493

1049410494
static PyObject *
10495-
os_timerfd_settime_impl(PyObject *module, int fd, int flags, double initial,
10496-
double interval)
10497-
/*[clinic end generated code: output=0dda31115317adb9 input=6c24e47e7a4d799e]*/
10495+
os_timerfd_settime_impl(PyObject *module, int fd, int flags,
10496+
double initial_double, double interval_double)
10497+
/*[clinic end generated code: output=df4c1bce6859224e input=81d2c0d7e936e8a7]*/
1049810498
{
10499-
struct itimerspec new_value;
10500-
struct itimerspec old_value;
10501-
int result;
10502-
if (_PyTime_AsTimespec(_PyTime_FromSecondsDouble(initial, _PyTime_ROUND_FLOOR), &new_value.it_value) < 0) {
10499+
PyTime_t initial, interval;
10500+
if (_PyTime_FromSecondsDouble(initial_double, _PyTime_ROUND_FLOOR,
10501+
&initial) < 0) {
10502+
return NULL;
10503+
}
10504+
if (_PyTime_FromSecondsDouble(interval_double, _PyTime_ROUND_FLOOR,
10505+
&interval) < 0) {
10506+
return NULL;
10507+
}
10508+
10509+
struct itimerspec new_value, old_value;
10510+
if (_PyTime_AsTimespec(initial, &new_value.it_value) < 0) {
1050310511
PyErr_SetString(PyExc_ValueError, "invalid initial value");
1050410512
return NULL;
1050510513
}
10506-
if (_PyTime_AsTimespec(_PyTime_FromSecondsDouble(interval, _PyTime_ROUND_FLOOR), &new_value.it_interval) < 0) {
10514+
if (_PyTime_AsTimespec(interval, &new_value.it_interval) < 0) {
1050710515
PyErr_SetString(PyExc_ValueError, "invalid interval value");
1050810516
return NULL;
1050910517
}
10518+
10519+
int result;
1051010520
Py_BEGIN_ALLOW_THREADS
1051110521
result = timerfd_settime(fd, flags, &new_value, &old_value);
1051210522
Py_END_ALLOW_THREADS

Python/pytime.c

+4-7
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,7 @@ pytime_from_double(PyTime_t *tp, double value, _PyTime_round_t round,
565565
/* See comments in pytime_double_to_denominator */
566566
if (!((double)PyTime_MIN <= d && d < -(double)PyTime_MIN)) {
567567
pytime_time_t_overflow();
568+
*tp = 0;
568569
return -1;
569570
}
570571
PyTime_t ns = (PyTime_t)d;
@@ -652,14 +653,10 @@ _PyTime_AsLong(PyTime_t ns)
652653
return PyLong_FromLongLong((long long)ns);
653654
}
654655

655-
PyTime_t
656-
_PyTime_FromSecondsDouble(double seconds, _PyTime_round_t round)
656+
int
657+
_PyTime_FromSecondsDouble(double seconds, _PyTime_round_t round, PyTime_t *result)
657658
{
658-
PyTime_t tp;
659-
if(pytime_from_double(&tp, seconds, round, SEC_TO_NS) < 0) {
660-
return -1;
661-
}
662-
return tp;
659+
return pytime_from_double(result, seconds, round, SEC_TO_NS);
663660
}
664661

665662

0 commit comments

Comments
 (0)