| 1645 | |
| 1646 | |
| 1647 | static 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); |
no test coverage detected