mirror of
https://github.com/msgpack/msgpack-python.git
synced 2025-10-20 04:13:16 +00:00
Packer accepts bytearray objects (#229)
This commit is contained in:
parent
a8d9162ca6
commit
f0f2c0b397
3 changed files with 23 additions and 4 deletions
|
@ -10,6 +10,8 @@ from msgpack import ExtType
|
||||||
cdef extern from "Python.h":
|
cdef extern from "Python.h":
|
||||||
|
|
||||||
int PyMemoryView_Check(object obj)
|
int PyMemoryView_Check(object obj)
|
||||||
|
int PyByteArray_Check(object obj)
|
||||||
|
int PyByteArray_CheckExact(object obj)
|
||||||
|
|
||||||
|
|
||||||
cdef extern from "pack.h":
|
cdef extern from "pack.h":
|
||||||
|
@ -39,6 +41,14 @@ cdef int DEFAULT_RECURSE_LIMIT=511
|
||||||
cdef size_t ITEM_LIMIT = (2**32)-1
|
cdef size_t ITEM_LIMIT = (2**32)-1
|
||||||
|
|
||||||
|
|
||||||
|
cdef inline int PyBytesLike_Check(object o):
|
||||||
|
return PyBytes_Check(o) or PyByteArray_Check(o)
|
||||||
|
|
||||||
|
|
||||||
|
cdef inline int PyBytesLike_CheckExact(object o):
|
||||||
|
return PyBytes_CheckExact(o) or PyByteArray_CheckExact(o)
|
||||||
|
|
||||||
|
|
||||||
cdef class Packer(object):
|
cdef class Packer(object):
|
||||||
"""
|
"""
|
||||||
MessagePack Packer
|
MessagePack Packer
|
||||||
|
@ -174,10 +184,10 @@ cdef class Packer(object):
|
||||||
else:
|
else:
|
||||||
dval = o
|
dval = o
|
||||||
ret = msgpack_pack_double(&self.pk, dval)
|
ret = msgpack_pack_double(&self.pk, dval)
|
||||||
elif PyBytes_CheckExact(o) if strict_types else PyBytes_Check(o):
|
elif PyBytesLike_CheckExact(o) if strict_types else PyBytesLike_Check(o):
|
||||||
L = len(o)
|
L = len(o)
|
||||||
if L > ITEM_LIMIT:
|
if L > ITEM_LIMIT:
|
||||||
raise PackValueError("bytes is too large")
|
raise PackValueError("%s is too large" % type(o).__name__)
|
||||||
rawval = o
|
rawval = o
|
||||||
ret = msgpack_pack_bin(&self.pk, L)
|
ret = msgpack_pack_bin(&self.pk, L)
|
||||||
if ret == 0:
|
if ret == 0:
|
||||||
|
|
|
@ -38,6 +38,8 @@ if hasattr(sys, 'pypy_version_info'):
|
||||||
def write(self, s):
|
def write(self, s):
|
||||||
if isinstance(s, memoryview):
|
if isinstance(s, memoryview):
|
||||||
s = s.tobytes()
|
s = s.tobytes()
|
||||||
|
elif isinstance(s, bytearray):
|
||||||
|
s = bytes(s)
|
||||||
self.builder.append(s)
|
self.builder.append(s)
|
||||||
def getvalue(self):
|
def getvalue(self):
|
||||||
return self.builder.build()
|
return self.builder.build()
|
||||||
|
@ -728,10 +730,10 @@ class Packer(object):
|
||||||
default_used = True
|
default_used = True
|
||||||
continue
|
continue
|
||||||
raise PackOverflowError("Integer value out of range")
|
raise PackOverflowError("Integer value out of range")
|
||||||
if check(obj, bytes):
|
if check(obj, (bytes, bytearray)):
|
||||||
n = len(obj)
|
n = len(obj)
|
||||||
if n >= 2**32:
|
if n >= 2**32:
|
||||||
raise PackValueError("Bytes is too large")
|
raise PackValueError("%s is too large" % type(obj).__name__)
|
||||||
self._pack_bin_header(n)
|
self._pack_bin_header(n)
|
||||||
return self._buffer.write(obj)
|
return self._buffer.write(obj)
|
||||||
if check(obj, Unicode):
|
if check(obj, Unicode):
|
||||||
|
|
|
@ -58,6 +58,13 @@ def testPackBytes():
|
||||||
for td in test_data:
|
for td in test_data:
|
||||||
check(td)
|
check(td)
|
||||||
|
|
||||||
|
def testPackByteArrays():
|
||||||
|
test_data = [
|
||||||
|
bytearray(b""), bytearray(b"abcd"), (bytearray(b"defgh"),),
|
||||||
|
]
|
||||||
|
for td in test_data:
|
||||||
|
check(td)
|
||||||
|
|
||||||
def testIgnoreUnicodeErrors():
|
def testIgnoreUnicodeErrors():
|
||||||
re = unpackb(packb(b'abc\xeddef'), encoding='utf-8', unicode_errors='ignore', use_list=1)
|
re = unpackb(packb(b'abc\xeddef'), encoding='utf-8', unicode_errors='ignore', use_list=1)
|
||||||
assert re == "abcdef"
|
assert re == "abcdef"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue