From ffd6208a78db1bd742a4332c106fc03212303015 Mon Sep 17 00:00:00 2001 From: jlacoline Date: Wed, 19 Jul 2017 16:29:37 +0200 Subject: [PATCH 1/4] stream.readchunk: update unit test to reflect expected behavior --- tests/test_streams.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_streams.py b/tests/test_streams.py index e7d545a8a08..9943fad3263 100644 --- a/tests/test_streams.py +++ b/tests/test_streams.py @@ -562,7 +562,7 @@ 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___repr__(self): From eef08000e437ad826694fc498c711524893d6125 Mon Sep 17 00:00:00 2001 From: jlacoline Date: Wed, 19 Jul 2017 17:15:54 +0200 Subject: [PATCH 2/4] stream.readchunk() bugfix: do not raise IndexError when buffer is empty --- aiohttp/streams.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/aiohttp/streams.py b/aiohttp/streams.py index 9a0e01136a9..8ad03e353b2 100644 --- a/aiohttp/streams.py +++ b/aiohttp/streams.py @@ -323,7 +323,9 @@ def readchunk(self): if self._exception is not None: raise self._exception - if not self._buffer and not self._eof: + if not self._buffer: + if self._eof: + return b"" yield from self._wait('readchunk') return self._read_nowait_chunk(-1) From 3f555af7633de1288be6dcdda7d5e7e68c2fc1f2 Mon Sep 17 00:00:00 2001 From: jlacoline Date: Wed, 19 Jul 2017 17:57:52 +0200 Subject: [PATCH 3/4] issue #2112: add news fragment to changes and update contributors --- CONTRIBUTORS.txt | 1 + changes/2112.bugfix | 1 + 2 files changed, 2 insertions(+) create mode 100644 changes/2112.bugfix diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index e49959e702a..477554dd5d3 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -104,6 +104,7 @@ Kirill Klenov Kirill Malovitsa Kyrylo Perevozchikov Lars P. Søndergaard +Loïc Lajeanne Louis-Philippe Huberdeau Lu Gong Lubomir Gelo diff --git a/changes/2112.bugfix b/changes/2112.bugfix new file mode 100644 index 00000000000..1571e663cb9 --- /dev/null +++ b/changes/2112.bugfix @@ -0,0 +1 @@ +Fix issue with IndexError being raised by the StreamReader.iter_chunks() generator. From f9281cb25f8d712a354e0b27303faf6bee5b9dac Mon Sep 17 00:00:00 2001 From: jlacoline Date: Thu, 20 Jul 2017 11:17:45 +0200 Subject: [PATCH 4/4] stream.readchunk: add a new unit test and change eof condition --- aiohttp/streams.py | 9 +++++---- tests/test_streams.py | 13 ++++++++++++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/aiohttp/streams.py b/aiohttp/streams.py index 8ad03e353b2..beb6a9d118c 100644 --- a/aiohttp/streams.py +++ b/aiohttp/streams.py @@ -323,12 +323,13 @@ def readchunk(self): if self._exception is not None: raise self._exception - if not self._buffer: - if self._eof: - return b"" + 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): diff --git a/tests/test_streams.py b/tests/test_streams.py index 9943fad3263..31bea1f1a84 100644 --- a/tests/test_streams.py +++ b/tests/test_streams.py @@ -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(): @@ -565,6 +564,18 @@ def cb(): 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("", repr(stream))