diff --git a/capnp/lib/capnp.pyx b/capnp/lib/capnp.pyx index 8c531a8..290c4cf 100644 --- a/capnp/lib/capnp.pyx +++ b/capnp/lib/capnp.pyx @@ -15,6 +15,8 @@ from libc.stdlib cimport malloc, free from libc.string cimport memcpy from cython.operator cimport dereference as deref from cpython.exc cimport PyErr_Clear +from cpython cimport Py_buffer +from cpython.buffer cimport PyBUF_SIMPLE from types import ModuleType as _ModuleType import os as _os @@ -3692,7 +3694,9 @@ cdef class _BufferView: cdef char * buf def __init__(self, other): - PyObject_GetBuffer(other, &self.view, 0) + cdef int ret = PyObject_GetBuffer(other, &self.view, PyBUF_SIMPLE) + if ret < 0: + raise ValueError("Invalid buffer passed to BufferView") self.buf = self.view.buf def __dealloc__(self): @@ -3717,7 +3721,7 @@ cdef class _FlatArrayMessageReader(_MessageReader): cdef char * ptr if type(buf) == _mmap.mmap: view = _BufferView(buf) - ptr = view.view.buf + ptr = view.buf self._object_to_pin = view else: ptr = buf diff --git a/test/test_serialization.py b/test/test_serialization.py index 87c77cf..00c08cc 100644 --- a/test/test_serialization.py +++ b/test/test_serialization.py @@ -6,6 +6,7 @@ import test_regression import tempfile import pickle import mmap +import sys this_dir = os.path.dirname(__file__) @@ -41,6 +42,7 @@ def test_roundtrip_bytes(all_types): msg = all_types.TestAllTypes.from_bytes(message_bytes) test_regression.check_all_types(msg) +@pytest.mark.skipif(sys.version_info.major < 3, reason="mmap doesn't implement the buffer interface under python 2.") def test_roundtrip_bytes_mmap(all_types): msg = all_types.TestAllTypes.new_message() test_regression.init_all_types(msg)