Skip to content

bpo-37404: Fix check for ssl.SSLSocket #17526

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions Lib/asyncio/selector_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,17 +341,23 @@ def remove_writer(self, fd):
self._ensure_fd_no_transport(fd)
return self._remove_writer(fd)

def _check_socket(self, sock):
"""Check socket type and configuration in debug mode
"""
if ssl is not None and isinstance(sock, ssl.SSLSocket):
raise TypeError("Socket cannot be of type SSLSocket")
if sock.gettimeout() != 0:
raise ValueError("the socket must be non-blocking")

async def sock_recv(self, sock, n):
"""Receive data from the socket.

The return value is a bytes object representing the data received.
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")
if self._debug and sock.gettimeout() != 0:
raise ValueError("the socket must be non-blocking")
if self._debug:
self._check_socket(sock)
try:
return sock.recv(n)
except (BlockingIOError, InterruptedError):
Expand Down Expand Up @@ -388,10 +394,8 @@ 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")
if self._debug and sock.gettimeout() != 0:
raise ValueError("the socket must be non-blocking")
if self._debug:
self._check_socket(sock)
try:
return sock.recv_into(buf)
except (BlockingIOError, InterruptedError):
Expand Down Expand Up @@ -429,10 +433,8 @@ 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")
if self._debug and sock.gettimeout() != 0:
raise ValueError("the socket must be non-blocking")
if self._debug:
self._check_socket(sock)
try:
n = sock.send(data)
except (BlockingIOError, InterruptedError):
Expand Down Expand Up @@ -478,10 +480,8 @@ 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")
if self._debug and sock.gettimeout() != 0:
raise ValueError("the socket must be non-blocking")
if self._debug:
self._check_socket(sock)

if not hasattr(socket, 'AF_UNIX') or sock.family != socket.AF_UNIX:
resolved = await self._ensure_resolved(
Expand Down Expand Up @@ -541,10 +541,9 @@ 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")
if self._debug and sock.gettimeout() != 0:
raise ValueError("the socket must be non-blocking")
if self._debug:
self._check_socket(sock)

fut = self.create_future()
self._sock_accept(fut, False, sock)
return await fut
Expand Down