Skip to content

Commit d132860

Browse files
authored
Merge pull request #17458 from QuLogic/image-cleanup
FIX: Cleanup C++ code and remove memory leak
2 parents 3964e71 + 2d2083c commit d132860

File tree

5 files changed

+34
-24
lines changed

5 files changed

+34
-24
lines changed

src/_image_wrapper.cpp

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ _get_transform_mesh(PyObject *py_affine, npy_intp *dims)
6767
out_dims[0] = dims[0] * dims[1];
6868
out_dims[1] = 2;
6969

70-
py_inverse = PyObject_CallMethod(
71-
py_affine, (char *)"inverted", (char *)"", NULL);
70+
py_inverse = PyObject_CallMethod(py_affine, "inverted", NULL);
7271
if (py_inverse == NULL) {
7372
return NULL;
7473
}
@@ -83,10 +82,8 @@ _get_transform_mesh(PyObject *py_affine, npy_intp *dims)
8382
}
8483
}
8584

86-
PyObject *output_mesh =
87-
PyObject_CallMethod(
88-
py_inverse, (char *)"transform", (char *)"O",
89-
(char *)input_mesh.pyobj(), NULL);
85+
PyObject *output_mesh = PyObject_CallMethod(
86+
py_inverse, "transform", "O", input_mesh.pyobj_steal());
9087

9188
Py_DECREF(py_inverse);
9289

@@ -120,7 +117,12 @@ image_resample(PyObject *self, PyObject* args, PyObject *kwargs)
120117
PyArrayObject *output_array = NULL;
121118
PyArrayObject *transform_mesh_array = NULL;
122119

120+
params.interpolation = NEAREST;
123121
params.transform_mesh = NULL;
122+
params.resample = false;
123+
params.norm = false;
124+
params.radius = 1.0;
125+
params.alpha = 1.0;
124126

125127
const char *kwlist[] = {
126128
"input_array", "output_array", "transform", "interpolation",
@@ -146,9 +148,18 @@ image_resample(PyObject *self, PyObject* args, PyObject *kwargs)
146148
goto error;
147149
}
148150

149-
output_array = (PyArrayObject *)PyArray_FromAny(
150-
py_output_array, NULL, 2, 3, NPY_ARRAY_C_CONTIGUOUS, NULL);
151-
if (output_array == NULL) {
151+
if (!PyArray_Check(py_output_array)) {
152+
PyErr_SetString(PyExc_ValueError, "output array must be a NumPy array");
153+
goto error;
154+
}
155+
output_array = (PyArrayObject *)py_output_array;
156+
if (!PyArray_IS_C_CONTIGUOUS(output_array)) {
157+
PyErr_SetString(PyExc_ValueError, "output array must be C-contiguous");
158+
goto error;
159+
}
160+
if (PyArray_NDIM(output_array) < 2 || PyArray_NDIM(output_array) > 3) {
161+
PyErr_SetString(PyExc_ValueError,
162+
"output array must be 2- or 3-dimensional");
152163
goto error;
153164
}
154165

@@ -330,11 +341,10 @@ image_resample(PyObject *self, PyObject* args, PyObject *kwargs)
330341

331342
Py_DECREF(input_array);
332343
Py_XDECREF(transform_mesh_array);
333-
return (PyObject *)output_array;
344+
Py_RETURN_NONE;
334345

335346
error:
336347
Py_XDECREF(input_array);
337-
Py_XDECREF(output_array);
338348
Py_XDECREF(transform_mesh_array);
339349
return NULL;
340350
}

src/_ttconv.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class PythonFileWriter : public TTStreamWriter
4949
if (decoded == NULL) {
5050
throw py::exception();
5151
}
52-
result = PyObject_CallFunction(_write_method, (char *)"O", decoded);
52+
result = PyObject_CallFunctionObjArgs(_write_method, decoded, NULL);
5353
Py_DECREF(decoded);
5454
if (!result) {
5555
throw py::exception();

src/mplutils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ int add_dict_int(PyObject *dict, const char *key, long val)
1010
return 1;
1111
}
1212

