From c19b6ed20ef4b375aca5dc10f3304d1619cdbdad Mon Sep 17 00:00:00 2001 From: Gabe Schwartz Date: Wed, 21 May 2014 14:29:54 -0400 Subject: [PATCH] Fixed pyopencv_to w/FLANN IndexParams in python3. The keys() and values() functions on dictionaries in Python 3 no longer return lists. pyopencv_to() for flann::IndexParams now iterates over the dictionary in a way that is version-agnostic. --- modules/python/src2/cv2.cpp | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/modules/python/src2/cv2.cpp b/modules/python/src2/cv2.cpp index 1372ff9..340242f 100644 --- a/modules/python/src2/cv2.cpp +++ b/modules/python/src2/cv2.cpp @@ -999,19 +999,18 @@ template<> bool pyopencv_to(PyObject *o, cv::flann::IndexParams& p, const char *name) { (void)name; - bool ok = false; - PyObject* keys = PyObject_CallMethod(o,(char*)"keys",0); - PyObject* values = PyObject_CallMethod(o,(char*)"values",0); - - if( keys && values ) - { - int i, n = (int)PyList_GET_SIZE(keys); - for( i = 0; i < n; i++ ) - { - PyObject* key = PyList_GET_ITEM(keys, i); - PyObject* item = PyList_GET_ITEM(values, i); - if( !PyString_Check(key) ) + bool ok = true; + PyObject* key = NULL; + PyObject* item = NULL; + Py_ssize_t pos = 0; + + if(PyDict_Check(o)) { + while(PyDict_Next(o, &pos, &key, &item)) { + if( !PyString_Check(key) ) { + ok = false; break; + } + String k = PyString_AsString(key); if( PyString_Check(item) ) { @@ -1034,14 +1033,14 @@ bool pyopencv_to(PyObject *o, cv::flann::IndexParams& p, const char *name) p.setDouble(k, value); } else + { + ok = false; break; + } } - ok = i == n && !PyErr_Occurred(); } - Py_XDECREF(keys); - Py_XDECREF(values); - return ok; + return ok && !PyErr_Occurred(); } template<> -- 2.7.4