--- Python.org/Lib/test/test_socket_ssl.py.org 2005年12月14日 23:04:19.000000000 +0100 +++ Python/Lib/test/test_socket_ssl.py 2005年12月14日 23:06:07.000000000 +0100 @@ -27,6 +27,19 @@ buf = f.read() f.close() +def test_timeout(): + test_support.requires('network') + + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.settimeout(30.0) + # connect to service which issues an welcome banner (without need to write anything) + s.connect(("gmail.org", 995)) + ss = socket.ssl(s) + # read part of return welcome banner twice,# read part of return welcome banner twice + ss.read(1) + ss.read(1) + s.close() + def test_rude_shutdown(): try: import thread @@ -63,6 +76,7 @@ raise test_support.TestSkipped("socket module has no ssl support") test_rude_shutdown() test_basic() + test_timeout() if __name__ == "__main__": test_main() --- Python.opg/Modules/_ssl.c.org 2005年12月15日 18:17:03.000000000 +0100 +++ Python/Modules/_ssl.c 2005年12月15日 18:18:42.000000000 +0100 @@ -455,6 +455,7 @@ int len = 1024; int sockstate; int err; + int pending; if (!PyArg_ParseTuple(args, "|i:read", &len)) return NULL; @@ -462,11 +463,17 @@ if (!(buf = PyString_FromStringAndSize((char *) 0, len))) return NULL; - sockstate = check_socket_and_wait_for_timeout(self->Socket, 0); - if (sockstate == SOCKET_HAS_TIMED_OUT) { - PyErr_SetString(PySSLErrorObject, "The read operation timed out"); - Py_DECREF(buf); - return NULL; + Py_BEGIN_ALLOW_THREADS + pending = SSL_pending(self->ssl); + Py_END_ALLOW_THREADS + + if (!pending) { + sockstate = check_socket_and_wait_for_timeout(self->Socket, 0); + if (sockstate == SOCKET_HAS_TIMED_OUT) { + PyErr_SetString(PySSLErrorObject, "The read operation timed out"); + Py_DECREF(buf); + return NULL; + } } do { err = 0;