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

Function array_strides_set

numpy/core/src/multiarray/getset.c:111–180  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

109}
110
111static int
112array_strides_set(PyArrayObject *self, PyObject *obj, void *NPY_UNUSED(ignored))
113{
114 PyArray_Dims newstrides = {NULL, -1};
115 PyArrayObject *new;
116 npy_intp numbytes = 0;
117 npy_intp offset = 0;
118 npy_intp lower_offset = 0;
119 npy_intp upper_offset = 0;
120 Py_buffer view;
121
122 if (obj == NULL) {
123 PyErr_SetString(PyExc_AttributeError,
124 "Cannot delete array strides");
125 return -1;
126 }
127 if (!PyArray_OptionalIntpConverter(obj, &newstrides) ||
128 newstrides.len == -1) {
129 PyErr_SetString(PyExc_TypeError, "invalid strides");
130 return -1;
131 }
132 if (newstrides.len != PyArray_NDIM(self)) {
133 PyErr_Format(PyExc_ValueError, "strides must be " \
134 " same length as shape (%d)", PyArray_NDIM(self));
135 goto fail;
136 }
137 new = self;
138 while(PyArray_BASE(new) && PyArray_Check(PyArray_BASE(new))) {
139 new = (PyArrayObject *)(PyArray_BASE(new));
140 }
141 /*
142 * Get the available memory through the buffer interface on
143 * PyArray_BASE(new) or if that fails from the current new
144 */
145 if (PyArray_BASE(new) &&
146 PyObject_GetBuffer(PyArray_BASE(new), &view, PyBUF_SIMPLE) >= 0) {
147 offset = PyArray_BYTES(self) - (char *)view.buf;
148 numbytes = view.len + offset;
149 PyBuffer_Release(&view);
150 }
151 else {
152 PyErr_Clear();
153 offset_bounds_from_strides(PyArray_ITEMSIZE(new), PyArray_NDIM(new),
154 PyArray_DIMS(new), PyArray_STRIDES(new),
155 &lower_offset, &upper_offset);
156
157 offset = PyArray_BYTES(self) - (PyArray_BYTES(new) + lower_offset);
158 numbytes = upper_offset - lower_offset;
159 }
160
161 /* numbytes == 0 is special here, but the 0-size array case always works */
162 if (!PyArray_CheckStrides(PyArray_ITEMSIZE(self), PyArray_NDIM(self),
163 numbytes, offset,
164 PyArray_DIMS(self), newstrides.ptr)) {
165 PyErr_SetString(PyExc_ValueError, "strides is not "\
166 "compatible with available memory");
167 goto fail;
168 }

Callers

nothing calls this directly

Calls 11

PyArray_NDIMFunction · 0.85
PyArray_BASEFunction · 0.85
PyArray_BYTESFunction · 0.85
PyArray_ITEMSIZEFunction · 0.85
PyArray_DIMSFunction · 0.85
PyArray_STRIDESFunction · 0.85
PyArray_CheckStridesFunction · 0.85
PyArray_UpdateFlagsFunction · 0.85
npy_free_cache_dim_objFunction · 0.85

Tested by

no test coverage detected