Skip to content

Commit

Permalink
Fix issue with IndexError being raised by the StreamReader.iter_chunk…
Browse files Browse the repository at this point in the history
…s() generator (#2113)

* stream.readchunk: update unit test to reflect expected behavior

* stream.readchunk() bugfix: do not raise IndexError when buffer is empty

* issue #2112: add news fragment to changes and update contributors

* stream.readchunk: add a new unit test and change eof condition
  • Loading branch information
jlacoline authored and fafhrd91 committed Jul 21, 2017
1 parent 7272c9f commit 240a0e2
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Expand Up @@ -104,6 +104,7 @@ Kirill Klenov
Kirill Malovitsa
Kyrylo Perevozchikov
Lars P. Søndergaard
Loïc Lajeanne
Louis-Philippe Huberdeau
Lu Gong
Lubomir Gelo
Expand Down
5 changes: 4 additions & 1 deletion aiohttp/streams.py
Expand Up @@ -326,7 +326,10 @@ def readchunk(self):
if not self._buffer and not self._eof:
yield from self._wait('readchunk')

return self._read_nowait_chunk(-1)
if self._buffer:
return self._read_nowait_chunk(-1)
else:
return b""

@asyncio.coroutine
def readexactly(self, n):
Expand Down
1 change: 1 addition & 0 deletions changes/2112.bugfix
@@ -0,0 +1 @@
Fix issue with IndexError being raised by the StreamReader.iter_chunks() generator.
15 changes: 13 additions & 2 deletions tests/test_streams.py
Expand Up @@ -547,7 +547,6 @@ def test_read_nowait_waiter(self):
self.assertRaises(RuntimeError, stream.read_nowait)

def test_readchunk(self):

stream = self._make_one()

def cb():
Expand All @@ -562,9 +561,21 @@ def cb():
data = self.loop.run_until_complete(stream.readchunk())
self.assertEqual(b'chunk2', data)

data = self.loop.run_until_complete(stream.read())
data = self.loop.run_until_complete(stream.readchunk())
self.assertEqual(b'', data)

def test_readchunk_wait_eof(self):
stream = self._make_one()

def cb():
yield from asyncio.sleep(0.1, loop=self.loop)
stream.feed_eof()

asyncio.Task(cb(), loop=self.loop)
data = self.loop.run_until_complete(stream.readchunk())
self.assertEqual(b"", data)
self.assertTrue(stream.is_eof())

def test___repr__(self):
stream = self._make_one()
self.assertEqual("<StreamReader>", repr(stream))
Expand Down

0 comments on commit 240a0e2

Please sign in to comment.