Skip to content

Commit be4e9cc

Browse files
miss-islingtonserhiy-storchaka
authored andcommitted
[3.6] bpo-31806: Use _PyTime_ROUND_TIMEOUT for the timeout argument parsing in more functions (GH-4026) (#4032)
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. (cherry picked from commit 59af94f)
1 parent 95602b3 commit be4e9cc

File tree

4 files changed

+10
-6
lines changed

4 files changed

+10
-6
lines changed
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
@@ -111,7 +111,7 @@ lock_acquire_parse_args(PyObject *args, PyObject *kwds,
111111

112112
if (timeout_obj
113113
&& _PyTime_FromSecondsObject(timeout,
114-
timeout_obj, _PyTime_ROUND_CEILING) < 0)
114+
timeout_obj, _PyTime_ROUND_TIMEOUT) < 0)
115115
return -1;
116116

117117
if (!blocking && *timeout != unset_timeout ) {
@@ -129,7 +129,7 @@ lock_acquire_parse_args(PyObject *args, PyObject *kwds,
129129
else if (*timeout != unset_timeout) {
130130
_PyTime_t microseconds;
131131

132-
microseconds = _PyTime_AsMicroseconds(*timeout, _PyTime_ROUND_CEILING);
132+
microseconds = _PyTime_AsMicroseconds(*timeout, _PyTime_ROUND_TIMEOUT);
133133
if (microseconds >= PY_TIMEOUT_MAX) {
134134
PyErr_SetString(PyExc_OverflowError,
135135
"timeout value is too large");

Modules/socketmodule.c

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

24562456
if (_PyTime_FromSecondsObject(timeout,
2457-
timeout_obj, _PyTime_ROUND_CEILING) < 0)
2457+
timeout_obj, _PyTime_ROUND_TIMEOUT) < 0)
24582458
return -1;
24592459

24602460
if (*timeout < 0) {
@@ -2463,10 +2463,10 @@ socket_parse_timeout(_PyTime_t *timeout, PyObject *timeout_obj)
24632463
}
24642464

24652465
#ifdef MS_WINDOWS
2466-
overflow |= (_PyTime_AsTimeval(*timeout, &tv, _PyTime_ROUND_CEILING) < 0);
2466+
overflow |= (_PyTime_AsTimeval(*timeout, &tv, _PyTime_ROUND_TIMEOUT) < 0);
24672467
#endif
24682468
#ifndef HAVE_POLL
2469-
ms = _PyTime_AsMilliseconds(*timeout, _PyTime_ROUND_CEILING);
2469+
ms = _PyTime_AsMilliseconds(*timeout, _PyTime_ROUND_TIMEOUT);
24702470
overflow |= (ms > INT_MAX);
24712471
#endif
24722472
if (overflow) {

Modules/timemodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ static PyObject *
225225
time_sleep(PyObject *self, PyObject *obj)
226226
{
227227
_PyTime_t secs;
228-
if (_PyTime_FromSecondsObject(&secs, obj, _PyTime_ROUND_CEILING))
228+
if (_PyTime_FromSecondsObject(&secs, obj, _PyTime_ROUND_TIMEOUT))
229229
return NULL;
230230
if (secs < 0) {
231231
PyErr_SetString(PyExc_ValueError,

0 commit comments

Comments
 (0)