Skip to content

Commit d4bb86c

Browse files
committed
Merge pull request #65 from msgpack/old-buffer
Stop using new style buffer API.
2 parents 63b9fa5 + 956f55e commit d4bb86c

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ doc:
1010
cd docs && make zip
1111

1212
cython:
13-
cython msgpack/*.pyx
13+
cython --cplus msgpack/*.pyx
1414

1515
python3: cython
1616
python3 setup.py build_ext -i -f

msgpack/_unpacker.pyx

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
#cython: embedsignature=True
33

44
from cpython cimport *
5+
cdef extern from "Python.h":
6+
ctypedef struct PyObject
7+
cdef int PyObject_AsReadBuffer(object o, const void* buff, Py_ssize_t* buf_len) except -1
58

69
from libc.stdlib cimport *
710
from libc.string cimport *
@@ -15,8 +18,8 @@ from msgpack.exceptions import (
1518
)
1619

1720

21+
1822
cdef extern from "unpack.h":
19-
ctypedef struct PyObject
2023
ctypedef struct msgpack_user:
2124
bint use_list
2225
PyObject* object_hook
@@ -87,33 +90,32 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
8790
cdef size_t off = 0
8891
cdef int ret
8992

90-
cdef Py_buffer buff
93+
cdef char* buf
94+
cdef Py_ssize_t buf_len
9195
cdef char* cenc = NULL
9296
cdef char* cerr = NULL
9397

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)
98+
PyObject_AsReadBuffer(packed, <const void*>&buf, &buf_len)
99+
100+
if encoding is not None:
101+
if isinstance(encoding, unicode):
102+
encoding = encoding.encode('ascii')
103+
cenc = PyBytes_AsString(encoding)
104+
105+
if unicode_errors is not None:
106+
if isinstance(unicode_errors, unicode):
107+
unicode_errors = unicode_errors.encode('ascii')
108+
cerr = PyBytes_AsString(unicode_errors)
109+
110+
init_ctx(&ctx, object_hook, object_pairs_hook, list_hook, use_list, cenc, cerr)
111+
ret = unpack_construct(&ctx, buf, buf_len, &off)
112+
if ret == 1:
113+
obj = unpack_data(&ctx)
114+
if off < buf_len:
115+
raise ExtraData(obj, PyBytes_FromStringAndSize(buf+off, buf_len-off))
116+
return obj
117+
else:
118+
raise UnpackValueError("Unpack failed: error = %d" % (ret,))
117119

118120

119121
def unpack(object stream, object object_hook=None, object list_hook=None,

0 commit comments

Comments
 (0)