Commit b7949976 authored by Stefan Behnel's avatar Stefan Behnel

Remove the outdated getbuffer/releasebuffer implementations for the NumPy...

Remove the outdated getbuffer/releasebuffer implementations for the NumPy 'ndarray' since there are probably no NumPy installations out there anymore that do not support the buffer protocol themselves and are still worth supporting.

See https://github.com/numpy/numpy/pull/12284#discussion_r420378155
See GH-3573
parent 4d6939ff
......@@ -281,97 +281,6 @@ cdef extern from "numpy/arrayobject.h":
"""
return PyArray_BYTES(self)
# Note: This syntax (function definition in pxd files) is an
# experimental exception made for __getbuffer__ and __releasebuffer__
# -- the details of this may change.
def __getbuffer__(ndarray self, Py_buffer* info, int flags):
# This implementation of getbuffer is geared towards Cython
# requirements, and does not yet fulfill the PEP.
# In particular strided access is always provided regardless
# of flags
cdef int i, ndim
cdef int endian_detector = 1
cdef bint little_endian = ((<char*>&endian_detector)[0] != 0)
ndim = PyArray_NDIM(self)
if ((flags & pybuf.PyBUF_C_CONTIGUOUS == pybuf.PyBUF_C_CONTIGUOUS)
and not PyArray_CHKFLAGS(self, NPY_ARRAY_C_CONTIGUOUS)):
raise ValueError(u"ndarray is not C contiguous")
if ((flags & pybuf.PyBUF_F_CONTIGUOUS == pybuf.PyBUF_F_CONTIGUOUS)
and not PyArray_CHKFLAGS(self, NPY_ARRAY_F_CONTIGUOUS)):
raise ValueError(u"ndarray is not Fortran contiguous")
info.buf = PyArray_DATA(self)
info.ndim = ndim
if sizeof(npy_intp) != sizeof(Py_ssize_t):
# Allocate new buffer for strides and shape info.
# This is allocated as one block, strides first.
info.strides = <Py_ssize_t*>PyObject_Malloc(sizeof(Py_ssize_t) * 2 * <size_t>ndim)
info.shape = info.strides + ndim
for i in range(ndim):
info.strides[i] = PyArray_STRIDES(self)[i]
info.shape[i] = PyArray_DIMS(self)[i]
else:
info.strides = <Py_ssize_t*>PyArray_STRIDES(self)
info.shape = <Py_ssize_t*>PyArray_DIMS(self)
info.suboffsets = NULL
info.itemsize = PyArray_ITEMSIZE(self)
info.readonly = not PyArray_ISWRITEABLE(self)
cdef int t
cdef char* f = NULL
cdef dtype descr = <dtype>PyArray_DESCR(self)
cdef int offset
info.obj = self
if not PyDataType_HASFIELDS(descr):
t = descr.type_num
if ((descr.byteorder == c'>' and little_endian) or
(descr.byteorder == c'<' and not little_endian)):
raise ValueError(u"Non-native byte order not supported")
if t == NPY_BYTE: f = "b"
elif t == NPY_UBYTE: f = "B"
elif t == NPY_SHORT: f = "h"
elif t == NPY_USHORT: f = "H"
elif t == NPY_INT: f = "i"
elif t == NPY_UINT: f = "I"
elif t == NPY_LONG: f = "l"
elif t == NPY_ULONG: f = "L"
elif t == NPY_LONGLONG: f = "q"
elif t == NPY_ULONGLONG: f = "Q"
elif t == NPY_FLOAT: f = "f"
elif t == NPY_DOUBLE: f = "d"
elif t == NPY_LONGDOUBLE: f = "g"
elif t == NPY_CFLOAT: f = "Zf"
elif t == NPY_CDOUBLE: f = "Zd"
elif t == NPY_CLONGDOUBLE: f = "Zg"
elif t == NPY_OBJECT: f = "O"
else:
raise ValueError(u"unknown dtype code in numpy.pxd (%d)" % t)
info.format = f
return
else:
info.format = <char*>PyObject_Malloc(_buffer_format_string_len)
info.format[0] = c'^' # Native data types, manual alignment
offset = 0
f = _util_dtypestring(descr, info.format + 1,
info.format + _buffer_format_string_len,
&offset)
f[0] = c'\0' # Terminate format string
def __releasebuffer__(ndarray self, Py_buffer* info):
if PyArray_HASFIELDS(self):
PyObject_Free(info.format)
if sizeof(npy_intp) != sizeof(Py_ssize_t):
PyObject_Free(info.strides)
# info.shape was stored after info.strides in the same block
ctypedef unsigned char npy_bool
ctypedef signed char npy_byte
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment