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

Function array_shape_set

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

Source from the content-addressed store, hash-verified

45
46
47static int
48array_shape_set(PyArrayObject *self, PyObject *val, void* NPY_UNUSED(ignored))
49{
50 int nd;
51 PyArrayObject *ret;
52
53 if (val == NULL) {
54 PyErr_SetString(PyExc_AttributeError,
55 "Cannot delete array shape");
56 return -1;
57 }
58 /* Assumes C-order */
59 ret = (PyArrayObject *)PyArray_Reshape(self, val);
60 if (ret == NULL) {
61 return -1;
62 }
63 if (PyArray_DATA(ret) != PyArray_DATA(self)) {
64 Py_DECREF(ret);
65 PyErr_SetString(PyExc_AttributeError,
66 "Incompatible shape for in-place modification. Use "
67 "`.reshape()` to make a copy with the desired shape.");
68 return -1;
69 }
70
71 nd = PyArray_NDIM(ret);
72 if (nd > 0) {
73 /* create new dimensions and strides */
74 npy_intp *_dimensions = npy_alloc_cache_dim(2 * nd);
75 if (_dimensions == NULL) {
76 Py_DECREF(ret);
77 PyErr_NoMemory();
78 return -1;
79 }
80 /* Free old dimensions and strides */
81 npy_free_cache_dim_array(self);
82 ((PyArrayObject_fields *)self)->nd = nd;
83 ((PyArrayObject_fields *)self)->dimensions = _dimensions;
84 ((PyArrayObject_fields *)self)->strides = _dimensions + nd;
85
86 if (nd) {
87 memcpy(PyArray_DIMS(self), PyArray_DIMS(ret), nd*sizeof(npy_intp));
88 memcpy(PyArray_STRIDES(self), PyArray_STRIDES(ret), nd*sizeof(npy_intp));
89 }
90 }
91 else {
92 /* Free old dimensions and strides */
93 npy_free_cache_dim_array(self);
94 ((PyArrayObject_fields *)self)->nd = 0;
95 ((PyArrayObject_fields *)self)->dimensions = NULL;
96 ((PyArrayObject_fields *)self)->strides = NULL;
97 }
98
99 Py_DECREF(ret);
100 PyArray_UpdateFlags(self, NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_F_CONTIGUOUS);
101 return 0;
102}
103
104

Callers

nothing calls this directly

Calls 8

PyArray_ReshapeFunction · 0.85
PyArray_DATAFunction · 0.85
PyArray_NDIMFunction · 0.85
npy_alloc_cache_dimFunction · 0.85
npy_free_cache_dim_arrayFunction · 0.85
PyArray_DIMSFunction · 0.85
PyArray_STRIDESFunction · 0.85
PyArray_UpdateFlagsFunction · 0.85

Tested by

no test coverage detected