[Python-checkins] cpython: Issue #22117: Fix usage of _PyTime_AsTimeval()

victor.stinner python-checkins at python.org
Mon Mar 30 02:55:49 CEST 2015


https://hg.python.org/cpython/rev/d938533b43f7
changeset:   95267:d938533b43f7
parent:      95265:2e1234208bab
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Mon Mar 30 02:51:13 2015 +0200
summary:
  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.

files:
  Include/pytime.h          |   8 +++++++-
  Modules/_ssl.c            |   4 +---
  Modules/_testcapimodule.c |   5 +----
  Modules/socketmodule.c    |   8 ++------
  Modules/timemodule.c      |   5 +----
  Python/pytime.c           |  19 +++++++++++++++++--
  6 files changed, 29 insertions(+), 20 deletions(-)


diff --git a/Include/pytime.h b/Include/pytime.h
--- a/Include/pytime.h
+++ b/Include/pytime.h
@@ -94,11 +94,17 @@
 
 /* Convert a timestamp to a timeval structure (microsecond resolution).
    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,
     struct timeval *tv,
     _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)
 /* Convert a timestamp to a timespec structure (nanosecond resolution).
    tv_nsec is always positive.
diff --git a/Modules/_ssl.c b/Modules/_ssl.c
--- a/Modules/_ssl.c
+++ b/Modules/_ssl.c
@@ -1651,9 +1651,7 @@
     if (!_PyIsSelectable_fd(s->sock_fd))
         return SOCKET_TOO_LARGE_FOR_SELECT;
 
-    /* conversion was already checked for overflow when
-       the timeout was set */
-    (void)_PyTime_AsTimeval(s->sock_timeout, &tv, _PyTime_ROUND_UP);
+    _PyTime_AsTimeval_noraise(s->sock_timeout, &tv, _PyTime_ROUND_UP);
 
     FD_ZERO(&fds);
     FD_SET(s->sock_fd, &fds);
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -3427,11 +3427,8 @@
     if (check_time_rounding(round) < 0)
         return NULL;
     t = _PyTime_FromNanoseconds(ns);
-    if (_PyTime_AsTimeval(t, &tv, round) < 0) {
-        PyErr_SetString(PyExc_OverflowError,
-                        "timeout doesn't fit into C timeval");
+    if (_PyTime_AsTimeval(t, &tv, round) < 0)
         return NULL;
-    }
 
     seconds = PyLong_FromLong((PY_LONG_LONG)tv.tv_sec);
     if (seconds == NULL)
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -641,9 +641,7 @@
     n = poll(&pollfd, 1, timeout_int);
     Py_END_ALLOW_THREADS;
 #else
-    /* conversion was already checked for overflow when
-       the timeout was set */
-    (void)_PyTime_AsTimeval(interval, &tv, _PyTime_ROUND_UP);
+    _PyTime_AsTimeval_noraise(interval, &tv, _PyTime_ROUND_UP);
 
     FD_ZERO(&fds);
     FD_SET(s->sock_fd, &fds);
@@ -2454,9 +2452,7 @@
         struct timeval tv;
         int conv;
 
-        /* conversion was already checked for overflow when
-           the timeout was set */
-        (void)_PyTime_AsTimeval(s->sock_timeout, &tv, _PyTime_ROUND_UP);
+        _PyTime_AsTimeval_noraise(s->sock_timeout, &tv, _PyTime_ROUND_UP);
 
         Py_BEGIN_ALLOW_THREADS
         FD_ZERO(&fds);
diff --git a/Modules/timemodule.c b/Modules/timemodule.c
--- a/Modules/timemodule.c
+++ b/Modules/timemodule.c
@@ -1405,11 +1405,8 @@
 
     do {
 #ifndef MS_WINDOWS
-        if (_PyTime_AsTimeval(secs, &timeout, _PyTime_ROUND_UP) < 0) {
-            PyErr_SetString(PyExc_OverflowError,
-                            "delay doesn't fit into C timeval");
+        if (_PyTime_AsTimeval(secs, &timeout, _PyTime_ROUND_UP) < 0)
             return -1;
-        }
 
         Py_BEGIN_ALLOW_THREADS
         err = select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &timeout);
diff --git a/Python/pytime.c b/Python/pytime.c
--- a/Python/pytime.c
+++ b/Python/pytime.c
@@ -311,8 +311,9 @@
     return _PyTime_Multiply(t, 1000 * 1000, round);
 }
 
-int
-_PyTime_AsTimeval(_PyTime_t t, struct timeval *tv, _PyTime_round_t round)
+static int
+_PyTime_AsTimeval_impl(_PyTime_t t, struct timeval *tv, _PyTime_round_t round,
+                       int raise)
 {
     _PyTime_t secs, ns;
     int res = 0;
@@ -357,9 +358,23 @@
         tv->tv_sec += 1;
     }
 
+    if (res && raise)
+        _PyTime_overflow();
     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)
 int
 _PyTime_AsTimespec(_PyTime_t t, struct timespec *ts)

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list