@@ -1856,16 +1856,7 @@ PyArray_FromAny(PyObject *op, PyArray_Descr *newtype, int min_depth,
1856
1856
ret = NULL ;
1857
1857
}
1858
1858
else {
1859
- if (PyArray_HASMASKNA ((PyArrayObject * )arr ) &&
1860
- (flags & NPY_ARRAY_ALLOWNA ) == 0 ) {
1861
- PyErr_SetString (PyExc_ValueError ,
1862
- "this operation does not support "
1863
- "arrays with NA masks" );
1864
- ret = NULL ;
1865
- }
1866
- else {
1867
- ret = (PyArrayObject * )PyArray_FromArray (arr , newtype , flags );
1868
- }
1859
+ ret = (PyArrayObject * )PyArray_FromArray (arr , newtype , flags );
1869
1860
Py_DECREF (arr );
1870
1861
}
1871
1862
}
@@ -1962,13 +1953,12 @@ PyArray_FromArray(PyArrayObject *arr, PyArray_Descr *newtype, int flags)
1962
1953
int copy = 0 ;
1963
1954
int arrflags ;
1964
1955
PyArray_Descr * oldtype ;
1965
- PyTypeObject * subtype ;
1966
1956
NPY_CASTING casting = NPY_SAFE_CASTING ;
1967
1957
1968
1958
oldtype = PyArray_DESCR (arr );
1969
- subtype = Py_TYPE (arr );
1970
1959
if (newtype == NULL ) {
1971
- newtype = oldtype ; Py_INCREF (oldtype );
1960
+ newtype = oldtype ;
1961
+ Py_INCREF (oldtype );
1972
1962
}
1973
1963
itemsize = newtype -> elsize ;
1974
1964
if (itemsize == 0 ) {
@@ -2005,141 +1995,79 @@ PyArray_FromArray(PyArrayObject *arr, PyArray_Descr *newtype, int flags)
2005
1995
return NULL ;
2006
1996
}
2007
1997
2008
- /* Don't copy if sizes are compatible */
2009
- if ((flags & NPY_ARRAY_ENSURECOPY ) ||
2010
- PyArray_EquivTypes (oldtype , newtype )) {
2011
- arrflags = PyArray_FLAGS (arr );
2012
- if (PyArray_NDIM (arr ) <= 1 && (flags & NPY_ARRAY_F_CONTIGUOUS )) {
2013
- flags |= NPY_ARRAY_C_CONTIGUOUS ;
2014
- }
2015
- copy = (flags & NPY_ARRAY_ENSURECOPY ) ||
2016
- ((flags & NPY_ARRAY_C_CONTIGUOUS ) &&
2017
- (!(arrflags & NPY_ARRAY_C_CONTIGUOUS )))
2018
- || ((flags & NPY_ARRAY_ALIGNED ) &&
2019
- (!(arrflags & NPY_ARRAY_ALIGNED )))
2020
- || (PyArray_NDIM (arr ) > 1 &&
2021
- ((flags & NPY_ARRAY_F_CONTIGUOUS ) &&
2022
- (!(arrflags & NPY_ARRAY_F_CONTIGUOUS ))))
2023
- || ((flags & NPY_ARRAY_WRITEABLE ) &&
2024
- (!(arrflags & NPY_ARRAY_WRITEABLE )));
2025
-
2026
- if (copy ) {
2027
- if ((flags & NPY_ARRAY_UPDATEIFCOPY ) &&
2028
- (!PyArray_ISWRITEABLE (arr ))) {
2029
- Py_DECREF (newtype );
2030
- PyErr_SetString (PyExc_ValueError ,
2031
- "cannot copy back to a read-only array" );
2032
- return NULL ;
2033
- }
2034
- if ((flags & NPY_ARRAY_ENSUREARRAY )) {
2035
- subtype = & PyArray_Type ;
2036
- }
2037
- ret = (PyArrayObject * )
2038
- PyArray_NewFromDescr (subtype , newtype ,
2039
- PyArray_NDIM (arr ),
2040
- PyArray_DIMS (arr ),
2041
- NULL , NULL ,
2042
- flags & NPY_ARRAY_F_CONTIGUOUS ,
2043
- (PyObject * )arr );
2044
- if (ret == NULL ) {
2045
- return NULL ;
2046
- }
2047
-
2048
- /* Allocate an NA mask if necessary from the input */
2049
- if (PyArray_HASMASKNA (arr )) {
2050
- if (PyArray_AllocateMaskNA (ret , 1 , 0 , 1 ) < 0 ) {
2051
- Py_DECREF (ret );
2052
- return NULL ;
2053
- }
2054
- }
2055
-
2056
- if (PyArray_CopyInto (ret , arr ) < 0 ) {
2057
- Py_DECREF (ret );
2058
- return NULL ;
2059
- }
1998
+ arrflags = PyArray_FLAGS (arr );
1999
+ if (PyArray_NDIM (arr ) <= 1 && (flags & NPY_ARRAY_F_CONTIGUOUS )) {
2000
+ flags |= NPY_ARRAY_C_CONTIGUOUS ;
2001
+ }
2002
+ copy = (flags & NPY_ARRAY_ENSURECOPY ) ||
2003
+ ((flags & NPY_ARRAY_C_CONTIGUOUS ) &&
2004
+ (!(arrflags & NPY_ARRAY_C_CONTIGUOUS )))
2005
+ || ((flags & NPY_ARRAY_ALIGNED ) &&
2006
+ (!(arrflags & NPY_ARRAY_ALIGNED )))
2007
+ || (PyArray_NDIM (arr ) > 1 &&
2008
+ ((flags & NPY_ARRAY_F_CONTIGUOUS ) &&
2009
+ (!(arrflags & NPY_ARRAY_F_CONTIGUOUS ))))
2010
+ || ((flags & NPY_ARRAY_WRITEABLE ) &&
2011
+ (!(arrflags & NPY_ARRAY_WRITEABLE ))) ||
2012
+ !PyArray_EquivTypes (oldtype , newtype );
2060
2013
2061
- /* Allocate an NA mask if requested but wasn't from the input */
2062
- if ((flags & (NPY_ARRAY_MASKNA | NPY_ARRAY_OWNMASKNA )) != 0 &&
2063
- !PyArray_HASMASKNA (ret )) {
2064
- if (PyArray_AllocateMaskNA (ret , 1 , 0 , 1 ) < 0 ) {
2065
- Py_DECREF (ret );
2066
- return NULL ;
2067
- }
2068
- }
2014
+ if (copy ) {
2015
+ NPY_ORDER order = NPY_KEEPORDER ;
2016
+ int subok = 1 ;
2069
2017
2070
- if (flags & NPY_ARRAY_UPDATEIFCOPY ) {
2071
- /*
2072
- * Don't use PyArray_SetBaseObject, because that compresses
2073
- * the chain of bases.
2074
- */
2075
- Py_INCREF (arr );
2076
- ((PyArrayObject_fieldaccess * )ret )-> base = (PyObject * )arr ;
2077
- PyArray_ENABLEFLAGS (ret , NPY_ARRAY_UPDATEIFCOPY );
2078
- PyArray_CLEARFLAGS (arr , NPY_ARRAY_WRITEABLE );
2079
- }
2018
+ /* Set the order for the copy being made based on the flags */
2019
+ if (flags & NPY_ARRAY_F_CONTIGUOUS ) {
2020
+ order = NPY_FORTRANORDER ;
2080
2021
}
2081
- /*
2082
- * If no copy then just increase the reference
2083
- * count and return the input
2084
- */
2085
- else {
2086
- Py_DECREF (newtype );
2087
- if ((flags & NPY_ARRAY_ENSUREARRAY ) &&
2088
- !PyArray_CheckExact (arr )) {
2089
- PyArray_Descr * dtype = PyArray_DESCR (arr );
2090
- Py_INCREF (dtype );
2091
- ret = (PyArrayObject * )
2092
- PyArray_NewFromDescr (& PyArray_Type ,
2093
- dtype ,
2094
- PyArray_NDIM (arr ),
2095
- PyArray_DIMS (arr ),
2096
- PyArray_STRIDES (arr ),
2097
- PyArray_DATA (arr ),
2098
- PyArray_FLAGS (arr ),
2099
- NULL );
2100
- if (ret == NULL ) {
2101
- return NULL ;
2102
- }
2103
- if (PyArray_SetBaseObject (ret , (PyObject * )arr )) {
2104
- Py_DECREF (ret );
2105
- return NULL ;
2106
- }
2107
- }
2108
- else {
2109
- ret = arr ;
2110
- }
2111
- Py_INCREF (arr );
2022
+ else if (flags & NPY_ARRAY_C_CONTIGUOUS ) {
2023
+ order = NPY_CORDER ;
2112
2024
}
2113
- }
2114
2025
2115
- /*
2116
- * The desired output type is different than the input
2117
- * array type and copy was not specified
2118
- */
2119
- else {
2120
2026
if ((flags & NPY_ARRAY_UPDATEIFCOPY ) &&
2121
2027
(!PyArray_ISWRITEABLE (arr ))) {
2122
2028
Py_DECREF (newtype );
2123
2029
PyErr_SetString (PyExc_ValueError ,
2124
- "cannot copy back to a read-only array B " );
2030
+ "cannot copy back to a read-only array" );
2125
2031
return NULL ;
2126
2032
}
2127
2033
if ((flags & NPY_ARRAY_ENSUREARRAY )) {
2128
- subtype = & PyArray_Type ;
2034
+ subok = 0 ;
2129
2035
}
2130
- ret = (PyArrayObject * )
2131
- PyArray_NewFromDescr (subtype , newtype ,
2132
- PyArray_NDIM (arr ), PyArray_DIMS (arr ),
2133
- NULL , NULL ,
2134
- flags & NPY_ARRAY_F_CONTIGUOUS ,
2135
- (PyObject * )arr );
2036
+ ret = (PyArrayObject * )PyArray_NewLikeArray (arr , order ,
2037
+ newtype , subok );
2136
2038
if (ret == NULL ) {
2137
2039
return NULL ;
2138
2040
}
2139
- if (PyArray_CastTo (ret , arr ) < 0 ) {
2041
+
2042
+ /*
2043
+ * Allocate an NA mask if necessary from the input,
2044
+ * is NAs are being allowed.
2045
+ */
2046
+ if (PyArray_HASMASKNA (arr ) && (flags & NPY_ARRAY_ALLOWNA )) {
2047
+ if (PyArray_AllocateMaskNA (ret , 1 , 0 , 1 ) < 0 ) {
2048
+ Py_DECREF (ret );
2049
+ return NULL ;
2050
+ }
2051
+ }
2052
+
2053
+ /*
2054
+ * If a ALLOWNA was not enabled, and 'arr' has an NA mask,
2055
+ * this will raise an error if 'arr' contains any NA values.
2056
+ */
2057
+ if (PyArray_CopyInto (ret , arr ) < 0 ) {
2140
2058
Py_DECREF (ret );
2141
2059
return NULL ;
2142
2060
}
2061
+
2062
+ /* Allocate an NA mask if requested but wasn't from the input */
2063
+ if ((flags & (NPY_ARRAY_MASKNA | NPY_ARRAY_OWNMASKNA )) != 0 &&
2064
+ !PyArray_HASMASKNA (ret )) {
2065
+ if (PyArray_AllocateMaskNA (ret , 1 , 0 , 1 ) < 0 ) {
2066
+ Py_DECREF (ret );
2067
+ return NULL ;
2068
+ }
2069
+ }
2070
+
2143
2071
if (flags & NPY_ARRAY_UPDATEIFCOPY ) {
2144
2072
/*
2145
2073
* Don't use PyArray_SetBaseObject, because that compresses
@@ -2151,6 +2079,28 @@ PyArray_FromArray(PyArrayObject *arr, PyArray_Descr *newtype, int flags)
2151
2079
PyArray_CLEARFLAGS (arr , NPY_ARRAY_WRITEABLE );
2152
2080
}
2153
2081
}
2082
+ /*
2083
+ * If no copy then just increase the reference
2084
+ * count and return the input
2085
+ */
2086
+ else {
2087
+ Py_DECREF (newtype );
2088
+ if ((flags & NPY_ARRAY_ENSUREARRAY ) &&
2089
+ !PyArray_CheckExact (arr )) {
2090
+ PyArray_Descr * dtype = PyArray_DESCR (arr );
2091
+ Py_INCREF (dtype );
2092
+
2093
+ ret = (PyArrayObject * )PyArray_View (arr , NULL , & PyArray_Type );
2094
+ if (ret == NULL ) {
2095
+ return NULL ;
2096
+ }
2097
+ }
2098
+ else {
2099
+ ret = arr ;
2100
+ }
2101
+ Py_INCREF (arr );
2102
+ }
2103
+
2154
2104
return (PyObject * )ret ;
2155
2105
}
2156
2106
0 commit comments