Skip to content

Commit

Permalink
Allow DatagramTransport.sendto to send empty data
Browse files Browse the repository at this point in the history
  • Loading branch information
ordinary-jamie committed Feb 9, 2024
1 parent 1a10437 commit 67d6bed
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 14 deletions.
4 changes: 4 additions & 0 deletions Doc/library/asyncio-protocol.rst
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,10 @@ Datagram Transports
This method does not block; it buffers the data and arranges
for it to be sent out asynchronously.

.. versionchanged:: 3.13
This method can be called with an empty bytes object to send a
zero-length datagram.

.. method:: DatagramTransport.abort()

Close the transport immediately, without waiting for pending
Expand Down
3 changes: 0 additions & 3 deletions Lib/asyncio/proactor_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,9 +487,6 @@ def sendto(self, data, addr=None):
raise TypeError('data argument must be bytes-like object (%r)',
type(data))

if not data:
return

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 Down
2 changes: 0 additions & 2 deletions Lib/asyncio/selector_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -1241,8 +1241,6 @@ 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 not data:
return

if self._address:
if addr not in (None, self._address):
Expand Down
2 changes: 2 additions & 0 deletions Lib/asyncio/transports.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ def sendto(self, data, addr=None):
to be sent out asynchronously.
addr is target socket address.
If addr is None use target address pointed on transport creation.
If data is an empty bytes object a zero-length datagram will be
sent.
"""
raise NotImplementedError

Expand Down
22 changes: 17 additions & 5 deletions Lib/test/test_asyncio/test_proactor_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,11 +585,10 @@ def test_sendto_memoryview(self):

def test_sendto_no_data(self):
transport = self.datagram_transport()
transport._buffer.append((b'data', ('0.0.0.0', 12345)))
transport.sendto(b'', ())
self.assertFalse(self.sock.sendto.called)
self.assertEqual(
[(b'data', ('0.0.0.0', 12345))], list(transport._buffer))
transport.sendto(b'', ('0.0.0.0', 1234))
self.assertTrue(self.proactor.sendto.called)
self.proactor.sendto.assert_called_with(
self.sock, b'', addr=('0.0.0.0', 1234))

def test_sendto_buffer(self):
transport = self.datagram_transport()
Expand Down Expand Up @@ -628,6 +627,19 @@ def test_sendto_buffer_memoryview(self):
list(transport._buffer))
self.assertIsInstance(transport._buffer[1][0], bytes)

def test_sendto_buffer_nodata(self):
data2 = b''
transport = self.datagram_transport()
transport._buffer.append((b'data1', ('0.0.0.0', 12345)))
transport._write_fut = object()
transport.sendto(data2, ('0.0.0.0', 12345))
self.assertFalse(self.proactor.sendto.called)
self.assertEqual(
[(b'data1', ('0.0.0.0', 12345)),
(b'', ('0.0.0.0', 12345))],
list(transport._buffer))
self.assertIsInstance(transport._buffer[1][0], bytes)

@mock.patch('asyncio.proactor_events.logger')
def test_sendto_exception(self, m_log):
data = b'data'
Expand Down
19 changes: 15 additions & 4 deletions Lib/test/test_asyncio/test_selector_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -1280,11 +1280,10 @@ def test_sendto_memoryview(self):

def test_sendto_no_data(self):
transport = self.datagram_transport()
transport._buffer.append((b'data', ('0.0.0.0', 12345)))
transport.sendto(b'', ())
self.assertFalse(self.sock.sendto.called)
transport.sendto(b'', ('0.0.0.0', 1234))
self.assertTrue(self.sock.sendto.called)
self.assertEqual(
[(b'data', ('0.0.0.0', 12345))], list(transport._buffer))
self.sock.sendto.call_args[0], (b'', ('0.0.0.0', 1234)))

def test_sendto_buffer(self):
transport = self.datagram_transport()
Expand Down Expand Up @@ -1320,6 +1319,18 @@ def test_sendto_buffer_memoryview(self):
list(transport._buffer))
self.assertIsInstance(transport._buffer[1][0], bytes)

def test_sendto_buffer_nodata(self):
data2 = b''
transport = self.datagram_transport()
transport._buffer.append((b'data1', ('0.0.0.0', 12345)))
transport.sendto(data2, ('0.0.0.0', 12345))
self.assertFalse(self.sock.sendto.called)
self.assertEqual(
[(b'data1', ('0.0.0.0', 12345)),
(b'', ('0.0.0.0', 12345))],
list(transport._buffer))
self.assertIsInstance(transport._buffer[1][0], bytes)

def test_sendto_tryagain(self):
data = b'data'

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:meth:`DatagramTransport.sendto` will now send zero-length datagrams if
called with an empty bytes object.

0 comments on commit 67d6bed

Please sign in to comment.