Skip to content

Commit bc962ef

Browse files
authored
Convert bytearray to bytes in StreamProtocol.receive() (#777)
Fixes #776.
1 parent 8a5b346 commit bc962ef

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

docs/versionhistory.rst

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ This library adheres to `Semantic Versioning 2.0 <http://semver.org/>`_.
2020
(`#696 <https://github.com/agronholm/anyio/issues/696>`_)
2121
- Fixed ``TaskInfo.has_pending_cancellation()`` on asyncio not respecting shielded
2222
scopes (`#771 <https://github.com/agronholm/anyio/issues/771>`_; PR by @gschaffner)
23+
- Fixed ``SocketStream.receive()`` returning ``bytearray`` instead of ``bytes`` when
24+
using asyncio with ``ProactorEventLoop`` (Windows)
25+
(`#776 <https://github.com/agronholm/anyio/issues/776>`_)
2326
- Fixed quitting the debugger in a pytest test session while in an active task group
2427
failing the test instead of exiting the test session (because the exit exception
2528
arrives in an exception group)

src/anyio/_backends/_asyncio.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1064,7 +1064,8 @@ def connection_lost(self, exc: Exception | None) -> None:
10641064
self.write_event.set()
10651065

10661066
def data_received(self, data: bytes) -> None:
1067-
self.read_queue.append(data)
1067+
# ProactorEventloop sometimes sends bytearray instead of bytes
1068+
self.read_queue.append(bytes(data))
10681069
self.read_event.set()
10691070

10701071
def eof_received(self) -> bool | None:

tests/test_sockets.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,9 @@ def serve() -> None:
202202
thread.start()
203203
response = b""
204204
while len(response) < len(buffer):
205-
response += await stream.receive()
205+
chunk = await stream.receive()
206+
assert isinstance(chunk, bytes)
207+
response += chunk
206208

207209
thread.join()
208210
assert response == buffer

0 commit comments

Comments
 (0)