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

Add number of bytes to stream.read_nowait #700

Merged
merged 1 commit into from
Dec 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions aiohttp/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,15 +297,15 @@ def readexactly(self, n):

return b''.join(blocks)

def read_nowait(self):
def read_nowait(self, n=None):
if self._exception is not None:
raise self._exception

if self._waiter and not self._waiter.done():
raise RuntimeError(
'Called while some coroutine is waiting for incoming data.')

return self._read_nowait()
return self._read_nowait(n)

def _read_nowait(self, n=None):
if not self._buffer:
Expand Down
14 changes: 14 additions & 0 deletions tests/test_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,20 @@ def test_read_nowait(self):
data = self.loop.run_until_complete(stream.read())
self.assertEqual(b'', data)

def test_read_nowait_n(self):
stream = self._make_one()
stream.feed_data(b'line1\nline2\n')

self.assertEqual(
stream.read_nowait(4), b'line')
self.assertEqual(
stream.read_nowait(), b'1\nline2\n')
self.assertIs(
stream.read_nowait(), streams.EOF_MARKER)
stream.feed_eof()
data = self.loop.run_until_complete(stream.read())
self.assertEqual(b'', data)

def test_read_nowait_exception(self):
stream = self._make_one()
stream.feed_data(b'line\n')
Expand Down