mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Issue #22117: Fix usage of _PyTime_AsTimeval()
Add _PyTime_AsTimeval_noraise() function. Call it when it's not possible (or not useful) to raise a Python exception on overflow.
This commit is contained in:
parent
160e819a1d
commit
ea9c0dd2c2
6 changed files with 29 additions and 20 deletions
|
@ -94,11 +94,17 @@ PyAPI_FUNC(PyObject *) _PyTime_AsNanosecondsObject(_PyTime_t t);
|
||||||
|
|
||||||
/* Convert a timestamp to a timeval structure (microsecond resolution).
|
/* Convert a timestamp to a timeval structure (microsecond resolution).
|
||||||
tv_usec is always positive.
|
tv_usec is always positive.
|
||||||
Return -1 if the conversion overflowed, return 0 on success. */
|
Raise an exception and return -1 if the conversion overflowed,
|
||||||
|
return 0 on success. */
|
||||||
PyAPI_FUNC(int) _PyTime_AsTimeval(_PyTime_t t,
|
PyAPI_FUNC(int) _PyTime_AsTimeval(_PyTime_t t,
|
||||||
struct timeval *tv,
|
struct timeval *tv,
|
||||||
_PyTime_round_t round);
|
_PyTime_round_t round);
|
||||||
|
|
||||||
|
/* Similar to _PyTime_AsTimeval(), but don't raise an exception on error. */
|
||||||
|
PyAPI_FUNC(int) _PyTime_AsTimeval_noraise(_PyTime_t t,
|
||||||
|
struct timeval *tv,
|
||||||
|
_PyTime_round_t round);
|
||||||
|
|
||||||
#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE)
|
#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE)
|
||||||
/* Convert a timestamp to a timespec structure (nanosecond resolution).
|
/* Convert a timestamp to a timespec structure (nanosecond resolution).
|
||||||
tv_nsec is always positive.
|
tv_nsec is always positive.
|
||||||
|
|
|
@ -1651,9 +1651,7 @@ check_socket_and_wait_for_timeout(PySocketSockObject *s, int writing)
|
||||||
if (!_PyIsSelectable_fd(s->sock_fd))
|
if (!_PyIsSelectable_fd(s->sock_fd))
|
||||||
return SOCKET_TOO_LARGE_FOR_SELECT;
|
return SOCKET_TOO_LARGE_FOR_SELECT;
|
||||||
|
|
||||||
/* conversion was already checked for overflow when
|
_PyTime_AsTimeval_noraise(s->sock_timeout, &tv, _PyTime_ROUND_UP);
|
||||||
the timeout was set */
|
|
||||||
(void)_PyTime_AsTimeval(s->sock_timeout, &tv, _PyTime_ROUND_UP);
|
|
||||||
|
|
||||||
FD_ZERO(&fds);
|
FD_ZERO(&fds);
|
||||||
FD_SET(s->sock_fd, &fds);
|
FD_SET(s->sock_fd, &fds);
|
||||||
|
|
|
@ -3427,11 +3427,8 @@ test_PyTime_AsTimeval(PyObject *self, PyObject *args)
|
||||||
if (check_time_rounding(round) < 0)
|
if (check_time_rounding(round) < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
t = _PyTime_FromNanoseconds(ns);
|
t = _PyTime_FromNanoseconds(ns);
|
||||||
if (_PyTime_AsTimeval(t, &tv, round) < 0) {
|
if (_PyTime_AsTimeval(t, &tv, round) < 0)
|
||||||
PyErr_SetString(PyExc_OverflowError,
|
|
||||||
"timeout doesn't fit into C timeval");
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
|
||||||
|
|
||||||
seconds = PyLong_FromLong((PY_LONG_LONG)tv.tv_sec);
|
seconds = PyLong_FromLong((PY_LONG_LONG)tv.tv_sec);
|
||||||
if (seconds == NULL)
|
if (seconds == NULL)
|
||||||
|
|
|
@ -641,9 +641,7 @@ internal_select_ex(PySocketSockObject *s, int writing, _PyTime_t interval)
|
||||||
n = poll(&pollfd, 1, timeout_int);
|
n = poll(&pollfd, 1, timeout_int);
|
||||||
Py_END_ALLOW_THREADS;
|
Py_END_ALLOW_THREADS;
|
||||||
#else
|
#else
|
||||||
/* conversion was already checked for overflow when
|
_PyTime_AsTimeval_noraise(interval, &tv, _PyTime_ROUND_UP);
|
||||||
the timeout was set */
|
|
||||||
(void)_PyTime_AsTimeval(interval, &tv, _PyTime_ROUND_UP);
|
|
||||||
|
|
||||||
FD_ZERO(&fds);
|
FD_ZERO(&fds);
|
||||||
FD_SET(s->sock_fd, &fds);
|
FD_SET(s->sock_fd, &fds);
|
||||||
|
@ -2454,9 +2452,7 @@ internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen,
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
int conv;
|
int conv;
|
||||||
|
|
||||||
/* conversion was already checked for overflow when
|
_PyTime_AsTimeval_noraise(s->sock_timeout, &tv, _PyTime_ROUND_UP);
|
||||||
the timeout was set */
|
|
||||||
(void)_PyTime_AsTimeval(s->sock_timeout, &tv, _PyTime_ROUND_UP);
|
|
||||||
|
|
||||||
Py_BEGIN_ALLOW_THREADS
|
Py_BEGIN_ALLOW_THREADS
|
||||||
FD_ZERO(&fds);
|
FD_ZERO(&fds);
|
||||||
|
|
|
@ -1405,11 +1405,8 @@ pysleep(_PyTime_t secs)
|
||||||
|
|
||||||
do {
|
do {
|
||||||
#ifndef MS_WINDOWS
|
#ifndef MS_WINDOWS
|
||||||
if (_PyTime_AsTimeval(secs, &timeout, _PyTime_ROUND_UP) < 0) {
|
if (_PyTime_AsTimeval(secs, &timeout, _PyTime_ROUND_UP) < 0)
|
||||||
PyErr_SetString(PyExc_OverflowError,
|
|
||||||
"delay doesn't fit into C timeval");
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
|
|
||||||
Py_BEGIN_ALLOW_THREADS
|
Py_BEGIN_ALLOW_THREADS
|
||||||
err = select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &timeout);
|
err = select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &timeout);
|
||||||
|
|
|
@ -311,8 +311,9 @@ _PyTime_AsMicroseconds(_PyTime_t t, _PyTime_round_t round)
|
||||||
return _PyTime_Multiply(t, 1000 * 1000, round);
|
return _PyTime_Multiply(t, 1000 * 1000, round);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
static int
|
||||||
_PyTime_AsTimeval(_PyTime_t t, struct timeval *tv, _PyTime_round_t round)
|
_PyTime_AsTimeval_impl(_PyTime_t t, struct timeval *tv, _PyTime_round_t round,
|
||||||
|
int raise)
|
||||||
{
|
{
|
||||||
_PyTime_t secs, ns;
|
_PyTime_t secs, ns;
|
||||||
int res = 0;
|
int res = 0;
|
||||||
|
@ -357,9 +358,23 @@ _PyTime_AsTimeval(_PyTime_t t, struct timeval *tv, _PyTime_round_t round)
|
||||||
tv->tv_sec += 1;
|
tv->tv_sec += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (res && raise)
|
||||||
|
_PyTime_overflow();
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
_PyTime_AsTimeval(_PyTime_t t, struct timeval *tv, _PyTime_round_t round)
|
||||||
|
{
|
||||||
|
return _PyTime_AsTimeval_impl(t, tv, round, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
_PyTime_AsTimeval_noraise(_PyTime_t t, struct timeval *tv, _PyTime_round_t round)
|
||||||
|
{
|
||||||
|
return _PyTime_AsTimeval_impl(t, tv, round, 0);
|
||||||
|
}
|
||||||
|
|
||||||
#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE)
|
#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_KQUEUE)
|
||||||
int
|
int
|
||||||
_PyTime_AsTimespec(_PyTime_t t, struct timespec *ts)
|
_PyTime_AsTimespec(_PyTime_t t, struct timespec *ts)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue