Skip to content

Commit 53971b6

Browse files
committed
Issue #10272: The ssl module now raises socket.timeout instead of a generic
SSLError on socket timeouts.
1 parent 26f0885 commit 53971b6

5 files changed

Lines changed: 15 additions & 9 deletions

File tree

Lib/test/test_ssl.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,7 +1499,7 @@ def serve():
14991499
c.settimeout(0.2)
15001500
c.connect((host, port))
15011501
# Will attempt handshake and time out
1502-
self.assertRaisesRegex(ssl.SSLError, "timed out",
1502+
self.assertRaisesRegex(socket.timeout, "timed out",
15031503
ssl.wrap_socket, c)
15041504
finally:
15051505
c.close()
@@ -1508,7 +1508,7 @@ def serve():
15081508
c = ssl.wrap_socket(c)
15091509
c.settimeout(0.2)
15101510
# Will attempt handshake and time out
1511-
self.assertRaisesRegex(ssl.SSLError, "timed out",
1511+
self.assertRaisesRegex(socket.timeout, "timed out",
15121512
c.connect, (host, port))
15131513
finally:
15141514
c.close()

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ Core and Builtins
3535
Library
3636
-------
3737

38+
- Issue #10272: The ssl module now raises socket.timeout instead of a generic
39+
SSLError on socket timeouts.
40+
3841
- Issue #10528: Allow translators to reorder placeholders in localizable
3942
messages from argparse.
4043

Modules/_ssl.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ static PyObject *PySSL_SSLdo_handshake(PySSLSocket *self)
370370
sockstate = SOCKET_OPERATION_OK;
371371
}
372372
if (sockstate == SOCKET_HAS_TIMED_OUT) {
373-
PyErr_SetString(PySSLErrorObject,
373+
PyErr_SetString(PySocketModule.timeout_error,
374374
ERRSTR("The handshake operation timed out"));
375375
goto error;
376376
} else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
@@ -1075,7 +1075,7 @@ static PyObject *PySSL_SSLwrite(PySSLSocket *self, PyObject *args)
10751075

10761076
sockstate = check_socket_and_wait_for_timeout(sock, 1);
10771077
if (sockstate == SOCKET_HAS_TIMED_OUT) {
1078-
PyErr_SetString(PySSLErrorObject,
1078+
PyErr_SetString(PySocketModule.timeout_error,
10791079
"The write operation timed out");
10801080
goto error;
10811081
} else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
@@ -1104,7 +1104,7 @@ static PyObject *PySSL_SSLwrite(PySSLSocket *self, PyObject *args)
11041104
sockstate = SOCKET_OPERATION_OK;
11051105
}
11061106
if (sockstate == SOCKET_HAS_TIMED_OUT) {
1107-
PyErr_SetString(PySSLErrorObject,
1107+
PyErr_SetString(PySocketModule.timeout_error,
11081108
"The write operation timed out");
11091109
goto error;
11101110
} else if (sockstate == SOCKET_HAS_BEEN_CLOSED) {
@@ -1211,7 +1211,7 @@ static PyObject *PySSL_SSLread(PySSLSocket *self, PyObject *args)
12111211
if (!count) {
12121212
sockstate = check_socket_and_wait_for_timeout(sock, 0);
12131213
if (sockstate == SOCKET_HAS_TIMED_OUT) {
1214-
PyErr_SetString(PySSLErrorObject,
1214+
PyErr_SetString(PySocketModule.timeout_error,
12151215
"The read operation timed out");
12161216
goto error;
12171217
} else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) {
@@ -1245,7 +1245,7 @@ static PyObject *PySSL_SSLread(PySSLSocket *self, PyObject *args)
12451245
sockstate = SOCKET_OPERATION_OK;
12461246
}
12471247
if (sockstate == SOCKET_HAS_TIMED_OUT) {
1248-
PyErr_SetString(PySSLErrorObject,
1248+
PyErr_SetString(PySocketModule.timeout_error,
12491249
"The read operation timed out");
12501250
goto error;
12511251
} else if (sockstate == SOCKET_IS_NONBLOCKING) {
@@ -1340,10 +1340,10 @@ static PyObject *PySSL_SSLshutdown(PySSLSocket *self)
13401340
break;
13411341
if (sockstate == SOCKET_HAS_TIMED_OUT) {
13421342
if (ssl_err == SSL_ERROR_WANT_READ)
1343-
PyErr_SetString(PySSLErrorObject,
1343+
PyErr_SetString(PySocketModule.timeout_error,
13441344
"The read operation timed out");
13451345
else
1346-
PyErr_SetString(PySSLErrorObject,
1346+
PyErr_SetString(PySocketModule.timeout_error,
13471347
"The write operation timed out");
13481348
goto error;
13491349
}

Modules/socketmodule.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4358,6 +4358,7 @@ static
43584358
PySocketModule_APIObject PySocketModuleAPI =
43594359
{
43604360
&sock_type,
4361+
NULL,
43614362
NULL
43624363
};
43634364

@@ -4425,6 +4426,7 @@ PyInit__socket(void)
44254426
socket_error, NULL);
44264427
if (socket_timeout == NULL)
44274428
return NULL;
4429+
PySocketModuleAPI.timeout_error = socket_timeout;
44284430
Py_INCREF(socket_timeout);
44294431
PyModule_AddObject(m, "timeout", socket_timeout);
44304432
Py_INCREF((PyObject *)&sock_type);

Modules/socketmodule.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ typedef struct {
196196
typedef struct {
197197
PyTypeObject *Sock_Type;
198198
PyObject *error;
199+
PyObject *timeout_error;
199200
} PySocketModule_APIObject;
200201

201202
#define PySocketModule_ImportModuleAndAPI() PyCapsule_Import(PySocket_CAPSULE_NAME, 1)

0 commit comments

Comments
 (0)