Skip to content

Commit

Permalink
Merge pull request #700 from eyal-stratoscale/master
Browse files Browse the repository at this point in the history
Add number of bytes to stream.read_nowait
  • Loading branch information
fafhrd91 committed Dec 28, 2015
2 parents 51b94b9 + 0f038b1 commit fa17261
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
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

0 comments on commit fa17261

Please sign in to comment.