Skip to content

memoryview objects are not fully supported #126

@himikof

Description

@himikof

memoryview is the python3 type for non-owning memory buffer objects, also backported to python 2.7. unpackb and Unpacker.feed should unpack them without copying, and packing functions should handle them the same as bytes.
The current support is quite limited due to multiple issues:

Python 3.3

>>> msgpack._unpacker.unpackb(memoryview(b'\x91\xc3'))
[True]
>>> msgpack.fallback.unpackb(memoryview(b'\x91\xc3'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/nofitserov/.local/lib64/python3.3/site-packages/msgpack/fallback.py", line 93, in unpackb
    ret = unpacker._fb_unpack()
  File "/home/nofitserov/.local/lib64/python3.3/site-packages/msgpack/fallback.py", line 383, in _fb_unpack
    typ, n, obj = self._read_header(execute, write_bytes)
  File "/home/nofitserov/.local/lib64/python3.3/site-packages/msgpack/fallback.py", line 274, in _read_header
    b = ord(c)
TypeError: ord() expected string of length 1, but memoryview found
>>> msgpack._packer.Packer().pack(memoryview(b'abc'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_packer.pyx", line 224, in msgpack._packer.Packer.pack (msgpack/_packer.cpp:224)
  File "_packer.pyx", line 226, in msgpack._packer.Packer.pack (msgpack/_packer.cpp:226)
  File "_packer.pyx", line 221, in msgpack._packer.Packer._pack (msgpack/_packer.cpp:221)
TypeError: can't serialize <memory at 0x7f37c3e41460>
>>> msgpack.fallback.Packer().pack(memoryview(b'abc'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/nofitserov/.local/lib64/python3.3/site-packages/msgpack/fallback.py", line 618, in pack
    self._pack(obj)
  File "/home/nofitserov/.local/lib64/python3.3/site-packages/msgpack/fallback.py", line 615, in _pack
    raise TypeError("Cannot serialize %r" % obj)
TypeError: Cannot serialize <memory at 0x7f37c3e41390>

Python 2.7

>>> msgpack._unpacker.unpackb(memoryview(b'\x91\xc3'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_unpacker.pyx", line 105, in msgpack._unpacker.unpackb (msgpack/_unpacker.cpp:105)
TypeError: expected a readable buffer object
>>> msgpack.fallback.unpackb(memoryview(b'\x91\xc3'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/nofitserov/.local/lib64/python2.7/site-packages/msgpack/fallback.py", line 93, in unpackb
    ret = unpacker._fb_unpack()
  File "/home/nofitserov/.local/lib64/python2.7/site-packages/msgpack/fallback.py", line 381, in _fb_unpack
    typ, n, obj = self._read_header(execute, write_bytes)
  File "/home/nofitserov/.local/lib64/python2.7/site-packages/msgpack/fallback.py", line 272, in _read_header
    b = ord(c)
TypeError: ord() expected string of length 1, but memoryview found
>>> msgpack._packer.Packer().pack(memoryview(b'abc'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_packer.pyx", line 206, in msgpack._packer.Packer.pack (msgpack/_packer.cpp:206)
  File "_packer.pyx", line 208, in msgpack._packer.Packer.pack (msgpack/_packer.cpp:208)
  File "_packer.pyx", line 203, in msgpack._packer.Packer._pack (msgpack/_packer.cpp:203)
TypeError: can't serialize <memory at 0x7f506e40ddf8>
>>> msgpack.fallback.Packer().pack(memoryview(b'abc'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/nofitserov/.local/lib64/python2.7/site-packages/msgpack/fallback.py", line 616, in pack
    self._pack(obj)
  File "/home/nofitserov/.local/lib64/python2.7/site-packages/msgpack/fallback.py", line 613, in _pack
    raise TypeError("Cannot serialize %r" % obj)
TypeError: Cannot serialize <memory at 0x7f506e40ddf8>

The only available workaround right now is to explicitly convert memoryview objects to bytes, needlessly copying the contents, which degrades performance, especially for unpacking large objects.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions