python: support Python list for cv::Range

This commit is contained in:
Alexander Alekhin 2019-03-01 20:52:35 +00:00
parent f5b58e5fc9
commit 030e955db7

View File

@ -819,6 +819,40 @@ bool pyopencv_to(PyObject* obj, Range& r, const char* name)
CV_UNUSED(name); CV_UNUSED(name);
if(!obj || obj == Py_None) if(!obj || obj == Py_None)
return true; return true;
while (PySequence_Check(obj))
{
PyObject *fi = PySequence_Fast(obj, name);
if (fi == NULL)
break;
if (2 != PySequence_Fast_GET_SIZE(fi))
{
failmsg("Range value for argument '%s' is longer than 2", name);
Py_DECREF(fi);
return false;
}
{
PyObject *item = PySequence_Fast_GET_ITEM(fi, 0);
if (PyInt_Check(item)) {
r.start = (int)PyInt_AsLong(item);
} else {
failmsg("Range.start value for argument '%s' is not integer", name);
Py_DECREF(fi);
break;
}
}
{
PyObject *item = PySequence_Fast_GET_ITEM(fi, 1);
if (PyInt_Check(item)) {
r.end = (int)PyInt_AsLong(item);
} else {
failmsg("Range.end value for argument '%s' is not integer", name);
Py_DECREF(fi);
break;
}
}
Py_DECREF(fi);
return true;
}
if(PyObject_Size(obj) == 0) if(PyObject_Size(obj) == 0)
{ {
r = Range::all(); r = Range::all();