Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 48b851f

Browse files
committed
Don't use exceptions in convert_rect
1 parent 6017a93 commit 48b851f

1 file changed

Lines changed: 21 additions & 30 deletions

File tree

src/py_converters.cpp

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -158,45 +158,36 @@ int convert_rect(PyObject *rectobj, void *rectp)
158158
rect->x2 = 0.0;
159159
rect->y2 = 0.0;
160160
} else {
161-
try
162-
{
163-
numpy::array_view<const double, 2> rect_arr(rectobj);
161+
PyArrayObject *rect_arr = (PyArrayObject *)PyArray_ContiguousFromAny(
162+
rectobj, NPY_DOUBLE, 1, 2);
163+
if (rect_arr == NULL) {
164+
return 0;
165+
}
164166

165-
if (rect_arr.dim(0) != 2 || rect_arr.dim(1) != 2) {
167+
if (PyArray_NDIM(rect_arr) == 2) {
168+
if (PyArray_DIM(rect_arr, 0) != 2 ||
169+
PyArray_DIM(rect_arr, 1) != 2) {
166170
PyErr_SetString(PyExc_ValueError, "Invalid bounding box");
171+
Py_DECREF(rect_arr);
167172
return 0;
168173
}
169174

170-
rect->x1 = rect_arr(0, 0);
171-
rect->y1 = rect_arr(0, 1);
172-
rect->x2 = rect_arr(1, 0);
173-
rect->y2 = rect_arr(1, 1);
174-
}
175-
catch (py::exception &)
176-
{
177-
PyErr_Clear();
178-
179-
try
180-
{
181-
numpy::array_view<const double, 1> rect_arr(rectobj);
182-
183-
if (rect_arr.dim(0) != 4) {
184-
PyErr_SetString(PyExc_ValueError, "Invalid bounding box");
185-
return 0;
186-
}
187-
188-
rect->x1 = rect_arr(0);
189-
rect->y1 = rect_arr(1);
190-
rect->x2 = rect_arr(2);
191-
rect->y2 = rect_arr(3);
192-
}
193-
catch (py::exception &)
194-
{
175+
} else { // PyArray_NDIM(rect_arr) == 1
176+
if (PyArray_DIM(rect_arr, 0) != 4) {
177+
PyErr_SetString(PyExc_ValueError, "Invalid bounding box");
178+
Py_DECREF(rect_arr);
195179
return 0;
196180
}
197181
}
198-
}
199182

183+
double *buff = (double *)PyArray_DATA(rect_arr);
184+
rect->x1 = buff[0];
185+
rect->y1 = buff[1];
186+
rect->x2 = buff[2];
187+
rect->y2 = buff[3];
188+
189+
Py_DECREF(rect_arr);
190+
}
200191
return 1;
201192
}
202193

0 commit comments

Comments
 (0)