mirror of
https://github.com/msgpack/msgpack-c.git
synced 2025-03-23 17:25:22 +01:00
Add use_tuple
option that returns tuple for array object to Unpacker.
This commit is contained in:
parent
4d33bd456c
commit
f4c5b15cc6
@ -151,7 +151,11 @@ def packb(object o):
|
|||||||
packs = packb
|
packs = packb
|
||||||
|
|
||||||
cdef extern from "unpack.h":
|
cdef extern from "unpack.h":
|
||||||
|
ctypedef struct msgpack_user:
|
||||||
|
int use_tuple
|
||||||
|
|
||||||
ctypedef struct template_context:
|
ctypedef struct template_context:
|
||||||
|
msgpack_user user
|
||||||
PyObject* obj
|
PyObject* obj
|
||||||
size_t count
|
size_t count
|
||||||
unsigned int ct
|
unsigned int ct
|
||||||
@ -170,6 +174,7 @@ def unpackb(object packed_bytes):
|
|||||||
cdef size_t off = 0
|
cdef size_t off = 0
|
||||||
cdef int ret
|
cdef int ret
|
||||||
template_init(&ctx)
|
template_init(&ctx)
|
||||||
|
ctx.user.use_tuple = 0
|
||||||
ret = template_execute(&ctx, p, len(packed_bytes), &off)
|
ret = template_execute(&ctx, p, len(packed_bytes), &off)
|
||||||
if ret == 1:
|
if ret == 1:
|
||||||
return template_data(&ctx)
|
return template_data(&ctx)
|
||||||
@ -225,6 +230,7 @@ cdef class Unpacker(object):
|
|||||||
cdef object file_like
|
cdef object file_like
|
||||||
cdef int read_size
|
cdef int read_size
|
||||||
cdef object waiting_bytes
|
cdef object waiting_bytes
|
||||||
|
cdef int use_tuple
|
||||||
|
|
||||||
def __cinit__(self):
|
def __cinit__(self):
|
||||||
self.buf = NULL
|
self.buf = NULL
|
||||||
@ -233,9 +239,10 @@ cdef class Unpacker(object):
|
|||||||
if self.buf:
|
if self.buf:
|
||||||
free(self.buf);
|
free(self.buf);
|
||||||
|
|
||||||
def __init__(self, file_like=None, int read_size=0):
|
def __init__(self, file_like=None, int read_size=0, use_tuple=0):
|
||||||
if read_size == 0:
|
if read_size == 0:
|
||||||
read_size = 1024*1024
|
read_size = 1024*1024
|
||||||
|
self.use_tuple = use_tuple
|
||||||
self.file_like = file_like
|
self.file_like = file_like
|
||||||
self.read_size = read_size
|
self.read_size = read_size
|
||||||
self.waiting_bytes = []
|
self.waiting_bytes = []
|
||||||
@ -244,6 +251,7 @@ cdef class Unpacker(object):
|
|||||||
self.buf_head = 0
|
self.buf_head = 0
|
||||||
self.buf_tail = 0
|
self.buf_tail = 0
|
||||||
template_init(&self.ctx)
|
template_init(&self.ctx)
|
||||||
|
self.ctx.user.use_tuple = use_tuple
|
||||||
|
|
||||||
def feed(self, next_bytes):
|
def feed(self, next_bytes):
|
||||||
if not isinstance(next_bytes, str):
|
if not isinstance(next_bytes, str):
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
#include "unpack_define.h"
|
#include "unpack_define.h"
|
||||||
|
|
||||||
typedef struct unpack_user {
|
typedef struct unpack_user {
|
||||||
|
int use_tuple;
|
||||||
} unpack_user;
|
} unpack_user;
|
||||||
|
|
||||||
|
|
||||||
@ -135,7 +136,8 @@ static inline int template_callback_false(unpack_user* u, msgpack_unpack_object*
|
|||||||
|
|
||||||
static inline int template_callback_array(unpack_user* u, unsigned int n, msgpack_unpack_object* o)
|
static inline int template_callback_array(unpack_user* u, unsigned int n, msgpack_unpack_object* o)
|
||||||
{
|
{
|
||||||
PyObject *p = PyList_New(n);
|
PyObject *p = u->use_tuple ? PyTuple_New(n) : PyList_New(n);
|
||||||
|
|
||||||
if (!p)
|
if (!p)
|
||||||
return -1;
|
return -1;
|
||||||
*o = p;
|
*o = p;
|
||||||
@ -143,7 +145,15 @@ static inline int template_callback_array(unpack_user* u, unsigned int n, msgpac
|
|||||||
}
|
}
|
||||||
|
|
||||||
static inline int template_callback_array_item(unpack_user* u, unsigned int current, msgpack_unpack_object* c, msgpack_unpack_object o)
|
static inline int template_callback_array_item(unpack_user* u, unsigned int current, msgpack_unpack_object* c, msgpack_unpack_object o)
|
||||||
{ PyList_SET_ITEM(*c, current, o); return 0; }
|
{
|
||||||
|
if (u->use_tuple) {
|
||||||
|
PyTuple_SET_ITEM(*c, current, o);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
PyList_SET_ITEM(*c, current, o);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static inline int template_callback_map(unpack_user* u, unsigned int n, msgpack_unpack_object* o)
|
static inline int template_callback_map(unpack_user* u, unsigned int n, msgpack_unpack_object* o)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user