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

Function PyArray_FromAny_int

numpy/core/src/multiarray/ctors.c:1531–1713  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1529 */
1530
1531NPY_NO_EXPORT PyObject *
1532PyArray_FromAny_int(PyObject *op, PyArray_Descr *in_descr,
1533 PyArray_DTypeMeta *in_DType, int min_depth, int max_depth,
1534 int flags, PyObject *context)
1535{
1536 /*
1537 * This is the main code to make a NumPy array from a Python
1538 * Object. It is called from many different places.
1539 */
1540 PyArrayObject *arr = NULL, *ret;
1541 PyArray_Descr *dtype = NULL;
1542 coercion_cache_obj *cache = NULL;
1543 int ndim = 0;
1544 npy_intp dims[NPY_MAXDIMS];
1545
1546 if (context != NULL) {
1547 PyErr_SetString(PyExc_RuntimeError, "'context' must be NULL");
1548 return NULL;
1549 }
1550
1551 ndim = PyArray_DiscoverDTypeAndShape(op,
1552 NPY_MAXDIMS, dims, &cache, in_DType, in_descr, &dtype,
1553 flags & NPY_ARRAY_ENSURENOCOPY);
1554
1555 if (ndim < 0) {
1556 return NULL;
1557 }
1558
1559 if (dtype == NULL) {
1560 dtype = PyArray_DescrFromType(NPY_DEFAULT_TYPE);
1561 }
1562
1563 if (min_depth != 0 && ndim < min_depth) {
1564 PyErr_SetString(PyExc_ValueError,
1565 "object of too small depth for desired array");
1566 Py_DECREF(dtype);
1567 npy_free_coercion_cache(cache);
1568 return NULL;
1569 }
1570 if (max_depth != 0 && ndim > max_depth) {
1571 PyErr_SetString(PyExc_ValueError,
1572 "object too deep for desired array");
1573 Py_DECREF(dtype);
1574 npy_free_coercion_cache(cache);
1575 return NULL;
1576 }
1577
1578 /* Got the correct parameters, but the cache may already hold the result */
1579 if (cache != NULL && !(cache->sequence)) {
1580 /*
1581 * There is only a single array-like and it was converted, it
1582 * may still have the incorrect type, but that is handled below.
1583 */
1584 assert(cache->converted_obj == op);
1585 arr = (PyArrayObject *)(cache->arr_or_sequence);
1586 /* we may need to cast or assert flags (e.g. copy) */
1587 PyObject *res = PyArray_FromArray(arr, dtype, flags);
1588 npy_unlink_coercion_cache(cache);

Callers 2

PyArray_FromAnyFunction · 0.85
PyArray_CheckFromAny_intFunction · 0.85

Calls 12

npy_free_coercion_cacheFunction · 0.85
PyArray_FromArrayFunction · 0.85
PyArray_FromScalarFunction · 0.85
PyArray_NewFromDescrFunction · 0.85
PyArray_NDIMFunction · 0.85
PyArray_DESCRFunction · 0.85
PyArray_PackFunction · 0.85
PyArray_BYTESFunction · 0.85
PyArray_AssignFromCacheFunction · 0.85

Tested by

no test coverage detected