]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-113812: Allow DatagramTransport.sendto to send empty data (#115199)
authorJamie Phan <jamie@ordinarylab.dev>
Sat, 17 Feb 2024 02:38:07 +0000 (13:38 +1100)
committerGitHub <noreply@github.com>
Sat, 17 Feb 2024 02:38:07 +0000 (18:38 -0800)
Also include the UDP packet header sizes (8 bytes per packet)
in the buffer size reported to the flow control subsystem.

Doc/library/asyncio-protocol.rst
Doc/whatsnew/3.13.rst
Lib/asyncio/proactor_events.py
Lib/asyncio/selector_events.py
Lib/asyncio/transports.py
Lib/test/test_asyncio/test_proactor_events.py
Lib/test/test_asyncio/test_selector_events.py
Misc/NEWS.d/next/Library/2024-02-09-12-22-47.gh-issue-113812.wOraaG.rst [new file with mode: 0644]

index ecd8cdc709af7d2d3bc6cc01f7ba869bb80e133d..7c08d65f26bc27e1495d479731b5da273c1d9b11 100644 (file)
@@ -362,6 +362,11 @@ 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. The buffer size calculation used for flow
+      control is also updated to account for the datagram header.
+
 .. method:: DatagramTransport.abort()
 
    Close the transport immediately, without waiting for pending
index 1e0764144a2855389f81dcdcfaf9597706153679..7c6a2af28758bee18a5606d8bb4be2b4803ab0a1 100644 (file)
@@ -218,6 +218,12 @@ asyncio
   the Unix socket when the server is closed.
   (Contributed by Pierre Ossman in :gh:`111246`.)
 
+* :meth:`asyncio.DatagramTransport.sendto` will now send zero-length
+  datagrams if called with an empty bytes object. The transport flow
+  control also now accounts for the datagram header when calculating
+  the buffer size.
+  (Contributed by Jamie Phan in :gh:`115199`.)
+
 copy
 ----
 
index 1e2a730cf368a99f51a049352a376eb4a63597b3..a512db6367b20ac0a8801f5361364a924ffd7d23 100644 (file)
@@ -487,9 +487,6 @@ class _ProactorDatagramTransport(_ProactorBasePipeTransport,
             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}')
@@ -502,7 +499,7 @@ class _ProactorDatagramTransport(_ProactorBasePipeTransport,
 
         # Ensure that what we buffer is immutable.
         self._buffer.append((bytes(data), addr))
-        self._buffer_size += len(data)
+        self._buffer_size += len(data) + 8  # include header bytes
 
         if self._write_fut is None:
             # No current write operations are active, kick one off
index 10fbdd76e93f79c079f155e8cc0a393b57b290e4..8e888d26ea073775d1c4ded0f87cd2e21d62d785 100644 (file)
@@ -1241,8 +1241,6 @@ class _SelectorDatagramTransport(_SelectorTransport, transports.DatagramTranspor
         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):
@@ -1278,7 +1276,7 @@ class _SelectorDatagramTransport(_SelectorTransport, transports.DatagramTranspor
 
         # Ensure that what we buffer is immutable.
         self._buffer.append((bytes(data), addr))
-        self._buffer_size += len(data)
+        self._buffer_size += len(data) + 8  # include header bytes
         self._maybe_pause_protocol()
 
     def _sendto_ready(self):
index 30fd41d49af71ff4594a88f67df96692d9ebea41..34c7ad44ffd8ab3e65f45f938f258a387f86cbb4 100644 (file)
@@ -181,6 +181,8 @@ class DatagramTransport(BaseTransport):
         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
 
index c42856e578b8cc8b6d2ce332e6ccbeea4815852c..fcaa2f6ade2b7675d4ce8c6f713e95416059087b 100644 (file)
@@ -585,11 +585,10 @@ class ProactorDatagramTransportTests(test_utils.TestCase):
 
     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()
@@ -628,6 +627,19 @@ class ProactorDatagramTransportTests(test_utils.TestCase):
             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'
index c22b780b5edcb834361e3195d902c75af7494d81..aaeda33dd0c677fb0cdcbfedd438b38128a0b990 100644 (file)
@@ -1280,11 +1280,10 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
 
     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()
@@ -1320,6 +1319,18 @@ class SelectorDatagramTransportTests(test_utils.TestCase):
             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'
 
diff --git a/Misc/NEWS.d/next/Library/2024-02-09-12-22-47.gh-issue-113812.wOraaG.rst b/Misc/NEWS.d/next/Library/2024-02-09-12-22-47.gh-issue-113812.wOraaG.rst
new file mode 100644 (file)
index 0000000..7ef7bc8
--- /dev/null
@@ -0,0 +1,3 @@
+:meth:`DatagramTransport.sendto` will now send zero-length datagrams if
+called with an empty bytes object. The transport flow control also now
+accounts for the datagram header when calculating the buffer size.