Skip to content

Cleanup C++ code #17458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions src/_image_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ _get_transform_mesh(PyObject *py_affine, npy_intp *dims)
out_dims[0] = dims[0] * dims[1];
out_dims[1] = 2;

py_inverse = PyObject_CallMethod(
py_affine, (char *)"inverted", (char *)"", NULL);
py_inverse = PyObject_CallMethod(py_affine, "inverted", NULL);
if (py_inverse == NULL) {
return NULL;
}
Expand All @@ -83,10 +82,8 @@ _get_transform_mesh(PyObject *py_affine, npy_intp *dims)
}
}

PyObject *output_mesh =
PyObject_CallMethod(
py_inverse, (char *)"transform", (char *)"O",
(char *)input_mesh.pyobj(), NULL);
PyObject *output_mesh = PyObject_CallMethod(
py_inverse, "transform", "O", input_mesh.pyobj_steal());

Py_DECREF(py_inverse);

Expand Down Expand Up @@ -120,7 +117,12 @@ image_resample(PyObject *self, PyObject* args, PyObject *kwargs)
PyArrayObject *output_array = NULL;
PyArrayObject *transform_mesh_array = NULL;

params.interpolation = NEAREST;
params.transform_mesh = NULL;
params.resample = false;
params.norm = false;
params.radius = 1.0;
params.alpha = 1.0;

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

output_array = (PyArrayObject *)PyArray_FromAny(
py_output_array, NULL, 2, 3, NPY_ARRAY_C_CONTIGUOUS, NULL);
if (output_array == NULL) {
if (!PyArray_Check(py_output_array)) {
PyErr_SetString(PyExc_ValueError, "output array must be a NumPy array");
goto error;
}
output_array = (PyArrayObject *)py_output_array;
if (!PyArray_IS_C_CONTIGUOUS(output_array)) {
PyErr_SetString(PyExc_ValueError, "output array must be C-contiguous");
goto error;
}
if (PyArray_NDIM(output_array) < 2 || PyArray_NDIM(output_array) > 3) {
PyErr_SetString(PyExc_ValueError,
"output array must be 2- or 3-dimensional");
goto error;
}

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

Py_DECREF(input_array);
Py_XDECREF(transform_mesh_array);
return (PyObject *)output_array;
Py_RETURN_NONE;

error:
Py_XDECREF(input_array);
Py_XDECREF(output_array);
Py_XDECREF(transform_mesh_array);
return NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion src/_ttconv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class PythonFileWriter : public TTStreamWriter
if (decoded == NULL) {
throw py::exception();
}
result = PyObject_CallFunction(_write_method, (char *)"O", decoded);
result = PyObject_CallFunctionObjArgs(_write_method, decoded, NULL);
Py_DECREF(decoded);
if (!result) {
throw py::exception();
Expand Down
2 changes: 1 addition & 1 deletion src/mplutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ int add_dict_int(PyObject *dict, const char *key, long val)
return 1;
}

if (PyDict_SetItemString(dict, (char *)key, valobj)) {
if (PyDict_SetItemString(dict, key, valobj)) {
Py_DECREF(valobj);
return 1;
}
Expand Down
14 changes: 7 additions & 7 deletions src/numpy_cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ class array_view : public detail::array_view_accessors<array_view, T, ND>
Py_XINCREF(arr);
m_shape = PyArray_DIMS(m_arr);
m_strides = PyArray_STRIDES(m_arr);
m_data = (char *)PyArray_BYTES(m_arr);
m_data = PyArray_BYTES(m_arr);
}

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

int set(PyObject *arr, bool contiguous = false)
bool set(PyObject *arr, bool contiguous = false)
{
PyArrayObject *tmp;

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

if (PyArray_NDIM(tmp) == 0 || PyArray_DIM(tmp, 0) == 0) {
Expand All @@ -469,7 +469,7 @@ class array_view : public detail::array_view_accessors<array_view, T, ND>
m_strides = zeros;
if (PyArray_NDIM(tmp) == 0 && ND == 0) {
m_arr = tmp;
return 1;
return true;
}
}
if (PyArray_NDIM(tmp) != ND) {
Expand All @@ -478,18 +478,18 @@ class array_view : public detail::array_view_accessors<array_view, T, ND>
ND,
PyArray_NDIM(tmp));
Py_DECREF(tmp);
return 0;
return false;
}

/* Copy some of the data to the view object for faster access */
Py_XDECREF(m_arr);
m_arr = tmp;
m_shape = PyArray_DIMS(m_arr);
m_strides = PyArray_STRIDES(m_arr);
m_data = (char *)PyArray_BYTES(tmp);
m_data = PyArray_BYTES(tmp);
}

return 1;
return true;
}

npy_intp dim(size_t i) const
Expand Down
8 changes: 4 additions & 4 deletions src/py_converters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ int convert_from_method(PyObject *obj, const char *name, converter func, void *p
{
PyObject *value;

value = PyObject_CallMethod(obj, (char *)name, NULL);
value = PyObject_CallMethod(obj, name, NULL);
if (value == NULL) {
if (!PyObject_HasAttrString(obj, (char *)name)) {
if (!PyObject_HasAttrString(obj, name)) {
PyErr_Clear();
return 1;
}
Expand All @@ -76,9 +76,9 @@ int convert_from_attr(PyObject *obj, const char *name, converter func, void *p)
{
PyObject *value;

value = PyObject_GetAttrString(obj, (char *)name);
value = PyObject_GetAttrString(obj, name);
if (value == NULL) {
if (!PyObject_HasAttrString(obj, (char *)name)) {
if (!PyObject_HasAttrString(obj, name)) {
PyErr_Clear();
return 1;
}
Expand Down