[Python-checkins] gh-99941: Ensure that asyncio.Protocol.data_received receives immutable bytes (#100053)
gvanrossum
webhook-mailer at python.org
Sat Dec 10 18:07:11 EST 2022
https://github.com/python/cpython/commit/1bb68ba6d9de6bb7f00aee11d135123163f15887
commit: 1bb68ba6d9de6bb7f00aee11d135123163f15887
branch: main
author: DarioDaF <dario.fagotto at gmail.com>
committer: gvanrossum <gvanrossum at gmail.com>
date: 2022-12-10T15:07:02-08:00
summary:
gh-99941: Ensure that asyncio.Protocol.data_received receives immutable bytes (#100053)
files:
A Misc/NEWS.d/next/Windows/2022-12-06-11-16-46.gh-issue-99941.GmUQ6o.rst
M Lib/asyncio/proactor_events.py
M Lib/asyncio/streams.py
M Lib/test/test_asyncio/test_proactor_events.py
diff --git a/Lib/asyncio/proactor_events.py b/Lib/asyncio/proactor_events.py
index c6aab408fc74..1e2a730cf368 100644
--- a/Lib/asyncio/proactor_events.py
+++ b/Lib/asyncio/proactor_events.py
@@ -288,7 +288,8 @@ def _loop_reading(self, fut=None):
# we got end-of-file so no need to reschedule a new read
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:
# the future will be replaced by next proactor.recv call
fut.cancel()
diff --git a/Lib/asyncio/streams.py b/Lib/asyncio/streams.py
index 3bd99043d096..0f9098b41956 100644
--- a/Lib/asyncio/streams.py
+++ b/Lib/asyncio/streams.py
@@ -688,7 +688,7 @@ async def read(self, n=-1):
await self._wait_for_data('read')
# 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]
self._maybe_resume_transport()
@@ -730,7 +730,7 @@ async def readexactly(self, n):
data = bytes(self._buffer)
self._buffer.clear()
else:
- data = bytes(self._buffer[:n])
+ data = bytes(memoryview(self._buffer)[:n])
del self._buffer[:n]
self._maybe_resume_transport()
return data
diff --git a/Lib/test/test_asyncio/test_proactor_events.py b/Lib/test/test_asyncio/test_proactor_events.py
index ae30185cef77..6cb7dc300c53 100644
--- a/Lib/test/test_asyncio/test_proactor_events.py
+++ b/Lib/test/test_asyncio/test_proactor_events.py
@@ -75,7 +75,10 @@ def test_loop_reading_data(self):
called_buf = bytearray(self.buffer_size)
called_buf[:len(buf)] = 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")
def test_loop_reading_no_data(self):
diff --git a/Misc/NEWS.d/next/Windows/2022-12-06-11-16-46.gh-issue-99941.GmUQ6o.rst b/Misc/NEWS.d/next/Windows/2022-12-06-11-16-46.gh-issue-99941.GmUQ6o.rst
new file mode 100644
index 000000000000..a019d7287207
--- /dev/null
+++ b/Misc/NEWS.d/next/Windows/2022-12-06-11-16-46.gh-issue-99941.GmUQ6o.rst
@@ -0,0 +1,2 @@
+Ensure that :func:`asyncio.Protocol.data_received` receives an immutable
+:class:`bytes` object (as documented), instead of :class:`bytearray`.
More information about the Python-checkins
mailing list