10000 Add raw_as_bytes option to Unpacker. by methane · Pull Request #265 · msgpack/msgpack-python · GitHub
[go: up one dir, main page]

Skip to content

Add raw_as_bytes option to Unpacker. #265

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 11 commits into from
Jan 11, 2018
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
Prev Previous commit
Next Next commit
wip
  • Loading branch information
methane committed Jan 10, 2018
commit 2fe47afc4044cb1f0ff317b491b5f8ac4da89b63
43 changes: 20 additions & 23 deletions msgpack/_unpacker.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ cdef inline int get_data_from_buffer(object obj,
return 1

def unpackb(object packed, object object_hook=None, object list_hook=None,
bint use_list=1, raw_as_bytes=None, encoding=None, unicode_errors="strict",
bint use_list=True, int raw_as_bytes=-1, encoding=None, unicode_errors="strict",
object_pairs_hook=None, ext_hook=ExtType,
Py_ssize_t max_str_len=2147483647, # 2**32-1
Py_ssize_t max_bin_len=2147483647,
Expand All @@ -184,32 +184,29 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
cdef int new_protocol = 0
cdef bint _raw_as_bytes

if raw_as_bytes == -1:
_raw_as_bytes = 1
else:
if encoding is not None:
raise TypeError("raw_as_bytes and encoding are mutually exclusive")
_raw_as_bytes = raw_as_bytes

if encoding is not None:
PyErr_WarnEx(
DeprecationWarning,
"encoding is deprecated, Use raw_as_bytes=False instead.",
1)
#PyErr_WarnEx(
# DeprecationWarning,
# "encoding is deprecated, Use raw_as_bytes=False instead.",
# 1)
_raw_as_bytes = 0
if isinstance(encoding, unicode):
encoding = encoding.encode('ascii')
cenc = PyBytes_AsString(encoding)

if unicode_errors is not None:
#PyErr_WarnEx(DeprecationWarning, "unicode_errors is deprecated", 1)
if isinstance(unicode_errors, unicode):
unicode_errors = unicode_errors.encode('ascii')
cerr = PyBytes_AsString(unicode_errors)

if raw_as_bytes is None:
PyErr_WarnEx(
FutureWarning,
"raw_as_bytes option is not specified. Default value of the option will be changed in future version.",
1)
_raw_as_bytes = 1
else:
if encoding is not None:
raise TypeError("raw_as_bytes and encoding are mutually exclusive")
_raw_as_bytes = raw_as_bytes
encoding = "utf_8"

get_data_from_buffer(packed, &view, &buf, &buf_len, &new_protocol)
try:
init_ctx(&ctx, object_hook, object_pairs_hook, list_hook, ext_hook,
Expand Down Expand Up @@ -350,7 +347,7 @@ cdef class Unpacker(object):
PyMem_Free(self.buf)
self.buf = NULL

def __init__(self, file_like=None, Py_ssize_t read_size=0, bint use_list=1, raw_as_bytes=None,
def __init__(self, file_like=None, Py_ssize_t read_size=0, bint use_list=1, bint raw_as_bytes=-1,
object object_hook=None, object object_pairs_hook=None, object list_hook=None,
encoding=None, unicode_errors='strict', int max_buffer_size=0,
object ext_hook=ExtType,
Expand All @@ -364,11 +361,11 @@ cdef class Unpacker(object):

cdef bint _raw_as_bytes

if raw_as_bytes is None:
PyErr_WarnEx(
FutureWarning,
"raw_as_bytes option is not specified. Default value of the option will be changed in future version.",
1)
if raw_as_bytes < 0:
#PyErr_WarnEx(
# FutureWarning,
# "raw_as_bytes option is not specified. Default value of the option will be changed in future version.",
# 1)
_raw_as_bytes = 1
else:
_raw_as_bytes = raw_as_bytes
Expand Down
7 changes: 5 additions & 2 deletions msgpack/unpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,13 @@ static inline int unpack_callback_raw(unpack_user* u, const char* b, const char*
}

PyObject *py;
if(u->encoding) {

if (u->encoding) {
py = PyUnicode_Decode(p, l, u->encoding, u->unicode_errors);
} else {
} else if (u->raw_as_bytes) {
py = PyBytes_FromStringAndSize(p, l);
} else {
py = PyUnicode_DecodeUTF8(p, l, NULL);
}
if (!py)
return -1;
Expand Down
0