@@ -171,17 +171,38 @@ array_view(PyArrayObject *self, PyObject *args, PyObject *kwds)
171
171
PyObject * out_type = NULL ;
172
172
PyArray_Descr * dtype = NULL ;
173
173
PyObject * ret ;
174
- int maskna = 0 , ownmaskna = 0 ;
174
+ int maskna = -1 , ownmaskna = 0 ;
175
+ PyObject * maskna_in = Py_None ;
175
176
176
177
static char * kwlist [] = {"dtype" , "type" , "maskna" , "ownmaskna" , NULL };
177
- if (!PyArg_ParseTupleAndKeywords (args , kwds , "|OOii " , kwlist ,
178
+ if (!PyArg_ParseTupleAndKeywords (args , kwds , "|OOOi " , kwlist ,
178
179
& out_dtype ,
179
180
& out_type ,
180
- & maskna ,
181
+ & maskna_in ,
181
182
& ownmaskna )) {
182
183
return NULL ;
183
184
}
184
185
186
+ /* Treat None the same as not providing the parameter */
187
+ if (maskna_in != Py_None ) {
188
+ maskna = PyObject_IsTrue (maskna_in );
189
+ if (maskna == -1 ) {
190
+ return NULL ;
191
+ }
192
+ }
193
+
194
+ /* 'ownmaskna' forces 'maskna' to be True */
195
+ if (ownmaskna ) {
196
+ if (maskna == 0 ) {
197
+ PyErr_SetString (PyExc_ValueError ,
198
+ "cannot specify maskna=False and ownmaskna=True" );
199
+ return NULL ;
200
+ }
201
+ else {
202
+ maskna = 1 ;
203
+ }
204
+ }
205
+
185
206
/* If user specified a positional argument, guess whether it
186
207
represents a type or a dtype for backward compatibility. */
187
208
if (out_dtype ) {
@@ -213,8 +234,11 @@ array_view(PyArrayObject *self, PyObject *args, PyObject *kwds)
213
234
}
214
235
215
236
ret = PyArray_View (self , dtype , (PyTypeObject * )out_type );
237
+ if (ret == NULL ) {
238
+ return NULL ;
239
+ }
216
240
217
- if (maskna || ownmaskna ) {
241
+ if (maskna == 1 ) {
218
242
/* Ensure there is an NA mask if requested */
219
243
if (PyArray_AllocateMaskNA ((PyArrayObject * )ret ,
220
244
ownmaskna , 0 , 1 ) < 0 ) {
@@ -223,6 +247,13 @@ array_view(PyArrayObject *self, PyObject *args, PyObject *kwds)
223
247
}
224
248
return ret ;
225
249
}
250
+ else if (maskna == 0 && PyArray_HASMASKNA (ret )) {
251
+ PyErr_SetString (PyExc_ValueError ,
252
+ "Cannot take a view of an NA-masked array "
253
+ "with maskna=False" );
254
+ Py_DECREF (ret );
255
+ return NULL ;
256
+ }
226
257
else {
227
258
return ret ;
228
259
}
0 commit comments