From fe6a3a872a93419d33186b8670a8150fe200e37c Mon Sep 17 00:00:00 2001 From: Ullrich Koethe Date: Mon, 23 Jun 2014 14:40:40 +0200 Subject: [PATCH] check for errno.WSA* on 64-bit Windows (fixes #1052) --- tornado/iostream.py | 14 +++++++++++++- tornado/netutil.py | 3 +++ tornado/test/iostream_test.py | 5 ++++- tornado/test/simple_httpclient_test.py | 6 ++++-- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/tornado/iostream.py b/tornado/iostream.py index 8b6142582..3ebcd586f 100644 --- a/tornado/iostream.py +++ b/tornado/iostream.py @@ -57,12 +57,24 @@ except ImportError: # some they differ. _ERRNO_WOULDBLOCK = (errno.EWOULDBLOCK, errno.EAGAIN) +if hasattr(errno, "WSAEWOULDBLOCK"): + _ERRNO_WOULDBLOCK += (errno.WSAEWOULDBLOCK,) + # These errnos indicate that a connection has been abruptly terminated. # They should be caught and handled less noisily than other errors. _ERRNO_CONNRESET = (errno.ECONNRESET, errno.ECONNABORTED, errno.EPIPE, errno.ETIMEDOUT) +if hasattr(errno, "WSAECONNRESET"): + _ERRNO_CONNRESET += (errno.WSAECONNRESET, errno.WSAECONNABORTED, errno.WSAETIMEDOUT) + +# More non-portable errnos: +_ERRNO_INPROGRESS = (errno.EINPROGRESS,) + +if hasattr(errno, "WSAEINPROGRESS"): + _ERRNO_INPROGRESS += (errno.WSAEINPROGRESS,) +####################################################### class StreamClosedError(IOError): """Exception raised by `IOStream` methods when the stream is closed. @@ -990,7 +1002,7 @@ class IOStream(BaseIOStream): # returned immediately when attempting to connect to # localhost, so handle them the same way as an error # reported later in _handle_connect. - if (errno_from_exception(e) != errno.EINPROGRESS and + if (errno_from_exception(e) not in _ERRNO_INPROGRESS and errno_from_exception(e) not in _ERRNO_WOULDBLOCK): gen_log.warning("Connect error on fd %s: %s", self.socket.fileno(), e) diff --git a/tornado/netutil.py b/tornado/netutil.py index a9e05d1e6..336c80628 100644 --- a/tornado/netutil.py +++ b/tornado/netutil.py @@ -57,6 +57,9 @@ u('foo').encode('idna') # some they differ. _ERRNO_WOULDBLOCK = (errno.EWOULDBLOCK, errno.EAGAIN) +if hasattr(errno, "WSAEWOULDBLOCK"): + _ERRNO_WOULDBLOCK += (errno.WSAEWOULDBLOCK,) + def bind_sockets(port, address=None, family=socket.AF_UNSPEC, backlog=128, flags=None): """Creates listening sockets bound to the given port and address. diff --git a/tornado/test/iostream_test.py b/tornado/test/iostream_test.py index e9d241a5d..01b0d95ac 100644 --- a/tornado/test/iostream_test.py +++ b/tornado/test/iostream_test.py @@ -232,8 +232,11 @@ class TestIOStreamMixin(object): self.assertFalse(self.connect_called) self.assertTrue(isinstance(stream.error, socket.error), stream.error) if sys.platform != 'cygwin': + _ERRNO_CONNREFUSED = (errno.ECONNREFUSED,) + if hasattr(errno, "WSAECONNREFUSED"): + _ERRNO_CONNREFUSED += (errno.WSAECONNREFUSED,) # cygwin's errnos don't match those used on native windows python - self.assertEqual(stream.error.args[0], errno.ECONNREFUSED) + self.assertTrue(stream.error.args[0] in _ERRNO_CONNREFUSED) def test_gaierror(self): # Test that IOStream sets its exc_info on getaddrinfo error diff --git a/tornado/test/simple_httpclient_test.py b/tornado/test/simple_httpclient_test.py index 2ba9f75d7..f17da7e02 100644 --- a/tornado/test/simple_httpclient_test.py +++ b/tornado/test/simple_httpclient_test.py @@ -321,8 +321,10 @@ class SimpleHTTPClientTestMixin(object): if sys.platform != 'cygwin': # cygwin returns EPERM instead of ECONNREFUSED here - self.assertTrue(str(errno.ECONNREFUSED) in str(response.error), - response.error) + contains_errno = str(errno.ECONNREFUSED) in str(response.error) + if not contains_errno and hasattr(errno, "WSAECONNREFUSED"): + contains_errno = str(errno.WSAECONNREFUSED) in str(response.error) + self.assertTrue(contains_errno, response.error) # This is usually "Connection refused". # On windows, strerror is broken and returns "Unknown error". expected_message = os.strerror(errno.ECONNREFUSED) -- 2.47.2