[Python-checkins] cpython: Issue #22117: Fix ssl to use _PyTime_t API on sock_timeout

victor.stinner python-checkins at python.org
Sat Mar 28 03:00:57 CET 2015


https://hg.python.org/cpython/rev/d1ef5ff79125
changeset:   95235:d1ef5ff79125
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Sat Mar 28 03:00:46 2015 +0100
summary:
  Issue #22117: Fix ssl to use _PyTime_t API on sock_timeout

I didn't notice that the ssl module uses private attributes of socket objects.

files:
  Modules/_ssl.c |  25 ++++++++++++++-----------
  1 files changed, 14 insertions(+), 11 deletions(-)


diff --git a/Modules/_ssl.c b/Modules/_ssl.c
--- a/Modules/_ssl.c
+++ b/Modules/_ssl.c
@@ -534,7 +534,7 @@
     /* If the socket is in non-blocking mode or timeout mode, set the BIO
      * to non-blocking mode (blocking is the default)
      */
-    if (sock && sock->sock_timeout >= 0.0) {
+    if (sock && sock->sock_timeout >= 0) {
         BIO_set_nbio(SSL_get_rbio(self->ssl), 1);
         BIO_set_nbio(SSL_get_wbio(self->ssl), 1);
     }
@@ -576,7 +576,7 @@
         Py_INCREF(sock);
 
         /* just in case the blocking state of the socket has been changed */
-        nonblocking = (sock->sock_timeout >= 0.0);
+        nonblocking = (sock->sock_timeout >= 0);
         BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
         BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
     }
@@ -1616,9 +1616,9 @@
     int rc;
 
     /* Nothing to do unless we're in timeout mode (not non-blocking) */
-    if ((s == NULL) || (s->sock_timeout == 0.0))
+    if ((s == NULL) || (s->sock_timeout == 0))
         return SOCKET_IS_NONBLOCKING;
-    else if (s->sock_timeout < 0.0)
+    else if (s->sock_timeout < 0)
         return SOCKET_IS_BLOCKING;
 
     /* Guard against closed socket */
@@ -1636,7 +1636,9 @@
         pollfd.events = writing ? POLLOUT : POLLIN;
 
         /* s->sock_timeout is in seconds, timeout in ms */
-        timeout = (int)(s->sock_timeout * 1000 + 0.5);
+        timeout = (int)_PyTime_AsMilliseconds(s->sock_timeout,
+                                              _PyTime_ROUND_UP);
+
         PySSL_BEGIN_ALLOW_THREADS
         rc = poll(&pollfd, 1, timeout);
         PySSL_END_ALLOW_THREADS
@@ -1649,9 +1651,10 @@
     if (!_PyIsSelectable_fd(s->sock_fd))
         return SOCKET_TOO_LARGE_FOR_SELECT;
 
-    /* Construct the arguments to select */
-    tv.tv_sec = (int)s->sock_timeout;
-    tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6);
+    /* conversion was already checked for overflow when
+       the timeout was set */
+    (void)_PyTime_AsTimeval(s->sock_timeout, &tv, _PyTime_ROUND_UP);
+
     FD_ZERO(&fds);
     FD_SET(s->sock_fd, &fds);
 
@@ -1704,7 +1707,7 @@
 
     if (sock != NULL) {
         /* just in case the blocking state of the socket has been changed */
-        nonblocking = (sock->sock_timeout >= 0.0);
+        nonblocking = (sock->sock_timeout >= 0);
         BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
         BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
     }
@@ -1836,7 +1839,7 @@
 
     if (sock != NULL) {
         /* just in case the blocking state of the socket has been changed */
-        nonblocking = (sock->sock_timeout >= 0.0);
+        nonblocking = (sock->sock_timeout >= 0);
         BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
         BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
     }
@@ -1915,7 +1918,7 @@
         Py_INCREF(sock);
 
         /* Just in case the blocking state of the socket has been changed */
-        nonblocking = (sock->sock_timeout >= 0.0);
+        nonblocking = (sock->sock_timeout >= 0);
         BIO_set_nbio(SSL_get_rbio(self->ssl), nonblocking);
         BIO_set_nbio(SSL_get_wbio(self->ssl), nonblocking);
     }

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


More information about the Python-checkins mailing list