MCPcopy Create free account
hub / github.com/mhammond/pywin32 / GetPythonTraceback

Function GetPythonTraceback

win32/src/PyWinTypesmodule.cpp:1143–1234  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1141// Function to format a python traceback into a character string.
1142#define GPEM_ERROR(what) {errorMsg = "<Error getting traceback - " ## what ## ">";goto done;}
1143char *GetPythonTraceback(PyObject *exc_type, PyObject *exc_value, PyObject *exc_tb)
1144{
1145 // Sleep (30000); // Time enough to attach the debugger (barely)
1146 char *result = NULL;
1147 char *errorMsg = NULL;
1148 PyObject *modStringIO = NULL;
1149 PyObject *modTB = NULL;
1150 PyObject *obFuncStringIO = NULL;
1151 PyObject *obStringIO = NULL;
1152 PyObject *obFuncTB = NULL;
1153 PyObject *argsTB = NULL;
1154 PyObject *obResult = NULL;
1155
1156 /* Import the modules we need - cStringIO and traceback */
1157#if (PY_VERSION_HEX < 0x03000000)
1158 modStringIO = PyImport_ImportModule("cStringIO");
1159#else
1160 // In py3k, cStringIO is in "io"
1161 modStringIO = PyImport_ImportModule("io");
1162#endif
1163
1164 if (modStringIO==NULL) GPEM_ERROR("cant import cStringIO");
1165 modTB = PyImport_ImportModule("traceback");
1166 if (modTB==NULL) GPEM_ERROR("cant import traceback");
1167
1168 /* Construct a cStringIO object */
1169 obFuncStringIO = PyObject_GetAttrString(modStringIO, "StringIO");
1170 if (obFuncStringIO==NULL) GPEM_ERROR("cant find cStringIO.StringIO");
1171 obStringIO = PyObject_CallObject(obFuncStringIO, NULL);
1172 if (obStringIO==NULL) GPEM_ERROR("cStringIO.StringIO() failed");
1173
1174 /* Get the traceback.print_exception function, and call it. */
1175 obFuncTB = PyObject_GetAttrString(modTB, "print_exception");
1176 if (obFuncTB==NULL) GPEM_ERROR("cant find traceback.print_exception");
1177 argsTB = Py_BuildValue("OOOOO"
1178#if (PY_VERSION_HEX >= 0x03000000)
1179 "i"
1180 // Py3k has added an undocumented 'chain' argument which defaults to True
1181 // and causes all kinds of exceptions while trying to print a goddam exception
1182#endif
1183 ,
1184 exc_type ? exc_type : Py_None,
1185 exc_value ? exc_value : Py_None,
1186 exc_tb ? exc_tb : Py_None,
1187 Py_None, // limit
1188 obStringIO
1189#if (PY_VERSION_HEX >= 0x03000000)
1190 ,0 // Goddam undocumented 'chain' param, which defaults to True
1191#endif
1192 );
1193 if (argsTB==NULL) GPEM_ERROR("cant make print_exception arguments");
1194
1195 obResult = PyObject_CallObject(obFuncTB, argsTB);
1196 if (obResult==NULL){
1197 // Chain parameter when True causes traceback.print_exception to fail, leaving no
1198 // way to see what the original problem is, or even what error print_exc raises
1199 // PyObject *t, *v, *tb;
1200 // PyErr_Fetch(&t, &v, &tb);

Callers 6

OnInitDialogMethod · 0.85
gui_print_errorFunction · 0.85
DefaultExceptionHandlerFunction · 0.85
_LogExceptionFunction · 0.85
ReportPythonErrorFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected