Throw an exception on invalid BufferView instantiation

Also adjust tests to skip mmap under Python 2.
This commit is contained in:
Jason Paryani
2016-03-04 12:42:45 -08:00
parent aabd61301c
commit 2be308db27
2 changed files with 8 additions and 2 deletions

View File

@@ -15,6 +15,8 @@ from libc.stdlib cimport malloc, free
from libc.string cimport memcpy from libc.string cimport memcpy
from cython.operator cimport dereference as deref from cython.operator cimport dereference as deref
from cpython.exc cimport PyErr_Clear from cpython.exc cimport PyErr_Clear
from cpython cimport Py_buffer
from cpython.buffer cimport PyBUF_SIMPLE
from types import ModuleType as _ModuleType from types import ModuleType as _ModuleType
import os as _os import os as _os
@@ -3692,7 +3694,9 @@ cdef class _BufferView:
cdef char * buf cdef char * buf
def __init__(self, other): 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 = <char*>self.view.buf self.buf = <char*>self.view.buf
def __dealloc__(self): def __dealloc__(self):
@@ -3717,7 +3721,7 @@ cdef class _FlatArrayMessageReader(_MessageReader):
cdef char * ptr cdef char * ptr
if type(buf) == _mmap.mmap: if type(buf) == _mmap.mmap:
view = _BufferView(buf) view = _BufferView(buf)
ptr = <char*>view.view.buf ptr = view.buf
self._object_to_pin = view self._object_to_pin = view
else: else:
ptr = buf ptr = buf

View File

@@ -6,6 +6,7 @@ import test_regression
import tempfile import tempfile
import pickle import pickle
import mmap import mmap
import sys
this_dir = os.path.dirname(__file__) this_dir = os.path.dirname(__file__)
@@ -41,6 +42,7 @@ def test_roundtrip_bytes(all_types):
msg = all_types.TestAllTypes.from_bytes(message_bytes) msg = all_types.TestAllTypes.from_bytes(message_bytes)
test_regression.check_all_types(msg) 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): def test_roundtrip_bytes_mmap(all_types):
msg = all_types.TestAllTypes.new_message() msg = all_types.TestAllTypes.new_message()
test_regression.init_all_types(msg) test_regression.init_all_types(msg)