diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2011-05-18 16:51:06 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2011-05-18 16:51:06 (GMT) |
commit | b4410dbea699eb59358d3d61f45749173167d69a (patch) | |
tree | 8cb1989e16c2045303068fe3489e47bdb2e35ece /Lib | |
parent | 3349bca46d66f993e97407ba756e3f515a863fad (diff) | |
download | cpython-b4410dbea699eb59358d3d61f45749173167d69a.zip cpython-b4410dbea699eb59358d3d61f45749173167d69a.tar.gz cpython-b4410dbea699eb59358d3d61f45749173167d69a.tar.bz2 |
Issue #12065: connect_ex() on an SSL socket now returns the original errno
when the socket's timeout expires (it used to return None).
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/ssl.py | 24 | ||||
-rw-r--r-- | Lib/test/test_ssl.py | 17 |
2 files changed, 30 insertions, 11 deletions
@@ -442,7 +442,7 @@ class SSLSocket(socket): finally: self.settimeout(timeout) - def _real_connect(self, addr, return_errno): + def _real_connect(self, addr, connect_ex): if self.server_side: raise ValueError("can't connect in server-side mode") # Here we assume that the socket is client-side, and not @@ -451,17 +451,19 @@ class SSLSocket(socket): raise ValueError("attempt to connect already-connected SSLSocket!") self._sslobj = self.context._wrap_socket(self, False, self.server_hostname) try: - socket.connect(self, addr) - if self.do_handshake_on_connect: - self.do_handshake() - except socket_error as e: - if return_errno: - return e.errno + if connect_ex: + rc = socket.connect_ex(self, addr) else: - self._sslobj = None - raise e - self._connected = True - return 0 + rc = None + socket.connect(self, addr) + if not rc: + if self.do_handshake_on_connect: + self.do_handshake() + self._connected = True + return rc + except socket_error: + self._sslobj = None + raise def connect(self, addr): """Connects to remote ADDR, and then wraps the connection in diff --git a/Lib/test/test_ssl.py b/Lib/test/test_ssl.py index f65aceb..7edf5b2 100644 --- a/Lib/test/test_ssl.py +++ b/Lib/test/test_ssl.py @@ -519,6 +519,23 @@ class NetworkedTests(unittest.TestCase): finally: s.close() + def test_timeout_connect_ex(self): + # Issue #12065: on a timeout, connect_ex() should return the original + # errno (mimicking the behaviour of non-SSL sockets). + with support.transient_internet("svn.python.org"): + s = ssl.wrap_socket(socket.socket(socket.AF_INET), + cert_reqs=ssl.CERT_REQUIRED, + ca_certs=SVN_PYTHON_ORG_ROOT_CERT, + do_handshake_on_connect=False) + try: + s.settimeout(0.0000001) + rc = s.connect_ex(('svn.python.org', 443)) + if rc == 0: + self.skipTest("svn.python.org responded too quickly") + self.assertIn(rc, (errno.EAGAIN, errno.EWOULDBLOCK)) + finally: + s.close() + def test_connect_with_context(self): with support.transient_internet("svn.python.org"): # Same as test_connect, but with a separately created context |