NUMPY_API * Resize (reallocate data). Only works if nothing else is referencing this * array and it is contiguous. If refcheck is 0, then the reference count is * not checked and assumed to be 1. You still must own this data and have no * weak-references and no base object. */
| 40 | * weak-references and no base object. |
| 41 | */ |
| 42 | NPY_NO_EXPORT PyObject * |
| 43 | PyArray_Resize(PyArrayObject *self, PyArray_Dims *newshape, int refcheck, |
| 44 | NPY_ORDER NPY_UNUSED(order)) |
| 45 | { |
| 46 | npy_intp oldnbytes, newnbytes; |
| 47 | npy_intp oldsize, newsize; |
| 48 | int new_nd=newshape->len, k, elsize; |
| 49 | int refcnt; |
| 50 | npy_intp* new_dimensions=newshape->ptr; |
| 51 | npy_intp new_strides[NPY_MAXDIMS]; |
| 52 | npy_intp *dimptr; |
| 53 | char *new_data; |
| 54 | |
| 55 | if (!PyArray_ISONESEGMENT(self)) { |
| 56 | PyErr_SetString(PyExc_ValueError, |
| 57 | "resize only works on single-segment arrays"); |
| 58 | return NULL; |
| 59 | } |
| 60 | |
| 61 | /* Compute total size of old and new arrays. The new size might overflow */ |
| 62 | oldsize = PyArray_SIZE(self); |
| 63 | newsize = 1; |
| 64 | for(k = 0; k < new_nd; k++) { |
| 65 | if (new_dimensions[k] == 0) { |
| 66 | newsize = 0; |
| 67 | break; |
| 68 | } |
| 69 | if (new_dimensions[k] < 0) { |
| 70 | PyErr_SetString(PyExc_ValueError, |
| 71 | "negative dimensions not allowed"); |
| 72 | return NULL; |
| 73 | } |
| 74 | if (npy_mul_sizes_with_overflow(&newsize, newsize, new_dimensions[k])) { |
| 75 | return PyErr_NoMemory(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /* Convert to number of bytes. The new count might overflow */ |
| 80 | elsize = PyArray_DESCR(self)->elsize; |
| 81 | oldnbytes = oldsize * elsize; |
| 82 | if (npy_mul_sizes_with_overflow(&newnbytes, newsize, elsize)) { |
| 83 | return PyErr_NoMemory(); |
| 84 | } |
| 85 | |
| 86 | if (oldnbytes != newnbytes) { |
| 87 | if (!(PyArray_FLAGS(self) & NPY_ARRAY_OWNDATA)) { |
| 88 | PyErr_SetString(PyExc_ValueError, |
| 89 | "cannot resize this array: it does not own its data"); |
| 90 | return NULL; |
| 91 | } |
| 92 | |
| 93 | if (PyArray_BASE(self) != NULL |
| 94 | || (((PyArrayObject_fields *)self)->weakreflist != NULL)) { |
| 95 | PyErr_SetString(PyExc_ValueError, |
| 96 | "cannot resize an array that " |
| 97 | "references or is referenced\n" |
| 98 | "by another array in this way. Use the np.resize function."); |
| 99 | return NULL; |
no test coverage detected