13-
if (PyDict_SetItemString(dict, (char *)key, valobj)) {
13+
if (PyDict_SetItemString(dict, key, valobj)) {
1414
Py_DECREF(valobj);
1515
return 1;
1616
}

src/numpy_cpp.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ class array_view : public detail::array_view_accessors<array_view, T, ND>
406406
Py_XINCREF(arr);
407407
m_shape = PyArray_DIMS(m_arr);
408408
m_strides = PyArray_STRIDES(m_arr);
409-
m_data = (char *)PyArray_BYTES(m_arr);
409+
m_data = PyArray_BYTES(m_arr);
410410
}
411411

412412
array_view(npy_intp shape[ND]) : m_arr(NULL), m_shape(NULL), m_strides(NULL), m_data(NULL)
@@ -441,7 +441,7 @@ class array_view : public detail::array_view_accessors<array_view, T, ND>
441441
return *this;
442442
}
443443

444-
int set(PyObject *arr, bool contiguous = false)
444+
bool set(PyObject *arr, bool contiguous = false)
445445
{
446446
PyArrayObject *tmp;
447447

@@ -458,7 +458,7 @@ class array_view : public detail::array_view_accessors<array_view, T, ND>
458458
tmp = (PyArrayObject *)PyArray_FromObject(arr, type_num_of<T>::value, 0, ND);
459459
}
460460
if (tmp == NULL) {
461-
return 0;
461+
return false;
462462
}
463463

464464
if (PyArray_NDIM(tmp) == 0 || PyArray_DIM(tmp, 0) == 0) {
@@ -469,7 +469,7 @@ class array_view : public detail::array_view_accessors<array_view, T, ND>
469469
m_strides = zeros;
470470
if (PyArray_NDIM(tmp) == 0 && ND == 0) {
471471
m_arr = tmp;
472-
return 1;
472+
return true;
473473
}
474474
}
475475
if (PyArray_NDIM(tmp) != ND) {
@@ -478,18 +478,18 @@ class array_view : public detail::array_view_accessors<array_view, T, ND>
478478
ND,
479479
PyArray_NDIM(tmp));
480480
Py_DECREF(tmp);
481-
return 0;
481+
return false;
482482
}
483483

484484
/* Copy some of the data to the view object for faster access */
485485
Py_XDECREF(m_arr);
486486
m_arr = tmp;
487487
m_shape = PyArray_DIMS(m_arr);
488488
m_strides = PyArray_STRIDES(m_arr);
489-
m_data = (char *)PyArray_BYTES(tmp);
489+
m_data = PyArray_BYTES(tmp);
490490
}
491491

492-
return 1;
492+
return true;
493493
}
494494

495495
npy_intp dim(size_t i) const

src/py_converters.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ int convert_from_method(PyObject *obj, const char *name, converter func, void *p
5454
{
5555
PyObject *value;
5656

57-
value = PyObject_CallMethod(obj, (char *)name, NULL);
57+
value = PyObject_CallMethod(obj, name, NULL);
5858
if (value == NULL) {
59-
if (!PyObject_HasAttrString(obj, (char *)name)) {
59+
if (!PyObject_HasAttrString(obj, name)) {
6060
PyErr_Clear();
6161
return 1;
6262
}
@@ -76,9 +76,9 @@ int convert_from_attr(PyObject *obj, const char *name, converter func, void *p)
7676
{
7777
PyObject *value;
7878

79-
value = PyObject_GetAttrString(obj, (char *)name);
79+
value = PyObject_GetAttrString(obj, name);
8080
if (value == NULL) {
81-
if (!PyObject_HasAttrString(obj, (char *)name)) {
81+
if (!PyObject_HasAttrString(obj, name)) {
8282
PyErr_Clear();
8383
return 1;
8484
}

0 commit comments

Comments
 (0)