diff options
author | Eric Snow <ericsnowcurrently@gmail.com> | 2023-10-17 23:05:49 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-17 23:05:49 (GMT) |
commit | c58c63fdf615a1c2bfc995dd0b938d82e32b6cde (patch) | |
tree | c55bd273d26fdcaf30013a44a7bf973940771a23 /Python/thread.c | |
parent | 7029c1a1c5b864056aa00298b1d0e0269f073f99 (diff) | |
download | cpython-c58c63fdf615a1c2bfc995dd0b938d82e32b6cde.zip cpython-c58c63fdf615a1c2bfc995dd0b938d82e32b6cde.tar.gz cpython-c58c63fdf615a1c2bfc995dd0b938d82e32b6cde.tar.bz2 |
gh-84570: Add Timeouts to SendChannel.send() and RecvChannel.recv() (gh-110567)
Diffstat (limited to 'Python/thread.c')
-rw-r--r-- | Python/thread.c | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/Python/thread.c b/Python/thread.c index 7185dd4..fefae83 100644 --- a/Python/thread.c +++ b/Python/thread.c @@ -93,6 +93,40 @@ PyThread_set_stacksize(size_t size) } +int +PyThread_ParseTimeoutArg(PyObject *arg, int blocking, PY_TIMEOUT_T *timeout_p) +{ + assert(_PyTime_FromSeconds(-1) == PyThread_UNSET_TIMEOUT); + if (arg == NULL || arg == Py_None) { + *timeout_p = blocking ? PyThread_UNSET_TIMEOUT : 0; + return 0; + } + if (!blocking) { + PyErr_SetString(PyExc_ValueError, + "can't specify a timeout for a non-blocking call"); + return -1; + } + + _PyTime_t timeout; + if (_PyTime_FromSecondsObject(&timeout, arg, _PyTime_ROUND_TIMEOUT) < 0) { + return -1; + } + if (timeout < 0) { + PyErr_SetString(PyExc_ValueError, + "timeout value must be a non-negative number"); + return -1; + } + + if (_PyTime_AsMicroseconds(timeout, + _PyTime_ROUND_TIMEOUT) > PY_TIMEOUT_MAX) { + PyErr_SetString(PyExc_OverflowError, + "timeout value is too large"); + return -1; + } + *timeout_p = timeout; + return 0; +} + PyLockStatus PyThread_acquire_lock_timed_with_retries(PyThread_type_lock lock, PY_TIMEOUT_T timeout) |