Repository: msgpack/msgpack-python

Issue:
Broken fallback implementation of Unpacker.read_bytes
```
>>> from msgpack import *
>>> u = Unpacker(); u.feed('x'); u.read_bytes(1)
'x'
>>> u.next()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "msgpack/_unpacker.pyx", line 562, in msgpack._cmsgpack.Unpacker.__next__
  File "msgpack/_unpacker.pyx", line 489, in msgpack._cmsgpack.Unpacker._unpack
StopIteration: No more data to unpack.
>>> u.feed('\1'); u.next()
1
```
So far so good. Now with fallback implémentation:
```
>>> from msgpack.fallback import *
>>> u = Unpacker(); u.feed('x'); u.read_bytes(1)
bytearray(b'x')
>>> u.next()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/dist-packages/msgpack/fallback.py", line 690, in __next__
    raise StopIteration
StopIteration
>>> u.feed('\1'); u.next()
120
```
Something seems to be wrongly reset on StopIteration.

Discussion:
codecov-io: # [Codecov](https://codecov.io/gh/msgpack/msgpack-python/pull/388?src=pr&el=h1) Report
> Merging [#388](https://codecov.io/gh/msgpack/msgpack-python/pull/388?src=pr&el=desc) into [master](https://codecov.io/gh/msgpack/msgpack-python/commit/7a8ce0f9ca910a851b6835d26b1d6970a188fa4e?src=pr&el=desc) will **increase** coverage by `0.02%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/msgpack/msgpack-python/pull/388/graphs/tree.svg?width=650&token=1HDgK8EAlP&height=150&src=pr)](https://codecov.io/gh/msgpack/msgpack-python/pull/388?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #388      +/-   ##
==========================================
+ Coverage   87.96%   87.99%   +0.02%     
==========================================
  Files           5        5              
  Lines         806      808       +2     
==========================================
+ Hits          709      711       +2     
  Misses         97       97
```


| [Impacted Files](https://codecov.io/gh/msgpack/msgpack-python/pull/388?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [msgpack/fallback.py](https://codecov.io/gh/msgpack/msgpack-python/pull/388/diff?src=pr&el=tree#diff-bXNncGFjay9mYWxsYmFjay5weQ==) | `89.03% <100%> (+0.03%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/msgpack/msgpack-python/pull/388?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/msgpack/msgpack-python/pull/388?src=pr&el=footer). Last update [7a8ce0f...15e017a](https://codecov.io/gh/msgpack/msgpack-python/pull/388?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/msgpack/fallback.py b/msgpack/fallback.py
index 388a5ab..85a711b 100644
--- a/msgpack/fallback.py
+++ b/msgpack/fallback.py
@@ -357,7 +357,9 @@ class Unpacker(object):
         return self._buffer[self._buff_i :]
 
     def read_bytes(self, n):
-        return self._read(n)
+        ret = self._read(n)
+        self._consume()
+        return ret
 
     def _read(self, n):
         # (int) -> bytearray


Test patch:
diff --git a/test/test_sequnpack.py b/test/test_sequnpack.py
index 9b69479..ad29de8 100644
--- a/test/test_sequnpack.py
+++ b/test/test_sequnpack.py
@@ -93,6 +93,15 @@ def test_readbytes():
     assert unpacker.unpack() == ord(b"a")
     assert unpacker.unpack() == ord(b"r")
 
+    # Issue 352
+    u = Unpacker()
+    u.feed(b"x")
+    assert bytes(u.read_bytes(1)) == b"x"
+    with raises(StopIteration):
+        next(u)
+    u.feed(b"\1")
+    assert next(u) == 1
+
 
 def test_issue124():
     unpacker = Unpacker()