Skip to content
Closed
Show file tree
Hide file tree
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
11 changes: 2 additions & 9 deletions Lib/asyncio/proactor_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,6 @@ def __init__(self, *args, **kw):
self._empty_waiter = None

def write(self, data):
if not isinstance(data, (bytes, bytearray, memoryview)):
raise TypeError(
f"data argument must be a bytes-like object, "
f"not {type(data).__name__}")
if self._eof_written:
raise RuntimeError('write_eof() already called')
if self._empty_waiter is not None:
Expand Down Expand Up @@ -485,10 +481,6 @@ def abort(self):
self._force_close(None)

def sendto(self, data, addr=None):
if not isinstance(data, (bytes, bytearray, memoryview)):
raise TypeError('data argument must be bytes-like object (%r)',
type(data))

if self._address is not None and addr not in (None, self._address):
raise ValueError(
f'Invalid address: must be None or {self._address}')
Expand All @@ -500,7 +492,8 @@ def sendto(self, data, addr=None):
return

# Ensure that what we buffer is immutable.
self._buffer.append((bytes(data), addr))
data = bytes(data)
self._buffer.append((data, addr))
self._buffer_size += len(data) + self._header_size

if self._write_fut is None:
Expand Down
13 changes: 3 additions & 10 deletions Lib/asyncio/selector_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -1049,9 +1049,6 @@ def _read_ready__on_eof(self):
self.close()

def write(self, data):
if not isinstance(data, (bytes, bytearray, memoryview)):
raise TypeError(f'data argument must be a bytes-like object, '
f'not {type(data).__name__!r}')
if self._eof:
raise RuntimeError('Cannot call write() after write_eof()')
if self._empty_waiter is not None:
Expand All @@ -1071,7 +1068,7 @@ def write(self, data):
n = self._sock.send(data)
except (BlockingIOError, InterruptedError):
pass
except (SystemExit, KeyboardInterrupt):
except (TypeError, SystemExit, KeyboardInterrupt):
raise
except BaseException as exc:
self._fatal_error(exc, 'Fatal write error on socket transport')
Expand All @@ -1084,7 +1081,7 @@ def write(self, data):
self._loop._add_writer(self._sock_fd, self._write_ready)

# Add it to the buffer.
self._buffer.append(data)
self._buffer.append(bytes(data))
self._maybe_pause_protocol()

def _get_sendmsg_buffer(self):
Expand Down Expand Up @@ -1255,10 +1252,6 @@ def _read_ready(self):
self._protocol.datagram_received(data, addr)

def sendto(self, data, addr=None):
if not isinstance(data, (bytes, bytearray, memoryview)):
raise TypeError(f'data argument must be a bytes-like object, '
f'not {type(data).__name__!r}')

if self._address:
if addr not in (None, self._address):
raise ValueError(
Expand All @@ -1284,7 +1277,7 @@ def sendto(self, data, addr=None):
except OSError as exc:
self._protocol.error_received(exc)
return
except (SystemExit, KeyboardInterrupt):
except (TypeError, SystemExit, KeyboardInterrupt):
raise
except BaseException as exc:
self._fatal_error(
Expand Down
6 changes: 2 additions & 4 deletions Lib/asyncio/sslproto.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,10 @@ def write(self, data):
This does not block; it buffers the data and arranges for it
to be sent out asynchronously.
"""
if not isinstance(data, (bytes, bytearray, memoryview)):
raise TypeError(f"data: expecting a bytes-like instance, "
f"got {type(data).__name__}")
if not data:
return
self._ssl_protocol._write_appdata((data,))
# Ensure that what we buffer is immutable.
self._ssl_protocol._write_appdata((bytes(data),))

def writelines(self, list_of_data):
"""Write a list (or any iterable) of data bytes to the transport.
Expand Down
3 changes: 3 additions & 0 deletions Lib/test/test_asyncio/test_selector_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -1499,6 +1499,9 @@ def test_sendto_error_received_connected(self):

def test_sendto_str(self):
transport = self.datagram_transport()
def sendto(data, addr):
bytes(data) # raises TypeError
self.sock.sendto = sendto
self.assertRaises(TypeError, transport.sendto, 'str', ())

def test_sendto_connected_addr(self):
Expand Down
Loading