mirror of
https://github.com/msgpack/msgpack-python.git
synced 2025-11-02 02:20:54 +00:00
Revert "Use new buffer interface."
This reverts commit 085db7f8dc.
Conflicts:
msgpack/_unpacker.pyx
This commit is contained in:
parent
08c56d66f6
commit
bbe86e7a92
1 changed files with 26 additions and 23 deletions
|
|
@ -2,6 +2,10 @@
|
||||||
#cython: embedsignature=True
|
#cython: embedsignature=True
|
||||||
|
|
||||||
from cpython cimport *
|
from cpython cimport *
|
||||||
|
cdef extern from "Python.h":
|
||||||
|
ctypedef char* const_void_ptr "const void*"
|
||||||
|
ctypedef struct PyObject
|
||||||
|
cdef int PyObject_AsReadBuffer(object o, const_void_ptr* buff, Py_ssize_t* buf_len) except -1
|
||||||
|
|
||||||
from libc.stdlib cimport *
|
from libc.stdlib cimport *
|
||||||
from libc.string cimport *
|
from libc.string cimport *
|
||||||
|
|
@ -15,8 +19,8 @@ from msgpack.exceptions import (
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
cdef extern from "unpack.h":
|
cdef extern from "unpack.h":
|
||||||
ctypedef struct PyObject
|
|
||||||
ctypedef struct msgpack_user:
|
ctypedef struct msgpack_user:
|
||||||
bint use_list
|
bint use_list
|
||||||
PyObject* object_hook
|
PyObject* object_hook
|
||||||
|
|
@ -87,33 +91,32 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
|
||||||
cdef size_t off = 0
|
cdef size_t off = 0
|
||||||
cdef int ret
|
cdef int ret
|
||||||
|
|
||||||
cdef Py_buffer buff
|
cdef char* buf
|
||||||
|
cdef Py_ssize_t buf_len
|
||||||
cdef char* cenc = NULL
|
cdef char* cenc = NULL
|
||||||
cdef char* cerr = NULL
|
cdef char* cerr = NULL
|
||||||
|
|
||||||
PyObject_GetBuffer(packed, &buff, PyBUF_SIMPLE)
|
PyObject_AsReadBuffer(packed, <const_void_ptr*>&buf, &buf_len)
|
||||||
try:
|
|
||||||
if encoding is not None:
|
|
||||||
if isinstance(encoding, unicode):
|
|
||||||
encoding = encoding.encode('ascii')
|
|
||||||
cenc = PyBytes_AsString(encoding)
|
|
||||||
|
|
||||||
if unicode_errors is not None:
|
if encoding is not None:
|
||||||
if isinstance(unicode_errors, unicode):
|
if isinstance(encoding, unicode):
|
||||||
unicode_errors = unicode_errors.encode('ascii')
|
encoding = encoding.encode('ascii')
|
||||||
cerr = PyBytes_AsString(unicode_errors)
|
cenc = PyBytes_AsString(encoding)
|
||||||
|
|
||||||
init_ctx(&ctx, object_hook, object_pairs_hook, list_hook, use_list, cenc, cerr)
|
if unicode_errors is not None:
|
||||||
ret = unpack_construct(&ctx, <char*>buff.buf, buff.len, &off)
|
if isinstance(unicode_errors, unicode):
|
||||||
if ret == 1:
|
unicode_errors = unicode_errors.encode('ascii')
|
||||||
obj = unpack_data(&ctx)
|
cerr = PyBytes_AsString(unicode_errors)
|
||||||
if off < buff.len:
|
|
||||||
raise ExtraData(obj, PyBytes_FromStringAndSize(<char*>buff.buf+off, buff.len-off))
|
init_ctx(&ctx, object_hook, object_pairs_hook, list_hook, use_list, cenc, cerr)
|
||||||
return obj
|
ret = unpack_construct(&ctx, buf, buf_len, &off)
|
||||||
else:
|
if ret == 1:
|
||||||
raise UnpackValueError("Unpack failed: error = %d" % (ret,))
|
obj = unpack_data(&ctx)
|
||||||
finally:
|
if off < buf_len:
|
||||||
PyBuffer_Release(&buff)
|
raise ExtraData(obj, PyBytes_FromStringAndSize(buf+off, buf_len-off))
|
||||||
|
return obj
|
||||||
|
else:
|
||||||
|
raise UnpackValueError("Unpack failed: error = %d" % (ret,))
|
||||||
|
|
||||||
|
|
||||||
def unpack(object stream, object object_hook=None, object list_hook=None,
|
def unpack(object stream, object object_hook=None, object list_hook=None,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue