Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StreamReader.iter_chunks() raises IndexError #2112

Closed
jlacoline opened this issue Jul 19, 2017 · 2 comments
Closed

StreamReader.iter_chunks() raises IndexError #2112

jlacoline opened this issue Jul 19, 2017 · 2 comments
Labels

Comments

@jlacoline
Copy link
Contributor

jlacoline commented Jul 19, 2017

Long story short

Any generator given by the iter_chunks() method on StreamReader will end raising an IndexError.
This comes from the readchunk() method which fails if the buffer is empty the end of file is reached.

Expected behaviour

stream.readchunk() should return b"" if the buffer is empty eof is reached
stream.iter_chunks() should return a generator that yields all the chunks and then raise StopAsyncIteration

Actual behaviour

stream.readchunk() raises IndexError if the buffer is empty eof is reached
stream.iter_chunks() returns a generator that yields all the chunks and then raises IndexError

Steps to reproduce

For stream.readchunk(): In the associated unit test in tests/test_streams.py, replace stream.read() with stream.readchunk()

    def test_readchunk(self):

        stream = self._make_one()

        def cb():
            stream.feed_data(b'chunk1')
            stream.feed_data(b'chunk2')
            stream.feed_eof()
        self.loop.call_soon(cb)

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

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

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

Result:

――――――――――――――――――――――――――――――――――― TestStreamReader.test_readchunk ――――――――――――――――――――――――――――――――――――

self = <test_streams.TestStreamReader testMethod=test_readchunk>

    def test_readchunk(self):
    
        stream = self._make_one()
    
        def cb():
            stream.feed_data(b'chunk1')
            stream.feed_data(b'chunk2')
            stream.feed_eof()
        self.loop.call_soon(cb)
    
        data = self.loop.run_until_complete(stream.readchunk())
        self.assertEqual(b'chunk1', data)
    
        data = self.loop.run_until_complete(stream.readchunk())
        self.assertEqual(b'chunk2', data)
    
>       data = self.loop.run_until_complete(stream.readchunk())

tests/test_streams.py:565: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/home/loic/.pyenv/versions/3.5.3/lib/python3.5/asyncio/base_events.py:466: in run_until_complete
    return future.result()
/home/loic/.pyenv/versions/3.5.3/lib/python3.5/asyncio/futures.py:293: in result
    raise self._exception
/home/loic/.pyenv/versions/3.5.3/lib/python3.5/asyncio/tasks.py:239: in _step
    result = coro.send(None)
aiohttp/streams.py:329: in readchunk
    return self._read_nowait_chunk(-1)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <StreamReader eof>, n = -1

    def _read_nowait_chunk(self, n):
>       first_buffer = self._buffer[0]
E       IndexError: deque index out of range

aiohttp/streams.py:363: IndexError

Your environment

  • aiohttp master branch (identified on 2.2.3)
  • Python 3.5.3
  • Debian 8.8
jlacoline added a commit to jlacoline/aiohttp that referenced this issue Jul 19, 2017
fafhrd91 pushed a commit that referenced this issue Jul 21, 2017
…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
@fafhrd91
Copy link
Member

Fixed by pr

@lock
Copy link

lock bot commented Oct 28, 2019

This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.

If you feel like there's important points made in this discussion,
please include those exceprts into that new issue.

@lock lock bot added the outdated label Oct 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants