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

Function PyArray_FromArray

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

NUMPY_API * steals reference to newtype --- acc. NULL */

Source from the content-addressed store, hash-verified

1838 * steals reference to newtype --- acc. NULL
1839 */
1840NPY_NO_EXPORT PyObject *
1841PyArray_FromArray(PyArrayObject *arr, PyArray_Descr *newtype, int flags)
1842{
1843
1844 PyArrayObject *ret = NULL;
1845 int copy = 0;
1846 int arrflags;
1847 PyArray_Descr *oldtype;
1848 NPY_CASTING casting = NPY_SAFE_CASTING;
1849
1850 oldtype = PyArray_DESCR(arr);
1851 if (newtype == NULL) {
1852 /*
1853 * Check if object is of array with Null newtype.
1854 * If so return it directly instead of checking for casting.
1855 */
1856 if (flags == 0) {
1857 Py_INCREF(arr);
1858 return (PyObject *)arr;
1859 }
1860 newtype = oldtype;
1861 Py_INCREF(oldtype);
1862 }
1863 else if (PyDataType_ISUNSIZED(newtype)) {
1864 PyArray_DESCR_REPLACE(newtype);
1865 if (newtype == NULL) {
1866 return NULL;
1867 }
1868 newtype->elsize = oldtype->elsize;
1869 }
1870
1871 /* If the casting if forced, use the 'unsafe' casting rule */
1872 if (flags & NPY_ARRAY_FORCECAST) {
1873 casting = NPY_UNSAFE_CASTING;
1874 }
1875
1876 /* Raise an error if the casting rule isn't followed */
1877 if (!PyArray_CanCastArrayTo(arr, newtype, casting)) {
1878 PyErr_Clear();
1879 npy_set_invalid_cast_error(
1880 PyArray_DESCR(arr), newtype, casting, PyArray_NDIM(arr) == 0);
1881 Py_DECREF(newtype);
1882 return NULL;
1883 }
1884
1885 arrflags = PyArray_FLAGS(arr);
1886 /* If a guaranteed copy was requested */
1887 copy = (flags & NPY_ARRAY_ENSURECOPY) ||
1888 /* If C contiguous was requested, and arr is not */
1889 ((flags & NPY_ARRAY_C_CONTIGUOUS) &&
1890 (!(arrflags & NPY_ARRAY_C_CONTIGUOUS))) ||
1891 /* If an aligned array was requested, and arr is not */
1892 ((flags & NPY_ARRAY_ALIGNED) &&
1893 (!(arrflags & NPY_ARRAY_ALIGNED))) ||
1894 /* If a Fortran contiguous array was requested, and arr is not */
1895 ((flags & NPY_ARRAY_F_CONTIGUOUS) &&
1896 (!(arrflags & NPY_ARRAY_F_CONTIGUOUS))) ||
1897 /* If a writeable array was requested, and arr is not */

Callers 11

PyArray_FromAny_intFunction · 0.85
PyArray_TakeFromFunction · 0.85
PyArray_PutToFunction · 0.85
PyArray_PutMaskFunction · 0.85
PyArray_ChooseFunction · 0.85
PyArray_SearchSortedFunction · 0.85
arr_placeFunction · 0.85
prepare_indexFunction · 0.85
_PyArray_ArgMinMaxCommonFunction · 0.85
PyArray_ClipFunction · 0.85

Calls 10

PyArray_DESCRFunction · 0.85
PyArray_CanCastArrayToFunction · 0.85
PyArray_NDIMFunction · 0.85
PyArray_FLAGSFunction · 0.85
PyArray_EquivTypesFunction · 0.85
PyArray_NewLikeArrayFunction · 0.85
PyArray_CopyIntoFunction · 0.85
PyArray_ViewFunction · 0.85

Tested by

no test coverage detected