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

Skip to content

Commit 59af94f

Browse files
pablogsalserhiy-storchaka
authored andcommitted
bpo-31806: Use _PyTime_ROUND_TIMEOUT for the timeout argument parsing in more functions (#4026)
Fix timeout rounding in time.sleep(), threading.Lock.acquire() and socket.socket.settimeout() to round correctly negative timeouts between -1.0 and 0.0. The functions now block waiting for events as expected. Previously, the call was incorrectly non-blocking.
1 parent ec12df1 commit 59af94f

4 files changed

Lines changed: 10 additions & 6 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Fix timeout rounding in time.sleep(), threading.Lock.acquire() and
2+
socket.socket.settimeout() to round correctly negative timeouts between -1.0 and
3+
0.0. The functions now block waiting for events as expected. Previously, the
4+
call was incorrectly non-blocking. Patch by Pablo Galindo.

Modules/_threadmodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ lock_acquire_parse_args(PyObject *args, PyObject *kwds,
104104

105105
if (timeout_obj
106106
&& _PyTime_FromSecondsObject(timeout,
107-
timeout_obj, _PyTime_ROUND_CEILING) < 0)
107+
timeout_obj, _PyTime_ROUND_TIMEOUT) < 0)
108108
return -1;
109109

110110
if (!blocking && *timeout != unset_timeout ) {
@@ -122,7 +122,7 @@ lock_acquire_parse_args(PyObject *args, PyObject *kwds,
122122
else if (*timeout != unset_timeout) {
123123
_PyTime_t microseconds;
124124

125-
microseconds = _PyTime_AsMicroseconds(*timeout, _PyTime_ROUND_CEILING);
125+
microseconds = _PyTime_AsMicroseconds(*timeout, _PyTime_ROUND_TIMEOUT);
126126
if (microseconds >= PY_TIMEOUT_MAX) {
127127
PyErr_SetString(PyExc_OverflowError,
128128
"timeout value is too large");

Modules/socketmodule.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2539,7 +2539,7 @@ socket_parse_timeout(_PyTime_t *timeout, PyObject *timeout_obj)
25392539
}
25402540

25412541
if (_PyTime_FromSecondsObject(timeout,
2542-
timeout_obj, _PyTime_ROUND_CEILING) < 0)
2542+
timeout_obj, _PyTime_ROUND_TIMEOUT) < 0)
25432543
return -1;
25442544

25452545
if (*timeout < 0) {
@@ -2548,10 +2548,10 @@ socket_parse_timeout(_PyTime_t *timeout, PyObject *timeout_obj)
25482548
}
25492549

25502550
#ifdef MS_WINDOWS
2551-
overflow |= (_PyTime_AsTimeval(*timeout, &tv, _PyTime_ROUND_CEILING) < 0);
2551+
overflow |= (_PyTime_AsTimeval(*timeout, &tv, _PyTime_ROUND_TIMEOUT) < 0);
25522552
#endif
25532553
#ifndef HAVE_POLL
2554-
ms = _PyTime_AsMilliseconds(*timeout, _PyTime_ROUND_CEILING);
2554+
ms = _PyTime_AsMilliseconds(*timeout, _PyTime_ROUND_TIMEOUT);
25552555
overflow |= (ms > INT_MAX);
25562556
#endif
25572557
if (overflow) {

Modules/timemodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ static PyObject *
245245
time_sleep(PyObject *self, PyObject *obj)
246246
{
247247
_PyTime_t secs;
248-
if (_PyTime_FromSecondsObject(&secs, obj, _PyTime_ROUND_CEILING))
248+
if (_PyTime_FromSecondsObject(&secs, obj, _PyTime_ROUND_TIMEOUT))
249249
return NULL;
250250
if (secs < 0) {
251251
PyErr_SetString(PyExc_ValueError,

0 commit comments

Comments
 (0)