Skip to content

Commit d47221a

Browse files
committed
Implement _PyTime_ROUND_UP and use it in the select module instead of _PyTime_ROUND_CEILING
1 parent 0555859 commit d47221a

3 files changed

Lines changed: 21 additions & 15 deletions

File tree

‎Include/pytime.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ typedef enum {
2929
_PyTime_ROUND_CEILING=1,
3030
/* Round to nearest with ties going to nearest even integer.
3131
For example, used to round from a Python float. */
32-
_PyTime_ROUND_HALF_EVEN
32+
_PyTime_ROUND_HALF_EVEN=2,
33+
/* Round away from zero
34+
For example, used for timeout if negative values are allowed. */
35+
_PyTime_ROUND_UP=3
3336
} _PyTime_round_t;
3437

3538
/* Convert a time_t to a PyLong. */

‎Modules/selectmodule.c‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ select_select(PyObject *self, PyObject *args)
221221
return NULL;
222222
}
223223

224-
if (_PyTime_AsTimeval(timeout, &tv, _PyTime_ROUND_CEILING) == -1)
224+
if (_PyTime_AsTimeval(timeout, &tv, _PyTime_ROUND_UP) == -1)
225225
return NULL;
226226
if (tv.tv_sec < 0) {
227227
PyErr_SetString(PyExc_ValueError, "timeout must be non-negative");
@@ -282,7 +282,7 @@ select_select(PyObject *self, PyObject *args)
282282
n = 0;
283283
break;
284284
}
285-
_PyTime_AsTimeval_noraise(timeout, &tv, _PyTime_ROUND_CEILING);
285+
_PyTime_AsTimeval_noraise(timeout, &tv, _PyTime_ROUND_UP);
286286
/* retry select() with the recomputed timeout */
287287
}
288288
} while (1);
@@ -540,15 +540,15 @@ poll_poll(pollObject *self, PyObject *args)
540540
}
541541
else {
542542
if (_PyTime_FromMillisecondsObject(&timeout, timeout_obj,
543-
_PyTime_ROUND_CEILING) < 0) {
543+
_PyTime_ROUND_UP) < 0) {
544544
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
545545
PyErr_SetString(PyExc_TypeError,
546546
"timeout must be an integer or None");
547547
}
548548
return NULL;
549549
}
550550

551-
ms = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_CEILING);
551+
ms = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_UP);
552552
if (ms < INT_MIN || ms > INT_MAX) {
553553
PyErr_SetString(PyExc_OverflowError, "timeout is too large");
554554
return NULL;
@@ -576,7 +576,7 @@ poll_poll(pollObject *self, PyObject *args)
576576
do {
577577
Py_BEGIN_ALLOW_THREADS
578578
errno = 0;
579-
poll_result = poll(self->ufds, self->ufd_len, ms > 0 ? (int)ms : -1);
579+
poll_result = poll(self->ufds, self->ufd_len, (int)ms);
580580
Py_END_ALLOW_THREADS
581581

582582
if (errno != EINTR)
@@ -594,7 +594,7 @@ poll_poll(pollObject *self, PyObject *args)
594594
poll_result = 0;
595595
break;
596596
}
597-
ms = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_CEILING);
597+
ms = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_UP);
598598
/* retry poll() with the recomputed timeout */
599599
}
600600
} while (1);
@@ -896,15 +896,15 @@ devpoll_poll(devpollObject *self, PyObject *args)
896896
}
897897
else {
898898
if (_PyTime_FromMillisecondsObject(&timeout, timeout_obj,
899-
_PyTime_ROUND_CEILING) < 0) {
899+
_PyTime_ROUND_UP) < 0) {
900900
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
901901
PyErr_SetString(PyExc_TypeError,
902902
"timeout must be an integer or None");
903903
}
904904
return NULL;
905905
}
906906

907-
ms = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_CEILING);
907+
ms = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_UP);
908908
if (ms < -1 || ms > INT_MAX) {
909909
PyErr_SetString(PyExc_OverflowError, "timeout is too large");
910910
return NULL;
@@ -941,7 +941,7 @@ devpoll_poll(devpollObject *self, PyObject *args)
941941
poll_result = 0;
942942
break;
943943
}
944-
ms = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_CEILING);
944+
ms = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_UP);
945945
dvp.dp_timeout = (int)ms;
946946
/* retry devpoll() with the recomputed timeout */
947947
}
@@ -1513,15 +1513,15 @@ pyepoll_poll(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
15131513
/* epoll_wait() has a resolution of 1 millisecond, round towards
15141514
infinity to wait at least timeout seconds. */
15151515
if (_PyTime_FromSecondsObject(&timeout, timeout_obj,
1516-
_PyTime_ROUND_CEILING) < 0) {
1516+
_PyTime_ROUND_UP) < 0) {
15171517
if (PyErr_ExceptionMatches(PyExc_TypeError)) {
15181518
PyErr_SetString(PyExc_TypeError,
15191519
"timeout must be an integer or None");
15201520
}
15211521
return NULL;
15221522
}
15231523

1524-
ms = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_CEILING);
1524+
ms = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_UP);
15251525
if (ms < INT_MIN || ms > INT_MAX) {
15261526
PyErr_SetString(PyExc_OverflowError, "timeout is too large");
15271527
return NULL;
@@ -1565,7 +1565,7 @@ pyepoll_poll(pyEpoll_Object *self, PyObject *args, PyObject *kwds)
15651565
nfds = 0;
15661566
break;
15671567
}
1568-
ms = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_CEILING);
1568+
ms = _PyTime_AsMilliseconds(timeout, _PyTime_ROUND_UP);
15691569
/* retry epoll_wait() with the recomputed timeout */
15701570
}
15711571
} while(1);
@@ -2128,7 +2128,7 @@ kqueue_queue_control(kqueue_queue_Object *self, PyObject *args)
21282128
}
21292129
else {
21302130
if (_PyTime_FromSecondsObject(&timeout,
2131-
otimeout, _PyTime_ROUND_CEILING) < 0) {
2131+
otimeout, _PyTime_ROUND_UP) < 0) {
21322132
PyErr_Format(PyExc_TypeError,
21332133
"timeout argument must be a number "
21342134
"or None, got %.200s",

‎Python/pytime.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,12 @@ _PyTime_Round(double x, _PyTime_round_t round)
9999
else if (round == _PyTime_ROUND_CEILING) {
100100
d = ceil(d);
101101
}
102-
else {
102+
else if (round == _PyTime_ROUND_FLOOR) {
103103
d = floor(d);
104104
}
105+
else {
106+
d = (d < 0.0) ? floor(d) : ceil(d);
107+
}
105108
return d;
106109
}
107110

0 commit comments

Comments
 (0)