[Python-checkins] bpo-39006: Fix asyncio when the ssl module is missing (GH-17524)

Miss Islington (bot) webhook-mailer at python.org
Mon Dec 9 09:20:32 EST 2019


https://github.com/python/cpython/commit/a0078d9a3335a5e99afe97590fdcb8e2f526ed7b
commit: a0078d9a3335a5e99afe97590fdcb8e2f526ed7b
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-12-09T06:20:27-08:00
summary:

bpo-39006: Fix asyncio when the ssl module is missing (GH-17524)


Fix asyncio when the ssl module is missing: only check for
ssl.SSLSocket instance if the ssl module is available.
(cherry picked from commit 82b4950b5e92bec343a436b3f9c116400b66e1b9)

Co-authored-by: Victor Stinner <vstinner at python.org>

files:
A Misc/NEWS.d/next/Library/2019-12-09-14-40-09.bpo-39006.v4VsPg.rst
M Lib/asyncio/selector_events.py

diff --git a/Lib/asyncio/selector_events.py b/Lib/asyncio/selector_events.py
index c539b5d387196..fa8f0cd759898 100644
--- a/Lib/asyncio/selector_events.py
+++ b/Lib/asyncio/selector_events.py
@@ -39,6 +39,11 @@ def _test_selector_event(selector, fd, event):
         return bool(key.events & event)
 
 
+def _check_ssl_socket(sock):
+    if ssl is not None and isinstance(sock, ssl.SSLSocket):
+        raise TypeError("Socket cannot be of type SSLSocket")
+
+
 class BaseSelectorEventLoop(base_events.BaseEventLoop):
     """Selector event loop.
 
@@ -345,8 +350,7 @@ def remove_writer(self, fd):
         The maximum amount of data to be received at once is specified by
         nbytes.
         """
-        if isinstance(sock, ssl.SSLSocket):
-            raise TypeError("Socket cannot be of type SSLSocket")
+        _check_ssl_socket(sock)
         if self._debug and sock.gettimeout() != 0:
             raise ValueError("the socket must be non-blocking")
         fut = self.create_future()
@@ -380,8 +384,7 @@ def _sock_recv(self, fut, registered_fd, sock, n):
         The received data is written into *buf* (a writable buffer).
         The return value is the number of bytes written.
         """
-        if isinstance(sock, ssl.SSLSocket):
-            raise TypeError("Socket cannot be of type SSLSocket")
+        _check_ssl_socket(sock)
         if self._debug and sock.gettimeout() != 0:
             raise ValueError("the socket must be non-blocking")
         fut = self.create_future()
@@ -419,8 +422,7 @@ def _sock_recv_into(self, fut, registered_fd, sock, buf):
         raised, and there is no way to determine how much data, if any, was
         successfully processed by the receiving end of the connection.
         """
-        if isinstance(sock, ssl.SSLSocket):
-            raise TypeError("Socket cannot be of type SSLSocket")
+        _check_ssl_socket(sock)
         if self._debug and sock.gettimeout() != 0:
             raise ValueError("the socket must be non-blocking")
         fut = self.create_future()
@@ -457,8 +459,7 @@ def _sock_sendall(self, fut, registered_fd, sock, data):
 
         This method is a coroutine.
         """
-        if isinstance(sock, ssl.SSLSocket):
-            raise TypeError("Socket cannot be of type SSLSocket")
+        _check_ssl_socket(sock)
         if self._debug and sock.gettimeout() != 0:
             raise ValueError("the socket must be non-blocking")
 
@@ -516,8 +517,7 @@ def _sock_connect_cb(self, fut, sock, address):
         object usable to send and receive data on the connection, and address
         is the address bound to the socket on the other end of the connection.
         """
-        if isinstance(sock, ssl.SSLSocket):
-            raise TypeError("Socket cannot be of type SSLSocket")
+        _check_ssl_socket(sock)
         if self._debug and sock.gettimeout() != 0:
             raise ValueError("the socket must be non-blocking")
         fut = self.create_future()
diff --git a/Misc/NEWS.d/next/Library/2019-12-09-14-40-09.bpo-39006.v4VsPg.rst b/Misc/NEWS.d/next/Library/2019-12-09-14-40-09.bpo-39006.v4VsPg.rst
new file mode 100644
index 0000000000000..8402845a5a047
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-12-09-14-40-09.bpo-39006.v4VsPg.rst
@@ -0,0 +1,2 @@
+Fix asyncio when the ssl module is missing: only check for ssl.SSLSocket
+instance if the ssl module is available.



More information about the Python-checkins mailing list