From f1ca05c822af606f9028b0bec2842e3ef8dc7530 Mon Sep 17 00:00:00 2001 From: Hamdi Sahloul Date: Tue, 21 Aug 2018 12:41:07 +0900 Subject: [PATCH] Extend python exception `cv.error` to provide `file`, `func`, `line`, `code`, `msg`, and `err` attributes --- modules/python/src2/cv2.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/modules/python/src2/cv2.cpp b/modules/python/src2/cv2.cpp index bf59d40a4f..a16292e7d9 100644 --- a/modules/python/src2/cv2.cpp +++ b/modules/python/src2/cv2.cpp @@ -110,7 +110,7 @@ PyObject* pyopencv_from(const TYPE& src) #include -static PyObject* opencv_error = 0; +static PyObject* opencv_error = NULL; static int failmsg(const char *fmt, ...) { @@ -171,6 +171,12 @@ try \ } \ catch (const cv::Exception &e) \ { \ + PyObject_SetAttrString(opencv_error, "file", PyString_FromString(e.file.c_str())); \ + PyObject_SetAttrString(opencv_error, "func", PyString_FromString(e.func.c_str())); \ + PyObject_SetAttrString(opencv_error, "line", PyInt_FromLong(e.line)); \ + PyObject_SetAttrString(opencv_error, "code", PyInt_FromLong(e.code)); \ + PyObject_SetAttrString(opencv_error, "msg", PyString_FromString(e.msg.c_str())); \ + PyObject_SetAttrString(opencv_error, "err", PyString_FromString(e.err.c_str())); \ PyErr_SetString(opencv_error, e.what()); \ return 0; \ } @@ -2030,7 +2036,15 @@ void initcv2() PyDict_SetItemString(d, "__version__", PyString_FromString(CV_VERSION)); - opencv_error = PyErr_NewException((char*)MODULESTR".error", NULL, NULL); + PyObject *opencv_error_dict = PyDict_New(); + PyDict_SetItemString(opencv_error_dict, "file", Py_None); + PyDict_SetItemString(opencv_error_dict, "func", Py_None); + PyDict_SetItemString(opencv_error_dict, "line", Py_None); + PyDict_SetItemString(opencv_error_dict, "code", Py_None); + PyDict_SetItemString(opencv_error_dict, "msg", Py_None); + PyDict_SetItemString(opencv_error_dict, "err", Py_None); + opencv_error = PyErr_NewException((char*)MODULESTR".error", NULL, opencv_error_dict); + Py_DECREF(opencv_error_dict); PyDict_SetItemString(d, "error", opencv_error); //Registering UMatWrapper python class in cv2 module: