Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c337838

Browse files
committed
Issue #22117: Use the new _PyTime_t API in the select module
1 parent f5faad2 commit c337838

3 files changed

Lines changed: 22 additions & 41 deletions

File tree

Include/pytime.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ PyAPI_FUNC(int) _PyTime_AsTimeval(_PyTime_t t,
124124
struct timeval *tv,
125125
_PyTime_round_t round);
126126

127-
#ifdef HAVE_CLOCK_GETTIME
127+
#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE)
128128
/* Convert a timestamp to a timespec structure (nanosecond resolution).
129129
tv_nsec is always positive.
130130
Raise an exception and return -1 on error, return 0 on success. */

Modules/selectmodule.c

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -206,38 +206,17 @@ select_select(PyObject *self, PyObject *args)
206206

207207
if (tout == Py_None)
208208
tvp = (struct timeval *)0;
209-
else if (!PyNumber_Check(tout)) {
210-
PyErr_SetString(PyExc_TypeError,
211-
"timeout must be a float or None");
212-
return NULL;
213-
}
214209
else {
215-
/* On OpenBSD 5.4, timeval.tv_sec is a long.
216-
* Example: long is 64-bit, whereas time_t is 32-bit. */
217-
time_t sec;
218-
/* On OS X 64-bit, timeval.tv_usec is an int (and thus still 4
219-
bytes as required), but no longer defined by a long. */
220-
long usec;
221-
if (_PyTime_ObjectToTimeval(tout, &sec, &usec,
222-
_PyTime_ROUND_UP) == -1)
223-
return NULL;
224-
#ifdef MS_WINDOWS
225-
/* On Windows, timeval.tv_sec is a long (32 bit),
226-
* whereas time_t can be 64-bit. */
227-
assert(sizeof(tv.tv_sec) == sizeof(long));
228-
#if SIZEOF_TIME_T > SIZEOF_LONG
229-
if (sec > LONG_MAX) {
230-
PyErr_SetString(PyExc_OverflowError,
231-
"timeout is too large");
210+
_PyTime_t ts;
211+
212+
if (_PyTime_FromSecondsObject(&ts, tout, _PyTime_ROUND_UP) < 0) {
213+
PyErr_SetString(PyExc_TypeError,
214+
"timeout must be a float or None");
232215
return NULL;
233216
}
234-
#endif
235-
tv.tv_sec = (long)sec;
236-
#else
237-
assert(sizeof(tv.tv_sec) >= sizeof(sec));
238-
tv.tv_sec = sec;
239-
#endif
240-
tv.tv_usec = usec;
217+
218+
if (_PyTime_AsTimeval(ts, &tv, _PyTime_ROUND_UP) == -1)
219+
return NULL;
241220
if (tv.tv_sec < 0) {
242221
PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
243222
return NULL;
@@ -2032,9 +2011,18 @@ kqueue_queue_control(kqueue_queue_Object *self, PyObject *args)
20322011
if (otimeout == Py_None || otimeout == NULL) {
20332012
ptimeoutspec = NULL;
20342013
}
2035-
else if (PyNumber_Check(otimeout)) {
2036-
if (_PyTime_ObjectToTimespec(otimeout, &timeout.tv_sec,
2037-
&timeout.tv_nsec, _PyTime_ROUND_UP) == -1)
2014+
else {
2015+
_PyTime_t ts;
2016+
2017+
if (_PyTime_FromSecondsObject(&ts, otimeout, _PyTime_ROUND_UP) < 0) {
2018+
PyErr_Format(PyExc_TypeError,
2019+
"timeout argument must be an number "
2020+
"or None, got %.200s",
2021+
Py_TYPE(otimeout)->tp_name);
2022+
return NULL;
2023+
}
2024+
2025+
if (_PyTime_AsTimespec(ts, &timeout) == -1)
20382026
return NULL;
20392027

20402028
if (timeout.tv_sec < 0) {
@@ -2044,13 +2032,6 @@ kqueue_queue_control(kqueue_queue_Object *self, PyObject *args)
20442032
}
20452033
ptimeoutspec = &timeout;
20462034
}
2047-
else {
2048-
PyErr_Format(PyExc_TypeError,
2049-
"timeout argument must be an number "
2050-
"or None, got %.200s",
2051-
Py_TYPE(otimeout)->tp_name);
2052-
return NULL;
2053-
}
20542035

20552036
if (ch != NULL && ch != Py_None) {
20562037
it = PyObject_GetIter(ch);

Python/pytime.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ _PyTime_AsTimeval(_PyTime_t t, struct timeval *tv, _PyTime_round_t round)
469469
return res;
470470
}
471471

472-
#ifdef HAVE_CLOCK_GETTIME
472+
#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE)
473473
int
474474
_PyTime_AsTimespec(_PyTime_t t, struct timespec *ts)
475475
{

0 commit comments

Comments
 (0)