473,385 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

traceback over C API and PyObject_CallObject

Hello group,

I'm trying to get the Python exception information (message and traceback)
stored into a string in my C++ code. However all i get back is the string
"None". All the checks pass and all pointers get a value from the python
API calls. I've also tried with a different function such as
PyObject_CallFunctionObjArgs but the result is the same.

Thanks

void get_python_exception(string& message, string& traceback)
{
GIL g;

PyObject* type = NULL;
PyObject* value = NULL;
PyObject* trace = NULL;

PyErr_Fetch(&type, &value, &trace);

py_ref ref_type(type);
py_ref ref_value(value);
py_ref ref_trace(trace);

py_ref name(PyString_FromString("traceback"));
py_ref module(PyImport_Import(name.get()));
if (module)
{
py_ref fun(PyObject_GetAttrString(module.get(), "format_exc"));
if (fun)
{
PyErr_Restore(type, value, trace);
ref_type.release();
ref_value.release();
ref_trace.release();

py_ref ret(PyObject_CallObject(fun.get(), NULL));
if (ret && PyString_Check(ret.get()))
{
char* str = PyString_AsString(ret.get());
message = str;
traceback = "traceback not available";
return;
}
}
}
message = "message not available";
traceback = "traceback not available";
}
Oct 19 '07 #1
1 4326
En Fri, 19 Oct 2007 13:53:03 -0300, Sami Vaisanen <en********@hotmail.com>
escribió:
Hello group,

I'm trying to get the Python exception information (message and
traceback)
stored into a string in my C++ code. However all i get back is the string
"None".
This is what you get (actually "None\n") when there is no error set.
All the checks pass and all pointers get a value from the python
API calls. I've also tried with a different function such as
PyObject_CallFunctionObjArgs but the result is the same.
Since you already know the three components (type, value, trace), I'd use
traceback.format_exception instead (and remove the PyErr_Restore call -
I'm unsure if it works the way you expect it).
In this case you have to pass three arguments, so yes, use
PyObject_CallFunctionObjArgs (remember the final NULL). Beware:
format_exception returns a formatted list, not a string. You have to
concatenate all the elements (either using ''.join or repeteadly calling
PyString_Concat)
void get_python_exception(string& message, string& traceback)
{
GIL g;
PyObject* type = NULL;
PyObject* value = NULL;
PyObject* trace = NULL;
PyErr_Fetch(&type, &value, &trace);
py_ref ref_type(type);
py_ref ref_value(value);
py_ref ref_trace(trace);
py_ref name(PyString_FromString("traceback"));
py_ref module(PyImport_Import(name.get()));
if (module)
{
py_ref fun(PyObject_GetAttrString(module.get(), "format_exc"));
if (fun)
{
PyErr_Restore(type, value, trace);
ref_type.release();
ref_value.release();
ref_trace.release();
py_ref ret(PyObject_CallObject(fun.get(), NULL));
if (ret && PyString_Check(ret.get()))
{
char* str = PyString_AsString(ret.get());
message = str;
traceback = "traceback not available";
return;
}
}
}
message = "message not available";
traceback = "traceback not available";
}

--
Gabriel Genellina

Oct 19 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Oliver Walczak | last post by:
This seems to be a quite difficult approach. Try this: ##################################################################### import traceback class MyTraceback: def __init__(self):...
7
by: Robin Becker | last post by:
def raise_an_error(): a = 3 b = 4 c = 0 try: a = a/c except: import sys, cgitb, traceback, inspect tbt,tbv,tb = sys.exc_info() print...
1
by: Josh Close | last post by:
When sys.excepthook is called, type, value and traceback are passed into it. How do I get the values of traceback? I've tried printing it, but I get the memory location. I'm gussing there are...
0
by: John Smith | last post by:
Greetings All, I have something similiar to this: Foo.py class Foo(object): def function(self): pass Bar.py
1
by: Thomas Guettler | last post by:
Hi, the line numbers of inspect.getinnerframes are different from traceback.format_exception. This results in wrong lines being shown in cgitb. An example is below. I looked at the...
5
by: Bob Greschke | last post by:
I want to cause any traceback output from my applications to show up in one of my dialog boxes, instead of in the command or terminal window (between running on Solaris, Linux, OSX and Windows...
8
by: gregpinero | last post by:
I'm running code via the "exec in context" statement within a much larger program. What I would like to do is capture any possible errors and show a pretty traceback just like the Python...
1
by: Sami Vaisanen | last post by:
This is becoming utterly painful process.... I found out that the return value from "format_exception" function is NOT a list, i.e. PyList_Check() fails. PySequence_Check() succeeds but then...
2
by: Sami | last post by:
Hello, In the Python book that I am using to learn the language it says that the traceback.print_exc() can be used to stop exception propagation and make the program keep running. Here is a...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.