MCPcopy Create free account
hub / github.com/numpy/numpy / array_getbuffer

Function array_getbuffer

numpy/core/src/multiarray/buffer.c:751–841  ·  view source on GitHub ↗

* Retrieving buffers for ndarray */

Source from the content-addressed store, hash-verified

749 * Retrieving buffers for ndarray
750 */
751static int
752array_getbuffer(PyObject *obj, Py_buffer *view, int flags)
753{
754 PyArrayObject *self;
755 _buffer_info_t *info = NULL;
756
757 self = (PyArrayObject*)obj;
758
759 /* Check whether we can provide the wanted properties */
760 if ((flags & PyBUF_C_CONTIGUOUS) == PyBUF_C_CONTIGUOUS &&
761 !PyArray_CHKFLAGS(self, NPY_ARRAY_C_CONTIGUOUS)) {
762 PyErr_SetString(PyExc_ValueError, "ndarray is not C-contiguous");
763 goto fail;
764 }
765 if ((flags & PyBUF_F_CONTIGUOUS) == PyBUF_F_CONTIGUOUS &&
766 !PyArray_CHKFLAGS(self, NPY_ARRAY_F_CONTIGUOUS)) {
767 PyErr_SetString(PyExc_ValueError, "ndarray is not Fortran contiguous");
768 goto fail;
769 }
770 if ((flags & PyBUF_ANY_CONTIGUOUS) == PyBUF_ANY_CONTIGUOUS
771 && !PyArray_ISONESEGMENT(self)) {
772 PyErr_SetString(PyExc_ValueError, "ndarray is not contiguous");
773 goto fail;
774 }
775 if ((flags & PyBUF_STRIDES) != PyBUF_STRIDES &&
776 !PyArray_CHKFLAGS(self, NPY_ARRAY_C_CONTIGUOUS)) {
777 /* Non-strided N-dim buffers must be C-contiguous */
778 PyErr_SetString(PyExc_ValueError, "ndarray is not C-contiguous");
779 goto fail;
780 }
781 if ((flags & PyBUF_WRITEABLE) == PyBUF_WRITEABLE) {
782 if (PyArray_FailUnlessWriteable(self, "buffer source array") < 0) {
783 goto fail;
784 }
785 }
786
787 if (view == NULL) {
788 PyErr_SetString(PyExc_ValueError, "NULL view in getbuffer");
789 goto fail;
790 }
791
792 /* Fill in information (and add it to _buffer_info if necessary) */
793 info = _buffer_get_info(
794 &((PyArrayObject_fields *)self)->_buffer_info, obj, flags);
795 if (info == NULL) {
796 goto fail;
797 }
798
799 view->buf = PyArray_DATA(self);
800 view->suboffsets = NULL;
801 view->itemsize = PyArray_ITEMSIZE(self);
802 /*
803 * If a read-only buffer is requested on a read-write array, we return a
804 * read-write buffer as per buffer protocol.
805 * We set a requested buffer to readonly also if the array will be readonly
806 * after a deprecation. This jumps the deprecation, but avoiding the
807 * warning is not convenient here. A warning is given if a writeable
808 * buffer is requested since `PyArray_FailUnlessWriteable` is called above

Callers

nothing calls this directly

Calls 5

PyArray_CHKFLAGSFunction · 0.85
_buffer_get_infoFunction · 0.85
PyArray_DATAFunction · 0.85
PyArray_ITEMSIZEFunction · 0.85

Tested by

no test coverage detected