From 40f12ab0c5c5adc4a8b4a03a57ffa94c87ecc2cb Mon Sep 17 00:00:00 2001 From: Antoine Pitrou Date: Fri, 28 Dec 2012 19:03:43 +0100 Subject: Backport Python 3.2 fix for issue #12065, and add another test for SSLSocket.connect_ex(). --- Lib/ssl.py | 20 +++++++++++--------- Lib/test/test_ssl.py | 28 ++++++++++++++++++++++++++++ Misc/NEWS | 3 +++ 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/Lib/ssl.py b/Lib/ssl.py index 1951a62..8829635 100644 --- a/Lib/ssl.py +++ b/Lib/ssl.py @@ -313,17 +313,19 @@ class SSLSocket(socket): self.cert_reqs, self.ssl_version, self.ca_certs, self.ciphers) 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 + 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 58da942..9f51387 100644 --- a/Lib/test/test_ssl.py +++ b/Lib/test/test_ssl.py @@ -280,6 +280,34 @@ 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 test_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_ex_error(self): + with test_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) + try: + self.assertEqual(errno.ECONNREFUSED, + s.connect_ex(("svn.python.org", 444))) + finally: + s.close() + @unittest.skipIf(os.name == "nt", "Can't use a socket as a file under Windows") def test_makefile_close(self): # Issue #5238: creating a file-like object with makefile() shouldn't diff --git a/Misc/NEWS b/Misc/NEWS index b718641..f087f41 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -175,6 +175,9 @@ Core and Builtins Library ------- +- Issue #12065: connect_ex() on an SSL socket now returns the original errno + when the socket's timeout expires (it used to return None). + - Issue #16504: IDLE now catches SyntaxErrors raised by tokenizer. Patch by Roger Serwy. -- cgit v0.12