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

Function _array_fromobject_generic

numpy/core/src/multiarray/multiarraymodule.c:1647–1788  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1645
1646
1647static inline PyObject *
1648_array_fromobject_generic(
1649 PyObject *op, PyArray_Descr *in_descr, PyArray_DTypeMeta *in_DType,
1650 _PyArray_CopyMode copy, NPY_ORDER order, npy_bool subok, int ndmin)
1651{
1652 PyArrayObject *oparr = NULL, *ret = NULL;
1653 PyArray_Descr *oldtype = NULL;
1654 int nd, flags = 0;
1655
1656 /* Hold on to `in_descr` as `dtype`, since we may also set it below. */
1657 Py_XINCREF(in_descr);
1658 PyArray_Descr *dtype = in_descr;
1659
1660 if (ndmin > NPY_MAXDIMS) {
1661 PyErr_Format(PyExc_ValueError,
1662 "ndmin bigger than allowable number of dimensions "
1663 "NPY_MAXDIMS (=%d)", NPY_MAXDIMS);
1664 goto finish;
1665 }
1666 /* fast exit if simple call */
1667 if (PyArray_CheckExact(op) || (subok && PyArray_Check(op))) {
1668 oparr = (PyArrayObject *)op;
1669
1670 if (dtype == NULL && in_DType == NULL) {
1671 /*
1672 * User did not ask for a specific dtype instance or class. So
1673 * we can return either self or a copy.
1674 */
1675 if (copy != NPY_COPY_ALWAYS && STRIDING_OK(oparr, order)) {
1676 ret = oparr;
1677 Py_INCREF(ret);
1678 goto finish;
1679 }
1680 else {
1681 if (copy == NPY_COPY_NEVER) {
1682 PyErr_SetString(PyExc_ValueError,
1683 "Unable to avoid copy while creating a new array.");
1684 goto finish;
1685 }
1686 ret = (PyArrayObject *)PyArray_NewCopy(oparr, order);
1687 goto finish;
1688 }
1689 }
1690 else if (dtype == NULL) {
1691 /*
1692 * If the user passed a DType class but not a dtype instance,
1693 * we must use `PyArray_AdaptDescriptorToArray` to find the
1694 * correct dtype instance.
1695 * Even if the fast-path doesn't work we will use this.
1696 */
1697 dtype = PyArray_AdaptDescriptorToArray(oparr, in_DType, NULL);
1698 if (dtype == NULL) {
1699 goto finish;
1700 }
1701 }
1702
1703 /* One more chance for faster exit if user specified the dtype. */
1704 oldtype = PyArray_DESCR(oparr);

Callers 5

array_arrayFunction · 0.85
array_asarrayFunction · 0.85
array_asanyarrayFunction · 0.85
array_ascontiguousarrayFunction · 0.85
array_asfortranarrayFunction · 0.85

Calls 11

PyArray_NewCopyFunction · 0.85
PyArray_DESCRFunction · 0.85
PyArray_EquivTypesFunction · 0.85
PyArray_NDIMFunction · 0.85
PyArray_DIMSFunction · 0.85
PyArray_STRIDESFunction · 0.85
PyArray_DATAFunction · 0.85
PyArray_FLAGSFunction · 0.85
PyArray_CheckFromAny_intFunction · 0.85

Tested by

no test coverage detected