summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortailhook <pc@gafol.net>2011-08-22 01:52:45 +0900
committerINADA Naoki <songofacandy@gmail.com>2011-08-22 01:52:45 +0900
commit8c3c8a250b5f6f129e5e077a224ec6916cc87437 (patch)
tree31059bfc338c441bbe09fb23747b39137894a3fb
parent4a1ce19addc6fc5faf26101b16124401113889d1 (diff)
downloadmsgpack-python-8c3c8a250b5f6f129e5e077a224ec6916cc87437.tar.gz
Fixed `encoding` argument for unpacker in Python
-rw-r--r--msgpack/_msgpack.pyx22
-rw-r--r--test/test_pack.py6
-rw-r--r--test3/test_pack.py9
3 files changed, 34 insertions, 3 deletions
diff --git a/msgpack/_msgpack.pyx b/msgpack/_msgpack.pyx
index 443cbd7..5a83ea0 100644
--- a/msgpack/_msgpack.pyx
+++ b/msgpack/_msgpack.pyx
@@ -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
diff --git a/test/test_pack.py b/test/test_pack.py
index 2b5f1ad..357cb3c 100644
--- a/test/test_pack.py
+++ b/test/test_pack.py
@@ -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:
diff --git a/test3/test_pack.py b/test3/test_pack.py
index e53f7e6..5ff04e7 100644
--- a/test3/test_pack.py
+++ b/test3/test_pack.py
@@ -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)