diff --git a/modules/python/src2/cv2_convert.cpp b/modules/python/src2/cv2_convert.cpp index 577c079dcd..1161995495 100644 --- a/modules/python/src2/cv2_convert.cpp +++ b/modules/python/src2/cv2_convert.cpp @@ -711,28 +711,29 @@ bool pyopencv_to(PyObject* obj, String &value, const ArgInfo& info) std::string str; #if ((PY_VERSION_HEX >= 0x03060000) && !defined(Py_LIMITED_API)) || (Py_LIMITED_API >= 0x03060000) + PyObject* path_obj = NULL; if (info.pathlike) { - obj = PyOS_FSPath(obj); + path_obj = PyOS_FSPath(obj); if (PyErr_Occurred()) { failmsg("Expected '%s' to be a str or path-like object", info.name); return false; } + obj = path_obj; } #endif + + bool result = false; if (getUnicodeString(obj, str)) { value = str; - return true; + result = true; } else { - // If error hasn't been already set by Python conversion functions if (!PyErr_Occurred()) { - // Direct access to underlying slots of PyObjectType is not allowed - // when limited API is enabled #ifdef Py_LIMITED_API failmsg("Can't convert object to 'str' for '%s'", info.name); #else @@ -741,7 +742,12 @@ bool pyopencv_to(PyObject* obj, String &value, const ArgInfo& info) #endif } } - return false; + +#if ((PY_VERSION_HEX >= 0x03060000) && !defined(Py_LIMITED_API)) || (Py_LIMITED_API >= 0x03060000) + Py_XDECREF(path_obj); +#endif + + return result; } template<>