mirror of
https://github.com/msgpack/msgpack-python.git
synced 2025-11-01 18:10:54 +00:00
Factor context initialisation from unpackb and Unpacker
This commit is contained in:
parent
96ed236c1d
commit
b06ed8eb75
1 changed files with 28 additions and 54 deletions
|
|
@ -213,6 +213,32 @@ cdef extern from "unpack.h":
|
|||
void template_init(template_context* ctx)
|
||||
object template_data(template_context* ctx)
|
||||
|
||||
cdef inline init_ctx(template_context *ctx, object object_hook, object list_hook, bint use_list, encoding, unicode_errors):
|
||||
template_init(ctx)
|
||||
ctx.user.use_list = use_list
|
||||
ctx.user.object_hook = ctx.user.list_hook = <PyObject*>NULL
|
||||
if object_hook is not None:
|
||||
if not PyCallable_Check(object_hook):
|
||||
raise TypeError("object_hook must be a callable.")
|
||||
ctx.user.object_hook = <PyObject*>object_hook
|
||||
if list_hook is not None:
|
||||
if not PyCallable_Check(list_hook):
|
||||
raise TypeError("list_hook must be a callable.")
|
||||
ctx.user.list_hook = <PyObject*>list_hook
|
||||
if encoding is None:
|
||||
ctx.user.encoding = NULL
|
||||
ctx.user.unicode_errors = NULL
|
||||
else:
|
||||
if isinstance(encoding, unicode):
|
||||
_bencoding = encoding.encode('ascii')
|
||||
else:
|
||||
_bencoding = encoding
|
||||
ctx.user.encoding = PyBytes_AsString(_bencoding)
|
||||
if isinstance(unicode_errors, unicode):
|
||||
_berrors = unicode_errors.encode('ascii')
|
||||
else:
|
||||
_berrors = unicode_errors
|
||||
ctx.user.unicode_errors = PyBytes_AsString(_berrors)
|
||||
|
||||
def unpackb(object packed, object object_hook=None, object list_hook=None,
|
||||
bint use_list=0, encoding=None, unicode_errors="strict",
|
||||
|
|
@ -229,34 +255,7 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
|
|||
cdef Py_ssize_t buf_len
|
||||
PyObject_AsReadBuffer(packed, <const_void_ptr*>&buf, &buf_len)
|
||||
|
||||
if encoding is None:
|
||||
enc = NULL
|
||||
err = NULL
|
||||
else:
|
||||
if isinstance(encoding, unicode):
|
||||
bencoding = encoding.encode('ascii')
|
||||
else:
|
||||
bencoding = encoding
|
||||
if isinstance(unicode_errors, unicode):
|
||||
berrors = unicode_errors.encode('ascii')
|
||||
else:
|
||||
berrors = unicode_errors
|
||||
enc = PyBytes_AsString(bencoding)
|
||||
err = PyBytes_AsString(berrors)
|
||||
|
||||
template_init(&ctx)
|
||||
ctx.user.use_list = use_list
|
||||
ctx.user.object_hook = ctx.user.list_hook = NULL
|
||||
ctx.user.encoding = <const_char_ptr>enc
|
||||
ctx.user.unicode_errors = <const_char_ptr>err
|
||||
if object_hook is not None:
|
||||
if not PyCallable_Check(object_hook):
|
||||
raise TypeError("object_hook must be a callable.")
|
||||
ctx.user.object_hook = <PyObject*>object_hook
|
||||
if list_hook is not None:
|
||||
if not PyCallable_Check(list_hook):
|
||||
raise TypeError("list_hook must be a callable.")
|
||||
ctx.user.list_hook = <PyObject*>list_hook
|
||||
init_ctx(&ctx, object_hook, list_hook, use_list, encoding, unicode_errors)
|
||||
ret = template_execute(&ctx, buf, buf_len, &off, 1)
|
||||
if ret == 1:
|
||||
obj = template_data(&ctx)
|
||||
|
|
@ -348,7 +347,6 @@ cdef class Unpacker(object):
|
|||
def __init__(self, file_like=None, Py_ssize_t read_size=0, bint use_list=0,
|
||||
object object_hook=None, object list_hook=None,
|
||||
encoding=None, unicode_errors='strict', int max_buffer_size=0,
|
||||
object object_pairs_hook=None,
|
||||
):
|
||||
self.use_list = use_list
|
||||
self.file_like = file_like
|
||||
|
|
@ -370,31 +368,7 @@ cdef class Unpacker(object):
|
|||
self.buf_size = read_size
|
||||
self.buf_head = 0
|
||||
self.buf_tail = 0
|
||||
template_init(&self.ctx)
|
||||
self.ctx.user.use_list = use_list
|
||||
self.ctx.user.object_hook = self.ctx.user.list_hook = <PyObject*>NULL
|
||||
if object_hook is not None:
|
||||
if not PyCallable_Check(object_hook):
|
||||
raise TypeError("object_hook must be a callable.")
|
||||
self.ctx.user.object_hook = <PyObject*>object_hook
|
||||
if list_hook is not None:
|
||||
if not PyCallable_Check(list_hook):
|
||||
raise TypeError("list_hook must be a callable.")
|
||||
self.ctx.user.list_hook = <PyObject*>list_hook
|
||||
if encoding is None:
|
||||
self.ctx.user.encoding = NULL
|
||||
self.ctx.user.unicode_errors = NULL
|
||||
else:
|
||||
if isinstance(encoding, unicode):
|
||||
self._bencoding = encoding.encode('ascii')
|
||||
else:
|
||||
self._bencoding = encoding
|
||||
self.ctx.user.encoding = PyBytes_AsString(self._bencoding)
|
||||
if isinstance(unicode_errors, unicode):
|
||||
self._berrors = unicode_errors.encode('ascii')
|
||||
else:
|
||||
self._berrors = unicode_errors
|
||||
self.ctx.user.unicode_errors = PyBytes_AsString(self._berrors)
|
||||
init_ctx(&self.ctx, object_hook, list_hook, use_list, encoding, unicode_errors)
|
||||
|
||||
def feed(self, object next_bytes):
|
||||
cdef char* buf
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue