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).

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

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.
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()

pip install -e .
parse_log_pytest
requirements.txt
pytest
apt-get update apt-get install -y gcc
3.7
requirements.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
certifi @ file:///croot/certifi_1671487769961/work/certifi
Cython==0.29.37
exceptiongroup==1.2.2
importlib-metadata==6.7.0
iniconfig==2.0.0
-e git+https://github.com/msgpack/msgpack-python.git@7a8ce0f9ca910a851b6835d26b1d6970a188fa4e#egg=msgpack
packaging==24.0
pluggy==1.2.0
pytest==7.4.4
tomli==2.0.1
typing_extensions==4.7.1
zipp==3.15.0

name: msgpack-python
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2022.12.7=py37h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - pip=22.3.1=py37h06a4308_0
  - python=3.7.16=h7a1cb2a_0
  - readline=8.2=h5eee18b_0
  - setuptools=65.6.3=py37h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.38.4=py37h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - cython==0.29.37
      - exceptiongroup==1.2.2
      - importlib-metadata==6.7.0
      - iniconfig==2.0.0
      - packaging==24.0
      - pluggy==1.2.0
      - pytest==7.4.4
      - tomli==2.0.1
      - typing-extensions==4.7.1
      - zipp==3.15.0
prefix: /opt/conda/envs/msgpack-python

test/test_sequnpack.py::test_readbytes
test/test_sequnpack.py::test_partialdata test/test_sequnpack.py::test_foobar test/test_sequnpack.py::test_foobar_skip test/test_sequnpack.py::test_maxbuffersize test/test_sequnpack.py::test_issue124 test/test_sequnpack.py::test_unpack_tell
msgpack/fallback.py
msgpack/fallback.py