mirror of
https://github.com/msgpack/msgpack-python.git
synced 2025-10-28 16:14:13 +00:00
parent
e1068087e0
commit
e0f0e145f1
2 changed files with 57 additions and 15 deletions
|
|
@ -1,6 +1,6 @@
|
|||
#!/usr/bin/env python
|
||||
from pytest import raises
|
||||
|
||||
from msgpack import packb, unpackb
|
||||
from msgpack import packb, unpackb, Packer
|
||||
|
||||
|
||||
def test_unpack_buffer():
|
||||
|
|
@ -27,3 +27,23 @@ def test_unpack_memoryview():
|
|||
assert [b"foo", b"bar"] == obj
|
||||
expected_type = bytes
|
||||
assert all(type(s) == expected_type for s in obj)
|
||||
|
||||
|
||||
def test_packer_getbuffer():
|
||||
packer = Packer(autoreset=False)
|
||||
packer.pack_array_header(2)
|
||||
packer.pack(42)
|
||||
packer.pack("hello")
|
||||
buffer = packer.getbuffer()
|
||||
assert isinstance(buffer, memoryview)
|
||||
assert bytes(buffer) == b"\x92*\xa5hello"
|
||||
|
||||
if Packer.__module__ == "msgpack._cmsgpack": # only for Cython
|
||||
# cython Packer supports buffer protocol directly
|
||||
assert bytes(packer) == b"\x92*\xa5hello"
|
||||
|
||||
with raises(BufferError):
|
||||
packer.pack(42)
|
||||
buffer.release()
|
||||
packer.pack(42)
|
||||
assert bytes(packer) == b"\x92*\xa5hello*"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue