Issue #10272: The ssl module now raises socket.timeout instead of a generic
authorAntoine Pitrou <solipsis@pitrou.net>
Fri, 3 Dec 2010 19:59:41 +0000 (19:59 +0000)
committerAntoine Pitrou <solipsis@pitrou.net>
Fri, 3 Dec 2010 19:59:41 +0000 (19:59 +0000)
SSLError on socket timeouts.

Lib/test/test_ssl.py
Misc/NEWS
Modules/_ssl.c
Modules/socketmodule.c
Modules/socketmodule.h

index c9f03c751dd29d4fd17cfaf456a1764bb8868861..e5a07a3e9f290423882ffc96980d24620c8bb1b4 100644 (file)
@@ -1499,7 +1499,7 @@ else:
                     c.settimeout(0.2)
                     c.connect((host, port))
                     # Will attempt handshake and time out
-                    self.assertRaisesRegex(ssl.SSLError, "timed out",
+                    self.assertRaisesRegex(socket.timeout, "timed out",
                                            ssl.wrap_socket, c)
                 finally:
                     c.close()
@@ -1508,7 +1508,7 @@ else:
                     c = ssl.wrap_socket(c)
                     c.settimeout(0.2)
                     # Will attempt handshake and time out
-                    self.assertRaisesRegex(ssl.SSLError, "timed out",
+                    self.assertRaisesRegex(socket.timeout, "timed out",
                                            c.connect, (host, port))
                 finally:
                     c.close()
index 4576fb38b705cc8311615c1cf43086105dab1f10..cab89de95a4c5fafb3f3d9b884baeafbf08b8cc9 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -35,6 +35,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #10272: The ssl module now raises socket.timeout instead of a generic
+  SSLError on socket timeouts.
+
 - Issue #10528: Allow translators to reorder placeholders in localizable
   messages from argparse.
 
index c2b976a843cc9671601c304e42a93e77b7439949..a5145552e8429a018ab72cb545aae0b507652d17 100644 (file)
@@ -370,7 +370,7 @@ static PyObject *PySSL_SSLdo_handshake(PySSLSocket *self)
             sockstate = SOCKET_OPERATION_OK;
         }
         if (sockstate == SOCKET_HAS_TIMED_OUT) {
-            PyErr_SetString(PySSLErrorObject,
+            PyErr_SetString(PySocketModule.timeout_error,
                             ERRSTR("The handshake operation timed out"));
             goto error;
         } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
@@ -1075,7 +1075,7 @@ static PyObject *PySSL_SSLwrite(PySSLSocket *self, PyObject *args)
 
     sockstate = check_socket_and_wait_for_timeout(sock, 1);
     if (sockstate == SOCKET_HAS_TIMED_OUT) {
-        PyErr_SetString(PySSLErrorObject,
+        PyErr_SetString(PySocketModule.timeout_error,
                         "The write operation timed out");
         goto error;
     } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
@@ -1104,7 +1104,7 @@ static PyObject *PySSL_SSLwrite(PySSLSocket *self, PyObject *args)
             sockstate = SOCKET_OPERATION_OK;
         }
         if (sockstate == SOCKET_HAS_TIMED_OUT) {
-            PyErr_SetString(PySSLErrorObject,
+            PyErr_SetString(PySocketModule.timeout_error,
                             "The write operation timed out");
             goto error;
         } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
@@ -1211,7 +1211,7 @@ static PyObject *PySSL_SSLread(PySSLSocket *self, PyObject *args)
     if (!count) {
         sockstate = check_socket_and_wait_for_timeout(sock, 0);
         if (sockstate == SOCKET_HAS_TIMED_OUT) {
-            PyErr_SetString(PySSLErrorObject,
+            PyErr_SetString(PySocketModule.timeout_error,
                             "The read operation timed out");
             goto error;
         } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
@@ -1245,7 +1245,7 @@ static PyObject *PySSL_SSLread(PySSLSocket *self, PyObject *args)
             sockstate = SOCKET_OPERATION_OK;
         }
         if (sockstate == SOCKET_HAS_TIMED_OUT) {
-            PyErr_SetString(PySSLErrorObject,
+            PyErr_SetString(PySocketModule.timeout_error,
                             "The read operation timed out");
             goto error;
         } else if (sockstate == SOCKET_IS_NONBLOCKING) {
@@ -1340,10 +1340,10 @@ static PyObject *PySSL_SSLshutdown(PySSLSocket *self)
             break;
         if (sockstate == SOCKET_HAS_TIMED_OUT) {
             if (ssl_err == SSL_ERROR_WANT_READ)
-                PyErr_SetString(PySSLErrorObject,
+                PyErr_SetString(PySocketModule.timeout_error,
                                 "The read operation timed out");
             else
-                PyErr_SetString(PySSLErrorObject,
+                PyErr_SetString(PySocketModule.timeout_error,
                                 "The write operation timed out");
             goto error;
         }
index e24bf546c7d9b783a44f714f2f8e6890399584de..abdd1b2b47ad2bd18921b1ebf686f6eb225a36c3 100644 (file)
@@ -4358,6 +4358,7 @@ static
 PySocketModule_APIObject PySocketModuleAPI =
 {
     &sock_type,
+    NULL,
     NULL
 };
 
@@ -4425,6 +4426,7 @@ PyInit__socket(void)
                                         socket_error, NULL);
     if (socket_timeout == NULL)
         return NULL;
+    PySocketModuleAPI.timeout_error = socket_timeout;
     Py_INCREF(socket_timeout);
     PyModule_AddObject(m, "timeout", socket_timeout);
     Py_INCREF((PyObject *)&sock_type);
index 4f426f5d85c937b1f3ea2a3889b50c39b7a71568..f064795b80f2662bd3f0d5cb4c0aabbc6438707a 100644 (file)
@@ -196,6 +196,7 @@ typedef struct {
 typedef struct {
     PyTypeObject *Sock_Type;
     PyObject *error;
+    PyObject *timeout_error;
 } PySocketModule_APIObject;
 
 #define PySocketModule_ImportModuleAndAPI() PyCapsule_Import(PySocket_CAPSULE_NAME, 1)