Skip to content

Commit

Permalink
[PR #7644/6639f7df backport][3.9] Fixed 7616: EmptyStreamReader.iter_…
Browse files Browse the repository at this point in the history
…chunks() never ends (#7645)

**This is a backport of PR #7644 as merged into master
(6639f7d).**
  • Loading branch information
patchback[bot] authored Oct 3, 2023
1 parent 58fcf2f commit 56d2be3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES/7616.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed ``EmptyStreamReader.iter_chunks()`` never ending -- by :user:`mind1m`
6 changes: 5 additions & 1 deletion aiohttp/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ def _read_nowait(self, n: int) -> bytes:

class EmptyStreamReader(StreamReader): # lgtm [py/missing-call-to-init]
def __init__(self) -> None:
pass
self._read_eof_chunk = False

def __repr__(self) -> str:
return "<%s>" % self.__class__.__name__
Expand Down Expand Up @@ -549,6 +549,10 @@ async def readany(self) -> bytes:
return b""

async def readchunk(self) -> Tuple[bytes, bool]:
if not self._read_eof_chunk:
self._read_eof_chunk = True
return (b"", False)

return (b"", True)

async def readexactly(self, n: int) -> bytes:
Expand Down
10 changes: 10 additions & 0 deletions tests/test_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -1135,12 +1135,22 @@ async def test_empty_stream_reader() -> None:
assert await s.read() == b""
assert await s.readline() == b""
assert await s.readany() == b""
assert await s.readchunk() == (b"", False)
assert await s.readchunk() == (b"", True)
with pytest.raises(asyncio.IncompleteReadError):
await s.readexactly(10)
assert s.read_nowait() == b""


async def test_empty_stream_reader_iter_chunks() -> None:
s = streams.EmptyStreamReader()

# check that iter_chunks() does not cause infinite loop
iter_chunks = s.iter_chunks()
with pytest.raises(StopAsyncIteration):
await iter_chunks.__anext__()


@pytest.fixture
async def buffer(loop):
return streams.DataQueue(loop)
Expand Down

0 comments on commit 56d2be3

Please sign in to comment.