Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Include/internal/pycore_time.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,10 @@ PyAPI_FUNC(int) _PyTime_ObjectToTimespec(
PyAPI_FUNC(PyTime_t) _PyTime_FromSeconds(int seconds);

// Create a timestamp from a number of seconds in double.
// Export for '_socket' shared extension.
PyAPI_FUNC(PyTime_t) _PyTime_FromSecondsDouble(double seconds, _PyTime_round_t round);
extern int _PyTime_FromSecondsDouble(
double seconds,
_PyTime_round_t round,
PyTime_t *result);

// Macro to create a timestamp from a number of seconds, no integer overflow.
// Only use the macro for small values, prefer _PyTime_FromSeconds().
Expand Down
24 changes: 12 additions & 12 deletions Modules/clinic/posixmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 20 additions & 10 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -10483,30 +10483,40 @@ os.timerfd_settime
*
flags: int = 0
0 or a bit mask of TFD_TIMER_ABSTIME or TFD_TIMER_CANCEL_ON_SET.
initial: double = 0.0
initial as initial_double: double = 0.0
The initial expiration time, in seconds.
interval: double = 0.0
interval as interval_double: double = 0.0
The timer's interval, in seconds.

Alter a timer file descriptor's internal timer in seconds.
[clinic start generated code]*/

static PyObject *
os_timerfd_settime_impl(PyObject *module, int fd, int flags, double initial,
double interval)
/*[clinic end generated code: output=0dda31115317adb9 input=6c24e47e7a4d799e]*/
os_timerfd_settime_impl(PyObject *module, int fd, int flags,
double initial_double, double interval_double)
/*[clinic end generated code: output=df4c1bce6859224e input=81d2c0d7e936e8a7]*/
{
struct itimerspec new_value;
struct itimerspec old_value;
int result;
if (_PyTime_AsTimespec(_PyTime_FromSecondsDouble(initial, _PyTime_ROUND_FLOOR), &new_value.it_value) < 0) {
PyTime_t initial, interval;
if (_PyTime_FromSecondsDouble(initial_double, _PyTime_ROUND_FLOOR,
&initial) < 0) {
return NULL;
}
if (_PyTime_FromSecondsDouble(interval_double, _PyTime_ROUND_FLOOR,
&interval) < 0) {
return NULL;
}

struct itimerspec new_value, old_value;
if (_PyTime_AsTimespec(initial, &new_value.it_value) < 0) {
PyErr_SetString(PyExc_ValueError, "invalid initial value");
return NULL;
}
if (_PyTime_AsTimespec(_PyTime_FromSecondsDouble(interval, _PyTime_ROUND_FLOOR), &new_value.it_interval) < 0) {
if (_PyTime_AsTimespec(interval, &new_value.it_interval) < 0) {
PyErr_SetString(PyExc_ValueError, "invalid interval value");
return NULL;
}

int result;
Py_BEGIN_ALLOW_THREADS
result = timerfd_settime(fd, flags, &new_value, &old_value);
Py_END_ALLOW_THREADS
Expand Down
11 changes: 4 additions & 7 deletions Python/pytime.c
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,7 @@ pytime_from_double(PyTime_t *tp, double value, _PyTime_round_t round,
/* See comments in pytime_double_to_denominator */
if (!((double)PyTime_MIN <= d && d < -(double)PyTime_MIN)) {
pytime_time_t_overflow();
*tp = 0;
return -1;
}
PyTime_t ns = (PyTime_t)d;
Expand Down Expand Up @@ -652,14 +653,10 @@ _PyTime_AsLong(PyTime_t ns)
return PyLong_FromLongLong((long long)ns);
}

PyTime_t
_PyTime_FromSecondsDouble(double seconds, _PyTime_round_t round)
int
_PyTime_FromSecondsDouble(double seconds, _PyTime_round_t round, PyTime_t *result)
{
PyTime_t tp;
if(pytime_from_double(&tp, seconds, round, SEC_TO_NS) < 0) {
return -1;
}
return tp;
return pytime_from_double(result, seconds, round, SEC_TO_NS);
}


Expand Down