| 214 | |
| 215 | |
| 216 | void PP_Fetch_Error_Text() |
| 217 | { |
| 218 | |
| 219 | char *tempstr = NULL; |
| 220 | PyObject *errobj = NULL, *errdata = NULL, *errtraceback = NULL, *pystring = NULL, *pydict = NULL; |
| 221 | |
| 222 | /* get latest python exception information */ |
| 223 | /* this also clears the current exception */ |
| 224 | |
| 225 | PyErr_Fetch(&errobj, &errdata, &errtraceback); /* all 3 incref'd */ |
| 226 | |
| 227 | |
| 228 | /* convert type and data to strings */ |
| 229 | /* calls str() on both to stringify */ |
| 230 | |
| 231 | pystring = NULL; |
| 232 | if (errobj != NULL && |
| 233 | (pystring = PyObject_Str(errobj)) != NULL && /* str(errobj) */ |
| 234 | (PyUnicode_Check(pystring)) ) /* str() increfs */ |
| 235 | { |
| 236 | strncpy(PP_last_error_type, PyUnicode_AsUTF8(pystring), MAX); /*Py->C*/ |
| 237 | PP_last_error_type[MAX-1] = '\0'; |
| 238 | } |
| 239 | else |
| 240 | { |
| 241 | PP_last_error_type[0] = '\0'; |
| 242 | } |
| 243 | |
| 244 | Py_XDECREF(pystring); |
| 245 | |
| 246 | pystring = NULL; |
| 247 | pydict = NULL; |
| 248 | if (errdata != NULL && |
| 249 | (PyDict_Check(errdata)) ) /* str() increfs */ |
| 250 | { |
| 251 | // PyDict_GetItemString returns a borrowed reference |
| 252 | // so we must make sure not to decrement the reference |
| 253 | PyObject* value = PyDict_GetItemString(errdata,"swhat"); |
| 254 | |
| 255 | if (value!=NULL) { |
| 256 | strncpy(PP_last_error_info, PyUnicode_AsUTF8(value), MAX); |
| 257 | PP_last_error_info[MAX-1] = '\0'; |
| 258 | } |
| 259 | |
| 260 | pydict = errdata; |
| 261 | Py_INCREF(pydict); |
| 262 | } |
| 263 | else if (errdata != NULL && |
| 264 | (pystring = PyObject_Str(errdata)) != NULL && /* str(): increfs */ |
| 265 | (PyUnicode_Check(pystring)) ) |
| 266 | { |
| 267 | strncpy(PP_last_error_info, PyUnicode_AsUTF8(pystring), MAX); /*Py->C*/ |
| 268 | PP_last_error_info[MAX-1] = '\0'; |
| 269 | } |
| 270 | else |
| 271 | strcpy(PP_last_error_info, "<unknown exception data>"); |
| 272 | |
| 273 | Py_XDECREF(pystring); |
no test coverage detected