mirror of
https://github.com/msgpack/msgpack-python.git
synced 2026-02-06 09:50:01 +00:00
Fixed encoding argument for unpacker in Python
This commit is contained in:
parent
4a1ce19add
commit
8c3c8a250b
3 changed files with 34 additions and 3 deletions
|
|
@ -203,6 +203,7 @@ def unpackb(object packed, object object_hook=None, object list_hook=None, bint
|
|||
|
||||
if encoding is None:
|
||||
enc = NULL
|
||||
err = NULL
|
||||
else:
|
||||
if isinstance(encoding, unicode):
|
||||
bencoding = encoding.encode('ascii')
|
||||
|
|
@ -267,6 +268,10 @@ cdef class Unpacker(object):
|
|||
cdef Py_ssize_t read_size
|
||||
cdef bint use_list
|
||||
cdef object object_hook
|
||||
cdef object _bencoding
|
||||
cdef object _berrors
|
||||
cdef char *encoding
|
||||
cdef char *unicode_errors
|
||||
|
||||
def __cinit__(self):
|
||||
self.buf = NULL
|
||||
|
|
@ -276,7 +281,8 @@ cdef class Unpacker(object):
|
|||
self.buf = NULL;
|
||||
|
||||
def __init__(self, file_like=None, Py_ssize_t read_size=0, bint use_list=0,
|
||||
object object_hook=None, object list_hook=None, encoding=None, unicode_errors=None):
|
||||
object object_hook=None, object list_hook=None,
|
||||
encoding=None, unicode_errors='strict'):
|
||||
if read_size == 0:
|
||||
read_size = 1024*1024
|
||||
self.use_list = use_list
|
||||
|
|
@ -303,6 +309,20 @@ cdef class Unpacker(object):
|
|||
if not PyCallable_Check(list_hook):
|
||||
raise TypeError("list_hook must be a callable.")
|
||||
self.ctx.user.list_hook = <PyObject*>list_hook
|
||||
if encoding is None:
|
||||
self.ctx.user.encoding = NULL
|
||||
self.ctx.user.unicode_errors = NULL
|
||||
else:
|
||||
if isinstance(encoding, unicode):
|
||||
self._bencoding = encoding.encode('ascii')
|
||||
else:
|
||||
self._bencoding = encoding
|
||||
self.ctx.user.encoding = PyBytes_AsString(self._bencoding)
|
||||
if isinstance(unicode_errors, unicode):
|
||||
self._berrors = unicode_errors.encode('ascii')
|
||||
else:
|
||||
self._berrors = unicode_errors
|
||||
self.ctx.user.unicode_errors = PyBytes_AsString(self._berrors)
|
||||
|
||||
def feed(self, object next_bytes):
|
||||
cdef char* buf
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@ from nose.plugins.skip import SkipTest
|
|||
|
||||
from msgpack import packs, unpacks
|
||||
|
||||
from StringIO import StringIO
|
||||
|
||||
def check(data):
|
||||
re = unpacks(packs(data))
|
||||
assert_equal(re, data)
|
||||
|
|
@ -32,6 +34,10 @@ def testPackUnicode():
|
|||
for td in test_data:
|
||||
re = unpacks(packs(td, encoding='utf-8'), encoding='utf-8')
|
||||
assert_equal(re, td)
|
||||
packer = Packer(encoding='utf-8')
|
||||
data = packer.pack(td)
|
||||
re = Unpacker(StringIO(data), encoding='utf-8').unpack()
|
||||
assert_equal(re, td)
|
||||
|
||||
def testPackUTF32():
|
||||
try:
|
||||
|
|
|
|||
|
|
@ -4,7 +4,9 @@
|
|||
from nose import main
|
||||
from nose.tools import *
|
||||
|
||||
from msgpack import packs, unpacks
|
||||
from msgpack import packs, unpacks, Unpacker, Packer
|
||||
|
||||
from io import BytesIO
|
||||
|
||||
def check(data):
|
||||
re = unpacks(packs(data))
|
||||
|
|
@ -31,13 +33,16 @@ def testPackUnicode():
|
|||
for td in test_data:
|
||||
re = unpacks(packs(td, encoding='utf-8'), encoding='utf-8')
|
||||
assert_equal(re, td)
|
||||
packer = Packer(encoding='utf-8')
|
||||
data = packer.pack(td)
|
||||
re = Unpacker(BytesIO(data), encoding='utf-8').unpack()
|
||||
assert_equal(re, td)
|
||||
|
||||
def testPackUTF32():
|
||||
test_data = [
|
||||
"", "abcd", ("defgh",), "Русский текст",
|
||||
]
|
||||
for td in test_data:
|
||||
print(packs(td, encoding='utf-32'))
|
||||
re = unpacks(packs(td, encoding='utf-32'), encoding='utf-32')
|
||||
assert_equal(re, td)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue