8000 Remove encoding option from Unpacker. (#380) · ossdev07/msgpack-python@e419cd8 · GitHub
[go: up one dir, main page]

Skip to content

Commit e419cd8

Browse files
authored
Remove encoding option from Unpacker. (msgpack#380)
1 parent 83ebb63 commit e419cd8

File tree

4 files changed

+15
-51
lines changed

4 files changed

+15
-51
lines changed

ChangeLog.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Release Date: TBD
55

66
* Remove Python 2 support from the ``msgpack/_cmsgpack``.
77
``msgpack/fallback`` still supports Python 2.
8-
* Remove ``encoding`` option from the Packer.
8+
* Remove ``encoding`` option from the Packer and Unpacker.
99

1010

1111
0.6.2

msgpack/_unpacker.pyx

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ cdef extern from "unpack.h":
3131
PyObject* object_hook
3232
PyObject* list_hook
3333
PyObject* ext_hook
34-
char *encoding
3534
char *unicode_errors
3635
Py_ssize_t max_str_len
3736
Py_ssize_t max_bin_len
@@ -58,7 +57,7 @@ cdef inline init_ctx(unpack_context *ctx,
5857
object object_hook, object object_pairs_hook,
5958
object list_hook, object ext_hook,
6059
bint use_list, bint raw, bint strict_map_key,
61-
const char* encoding, const char* unicode_errors,
60+
const char* unicode_errors,
6261
Py_ssize_t max_str_len, Py_ssize_t max_bin_len,
6362
Py_ssize_t max_array_len, Py_ssize_t max_map_len,
6463
Py_ssize_t max_ext_len):
@@ -99,7 +98,6 @@ cdef inline init_ctx(unpack_context *ctx,
9998
raise TypeError("ext_hook must be a callable.")
10099
ctx.user.ext_hook = <PyObject*>ext_hook
101100

102-
ctx.user.encoding = encoding
103101
ctx.user.unicode_errors = unicode_errors
104102

105103
def default_read_extended_type(typecode, data):
@@ -141,9 +139,9 @@ cdef inline int get_data_from_buffer(object obj,
141139
1)
142140
return 1
143141

144-
def unpackb(object packed, object object_hook=None, object list_hook=None,
142+
def unpackb(object packed, *, object object_hook=None, object list_hook=None,
145143
bint use_list=True, bint raw=True, bint strict_map_key=False,
146-
encoding=None, unicode_errors=None,
144+
unicode_errors=None,
147145
object_pairs_hook=None, ext_hook=ExtType,
148146
Py_ssize_t max_str_len=-1,
149147
Py_ssize_t max_bin_len=-1,
@@ -170,14 +168,9 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
170168
cdef Py_buffer view
171169
cdef char* buf = NULL
172170
cdef Py_ssize_t buf_len
173-
cdef const char* cenc = NULL
174171
cdef const char* cerr = NULL
175172
cdef int new_protocol = 0
176173

177-
if encoding is not None:
178-
PyErr_WarnEx(DeprecationWarning, "encoding is deprecated, Use raw=False instead.", 1)
179-
cenc = encoding
180-
181174
if unicode_errors is not None:
182175
cerr = unicode_errors
183176

@@ -196,7 +189,7 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
196189

197190
try:
198191
init_ctx(&ctx, object_hook, object_pairs_hook, list_hook, ext_hook,
199-
use_list, raw, strict_map_key, cenc, cerr,
192+
use_list, raw, strict_map_key, cerr,
200193
max_str_len, max_bin_len, max_array_len, max_map_len, max_ext_len)
201194
ret = unpack_construct(&ctx, buf, buf_len, &off)
202195
finally:
@@ -250,8 +243,6 @@ cdef class Unpacker(object):
250243
near future. So you must specify it explicitly for keeping backward
251244
compatibility.
252245
253-
*encoding* option which is deprecated overrides this option.
254-
255246
:param bool strict_map_key:
256247
If true, only str or bytes are accepted for map (dict) keys.
257248
It's False by default for backward-compatibility.
@@ -290,11 +281,6 @@ cdef class Unpacker(object):
290281
Deprecated, use *max_buffer_size* instead.
291282
Limits max size of ext type. (default: max_buffer_size or 1024*1024)
292283
293-
:param str encoding:
294-
Deprecated, use ``raw=False`` instead.
295-
Encoding used for decoding msgpack raw.
296-
If it is None (default), msgpack raw is deserialized to Python bytes.
297-
298284
:param str unicode_errors:
299285
Error handler used for decoding str type. (default: `'strict'`)
300286
@@ -330,7 +316,7 @@ cdef class Unpacker(object):
330316
cdef Py_ssize_t read_size
331317
# To maintain refcnt.
332318
cdef object object_hook, object_pairs_hook, list_hook, ext_hook
333-
cdef object encoding, unicode_errors
319+
cdef object unicode_errors
334320
cdef Py_ssize_t max_buffer_size
335321
cdef uint64_t stream_offset
336322

@@ -341,17 +327,16 @@ cdef class Unpacker(object):
341327
PyMem_Free(self.buf)
342328
self.buf = NULL
343329

344-
def __init__(self, file_like=None, Py_ssize_t read_size=0,
330+
def __init__(self, file_like=None, *, Py_ssize_t read_size=0,
345331
bint use_list=True, bint raw=True, bint strict_map_key=False,
346332
object object_hook=None, object object_pairs_hook=None, object list_hook=None,
347-
encoding=None, unicode_errors=None, Py_ssize_t max_buffer_size=0,
333+
unicode_errors=None, Py_ssize_t max_buffer_size=0,
348334
object ext_hook=ExtType,
349335
Py_ssize_t max_str_len=-1,
350336
Py_ssize_t max_bin_len=-1,
351337
Py_ssize_t max_array_len=-1,
352338
Py_ssize_t max_map_len=-1,
353339
Py_ssize_t max_ext_len=-1):
354-
cdef const char *cenc=NULL,
355340
cdef const char *cerr=NULL
356341

357342
self.object_hook = object_hook
@@ -392,17 +377,12 @@ cdef class Unpacker(object):
392377
self.buf_tail = 0
393378
self.stream_offset = 0
394379

395-
if encoding is not None:
396-
PyErr_WarnEx(DeprecationWarning, "encoding is deprecated, Use raw=False instead.", 1)
397-
self.encoding = encoding
398-
cenc = encoding
399-
400380
if unicode_errors is not None:
401381
self.unicode_errors = unicode_errors
402382
cerr = unicode_errors
403383

404384
init_ctx(&self.ctx, object_hook, object_pairs_hook, list_hook,
405-
ext_hook, use_list, raw, strict_map_key, cenc, cerr,
385+
ext_hook, use_list, raw, strict_map_key, cerr,
406386
max_str_len, max_bin_len, max_array_len,
407387
max_map_len, max_ext_len)
408388

msgpack/fallback.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,6 @@ class Unpacker(object):
176176
near future. So you must specify it explicitly for keeping backward
177177
compatibility.
178178
179-
*encoding* option which is deprecated overrides this option.
180-
181179
:param bool strict_map_key:
182180
If true, only str or bytes are accepted for map (dict) keys.
183181
It's False by default for backward-compatibility.
@@ -193,13 +191,10 @@ class Unpacker(object):
193191
Unpacker calls it with a list of key-value pairs after unpacking msgpack map.
194192
(See also simplejson)
195193
196-
:param str encoding:
197-
Encoding used for decoding msgpack raw.
198-
If it is None (default), msgpack raw is deserialized to Python bytes.
199-
200194
:param str unicode_errors:
201-
(deprecated) Used for decoding msgpack raw with *encoding*.
202-
(default: `'strict'`)
195+
The error handler for decoding unicode. (default: 'strict')
196+
This option should be used only when you have msgpack data which
197+
contains invalid UTF-8 string.
203198
204199
:param int max_buffer_size:
205200
Limits size of data waiting unpacked. 0 means system's INT_MAX (default).
@@ -252,18 +247,13 @@ class Unpacker(object):
252247

253248
def __init__(self, file_like=None, read_size=0, use_list=True, raw=True, strict_map_key=False,
254249
object_hook=None, object_pairs_hook=None, list_hook=None,
255-
encoding=None, unicode_errors=None, max_buffer_size=0,
250+
unicode_errors=None, max_buffer_size=0,
256251
ext_hook=ExtType,
257252
max_str_len=-1,
258253
max_bin_len=-1,
259254
max_array_len=-1,
260255
max_map_len=-1,
261256
max_ext_len=-1):
262-
if encoding is not None:
263-
warnings.warn(
264-
"encoding is deprecated, Use raw=False instead.",
265-
DeprecationWarning, stacklevel=2)
266-
267257
if unicode_errors is None:
268258
unicode_errors = 'strict'
269259

@@ -306,7 +296,6 @@ def __init__(self, file_like=None, read_size=0, use_list=True, raw=True, strict_
306296
self._read_size = read_size or min(self._max_buffer_size, 16*1024)
307297
self._raw = bool(raw)
308298
self._strict_map_key = bool(strict_map_key)
309-
self._encoding = encoding
310299
self._unicode_errors = unicode_errors
311300
self._use_list = use_list
312301
self._list_hook = list_hook
@@ -662,9 +651,7 @@ def _unpack(self, execute=EX_CONSTRUCT):
662651
if execute == EX_SKIP:
663652
return
664653
if typ == TYPE_RAW:
665-
if self._encoding is not None:
666-
obj = obj.decode(self._encoding, self._unicode_errors)
667-
elif self._raw:
654+
if self._raw:
668655
obj = bytes(obj)
669656
else:
670657
obj = obj.decode('utf_8', self._unicode_errors)

msgpack/unpack.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ typedef struct unpack_user {
2727
PyObject *object_hook;
2828
PyObject *list_hook;
2929
PyObject *ext_hook;
30-
const char *encoding;
3130
const char *unicode_errors;
3231
Py_ssize_t max_str_len, max_bin_len, max_array_len, max_map_len, max_ext_len;
3332
} unpack_user;
@@ -232,9 +231,7 @@ static inline int unpack_callback_raw(unpack_user* u, const char* b, const char*
232231

233232
PyObject *py;
234233

235-
if (u->encoding) {
236-
py = PyUnicode_Decode(p, l, u->encoding, u->unicode_errors);
237-
} else if (u->raw) {
234+
if (u->raw) {
238235
py = PyBytes_FromStringAndSize(p, l);
239236
} else {
240237
py = PyUnicode_DecodeUTF8(p, l, u->unicode_errors);

0 commit comments

Comments
 (0)
0