Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-113812: Allow DatagramTransport.sendto to send empty data#115199

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall 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
5 changes: 5 additions & 0 deletionsDoc/library/asyncio-protocol.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -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
Expand Down
6 changes: 6 additions & 0 deletionsDoc/whatsnew/3.13.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -200,6 +200,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
----

Expand Down
5 changes: 1 addition & 4 deletionsLib/asyncio/proactor_events.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -487,9 +487,6 @@ def sendto(self, data, addr=None):
raiseTypeError('data argument must be bytes-like object (%r)',
type(data))

ifnotdata:
return

ifself._addressisnotNoneandaddrnotin (None,self._address):
raiseValueError(
f'Invalid address: must be None or{self._address}')
Expand All@@ -502,7 +499,7 @@ def sendto(self, data, addr=None):

# 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

ifself._write_futisNone:
# No current write operations are active, kick one off
Expand Down
4 changes: 1 addition & 3 deletionsLib/asyncio/selector_events.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1241,8 +1241,6 @@ def sendto(self, data, addr=None):
ifnotisinstance(data, (bytes,bytearray,memoryview)):
raiseTypeError(f'data argument must be a bytes-like object, '
f'not{type(data).__name__!r}')
ifnotdata:
return

ifself._address:
ifaddrnotin (None,self._address):
Expand DownExpand Up@@ -1278,7 +1276,7 @@ def sendto(self, data, addr=None):

# 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):
Expand Down
2 changes: 2 additions & 0 deletionsLib/asyncio/transports.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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.
"""
raiseNotImplementedError

Expand Down
22 changes: 17 additions & 5 deletionsLib/test/test_asyncio/test_proactor_events.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -585,11 +585,10 @@ def test_sendto_memoryview(self):

deftest_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))

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

deftest_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')
deftest_sendto_exception(self,m_log):
data=b'data'
Expand Down
19 changes: 15 additions & 4 deletionsLib/test/test_asyncio/test_selector_events.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1280,11 +1280,10 @@ def test_sendto_memoryview(self):

deftest_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)))

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

deftest_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)

deftest_sendto_tryagain(self):
data=b'data'

Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -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.

[8]ページ先頭

©2009-2025 Movatter.jp