From a30220da2e22dcc3d87c1987d52940725c8ed806 Mon Sep 17 00:00:00 2001 From: Frederick Price Date: Tue, 11 Jun 2024 22:42:34 -0400 Subject: [PATCH 1/2] Fix socket bugs caused by switching to a newer MSVC compiler Windows uses different error codes than Unix does, and our socket code was only checking for Unix errors by number. This was causing some things to fail that should have worked. I believe that in the newer Python's that these errors are folded from the Windows errors to Unix errors, but I can't find where to do that here in Python2. --- Lib/asyncore.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/Lib/asyncore.py b/Lib/asyncore.py index 105982f790cd1b..1dd2458679a828 100644 --- a/Lib/asyncore.py +++ b/Lib/asyncore.py @@ -53,12 +53,14 @@ import warnings import os -from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, EINVAL, \ +from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, EINVAL, \ ENOTCONN, ESHUTDOWN, EINTR, EISCONN, EBADF, ECONNABORTED, EPIPE, EAGAIN, \ - errorcode + errorcode, \ + WSAEWOULDBLOCK, WSAENOTCONN, WSAEINPROGRESS, WSAEALREADY, WSAEISCONN, \ + WSAECONNABORTED, WSAENOTCONN, WSAEBADF _DISCONNECTED = frozenset((ECONNRESET, ENOTCONN, ESHUTDOWN, ECONNABORTED, EPIPE, - EBADF)) + EBADF, WSAENOTCONN, WSAECONNABORTED, WSAEBADF)) try: socket_map @@ -249,7 +251,7 @@ def __init__(self, sock=None, map=None): try: self.addr = sock.getpeername() except socket.error, err: - if err.args[0] in (ENOTCONN, EINVAL): + if err.args[0] in (ENOTCONN, EINVAL, WSAENOTCONN): # To handle the case where we got an unconnected # socket. self.connected = False @@ -345,7 +347,7 @@ def connect(self, address): self.connected = False self.connecting = True err = self.socket.connect_ex(address) - if err in (EINPROGRESS, EALREADY, EWOULDBLOCK) \ + if err in (EINPROGRESS, EALREADY, EWOULDBLOCK, WSAEINPROGRESS, WSAEALREADY, WSAEWOULDBLOCK) \ or err == EINVAL and os.name in ('nt', 'ce'): self.addr = address return @@ -362,7 +364,7 @@ def accept(self): except TypeError: return None except socket.error as why: - if why.args[0] in (EWOULDBLOCK, ECONNABORTED, EAGAIN): + if why.args[0] in (EWOULDBLOCK, ECONNABORTED, EAGAIN, WSAEWOULDBLOCK, WSAECONNABORTED): return None else: raise @@ -374,7 +376,7 @@ def send(self, data): result = self.socket.send(data) return result except socket.error, why: - if why.args[0] == EWOULDBLOCK: + if why.args[0] in(EWOULDBLOCK, WSAEWOULDBLOCK): return 0 elif why.args[0] in _DISCONNECTED: self.handle_close() @@ -408,7 +410,7 @@ def close(self): try: self.socket.close() except socket.error, why: - if why.args[0] not in (ENOTCONN, EBADF): + if why.args[0] not in (ENOTCONN, EBADF, WSAENOTCONN, WSAEBADF): raise # cheap inheritance, used to pass all other attribute From 115210ba1a01fb3b454bed49789fd65bd9c9fcf9 Mon Sep 17 00:00:00 2001 From: Frederick Price Date: Tue, 18 Jun 2024 22:19:28 -0400 Subject: [PATCH 2/2] Fix formatting problem --- Lib/asyncore.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/asyncore.py b/Lib/asyncore.py index 1dd2458679a828..3134ddb6164c1e 100644 --- a/Lib/asyncore.py +++ b/Lib/asyncore.py @@ -53,14 +53,14 @@ import warnings import os -from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, EINVAL, \ +from errno import EALREADY, EINPROGRESS, EWOULDBLOCK, ECONNRESET, EINVAL, \ ENOTCONN, ESHUTDOWN, EINTR, EISCONN, EBADF, ECONNABORTED, EPIPE, EAGAIN, \ errorcode, \ WSAEWOULDBLOCK, WSAENOTCONN, WSAEINPROGRESS, WSAEALREADY, WSAEISCONN, \ WSAECONNABORTED, WSAENOTCONN, WSAEBADF _DISCONNECTED = frozenset((ECONNRESET, ENOTCONN, ESHUTDOWN, ECONNABORTED, EPIPE, - EBADF, WSAENOTCONN, WSAECONNABORTED, WSAEBADF)) + EBADF, WSAENOTCONN, WSAECONNABORTED, WSAEBADF)) try: socket_map