mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
gh-99941: Ensure that asyncio.Protocol.data_received receives immutable bytes (#100053)
This commit is contained in:
parent
d5f8a2b6ad
commit
1bb68ba6d9
4 changed files with 10 additions and 4 deletions
|
@ -288,7 +288,8 @@ class _ProactorReadPipeTransport(_ProactorBasePipeTransport,
|
||||||
# we got end-of-file so no need to reschedule a new read
|
# we got end-of-file so no need to reschedule a new read
|
||||||
return
|
return
|
||||||
|
|
||||||
data = self._data[:length]
|
# It's a new slice so make it immutable so protocols upstream don't have problems
|
||||||
|
data = bytes(memoryview(self._data)[:length])
|
||||||
else:
|
else:
|
||||||
# the future will be replaced by next proactor.recv call
|
# the future will be replaced by next proactor.recv call
|
||||||
fut.cancel()
|
fut.cancel()
|
||||||
|
|
|
@ -688,7 +688,7 @@ class StreamReader:
|
||||||
await self._wait_for_data('read')
|
await self._wait_for_data('read')
|
||||||
|
|
||||||
# This will work right even if buffer is less than n bytes
|
# This will work right even if buffer is less than n bytes
|
||||||
data = bytes(self._buffer[:n])
|
data = bytes(memoryview(self._buffer)[:n])
|
||||||
del self._buffer[:n]
|
del self._buffer[:n]
|
||||||
|
|
||||||
self._maybe_resume_transport()
|
self._maybe_resume_transport()
|
||||||
|
@ -730,7 +730,7 @@ class StreamReader:
|
||||||
data = bytes(self._buffer)
|
data = bytes(self._buffer)
|
||||||
self._buffer.clear()
|
self._buffer.clear()
|
||||||
else:
|
else:
|
||||||
data = bytes(self._buffer[:n])
|
data = bytes(memoryview(self._buffer)[:n])
|
||||||
del self._buffer[:n]
|
del self._buffer[:n]
|
||||||
self._maybe_resume_transport()
|
self._maybe_resume_transport()
|
||||||
return data
|
return data
|
||||||
|
|
|
@ -75,7 +75,10 @@ class ProactorSocketTransportTests(test_utils.TestCase):
|
||||||
called_buf = bytearray(self.buffer_size)
|
called_buf = bytearray(self.buffer_size)
|
||||||
called_buf[:len(buf)] = buf
|
called_buf[:len(buf)] = buf
|
||||||
self.loop._proactor.recv_into.assert_called_with(self.sock, called_buf)
|
self.loop._proactor.recv_into.assert_called_with(self.sock, called_buf)
|
||||||
self.protocol.data_received.assert_called_with(bytearray(buf))
|
self.protocol.data_received.assert_called_with(buf)
|
||||||
|
# assert_called_with maps bytearray and bytes to the same thing so check manually
|
||||||
|
# regression test for https://github.com/python/cpython/issues/99941
|
||||||
|
self.assertIsInstance(self.protocol.data_received.call_args.args[0], bytes)
|
||||||
|
|
||||||
@unittest.skipIf(sys.flags.optimize, "Assertions are disabled in optimized mode")
|
@unittest.skipIf(sys.flags.optimize, "Assertions are disabled in optimized mode")
|
||||||
def test_loop_reading_no_data(self):
|
def test_loop_reading_no_data(self):
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Ensure that :func:`asyncio.Protocol.data_received` receives an immutable
|
||||||
|
:class:`bytes` object (as documented), instead of :class:`bytearray`.
|
Loading…
Add table
Add a link
Reference in a new issue