Support generic buffers in from_bytes()
(Also throw a TypeError if it's not a bytes object or a buffer)
This commit is contained in:
@@ -15,7 +15,7 @@ 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 cimport Py_buffer, PyObject_CheckBuffer
|
||||||
from cpython.buffer cimport PyBUF_SIMPLE
|
from cpython.buffer cimport PyBUF_SIMPLE
|
||||||
|
|
||||||
from types import ModuleType as _ModuleType
|
from types import ModuleType as _ModuleType
|
||||||
@@ -3756,11 +3756,7 @@ cdef class _FlatArrayMessageReader(_MessageReader):
|
|||||||
raise ValueError("input length must be a multiple of eight bytes")
|
raise ValueError("input length must be a multiple of eight bytes")
|
||||||
|
|
||||||
cdef char * ptr
|
cdef char * ptr
|
||||||
if type(buf) == _mmap.mmap:
|
if isinstance(buf, bytes):
|
||||||
view = _BufferView(buf)
|
|
||||||
ptr = view.buf
|
|
||||||
self._object_to_pin = view
|
|
||||||
else:
|
|
||||||
ptr = buf
|
ptr = buf
|
||||||
if (<uintptr_t>ptr) % 8 != 0:
|
if (<uintptr_t>ptr) % 8 != 0:
|
||||||
aligned = _AlignedBuffer(buf)
|
aligned = _AlignedBuffer(buf)
|
||||||
@@ -3768,6 +3764,12 @@ cdef class _FlatArrayMessageReader(_MessageReader):
|
|||||||
self._object_to_pin = aligned
|
self._object_to_pin = aligned
|
||||||
else:
|
else:
|
||||||
self._object_to_pin = buf
|
self._object_to_pin = buf
|
||||||
|
elif PyObject_CheckBuffer(buf):
|
||||||
|
view = _BufferView(buf)
|
||||||
|
ptr = view.buf
|
||||||
|
self._object_to_pin = view
|
||||||
|
else:
|
||||||
|
raise TypeError('expected buffer-like object in FlatArrayMessageReader')
|
||||||
|
|
||||||
self.thisptr = new schema_cpp.FlatArrayMessageReader(
|
self.thisptr = new schema_cpp.FlatArrayMessageReader(
|
||||||
schema_cpp.WordArrayPtr(<schema_cpp.word*>ptr, sz//8),
|
schema_cpp.WordArrayPtr(<schema_cpp.word*>ptr, sz//8),
|
||||||
|
|||||||
Reference in New Issue
Block a user