* Generic new array creation routine. * Internal variant with calloc argument for PyArray_Zeros. * * steals a reference to descr. On failure or descr->subarray, descr will * be decrefed. */
| 625 | * be decrefed. |
| 626 | */ |
| 627 | NPY_NO_EXPORT PyObject * |
| 628 | PyArray_NewFromDescr_int( |
| 629 | PyTypeObject *subtype, PyArray_Descr *descr, int nd, |
| 630 | npy_intp const *dims, npy_intp const *strides, void *data, |
| 631 | int flags, PyObject *obj, PyObject *base, _NPY_CREATION_FLAGS cflags) |
| 632 | { |
| 633 | PyArrayObject_fields *fa; |
| 634 | npy_intp nbytes; |
| 635 | |
| 636 | if (descr == NULL) { |
| 637 | return NULL; |
| 638 | } |
| 639 | if (nd > NPY_MAXDIMS || nd < 0) { |
| 640 | PyErr_Format(PyExc_ValueError, |
| 641 | "number of dimensions must be within [0, %d]", NPY_MAXDIMS); |
| 642 | Py_DECREF(descr); |
| 643 | return NULL; |
| 644 | } |
| 645 | |
| 646 | nbytes = descr->elsize; |
| 647 | /* |
| 648 | * Unless explicitly asked not to, we do replace dtypes in some cases. |
| 649 | * This mainly means that we never create arrays with a subarray dtype |
| 650 | * (unless for internal use when requested). And neither do we create |
| 651 | * S0/U0 arrays in most cases (unless data == NULL so this is probably |
| 652 | * a view where growing the dtype would be presumable wrong). |
| 653 | */ |
| 654 | if (!(cflags & _NPY_ARRAY_ENSURE_DTYPE_IDENTITY)) { |
| 655 | if (descr->subarray) { |
| 656 | PyObject *ret; |
| 657 | npy_intp newdims[2*NPY_MAXDIMS]; |
| 658 | npy_intp *newstrides = NULL; |
| 659 | memcpy(newdims, dims, nd*sizeof(npy_intp)); |
| 660 | if (strides) { |
| 661 | newstrides = newdims + NPY_MAXDIMS; |
| 662 | memcpy(newstrides, strides, nd*sizeof(npy_intp)); |
| 663 | } |
| 664 | nd =_update_descr_and_dimensions(&descr, newdims, |
| 665 | newstrides, nd); |
| 666 | ret = PyArray_NewFromDescr_int( |
| 667 | subtype, descr, |
| 668 | nd, newdims, newstrides, data, |
| 669 | flags, obj, base, cflags); |
| 670 | return ret; |
| 671 | } |
| 672 | |
| 673 | /* Check datatype element size */ |
| 674 | if (PyDataType_ISUNSIZED(descr)) { |
| 675 | if (!PyDataType_ISFLEXIBLE(descr) && |
| 676 | NPY_DT_is_legacy(NPY_DTYPE(descr))) { |
| 677 | PyErr_SetString(PyExc_TypeError, "Empty data-type"); |
| 678 | Py_DECREF(descr); |
| 679 | return NULL; |
| 680 | } |
| 681 | else if (PyDataType_ISSTRING(descr) |
| 682 | && !(cflags & _NPY_ARRAY_ALLOW_EMPTY_STRING) |
| 683 | && data == NULL) { |
| 684 | PyArray_DESCR_REPLACE(descr); |
no test coverage detected