[C++-sig] debuging
Bjorn Pettersen
BPettersen at NAREX.com
Tue Nov 19 22:28:01 CET 2002
> From: Mike Rovner [mailto:mike at bindkey.com]
>
> Howdy, folks.
>
> How do you debug BPL applications?
>
> I just got 'TypeError: bad argument type for built-in
> operation' and have absolutely no idea what to do next.
Sounds like you need to get the traceback... Here's how I do it (you'll
probably want to modify it to throw a more standard exception ;-)
-- bjorn
std::string getTraceback() {
std::string result;
PyObject *exception, *v, *traceback;
PyErr_Fetch(&exception, &v, &traceback);
PyErr_NormalizeException(&exception, &v, &traceback);
/*
import traceback
lst = traceback.format_exception(exception, v, traceback)
for line in lst:
result += line
*/
PyObject* tbstr = PyString_FromString("traceback");
PyObject* tbmod = PyImport_Import(tbstr);
if (!tbmod) throw new NException("Unable to import traceback
module. Is your Python installed?", N_EX_NULL);
PyObject* tbdict = PyModule_GetDict(tbmod);
PyObject* formatFunc = PyDict_GetItemString(tbdict,
"format_exception");
if (!formatFunc) throw new NException("Can't find
traceback.format_exception", N_EX_NULL);
if (!traceback) {
traceback = Py_None;
Py_INCREF(Py_None);
}
PyObject* args = Py_BuildValue("(OOO)", exception, v,
traceback);
PyObject* lst = PyObject_CallObject(formatFunc, args);
for (int i=0; i<PyList_GET_SIZE(lst); i++) {
result += PyString_AsString(PyList_GetItem(lst, i));
}
Py_DECREF(args);
Py_DECREF(lst);
return result;
}
More information about the Cplusplus-sig
mailing list