8000 Remove encoding/unicode_errors options from Packer (#378) · eb-emilio/msgpack-python@e1ed004 · GitHub
[go: up one dir, main page]

Skip to content

Commit e1ed004

Browse files
authored
Remove encoding/unicode_errors options from Packer (msgpack#378)
1 parent cc3a866 commit e1ed004

File tree

3 files changed

+9
-93
lines changed

3 files changed

+9
-93
lines changed

msgpack/_packer.pyx

Lines changed: 7 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,9 @@ cdef class Packer(object):
8989
Additionally tuples will not be serialized as lists.
9090
This is useful when trying to implement accurate serialization
9191
for python types.
92-
93-
:param str unicode_errors:
94-
Error handler for encoding unicode. (default: 'strict')
95-
96-
:param str encoding:
97-
(deprecated) Convert unicode to bytes with this encoding. (default: 'utf-8')
9892
"""
9993
cdef msgpack_packer pk
10094
cdef object _default
101-
cdef object _bencoding
102-
cdef object _berrors
103-
cdef const char *encoding
104-
cdef const char *unicode_errors
10595
cdef bint strict_types
10696
cdef bool use_float
10797
cdef bint autoreset
@@ -114,11 +104,11 @@ cdef class Packer(object):
114104
self.pk.buf_size = buf_size
115105
self.pk.length = 0
116106

117-
def __init__(self, default=None, encoding=None, unicode_errors=None,
118-
bint use_single_float=False, bint autoreset=True, bint use_bin_type=False,
107+
def __init__(self, default=None,
108+
bint use_single_float=False,
109+
bint autoreset=True,
110+
bint use_bin_type=False,
119111
bint strict_types=False):
120-
if encoding is not None:
121-
PyErr_WarnEx(DeprecationWarning, "encoding is deprecated.", 1)
122112
self.use_float = use_single_float
123113
self.strict_types = strict_types
124114
self.autoreset = autoreset
@@ -128,18 +118,6 @@ cdef class Packer(object):
128118
raise TypeError("default must be a callable.")
129119
self._default = default
130120

131-
self._bencoding = encoding
132-
if encoding is None:
133-
self.encoding = 'utf-8'
134-
else:
135-
self.encoding = self._bencoding
136-
137-
self._berrors = unicode_errors
138-
if unicode_errors is None:
139-
self.unicode_errors = NULL
140-
else:
141-
self.unicode_errors = self._berrors
142-
143121
def __dealloc__(self):
144122
PyMem_Free(self.pk.buf)
145123
self.pk.buf = NULL
@@ -205,19 +183,9 @@ cdef class Packer(object):
205183
if ret == 0:
206184
ret = msgpack_pack_raw_body(&self.pk, rawval, L)
207185
elif PyUnicode_CheckExact(o) if strict_types else PyUnicode_Check(o):
208-
if self.encoding == NULL and self.unicode_errors == NULL:
209-
ret = msgpack_pack_unicode(&self.pk, o, ITEM_LIMIT);
210-
if ret == -2:
211-
raise ValueError("unicode string is too large")
212-
else:
213-
o = PyUnicode_AsEncodedString(o, self.encoding, self.unicode_errors)
214-
L = Py_SIZE(o)
215-
if L > ITEM_LIMIT:
216-
raise ValueError("unicode string is too large")
217-
ret = msgpack_pack_raw(&self.pk, L)
218-
if ret == 0:
219-
rawval = o
220-
ret = msgpack_pack_raw_body(&self.pk, rawval, L)
186+
ret = msgpack_pack_unicode(&self.pk, o, ITEM_LIMIT);
187+
if ret == -2:
188+
raise ValueError("unicode string is too large")
221189
elif PyDict_CheckExact(o):
222190
d = <dict>o
223191
L = len(d)

msgpack/fallback.py

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -752,32 +752,14 @@ class Packer(object):
752752
Additionally tuples will not be serialized as lists.
753753
This is useful when trying to implement accurate serialization
754754
for python types.
755-
756-
:param str encoding:
757-
(deprecated) Convert unicode to bytes with this encoding. (default: 'utf-8')
758-
759-
:param str unicode_errors:
760-
Error handler for encoding unicode. (default: 'strict')
761755
"""
762-
def __init__(self, default=None, encoding=None, unicode_errors=None, 6D47
756+
def __init__(self, default=None,
763757
use_single_float=False, autoreset=True, use_bin_type=False,
764758
strict_types=False):
765-
if encoding is None:
766-
encoding = 'utf_8'
767-
else:
768-
warnings.warn(
769-
"encoding is deprecated, Use raw=False instead.",
770-
DeprecationWarning, stacklevel=2)
771-
772-
if unicode_errors is None:
773-
unicode_errors = 'strict'
774-
775759
self._strict_types = strict_types
776760
self._use_float = use_single_float
777761
self._autoreset = autoreset
778762
self._use_bin_type = use_bin_type
779-
self._encoding = encoding
780-
self._unicode_errors = unicode_errors
781763
self._buffer = StringIO()
782764
if default is not None:
783765
if not callable(default):
@@ -834,11 +816,7 @@ def _pack(self, obj, nest_limit=DEFAULT_RECURSE_LIMIT,
834816
self._pack_bin_header(n)
835817
return self._buffer.write(obj)
836818
if check(obj, unicode):
837-
if self._encoding is None:
838-
raise TypeError(
839-
"Can't encode unicode string: "
840-
"no encoding is specified")
841-
obj = obj.encode(self._encoding, self._unicode_errors)
819+
obj = obj.encode("utf-8")
842820
n = len(obj)
843821
if n >= 2**32:
844822
raise ValueError("String is too large")

test/test_pack.py

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,6 @@ def testPackUnicode():
4040
re = Unpacker(BytesIO(data), raw=False, use_list=1).unpack()
4141
assert re == td
4242

43-
def testPackUTF32(): # deprecated
44-
try:
45-
test_data = [
46-
"",
47-
"abcd",
48-
["defgh"],
49-
"Русский текст",
50-
]
51-
for td in test_data:
52-
with pytest.deprecated_call():
53-
re = unpackb(packb(td, encoding='utf-32'), use_list=1, encoding='utf-32')
54-
assert re == td
55-
except LookupError as e:
56-
xfail(e)
57-
5843
def testPackBytes():
5944
test_data = [
6045
b"", b"abcd", (b"defgh",),
@@ -69,26 +54,11 @@ def testPackByteArrays():
6954
for td in test_data:
7055
check(td)
7156

72-
def testIgnoreUnicodeErrors(): # deprecated
73-
with pytest.deprecated_call():
74-
re = unpackb(packb(b'abc\xeddef'), encoding='utf-8', unicode_errors='ignore', use_list=1)
75-
assert re == "abcdef"
76-
7757
def testStrictUnicodeUnpack():
7858
packed = packb(b'abc\xeddef')
7959
with pytest.raises(UnicodeDecodeError):
8060
unpackb(packed, raw=False, use_list=1)
8161

82-
def testStrictUnicodePack(): # deprecated
83-
with raises(UnicodeEncodeError):
84-
with pytest.deprecated_call():
85-
packb("abc\xeddef", encoding='ascii', unicode_errors='strict')
86-
87-
def testIgnoreErrorsPack(): # deprecated
88-
with pytest.deprecated_call():
89-
re = unpackb(packb("abcФФФdef", encoding='ascii', unicode_errors='ignore'), raw=False, use_list=1)
90-
assert re == "abcdef"
91-
9262
def testDecodeBinary():
9363
re = unpackb(packb(b"abc"), encoding=None, use_list=1)
9464
assert re == b"abc"

0 commit comments

Comments
 (0)
0