From d28424933a6673bc285e00c6f01ede67e8e2f785 Mon Sep 17 00:00:00 2001 From: Abdullah Tahiri Date: Sun, 7 May 2017 00:05:24 +0200 Subject: [PATCH] Extend PyTools to export exception dictionary if present --- src/Base/PyTools.c | 61 ++++++++++++++++++++++++++++++++++++++++------ src/Base/PyTools.h | 11 +++++++++ 2 files changed, 64 insertions(+), 8 deletions(-) diff --git a/src/Base/PyTools.c b/src/Base/PyTools.c index c08d3b886f5b..200f792a0c22 100644 --- a/src/Base/PyTools.c +++ b/src/Base/PyTools.c @@ -17,7 +17,6 @@ is provided on an as is basis, without warranties of any kind. #include #include - #if PY_VERSION_HEX <= 0x02050000 #error "Use Python2.5.x or higher" #endif @@ -214,10 +213,20 @@ FC_OS_LINUX: This is dangerous. How about PY_EXCEPT_MAX? */ /* exception text is here after PP_Fetch_Error_Text call */ -char PP_last_error_type[MAX]; /* exception name text */ -char PP_last_error_info[MAX]; /* exception data text */ -char PP_last_error_trace[MAX]; /* exception traceback text */ -PyObject *PP_last_traceback = NULL; /* saved exception traceback object */ +char PP_last_error_type[MAX]; /* exception name text */ +char PP_last_error_info[MAX]; /* exception data text */ + +//char PP_last_error_file[MAX]; /* exception file text */ +//unsigned int PP_last_error_line; /* exception line */ +//unsigned int PP_last_error_classindex; /* exception class type index (key of Base::Type) */ +//char PP_last_error_function[MAX]; /* exception function text */ +//char PP_last_error_message[MAX]; /* exception message text */ + +PyObject *PP_PyDict_Object = NULL; /* saved exception dictionary object */ + +char PP_last_error_trace[MAX]; /* exception traceback text */ +PyObject *PP_last_traceback = NULL; /* saved exception traceback object */ +//bool PP_last_error_isDictType = false; void PP_Fetch_Error_Text() @@ -226,7 +235,7 @@ void PP_Fetch_Error_Text() //assert(PyErr_Occurred()); char *tempstr; - PyObject *errobj, *errdata, *errtraceback, *pystring; + PyObject *errobj, *errdata, *errtraceback, *pystring, *pydict; /* get latest python exception information */ /* this also clears the current exception */ @@ -245,13 +254,47 @@ void PP_Fetch_Error_Text() strncpy(PP_last_error_type, PyString_AsString(pystring), MAX); /*Py->C*/ PP_last_error_type[MAX-1] = '\0'; } - else + else + { strcpy(PP_last_error_type, ""); + } + Py_XDECREF(pystring); pystring = NULL; + pydict = NULL; if (errdata != NULL && + (PyDict_Check(errdata)) ) /* str() increfs */ + { + /* + pystring = PyDict_GetItemString(errdata,"swhat"); + strncpy(PP_last_error_info, PyString_AsString(pystring), MAX); + PP_last_error_info[MAX-1] = '\0'; + + pystring = PyDict_GetItemString(errdata,"sfile"); + strncpy(PP_last_error_file, PyString_AsString(pystring), MAX); + PP_last_error_file[MAX-1] = '\0'; + + pystring = PyDict_GetItemString(errdata,"sfunction"); + strncpy(PP_last_error_function, PyString_AsString(pystring), MAX); + PP_last_error_function[MAX-1] = '\0'; + + pystring = PyDict_GetItemString(errdata,"sErrMsg"); + strncpy(PP_last_error_message, PyString_AsString(pystring), MAX); + PP_last_error_message[MAX-1] = '\0'; + + pystring = PyDict_GetItemString(errdata,"iline"); + PP_last_error_line = PyInt_AsLong(pystring); + + pystring = PyDict_GetItemString(errdata,"classindex"); + PP_last_error_classindex = PyInt_AsLong(pystring); + + PP_last_error_isDictType = true; + */ + pydict = errdata; + } + else if (errdata != NULL && (pystring = PyObject_Str(errdata)) != NULL && /* str(): increfs */ (PyString_Check(pystring)) ) { @@ -260,9 +303,9 @@ void PP_Fetch_Error_Text() } else strcpy(PP_last_error_info, ""); + Py_XDECREF(pystring); - /* convert traceback to string */ /* print text to a StringIO.StringIO() internal file object, then */ /* fetch by calling object's .getvalue() method (see lib manual); */ @@ -285,7 +328,9 @@ void PP_Fetch_Error_Text() Py_XDECREF(errobj); Py_XDECREF(errdata); /* this function owns all 3 objects */ Py_XDECREF(PP_last_traceback); /* they've been NULL'd out in Python */ + Py_XDECREF(PP_PyDict_Object); PP_last_traceback = errtraceback; /* save/export raw traceback object */ + PP_PyDict_Object = pydict; } diff --git a/src/Base/PyTools.h b/src/Base/PyTools.h index f190b334bbf9..3e0b6cbf1340 100644 --- a/src/Base/PyTools.h +++ b/src/Base/PyTools.h @@ -73,6 +73,8 @@ extern "C" { /* a C library, but callable from C++ */ # undef _POSIX_C_SOURCE #endif // (re-)defined in pyconfig.h #include + +//#include extern int PP_RELOAD; /* 1=reload py modules when attributes referenced */ extern int PP_DEBUG; /* 1=start debugger when string/function/member run */ @@ -180,6 +182,15 @@ extern char PP_last_error_type[]; /* exception name text */ extern char PP_last_error_info[]; /* exception data text */ extern char PP_last_error_trace[]; /* exception traceback text */ +//extern char PP_last_error_file[]; /* exception file text */ +//extern unsigned int PP_last_error_line; /* exception line */ +//extern unsigned int PP_last_error_classindex; /* exception class type index (key of Base::Type) */ +//extern char PP_last_error_function[]; /* exception function text */ +//extern char PP_last_error_message[]; /* exception message text */ +//extern bool PP_last_error_isDictType; + +extern PyObject *PP_PyDict_Object; /* saved PyDict object */ + extern PyObject *PP_last_traceback; /* saved exception traceback object */