mirror of
https://github.com/msgpack/msgpack-python.git
synced 2025-10-20 04:13:16 +00:00
Use C++ function templating for skip()/construct()
This commit is contained in:
parent
96ed236c1d
commit
d56e2b2c8a
4 changed files with 27 additions and 22 deletions
|
@ -208,8 +208,10 @@ cdef extern from "unpack.h":
|
||||||
unsigned int ct
|
unsigned int ct
|
||||||
PyObject* key
|
PyObject* key
|
||||||
|
|
||||||
int template_execute(template_context* ctx, const_char_ptr data,
|
ctypedef int (*execute_fn)(template_context* ctx, const_char_ptr data,
|
||||||
size_t len, size_t* off, bint construct) except -1
|
size_t len, size_t* off) except -1
|
||||||
|
execute_fn template_construct
|
||||||
|
execute_fn template_skip
|
||||||
void template_init(template_context* ctx)
|
void template_init(template_context* ctx)
|
||||||
object template_data(template_context* ctx)
|
object template_data(template_context* ctx)
|
||||||
|
|
||||||
|
@ -257,7 +259,7 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
|
||||||
if not PyCallable_Check(list_hook):
|
if not PyCallable_Check(list_hook):
|
||||||
raise TypeError("list_hook must be a callable.")
|
raise TypeError("list_hook must be a callable.")
|
||||||
ctx.user.list_hook = <PyObject*>list_hook
|
ctx.user.list_hook = <PyObject*>list_hook
|
||||||
ret = template_execute(&ctx, buf, buf_len, &off, 1)
|
ret = template_construct(&ctx, buf, buf_len, &off)
|
||||||
if ret == 1:
|
if ret == 1:
|
||||||
obj = template_data(&ctx)
|
obj = template_data(&ctx)
|
||||||
if off < buf_len:
|
if off < buf_len:
|
||||||
|
@ -455,16 +457,13 @@ cdef class Unpacker(object):
|
||||||
else:
|
else:
|
||||||
self.file_like = None
|
self.file_like = None
|
||||||
|
|
||||||
cdef object _unpack(self, bint construct):
|
cdef object _unpack(self, execute_fn execute):
|
||||||
cdef int ret
|
cdef int ret
|
||||||
cdef object obj
|
cdef object obj
|
||||||
while 1:
|
while 1:
|
||||||
ret = template_execute(&self.ctx, self.buf, self.buf_tail, &self.buf_head, construct)
|
ret = execute(&self.ctx, self.buf, self.buf_tail, &self.buf_head)
|
||||||
if ret == 1:
|
if ret == 1:
|
||||||
if construct:
|
|
||||||
obj = template_data(&self.ctx)
|
obj = template_data(&self.ctx)
|
||||||
else:
|
|
||||||
obj = None
|
|
||||||
template_init(&self.ctx)
|
template_init(&self.ctx)
|
||||||
return obj
|
return obj
|
||||||
elif ret == 0:
|
elif ret == 0:
|
||||||
|
@ -477,17 +476,17 @@ cdef class Unpacker(object):
|
||||||
|
|
||||||
def unpack(self):
|
def unpack(self):
|
||||||
"""unpack one object"""
|
"""unpack one object"""
|
||||||
return self._unpack(1)
|
return self._unpack(template_construct)
|
||||||
|
|
||||||
def skip(self):
|
def skip(self):
|
||||||
"""read and ignore one object, returning None"""
|
"""read and ignore one object, returning None"""
|
||||||
return self._unpack(0)
|
return self._unpack(template_skip)
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __next__(self):
|
def __next__(self):
|
||||||
return self._unpack(1)
|
return self._unpack(template_construct)
|
||||||
|
|
||||||
# for debug.
|
# for debug.
|
||||||
#def _buf(self):
|
#def _buf(self):
|
||||||
|
|
|
@ -41,6 +41,7 @@ typedef struct unpack_user {
|
||||||
|
|
||||||
#define msgpack_unpack_user unpack_user
|
#define msgpack_unpack_user unpack_user
|
||||||
|
|
||||||
|
typedef int (*execute_fn)(msgpack_unpack_struct(_context)* ctx, const char* data, size_t len, size_t* off);
|
||||||
|
|
||||||
struct template_context;
|
struct template_context;
|
||||||
typedef struct template_context template_context;
|
typedef struct template_context template_context;
|
||||||
|
|
|
@ -95,7 +95,8 @@ msgpack_unpack_func(msgpack_unpack_object, _data)(msgpack_unpack_struct(_context
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const char* data, size_t len, size_t* off, int construct)
|
template <bool construct>
|
||||||
|
msgpack_unpack_func(int, _execute)(msgpack_unpack_struct(_context)* ctx, const char* data, size_t len, size_t* off)
|
||||||
{
|
{
|
||||||
assert(len >= *off);
|
assert(len >= *off);
|
||||||
|
|
||||||
|
@ -380,6 +381,8 @@ _header_again:
|
||||||
|
|
||||||
|
|
||||||
_finish:
|
_finish:
|
||||||
|
if (!construct)
|
||||||
|
msgpack_unpack_callback(_nil)(user, &obj);
|
||||||
stack[0].obj = obj;
|
stack[0].obj = obj;
|
||||||
++p;
|
++p;
|
||||||
ret = 1;
|
ret = 1;
|
||||||
|
@ -405,13 +408,6 @@ _end:
|
||||||
#undef construct_cb
|
#undef construct_cb
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#undef msgpack_unpack_func
|
|
||||||
#undef msgpack_unpack_callback
|
|
||||||
#undef msgpack_unpack_struct
|
|
||||||
#undef msgpack_unpack_object
|
|
||||||
#undef msgpack_unpack_user
|
|
||||||
|
|
||||||
#undef push_simple_value
|
#undef push_simple_value
|
||||||
#undef push_fixed_value
|
#undef push_fixed_value
|
||||||
#undef push_variable_value
|
#undef push_variable_value
|
||||||
|
@ -419,6 +415,15 @@ _end:
|
||||||
#undef again_fixed_trail_if_zero
|
#undef again_fixed_trail_if_zero
|
||||||
#undef start_container
|
#undef start_container
|
||||||
|
|
||||||
|
static const execute_fn template_construct = &template_execute<true>;
|
||||||
|
static const execute_fn template_skip = &template_execute<false>;
|
||||||
|
|
||||||
|
#undef msgpack_unpack_func
|
||||||
|
#undef msgpack_unpack_callback
|
||||||
|
#undef msgpack_unpack_struct
|
||||||
|
#undef msgpack_unpack_object
|
||||||
|
#undef msgpack_unpack_user
|
||||||
|
|
||||||
#undef NEXT_CS
|
#undef NEXT_CS
|
||||||
|
|
||||||
/* vim: set ts=4 sw=4 noexpandtab */
|
/* vim: set ts=4 sw=4 noexpandtab */
|
||||||
|
|
4
setup.py
4
setup.py
|
@ -18,7 +18,7 @@ except ImportError:
|
||||||
|
|
||||||
def cythonize(src):
|
def cythonize(src):
|
||||||
sys.stderr.write("cythonize: %r\n" % (src,))
|
sys.stderr.write("cythonize: %r\n" % (src,))
|
||||||
cython_compiler.compile([src])
|
cython_compiler.compile([src], cplus=True)
|
||||||
|
|
||||||
def ensure_source(src):
|
def ensure_source(src):
|
||||||
pyx = os.path.splitext(src)[0] + '.pyx'
|
pyx = os.path.splitext(src)[0] + '.pyx'
|
||||||
|
@ -67,7 +67,7 @@ if have_cython:
|
||||||
else:
|
else:
|
||||||
Sdist = sdist
|
Sdist = sdist
|
||||||
|
|
||||||
sources = ['msgpack/_msgpack.c']
|
sources = ['msgpack/_msgpack.cpp']
|
||||||
libraries = []
|
libraries = []
|
||||||
if sys.platform == 'win32':
|
if sys.platform == 'win32':
|
||||||
libraries.append('ws2_32')
|
libraries.append('ws2_32')
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue