Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 82b4950

Browse files
authored
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.
1 parent 0131aba commit 82b4950

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

Lib/asyncio/selector_events.py

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

4242

43+
def _check_ssl_socket(sock):
44+
if ssl is not None and isinstance(sock, ssl.SSLSocket):
45+
raise TypeError("Socket cannot be of type SSLSocket")
46+
47+
4348
class BaseSelectorEventLoop(base_events.BaseEventLoop):
4449
"""Selector event loop.
4550
@@ -348,8 +353,7 @@ async def sock_recv(self, sock, n):
348353
The maximum amount of data to be received at once is specified by
349354
nbytes.
350355
"""
351-
if isinstance(sock, ssl.SSLSocket):
352-
raise TypeError("Socket cannot be of type SSLSocket")
356+
_check_ssl_socket(sock)
353357
if self._debug and sock.gettimeout() != 0:
354358
raise ValueError("the socket must be non-blocking")
355359
try:
@@ -388,8 +392,7 @@ async def sock_recv_into(self, sock, buf):
388392
The received data is written into *buf* (a writable buffer).
389393
The return value is the number of bytes written.
390394
"""
391-
if isinstance(sock, ssl.SSLSocket):
392-
raise TypeError("Socket cannot be of type SSLSocket")
395+
_check_ssl_socket(sock)
393396
if self._debug and sock.gettimeout() != 0:
394397
raise ValueError("the socket must be non-blocking")
395398
try:
@@ -429,8 +432,7 @@ async def sock_sendall(self, sock, data):
429432
raised, and there is no way to determine how much data, if any, was
430433
successfully processed by the receiving end of the connection.
431434
"""
432-
if isinstance(sock, ssl.SSLSocket):
433-
raise TypeError("Socket cannot be of type SSLSocket")
435+
_check_ssl_socket(sock)
434436
if self._debug and sock.gettimeout() != 0:
435437
raise ValueError("the socket must be non-blocking")
436438
try:
@@ -478,8 +480,7 @@ async def sock_connect(self, sock, address):
478480
479481
This method is a coroutine.
480482
"""
481-
if isinstance(sock, ssl.SSLSocket):
482-
raise TypeError("Socket cannot be of type SSLSocket")
483+
_check_ssl_socket(sock)
483484
if self._debug and sock.gettimeout() != 0:
484485
raise ValueError("the socket must be non-blocking")
485486

@@ -541,8 +542,7 @@ async def sock_accept(self, sock):
541542
object usable to send and receive data on the connection, and address
542543
is the address bound to the socket on the other end of the connection.
543544
"""
544-
if isinstance(sock, ssl.SSLSocket):
545-
raise TypeError("Socket cannot be of type SSLSocket")
545+
_check_ssl_socket(sock)
546546
if self._debug and sock.gettimeout() != 0:
547547
raise ValueError("the socket must be non-blocking")
548548
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)