mirror of
https://github.com/msgpack/msgpack-python.git
synced 2025-11-01 10:00:54 +00:00
Remove encoding/unicode_errors options from Packer (#378)
This commit is contained in:
parent
cc3a8665d6
commit
e1ed0044bf
3 changed files with 9 additions and 93 deletions
|
|
@ -89,19 +89,9 @@ cdef class Packer(object):
|
||||||
Additionally tuples will not be serialized as lists.
|
Additionally tuples will not be serialized as lists.
|
||||||
This is useful when trying to implement accurate serialization
|
This is useful when trying to implement accurate serialization
|
||||||
for python types.
|
for python types.
|
||||||
|
|
||||||
:param str unicode_errors:
|
|
||||||
Error handler for encoding unicode. (default: 'strict')
|
|
||||||
|
|
||||||
:param str encoding:
|
|
||||||
(deprecated) Convert unicode to bytes with this encoding. (default: 'utf-8')
|
|
||||||
"""
|
"""
|
||||||
cdef msgpack_packer pk
|
cdef msgpack_packer pk
|
||||||
cdef object _default
|
cdef object _default
|
||||||
cdef object _bencoding
|
|
||||||
cdef object _berrors
|
|
||||||
cdef const char *encoding
|
|
||||||
cdef const char *unicode_errors
|
|
||||||
cdef bint strict_types
|
cdef bint strict_types
|
||||||
cdef bool use_float
|
cdef bool use_float
|
||||||
cdef bint autoreset
|
cdef bint autoreset
|
||||||
|
|
@ -114,11 +104,11 @@ cdef class Packer(object):
|
||||||
self.pk.buf_size = buf_size
|
self.pk.buf_size = buf_size
|
||||||
self.pk.length = 0
|
self.pk.length = 0
|
||||||
|
|
||||||
def __init__(self, default=None, encoding=None, unicode_errors=None,
|
def __init__(self, default=None,
|
||||||
bint use_single_float=False, bint autoreset=True, bint use_bin_type=False,
|
bint use_single_float=False,
|
||||||
|
bint autoreset=True,
|
||||||
|
bint use_bin_type=False,
|
||||||
bint strict_types=False):
|
bint strict_types=False):
|
||||||
if encoding is not None:
|
|
||||||
PyErr_WarnEx(DeprecationWarning, "encoding is deprecated.", 1)
|
|
||||||
self.use_float = use_single_float
|
self.use_float = use_single_float
|
||||||
self.strict_types = strict_types
|
self.strict_types = strict_types
|
||||||
self.autoreset = autoreset
|
self.autoreset = autoreset
|
||||||
|
|
@ -128,18 +118,6 @@ cdef class Packer(object):
|
||||||
raise TypeError("default must be a callable.")
|
raise TypeError("default must be a callable.")
|
||||||
self._default = default
|
self._default = default
|
||||||
|
|
||||||
self._bencoding = encoding
|
|
||||||
if encoding is None:
|
|
||||||
self.encoding = 'utf-8'
|
|
||||||
else:
|
|
||||||
self.encoding = self._bencoding
|
|
||||||
|
|
||||||
self._berrors = unicode_errors
|
|
||||||
if unicode_errors is None:
|
|
||||||
self.unicode_errors = NULL
|
|
||||||
else:
|
|
||||||
self.unicode_errors = self._berrors
|
|
||||||
|
|
||||||
def __dealloc__(self):
|
def __dealloc__(self):
|
||||||
PyMem_Free(self.pk.buf)
|
PyMem_Free(self.pk.buf)
|
||||||
self.pk.buf = NULL
|
self.pk.buf = NULL
|
||||||
|
|
@ -205,19 +183,9 @@ cdef class Packer(object):
|
||||||
if ret == 0:
|
if ret == 0:
|
||||||
ret = msgpack_pack_raw_body(&self.pk, rawval, L)
|
ret = msgpack_pack_raw_body(&self.pk, rawval, L)
|
||||||
elif PyUnicode_CheckExact(o) if strict_types else PyUnicode_Check(o):
|
elif PyUnicode_CheckExact(o) if strict_types else PyUnicode_Check(o):
|
||||||
if self.encoding == NULL and self.unicode_errors == NULL:
|
|
||||||
ret = msgpack_pack_unicode(&self.pk, o, ITEM_LIMIT);
|
ret = msgpack_pack_unicode(&self.pk, o, ITEM_LIMIT);
|
||||||
if ret == -2:
|
if ret == -2:
|
||||||
raise ValueError("unicode string is too large")
|
raise ValueError("unicode string is too large")
|
||||||
else:
|
|
||||||
o = PyUnicode_AsEncodedString(o, self.encoding, self.unicode_errors)
|
|
||||||
L = Py_SIZE(o)
|
|
||||||
if L > ITEM_LIMIT:
|
|
||||||
raise ValueError("unicode string is too large")
|
|
||||||
ret = msgpack_pack_raw(&self.pk, L)
|
|
||||||
if ret == 0:
|
|
||||||
rawval = o
|
|
||||||
ret = msgpack_pack_raw_body(&self.pk, rawval, L)
|
|
||||||
elif PyDict_CheckExact(o):
|
elif PyDict_CheckExact(o):
|
||||||
d = <dict>o
|
d = <dict>o
|
||||||
L = len(d)
|
L = len(d)
|
||||||
|
|
|
||||||
|
|
@ -752,32 +752,14 @@ class Packer(object):
|
||||||
Additionally tuples will not be serialized as lists.
|
Additionally tuples will not be serialized as lists.
|
||||||
This is useful when trying to implement accurate serialization
|
This is useful when trying to implement accurate serialization
|
||||||
for python types.
|
for python types.
|
||||||
|
|
||||||
:param str encoding:
|
|
||||||
(deprecated) Convert unicode to bytes with this encoding. (default: 'utf-8')
|
|
||||||
|
|
||||||
:param str unicode_errors:
|
|
||||||
Error handler for encoding unicode. (default: 'strict')
|
|
||||||
"""
|
"""
|
||||||
def __init__(self, default=None, encoding=None, unicode_errors=None,
|
def __init__(self, default=None,
|
||||||
use_single_float=False, autoreset=True, use_bin_type=False,
|
use_single_float=False, autoreset=True, use_bin_type=False,
|
||||||
strict_types=False):
|
strict_types=False):
|
||||||
if encoding is None:
|
|
||||||
encoding = 'utf_8'
|
|
||||||
else:
|
|
||||||
warnings.warn(
|
|
||||||
"encoding is deprecated, Use raw=False instead.",
|
|
||||||
DeprecationWarning, stacklevel=2)
|
|
||||||
|
|
||||||
if unicode_errors is None:
|
|
||||||
unicode_errors = 'strict'
|
|
||||||
|
|
||||||
self._strict_types = strict_types
|
self._strict_types = strict_types
|
||||||
self._use_float = use_single_float
|
self._use_float = use_single_float
|
||||||
self._autoreset = autoreset
|
self._autoreset = autoreset
|
||||||
self._use_bin_type = use_bin_type
|
self._use_bin_type = use_bin_type
|
||||||
self._encoding = encoding
|
|
||||||
self._unicode_errors = unicode_errors
|
|
||||||
self._buffer = StringIO()
|
self._buffer = StringIO()
|
||||||
if default is not None:
|
if default is not None:
|
||||||
if not callable(default):
|
if not callable(default):
|
||||||
|
|
@ -834,11 +816,7 @@ class Packer(object):
|
||||||
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):
|
||||||
if self._encoding is None:
|
obj = obj.encode("utf-8")
|
||||||
raise TypeError(
|
|
||||||
"Can't encode unicode string: "
|
|
||||||
"no encoding is specified")
|
|
||||||
obj = obj.encode(self._encoding, self._unicode_errors)
|
|
||||||
n = len(obj)
|
n = len(obj)
|
||||||
if n >= 2**32:
|
if n >= 2**32:
|
||||||
raise ValueError("String is too large")
|
raise ValueError("String is too large")
|
||||||
|
|
|
||||||
|
|
@ -40,21 +40,6 @@ def testPackUnicode():
|
||||||
re = Unpacker(BytesIO(data), raw=False, use_list=1).unpack()
|
re = Unpacker(BytesIO(data), raw=False, use_list=1).unpack()
|
||||||
assert re == td
|
assert re == td
|
||||||
|
|
||||||
def testPackUTF32(): # deprecated
|
|
||||||
try:
|
|
||||||
test_data = [
|
|
||||||
"",
|
|
||||||
"abcd",
|
|
||||||
["defgh"],
|
|
||||||
"Русский текст",
|
|
||||||
]
|
|
||||||
for td in test_data:
|
|
||||||
with pytest.deprecated_call():
|
|
||||||
re = unpackb(packb(td, encoding='utf-32'), use_list=1, encoding='utf-32')
|
|
||||||
assert re == td
|
|
||||||
except LookupError as e:
|
|
||||||
xfail(e)
|
|
||||||
|
|
||||||
def testPackBytes():
|
def testPackBytes():
|
||||||
test_data = [
|
test_data = [
|
||||||
b"", b"abcd", (b"defgh",),
|
b"", b"abcd", (b"defgh",),
|
||||||
|
|
@ -69,26 +54,11 @@ def testPackByteArrays():
|
||||||
for td in test_data:
|
for td in test_data:
|
||||||
check(td)
|
check(td)
|
||||||
|
|
||||||
def testIgnoreUnicodeErrors(): # deprecated
|
|
||||||
with pytest.deprecated_call():
|
|
||||||
re = unpackb(packb(b'abc\xeddef'), encoding='utf-8', unicode_errors='ignore', use_list=1)
|
|
||||||
assert re == "abcdef"
|
|
||||||
|
|
||||||
def testStrictUnicodeUnpack():
|
def testStrictUnicodeUnpack():
|
||||||
packed = packb(b'abc\xeddef')
|
packed = packb(b'abc\xeddef')
|
||||||
with pytest.raises(UnicodeDecodeError):
|
with pytest.raises(UnicodeDecodeError):
|
||||||
unpackb(packed, raw=False, use_list=1)
|
unpackb(packed, raw=False, use_list=1)
|
||||||
|
|
||||||
def testStrictUnicodePack(): # deprecated
|
|
||||||
with raises(UnicodeEncodeError):
|
|
||||||
with pytest.deprecated_call():
|
|
||||||
packb("abc\xeddef", encoding='ascii', unicode_errors='strict')
|
|
||||||
|
|
||||||
def testIgnoreErrorsPack(): # deprecated
|
|
||||||
with pytest.deprecated_call():
|
|
||||||
re = unpackb(packb("abcФФФdef", encoding='ascii', unicode_errors='ignore'), raw=False, use_list=1)
|
|
||||||
assert re == "abcdef"
|
|
||||||
|
|
||||||
def testDecodeBinary():
|
def testDecodeBinary():
|
||||||
re = unpackb(packb(b"abc"), encoding=None, use_list=1)
|
re = unpackb(packb(b"abc"), encoding=None, use_list=1)
|
||||||
assert re == b"abc"
|
assert re == b"abc"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue