Skip to content

Commit a0078d9

Browse files
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 82b4950) Co-authored-by: Victor Stinner <[email protected]>
1 parent 21e1138 commit a0078d9

2 files changed

Lines changed: 12 additions & 10 deletions

File tree

Lib/asyncio/selector_events.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ def _test_selector_event(selector, fd, event):
3939
return bool(key.events & event)
4040

4141

42+
def _check_ssl_socket(sock):
43+
if ssl is not None and isinstance(sock, ssl.SSLSocket):
44+
raise TypeError("Socket cannot be of type SSLSocket")
45+
46+
4247
class BaseSelectorEventLoop(base_events.BaseEventLoop):
4348
"""Selector event loop.
4449
@@ -345,8 +350,7 @@ async def sock_recv(self, sock, n):
345350
The maximum amount of data to be received at once is specified by
346351
nbytes.
347352
"""
348-
if isinstance(sock, ssl.SSLSocket):
349-
raise TypeError("Socket cannot be of type SSLSocket")
353+
_check_ssl_socket(sock)
350354
if self._debug and sock.gettimeout() != 0:
351355
raise ValueError("the socket must be non-blocking")
352356
fut = self.create_future()
@@ -380,8 +384,7 @@ async def sock_recv_into(self, sock, buf):
380384
The received data is written into *buf* (a writable buffer).
381385
The return value is the number of bytes written.
382386
"""
383-
if isinstance(sock, ssl.SSLSocket):
384-
raise TypeError("Socket cannot be of type SSLSocket")
387+
_check_ssl_socket(sock)
385388
if self._debug and sock.gettimeout() != 0:
386389
raise ValueError("the socket must be non-blocking")
387390
fut = self.create_future()
@@ -419,8 +422,7 @@ async def sock_sendall(self, sock, data):
419422
raised, and there is no way to determine how much data, if any, was
420423
successfully processed by the receiving end of the connection.
421424
"""
422-
if isinstance(sock, ssl.SSLSocket):
423-
raise TypeError("Socket cannot be of type SSLSocket")
425+
_check_ssl_socket(sock)
424426
if self._debug and sock.gettimeout() != 0:
425427
raise ValueError("the socket must be non-blocking")
426428
fut = self.create_future()
@@ -457,8 +459,7 @@ async def sock_connect(self, sock, address):
457459
458460
This method is a coroutine.
459461
"""
460-
if isinstance(sock, ssl.SSLSocket):
461-
raise TypeError("Socket cannot be of type SSLSocket")
462+
_check_ssl_socket(sock)
462463
if self._debug and sock.gettimeout() != 0:
463464
raise ValueError("the socket must be non-blocking")
464465

@@ -516,8 +517,7 @@ async def sock_accept(self, sock):
516517
object usable to send and receive data on the connection, and address
517518
is the address bound to the socket on the other end of the connection.
518519
"""
519-
if isinstance(sock, ssl.SSLSocket):
520-
raise TypeError("Socket cannot be of type SSLSocket")
520+
_check_ssl_socket(sock)
521521
if self._debug and sock.gettimeout() != 0:
522522
raise ValueError("the socket must be non-blocking")
523523
fut = self.create_future()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix asyncio when the ssl module is missing: only check for ssl.SSLSocket
2+
instance if the ssl module is available.

0 commit comments

Comments
 (0)