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 82b4950b5e)

Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
Miss Islington (bot) 2019-12-09 06:19:48 -08:00 committed by GitHub
parent 0381ea79ac
commit b22183f273
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 10 deletions

View File

@ -40,6 +40,11 @@ def _test_selector_event(selector, fd, event):
return bool(key.events & 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): class BaseSelectorEventLoop(base_events.BaseEventLoop):
"""Selector event loop. """Selector event loop.
@ -348,8 +353,7 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
The maximum amount of data to be received at once is specified by The maximum amount of data to be received at once is specified by
nbytes. nbytes.
""" """
if isinstance(sock, ssl.SSLSocket): _check_ssl_socket(sock)
raise TypeError("Socket cannot be of type SSLSocket")
if self._debug and sock.gettimeout() != 0: if self._debug and sock.gettimeout() != 0:
raise ValueError("the socket must be non-blocking") raise ValueError("the socket must be non-blocking")
try: try:
@ -388,8 +392,7 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
The received data is written into *buf* (a writable buffer). The received data is written into *buf* (a writable buffer).
The return value is the number of bytes written. The return value is the number of bytes written.
""" """
if isinstance(sock, ssl.SSLSocket): _check_ssl_socket(sock)
raise TypeError("Socket cannot be of type SSLSocket")
if self._debug and sock.gettimeout() != 0: if self._debug and sock.gettimeout() != 0:
raise ValueError("the socket must be non-blocking") raise ValueError("the socket must be non-blocking")
try: try:
@ -429,8 +432,7 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
raised, and there is no way to determine how much data, if any, was raised, and there is no way to determine how much data, if any, was
successfully processed by the receiving end of the connection. successfully processed by the receiving end of the connection.
""" """
if isinstance(sock, ssl.SSLSocket): _check_ssl_socket(sock)
raise TypeError("Socket cannot be of type SSLSocket")
if self._debug and sock.gettimeout() != 0: if self._debug and sock.gettimeout() != 0:
raise ValueError("the socket must be non-blocking") raise ValueError("the socket must be non-blocking")
try: try:
@ -478,8 +480,7 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
This method is a coroutine. This method is a coroutine.
""" """
if isinstance(sock, ssl.SSLSocket): _check_ssl_socket(sock)
raise TypeError("Socket cannot be of type SSLSocket")
if self._debug and sock.gettimeout() != 0: if self._debug and sock.gettimeout() != 0:
raise ValueError("the socket must be non-blocking") raise ValueError("the socket must be non-blocking")
@ -541,8 +542,7 @@ class BaseSelectorEventLoop(base_events.BaseEventLoop):
object usable to send and receive data on the connection, and 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. is the address bound to the socket on the other end of the connection.
""" """
if isinstance(sock, ssl.SSLSocket): _check_ssl_socket(sock)
raise TypeError("Socket cannot be of type SSLSocket")
if self._debug and sock.gettimeout() != 0: if self._debug and sock.gettimeout() != 0:
raise ValueError("the socket must be non-blocking") raise ValueError("the socket must be non-blocking")
fut = self.create_future() fut = self.create_future()

View File

@ -0,0 +1,2 @@
Fix asyncio when the ssl module is missing: only check for ssl.SSLSocket
instance if the ssl module is available.