NUMPY_API * steals reference to newtype --- acc. NULL */
| 1838 | * steals reference to newtype --- acc. NULL |
| 1839 | */ |
| 1840 | NPY_NO_EXPORT PyObject * |
| 1841 | PyArray_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 */ |
no test coverage detected