Skip to content

Commit

Permalink
bpo-39006: Fix asyncio when the ssl module is missing (pythonGH-17524)
Browse files Browse the repository at this point in the history
Fix asyncio when the ssl module is missing: only check for
ssl.SSLSocket instance if the ssl module is available.
  • Loading branch information
vstinner authored Dec 9, 2019
1 parent 0131aba commit 82b4950
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
20 changes: 10 additions & 10 deletions Lib/asyncio/selector_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,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.
Expand Down Expand Up @@ -348,8 +353,7 @@ async def sock_recv(self, sock, n):
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")
try:
Expand Down Expand Up @@ -388,8 +392,7 @@ async def sock_recv_into(self, sock, buf):
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")
try:
Expand Down Expand Up @@ -429,8 +432,7 @@ async def sock_sendall(self, sock, data):
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")
try:
Expand Down Expand Up @@ -478,8 +480,7 @@ async def sock_connect(self, sock, address):
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")

Expand Down Expand Up @@ -541,8 +542,7 @@ async def sock_accept(self, sock):
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()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix asyncio when the ssl module is missing: only check for ssl.SSLSocket
instance if the ssl module is available.

0 comments on commit 82b4950

Please sign in to comment.