Skip to content

Commit e5abc17

Browse files
anntzerQuLogic
andcommitted
image: Avoid excess NumPy array refcount twiddling.
A new array is always supplied, so just check that it is the right type/shape. Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
1 parent 89be34e commit e5abc17

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/_image_wrapper.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,18 @@ image_resample(PyObject *self, PyObject* args, PyObject *kwargs)
151151
goto error;
152152
}
153153

154-
output_array = (PyArrayObject *)PyArray_FromAny(
155-
py_output_array, NULL, 2, 3, NPY_ARRAY_C_CONTIGUOUS, NULL);
156-
if (output_array == NULL) {
154+
if (!PyArray_Check(py_output_array)) {
155+
PyErr_SetString(PyExc_ValueError, "output array must be a NumPy array");
156+
goto error;
157+
}
158+
output_array = (PyArrayObject *)py_output_array;
159+
if (!PyArray_IS_C_CONTIGUOUS(output_array)) {
160+
PyErr_SetString(PyExc_ValueError, "output array must be C-contiguous");
161+
goto error;
162+
}
163+
if (PyArray_NDIM(output_array) < 2 || PyArray_NDIM(output_array) > 3) {
164+
PyErr_SetString(PyExc_ValueError,
165+
"output array must be 2- or 3-dimensional");
157166
goto error;
158167
}
159168

@@ -335,11 +344,10 @@ image_resample(PyObject *self, PyObject* args, PyObject *kwargs)
335344

336345
Py_DECREF(input_array);
337346
Py_XDECREF(transform_mesh_array);
338-
return (PyObject *)output_array;
347+
Py_RETURN_NONE;
339348

340349
error:
341350
Py_XDECREF(input_array);
342-
Py_XDECREF(output_array);
343351
Py_XDECREF(transform_mesh_array);
344352
return NULL;
345353
}

0 commit comments

Comments
 (0)