By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,033 Members | 1,952 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,033 IT Pros & Developers. It's quick & easy.

traceback from embedded python

P: n/a
hi,

I'm using boost::python for calling some python code and when the
exception is thrown I want to get type, value and traceback of it.

The problem is with traceback: I got only *one* frame (the first one)

// this always returns Py_None
tb_frame = PyObject_GetAttrString(tb_frame, "f_back");

What I'm doing wrong?

void
Deployer::showError() {
// PyErr_Print();

PyObject *exc_type, *exc_value, *exc_traceback, *pystring;

PyErr_Fetch(&exc_type, &exc_value, &exc_traceback);

// type works
pystring = PyObject_Str(exc_type);
char *type = QString(python::extract<char*>(pystring));
Py_XDECREF(pystring);

// value works
pystring = PyObject_Str(exc_value);
char *value = QString(python::extract<char*>(pystring));
Py_XDECREF(pystring);

// traceback does't work ;(
PyObject *tb_frame = PyObject_GetAttrString(exc_traceback, "tb_frame");
while (tb_frame != Py_None) {
PyObject *f_lineno = PyObject_GetAttrString(tb_frame, "f_lineno");
int lineno = python::extract<int>(f_lineno);
Py_XDECREF(f_lineno);

PyObject *f_code = PyObject_GetAttrString(tb_frame, "f_code");
PyObject *co_filename = PyObject_GetAttrString(f_code,
"co_filename");
char *filename = python::extract<char*>(co_filename);
Py_XDECREF(f_code);
Py_XDECREF(co_filename);

PyObject *tmp = tb_frame;
// PyObject_Print(tb_frame, stderr, Py_PRINT_RAW);
// fprintf(stderr, "\n");
tb_frame = PyObject_GetAttrString(tb_frame, "f_back");
// PyObject_Print(tb_frame, stderr, Py_PRINT_RAW);
// fprintf(stderr, "\n");
Py_XDECREF(tmp);
}

Py_XDECREF(exc_type);
Py_XDECREF(exc_value);
Py_XDECREF(exc_traceback);
}
thanks,
Skink
Sep 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.