* If the type is changed. * Also needing change: strides, itemsize * * Either itemsize is exactly the same or the array is single-segment * (contiguous or fortran) with compatible dimensions The shape and strides * will be adjusted in that case as well. */
| 448 | * will be adjusted in that case as well. |
| 449 | */ |
| 450 | static int |
| 451 | array_descr_set(PyArrayObject *self, PyObject *arg, void *NPY_UNUSED(ignored)) |
| 452 | { |
| 453 | PyArray_Descr *newtype = NULL; |
| 454 | |
| 455 | if (arg == NULL) { |
| 456 | PyErr_SetString(PyExc_AttributeError, |
| 457 | "Cannot delete array dtype"); |
| 458 | return -1; |
| 459 | } |
| 460 | |
| 461 | if (!(PyArray_DescrConverter(arg, &newtype)) || |
| 462 | newtype == NULL) { |
| 463 | PyErr_SetString(PyExc_TypeError, |
| 464 | "invalid data-type for array"); |
| 465 | return -1; |
| 466 | } |
| 467 | |
| 468 | /* check that we are not reinterpreting memory containing Objects. */ |
| 469 | if (_may_have_objects(PyArray_DESCR(self)) || _may_have_objects(newtype)) { |
| 470 | static PyObject *checkfunc = NULL; |
| 471 | PyObject *safe; |
| 472 | |
| 473 | npy_cache_import("numpy.core._internal", "_view_is_safe", &checkfunc); |
| 474 | if (checkfunc == NULL) { |
| 475 | goto fail; |
| 476 | } |
| 477 | |
| 478 | safe = PyObject_CallFunction(checkfunc, "OO", |
| 479 | PyArray_DESCR(self), newtype); |
| 480 | if (safe == NULL) { |
| 481 | goto fail; |
| 482 | } |
| 483 | Py_DECREF(safe); |
| 484 | } |
| 485 | |
| 486 | /* |
| 487 | * Viewing as an unsized void implies a void dtype matching the size of the |
| 488 | * current dtype. |
| 489 | */ |
| 490 | if (newtype->type_num == NPY_VOID && |
| 491 | PyDataType_ISUNSIZED(newtype) && |
| 492 | newtype->elsize != PyArray_DESCR(self)->elsize) { |
| 493 | PyArray_DESCR_REPLACE(newtype); |
| 494 | if (newtype == NULL) { |
| 495 | return -1; |
| 496 | } |
| 497 | newtype->elsize = PyArray_DESCR(self)->elsize; |
| 498 | } |
| 499 | |
| 500 | /* Changing the size of the dtype results in a shape change */ |
| 501 | if (newtype->elsize != PyArray_DESCR(self)->elsize) { |
| 502 | /* forbidden cases */ |
| 503 | if (PyArray_NDIM(self) == 0) { |
| 504 | PyErr_SetString(PyExc_ValueError, |
| 505 | "Changing the dtype of a 0d array is only supported " |
| 506 | "if the itemsize is unchanged"); |
| 507 | goto fail; |
nothing calls this directly
no test coverage detected