Skip to content

Commit 76b2888

Browse files
committed
Not fully tested: raw bytes as memoryview
Signed-off-by: Rodrigo Tobar <rtobar@icrar.org>
1 parent edb61db commit 76b2888

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

msgpack/_unpacker.pyx

+15-6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ cdef extern from "unpack.h":
2626
ctypedef struct msgpack_user:
2727
bint use_list
2828
bint raw
29+
bint bytes_memoryview
2930
bint has_pairs_hook # call object_hook with k-v pairs
3031
bint strict_map_key
3132
int timestamp
@@ -60,7 +61,8 @@ cdef extern from "unpack.h":
6061
cdef inline init_ctx(unpack_context *ctx,
6162
object object_hook, object object_pairs_hook,
6263
object list_hook, object ext_hook,
63-
bint use_list, bint raw, int timestamp,
64+
bint use_list, bint raw, bint bytes_memoryview,
65+
int timestamp,
6466
bint strict_map_key,
6567
const char* unicode_errors,
6668
Py_ssize_t max_str_len, Py_ssize_t max_bin_len,
@@ -69,6 +71,7 @@ cdef inline init_ctx(unpack_context *ctx,
6971
unpack_init(ctx)
7072
ctx.user.use_list = use_list
7173
ctx.user.raw = raw
74+
ctx.user.bytes_memoryview = bytes_memoryview
7275
ctx.user.strict_map_key = strict_map_key
7376
ctx.user.object_hook = ctx.user.list_hook = <PyObject*>NULL
7477
ctx.user.max_str_len = max_str_len
@@ -141,7 +144,8 @@ cdef inline int get_data_from_buffer(object obj,
141144

142145

143146
def unpackb(object packed, *, object object_hook=None, object list_hook=None,
144-
bint use_list=True, bint raw=False, int timestamp=0, bint strict_map_key=True,
147+
bint use_list=True, bint raw=False, bint bytes_memoryview=False,
148+
int timestamp=0, bint strict_map_key=True,
145149
unicode_errors=None,
146150
object_pairs_hook=None, ext_hook=ExtType,
147151
Py_ssize_t max_str_len=-1,
@@ -189,7 +193,7 @@ def unpackb(object packed, *, object object_hook=None, object list_hook=None,
189193

190194
try:
191195
init_ctx(&ctx, object_hook, object_pairs_hook, list_hook, ext_hook,
192-
use_list, raw, timestamp, strict_map_key, cerr,
196+
use_list, raw, bytes_memoryview, timestamp, strict_map_key, cerr,
193197
max_str_len, max_bin_len, max_array_len, max_map_len, max_ext_len)
194198
ret = unpack_construct(&ctx, buf, buf_len, &off)
195199
finally:
@@ -227,9 +231,13 @@ cdef class Unpacker(object):
227231
Otherwise, unpack to Python tuple. (default: True)
228232
229233
:param bool raw:
230-
If true, unpack msgpack raw to Python bytes.
234+
If true, unpack msgpack raw strings to Python bytes.
231235
Otherwise, unpack to Python str by decoding with UTF-8 encoding (default).
232236
237+
:param bool bytes_memoryview:
238+
If true, and if possible, unpack msgpack bytes to a memory_view.
239+
Otherwise, unpack into a Python bytes (default).
240+
233241
:param int timestamp:
234242
Control how timestamp type is unpacked:
235243
@@ -325,7 +333,8 @@ cdef class Unpacker(object):
325333
self.buf = NULL
326334

327335
def __init__(self, file_like=None, *, Py_ssize_t read_size=0,
328-
bint use_list=True, bint raw=False, int timestamp=0, bint strict_map_key=True,
336+
bint use_list=True, bint raw=False, bint bytes_memoryview=False,
337+
int timestamp=0, bint strict_map_key=True,
329338
object object_hook=None, object object_pairs_hook=None, object list_hook=None,
330339
unicode_errors=None, Py_ssize_t max_buffer_size=100*1024*1024,
331340
object ext_hook=ExtType,
@@ -380,7 +389,7 @@ cdef class Unpacker(object):
380389
cerr = unicode_errors
381390

382391
init_ctx(&self.ctx, object_hook, object_pairs_hook, list_hook,
383-
ext_hook, use_list, raw, timestamp, strict_map_key, cerr,
392+
ext_hook, use_list, raw, bytes_memoryview, timestamp, strict_map_key, cerr,
384393
max_str_len, max_bin_len, max_array_len,
385394
max_map_len, max_ext_len)
386395

msgpack/unpack.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
typedef struct unpack_user {
2323
bool use_list;
2424
bool raw;
25+
bool bytes_memoryview;
2526
bool has_pairs_hook;
2627
bool strict_map_key;
2728
int timestamp;
@@ -256,7 +257,13 @@ static inline int unpack_callback_bin(unpack_user* u, const char* b, const char*
256257
return -1;
257258
}
258259

259-
PyObject *py = PyBytes_FromStringAndSize(p, l);
260+
PyObject *py;
261+
if (u->bytes_memoryview) {
262+
py = PyMemoryView_FromMemory((char *)p, l, PyBUF_READ);
263+
}
264+
else {
265+
py = PyBytes_FromStringAndSize(p, l);
266+
}
260267
if (!py)
261268
return -1;
262269
*o = py;

0 commit comments

Comments
 (0)