22# cython: embedsignature=True
33
44from cpython cimport *
5+ cdef extern from " Python.h" :
6+ ctypedef char * const_void_ptr " const void*"
7+ ctypedef struct PyObject
8+ cdef int PyObject_AsReadBuffer(object o, const_void_ptr* buff, Py_ssize_t* buf_len) except - 1
59
610from libc.stdlib cimport *
711from libc.string cimport *
@@ -15,8 +19,8 @@ from msgpack.exceptions import (
1519 )
1620
1721
22+
1823cdef extern from " unpack.h" :
19- ctypedef struct PyObject
2024 ctypedef struct msgpack_user:
2125 bint use_list
2226 PyObject* object_hook
@@ -87,33 +91,32 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
8791 cdef size_t off = 0
8892 cdef int ret
8993
90- cdef Py_buffer buff
94+ cdef char * buf
95+ cdef Py_ssize_t buf_len
9196 cdef char * cenc = NULL
9297 cdef char * cerr = NULL
9398
94- PyObject_GetBuffer(packed, & buff, PyBUF_SIMPLE)
95- try :
96- if encoding is not None :
97- if isinstance (encoding, unicode ):
98- encoding = encoding.encode(' ascii' )
99- cenc = PyBytes_AsString(encoding)
100-
101- if unicode_errors is not None :
102- if isinstance (unicode_errors, unicode ):
103- unicode_errors = unicode_errors.encode(' ascii' )
104- cerr = PyBytes_AsString(unicode_errors)
105-
106- init_ctx(& ctx, object_hook, object_pairs_hook, list_hook, use_list, cenc, cerr)
107- ret = unpack_construct(& ctx, < char * > buff.buf, buff.len, & off)
108- if ret == 1 :
109- obj = unpack_data(& ctx)
110- if off < buff.len:
111- raise ExtraData(obj, PyBytes_FromStringAndSize(< char * > buff.buf+ off, buff.len- off))
112- return obj
113- else :
114- raise UnpackValueError(" Unpack failed: error = %d " % (ret,))
115- finally :
116- PyBuffer_Release(& buff)
99+ PyObject_AsReadBuffer(packed, < const_void_ptr* > & buf, & buf_len)
100+
101+ if encoding is not None :
102+ if isinstance (encoding, unicode ):
103+ encoding = encoding.encode(' ascii' )
104+ cenc = PyBytes_AsString(encoding)
105+
106+ if unicode_errors is not None :
107+ if isinstance (unicode_errors, unicode ):
108+ unicode_errors = unicode_errors.encode(' ascii' )
109+ cerr = PyBytes_AsString(unicode_errors)
110+
111+ init_ctx(& ctx, object_hook, object_pairs_hook, list_hook, use_list, cenc, cerr)
112+ ret = unpack_construct(& ctx, buf, buf_len, & off)
113+ if ret == 1 :
114+ obj = unpack_data(& ctx)
115+ if off < buf_len:
116+ raise ExtraData(obj, PyBytes_FromStringAndSize(buf+ off, buf_len- off))
117+ return obj
118+ else :
119+ raise UnpackValueError(" Unpack failed: error = %d " % (ret,))
117120
118121
119122def unpack (object stream , object object_hook = None , object list_hook = None ,
0 commit comments