8000 Remove encoding option from Unpacker. by methane · Pull Request #380 · msgpack/msgpack-python · GitHub
[go: up one dir, main page]

Skip to content

Remove encoding option from Unpacker. #380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Remove *encoding* option from Unpacker
  • Loading branch information
methane committed Dec 3, 2019
commit a3c9bee23de31b0f7fb7cb55f1e0e917f1486251
34 changes: 7 additions & 27 deletions msgpack/_unpacker.pyx
10000
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ cdef extern from "unpack.h":
PyObject* object_hook
PyObject* list_hook
PyObject* ext_hook
char *encoding
char *unicode_errors
Py_ssize_t max_str_len
Py_ssize_t max_bin_len
Expand All @@ -58,7 +57,7 @@ cdef inline init_ctx(unpack_context *ctx,
object object_hook, object object_pairs_hook,
object list_hook, object ext_hook,
bint use_list, bint raw, bint strict_map_key,
const char* encoding, const char* unicode_errors,
const char* unicode_errors,
Py_ssize_t max_str_len, Py_ssize_t max_bin_len,
Py_ssize_t max_array_len, Py_ssize_t max_map_len,
Py_ssize_t max_ext_len):
Expand Down Expand Up @@ -99,7 +98,6 @@ cdef inline init_ctx(unpack_context *ctx,
raise TypeError("ext_hook must be a callable.")
ctx.user.ext_hook = <PyObject*>ext_hook

ctx.user.encoding = encoding
ctx.user.unicode_errors = unicode_errors

def default_read_extended_type(typecode, data):
Expand Down Expand Up @@ -143,7 +141,7 @@ cdef inline int get_data_from_buffer(object obj,

def unpackb(object packed, object object_hook=None, object list_hook=None,
bint use_list=True, bint raw=True, bint strict_map_key=False,
encoding=None, unicode_errors=None,
unicode_errors=None,
object_pairs_hook=None, ext_hook=ExtType,
Py_ssize_t max_str_len=-1,
Py_ssize_t max_bin_len=-1,
Expand All @@ -170,14 +168,9 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
cdef Py_buffer view
cdef char* buf = NULL
cdef Py_ssize_t buf_len
cdef const char* cenc = NULL
cdef const char* cerr = NULL
cdef int new_protocol = 0

if encoding is not None:
PyErr_WarnEx(DeprecationWarning, "encoding is deprecated, Use raw=False instead.", 1)
cenc = encoding

if unicode_errors is not None:
cerr = unicode_errors

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

try:
init_ctx(&ctx, object_hook, object_pairs_hook, list_hook, ext_hook,
use_list, raw, strict_map_key, cenc, cerr,
use_list, raw, strict_map_key, cerr,
max_str_len, max_bin_len, max_array_len, max_map_len, max_ext_len)
ret = unpack_construct(&ctx, buf, buf_len, &off)
finally:
Expand Down Expand Up @@ -250,8 +243,6 @@ cdef class Unpacker(object):
near future. So you must specify it explicitly for keeping backward
compatibility.

*encoding* option which is deprecated overrides this option.

:param bool strict_map_key:
If true, only str or bytes are accepted for map (dict) keys.
It's False by default for backward-compatibility.
Expand Down Expand Up @@ -290,11 +281,6 @@ cdef class Unpacker(object):
Deprecated, use *max_buffer_size* instead.
Limits max size of ext type. (default: max_buffer_size or 1024*1024)

:param str encoding:
Deprecated, use ``raw=False`` instead.
Encoding used for decoding msgpack raw.
If it is None (default), msgpack raw is deserialized to Python bytes.

:param str unicode_errors:
Error handler used for decoding str type. (default: `'strict'`)

Expand Down Expand Up @@ -330,7 +316,7 @@ cdef class Unpacker(object):
cdef Py_ssize_t read_size
# To maintain refcnt.
cdef object object_hook, object_pairs_hook, list_hook, ext_hook
cdef object encoding, unicode_errors
cdef object unicode_errors
cdef Py_ssize_t max_buffer_size
cdef uint64_t stream_offset

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

def __init__(self, file_like=None, Py_ssize_t read_size=0,
def __init__(self, file_like=None, *, Py_ssize_t read_size=0,
bint use_list=True, bint raw=True, bint strict_map_key=False,
object object_hook=None, object object_pairs_hook=None, object list_hook=None,
encoding=None, unicode_errors=None, Py_ssize_t max_buffer_size=0,
unicode_errors=None, Py_ssize_t max_buffer_size=0,
object ext_hook=ExtType,
Py_ssize_t max_str_len=-1,
Py_ssize_t max_bin_len=-1,
Py_ssize_t max_array_len=-1,
Py_ssize_t max_map_len=-1,
Py_ssize_t max_ext_len=-1):
cdef const char *cenc=NULL,
cdef const char *cerr=NULL

self.object_hook = object_hook
Expand Down Expand Up @@ -392,17 +377,12 @@ cdef class Unpacker(object):
self.buf_tail = 0
self.stream_offset = 0

if encoding is not None:
PyErr_WarnEx(DeprecationWarning, "encoding is deprecated, Use raw=False instead.", 1)
self.encoding = encoding
cenc = encoding

if unicode_errors is not None:
self.unicode_errors = unicode_errors
cerr = unicode_errors

init_ctx(&self.ctx, object_hook, object_pairs_hook, list_hook,
ext_hook, use_list, raw, strict_map_key, cenc, cerr,
ext_hook, use_list, raw, strict_map_key, cerr,
max_str_len, max_bin_len, max_array_len,
max_map_len, max_ext_len)

Expand Down
23 changes: 5 additions & 18 deletions msgpack/fallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,6 @@ class Unpacker(object):
near future. So you must specify it explicitly for keeping backward
compatibility.

*encoding* option which is deprecated overrides this option.

:param bool strict_map_key:
If true, only str or bytes are accepted for map (dict) keys.
It's False by default for backward-compatibility.
Expand All @@ -193,13 +191,10 @@ class Unpacker(object):
Unpacker calls it with a list of key-value pairs after unpacking msgpack map.
(See also simplejson)

:param str encoding:
Encoding used for decoding msgpack raw.
If it is None (default), msgpack raw is deserialized to Python bytes.

:param str unicode_errors:
(deprecated) Used for decoding msgpack raw with *encoding*.
(default: `'strict'`)
The error handler for decoding unicode. (default: 'strict')
This option should be used only when you have msgpack data which
contains invalid UTF-8 string.

:param int max_buffer_size:
Limits size of data waiting unpacked. 0 means system's INT_MAX (default).
Expand Down Expand Up @@ -252,18 +247,13 @@ class Unpacker(object):

def __init__(self, file_like=None, read_size=0, use_list=True, raw=True, strict_map_key=False,
object_hook=None, object_pairs_hook=None, list_hook=None,
encoding=None, unicode_errors=None, max_buffer_size=0,
unicode_errors=None, max_buffer_size=0,
ext_hook=ExtType,
max_str_len=-1,
max_bin_len=-1,
max_array_len=-1,
max_map_len=-1,
max_ext_len=-1):
if encoding is not None:
warnings.warn(
"encoding is deprecated, Use raw=False instead.",
DeprecationWarning, stacklevel=2)

if unicode_errors is None:
unicode_errors = 'strict'

Expand Down Expand Up @@ -306,7 +296,6 @@ def __init__(self, file_like=None, read_size=0, use_list=True, raw=True, strict_
self._read_size = read_size or min(self._max_buffer_size, 16*1024)
self._raw = bool(raw)
self._strict_map_key = bool(strict_map_key)
self._encoding = encoding
self._unicode_errors = unicode_errors
self._use_list = use_list
self._list_hook = list_hook
Expand Down Expand Up @@ -662,9 +651,7 @@ def _unpack(self, execute=EX_CONSTRUCT):
if execute == EX_SKIP:
return
if typ == TYPE_RAW:
if self._encoding is not None:
obj = obj.decode(self._encoding, self._unicode_errors)
elif self._raw:
if self._raw:
obj = bytes(obj)
else:
obj = obj.decode('utf_8', self._unicode_errors)
Expand Down
5 changes: 1 addition & 4 deletions msgpack/unpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ typedef struct unpack_user {
PyObject *object_hook;
PyObject *list_hook;
PyObject *ext_hook;
const char *encoding;
const char *unicode_errors;
Py_ssize_t max_str_len, max_bin_len, max_array_len, max_map_len, max_ext_len;
} unpack_user;
Expand Down Expand Up @@ -232,9 +231,7 @@ static inline int unpack_callback_raw(unpack_user* u, const char* b, const char*

PyObject *py;

if (u->encoding) {
py = PyUnicode_Decode(p, l, u->encoding, u->unicode_errors);
} else if (u->raw) {
if (u->raw) {
py = PyBytes_FromStringAndSize(p, l);
} else {
py = PyUnicode_DecodeUTF8(p, l, u->unicode_errors);
Expand Down
0