10000 Packer accepts bytearray objects by jfolz · Pull Request #229 · msgpack/msgpack-python · GitHub
[go: up one dir, main page]

Skip to content

Packer accepts bytearray objects #229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions msgpack/_packer.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ from msgpack import ExtType
cdef extern from "Python.h":

int PyMemoryView_Check(object obj)
int PyByteArray_Check(object obj)
int PyByteArray_CheckExact(object obj)


cdef extern from "pack.h":
Expand Down Expand Up @@ -39,6 +41,14 @@ cdef int DEFAULT_RECURSE_LIMIT=511
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):
"""
MessagePack Packer
Expand Down Expand Up @@ -174,10 +184,10 @@ cdef class Packer(object):
else:
dval = o
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)
if L > ITEM_LIMIT:
raise PackValueError("bytes is too large")
raise PackValueError("%s is too large" % type(o).__name__)
rawval = o
ret = msgpack_pack_bin(&self.pk, L)
if ret == 0:
Expand Down
6 changes: 4 additions & 2 deletions msgpack/fallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ def __init__(self, s=b''):
def write(self, s):
if isinstance(s, memoryview):
s = s.tobytes()
elif isinstance(s, bytearray):
s = bytes(s)
self.builder.append(s)
def getvalue(self):
return self.builder.build()
Expand Down Expand Up @@ -728,10 +730,10 @@ def _pack(self, obj, nest_limit=DEFAULT_RECURSE_LIMIT,
default_used = True
continue
raise PackOverflowError("Integer value out of range")
if check(obj, bytes):
if check(obj, (bytes, bytearray)):
n = len(obj)
if n >= 2**32:
raise PackValueError("Bytes is too large")
raise PackValueError("%s is too large" % type(obj).__name__)
self._pack_bin_header(n)
return self._buffer.write(obj)
if check(obj, Unicode):
Expand Down
7 changes: 7 additions & 0 deletions test/test_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ def testPackBytes():
for td in test_data:
check(td)

def testPackByteArrays():
test_data = [
bytearray(b""), bytearray(b"abcd"), (bytearray(b"defgh"),),
]
for td in test_data:
check(td)

def testIgnoreUnicodeErrors():
re = unpackb(packb(b'abc\xeddef'), encoding='utf-8', unicode_errors='ignore', use_list=1)
assert re == "abcdef"
Expand Down
0