Skip to content

Commit

Permalink
fallback: Fix packing multidim memoryview (#527)
Browse files Browse the repository at this point in the history
Fix #526
  • Loading branch information
methane authored Jan 18, 2023
1 parent c399566 commit b82d0b6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion msgpack/fallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ def _pack(
self._pack_raw_header(n)
return self._buffer.write(obj)
if check(obj, memoryview):
n = len(obj) * obj.itemsize
n = obj.nbytes
if n >= 2**32:
raise ValueError("Memoryview is too large")
self._pack_bin_header(n)
Expand Down
8 changes: 8 additions & 0 deletions test/test_memoryview.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,11 @@ def test_bin32_from_byte():

def test_bin32_from_float():
_runtest("f", 2**16, b"\xc6", b"\x00\x01\x00\x00", True)


def test_multidim_memoryview():
# See https://github.com/msgpack/msgpack-python/issues/526
view = memoryview(b"\00" * 6)
data = view.cast(view.format, (3, 2))
packed = packb(data)
assert packed == b'\xc4\x06\x00\x00\x00\x00\x00\x00'

0 comments on commit b82d0b6

Please sign in to comment.