473,395 Members | 1,458 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,395 software developers and data experts.

Getting error message/trace over the C API

Hello group,

I'm writing a C++ based application that embeds the python engine. Now I
have a problem regarding exception/error information. Is there a way to
get the exception message and possibly the traceback into a string for
example? I've been eyeballing the PyErr_ module and it seems fairly
limited. In other words PyErr_Print() calls the right functions for
getting the exception information but unfortunately it is hardwired to
print this data directly into sys.stderr, and for an embedded application
this is completely inappropriate.

I have seen solutions that propose writing a custom Python class with
write method and using that to grab the output from sys.stderr and then
saving the data for example into a variable for later access, but unless
Im mistaken this solution (not only that it is extremely ugly) is not
thread safe. Even if a thread in my application is holding GIL, the lock
can be released by extensions/the interpreter and thus opens up a race
condition regarding that grabber object.

Please advice how to do this.

-sami
Oct 16 '07 #1
3 3281
Sami Vaisanen schrieb:
Hello group,

I'm writing a C++ based application that embeds the python engine. Now I
have a problem regarding exception/error information. Is there a way to
get the exception message and possibly the traceback into a string for
example? I've been eyeballing the PyErr_ module and it seems fairly
limited. In other words PyErr_Print() calls the right functions for
getting the exception information but unfortunately it is hardwired to
print this data directly into sys.stderr, and for an embedded application
this is completely inappropriate.

I have seen solutions that propose writing a custom Python class with
write method and using that to grab the output from sys.stderr and then
saving the data for example into a variable for later access, but unless
Im mistaken this solution (not only that it is extremely ugly) is not
thread safe. Even if a thread in my application is holding GIL, the lock
can be released by extensions/the interpreter and thus opens up a race
condition regarding that grabber object.
You should call 'import traceback; traceback.format_exc()' from your C code;
it returns a string (a 'PyObject *' that contains a string).

Thomas

Oct 16 '07 #2
Sami Vaisanen <en********@hotmail.comwrote:
Hello group,

I'm writing a C++ based application that embeds the python engine. Now I
have a problem regarding exception/error information. Is there a way to
get the exception message and possibly the traceback into a string for
example? I've been eyeballing the PyErr_ module and it seems fairly
limited. In other words PyErr_Print() calls the right functions for
getting the exception information but unfortunately it is hardwired to
print this data directly into sys.stderr, and for an embedded application
this is completely inappropriate.
Please advice how to do this.
All you have to do is call whatever functions you would call from Python.
e.g. from C you need to import traceback, then call getattr to get the
appropriate function (e.g. format_exc or format_exception), then just call
it.
Oct 16 '07 #3
On Tue, 16 Oct 2007 18:55:22 +0000, Duncan Booth wrote:
Sami Vaisanen <en********@hotmail.comwrote:
>Hello group,

I'm writing a C++ based application that embeds the python engine. Now I
have a problem regarding exception/error information. Is there a way to
get the exception message and possibly the traceback into a string for
example? I've been eyeballing the PyErr_ module and it seems fairly
limited. In other words PyErr_Print() calls the right functions for
getting the exception information but unfortunately it is hardwired to
print this data directly into sys.stderr, and for an embedded application
this is completely inappropriate.
Please advice how to do this.
All you have to do is call whatever functions you would call from Python.
e.g. from C you need to import traceback, then call getattr to get the
appropriate function (e.g. format_exc or format_exception), then just call
it.


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";
}

str evaluates to "None", any ideas what gives here? I've also tried to
call the traceback with a different function, such as
PyObject_CallFunctionObjArgs but the result is still same.

Thanks

Oct 18 '07 #4

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

Similar topics

3
by: Jas Shultz | last post by:
I'm using Win2K3 Enterprise edition with the latest .NET framework installed. I have this problem with getting "out of disk space" errors. It doesn't happen all the time but it does happen. When...
10
by: Brian Conway | last post by:
I have no idea what is going on. I have a Login screen where someone types in their login information and this populates a datagrid based off of the login. Works great in debug and test through...
0
by: Jas Shultz | last post by:
I'm using Win2K3 Enterprise edition with the latest .NET framework installed. I have this problem with getting "out of disk space" errors. It doesn't happen all the time but it does happen. When...
4
by: tommy | last post by:
hello everbody, i write a little asp-application with forms-authentication. i copy my aspx-files with web.config to my webspace and i get the error above... i tried to set the...
3
by: Sean | last post by:
HI There, I am having trouble deploying my .aspx pages to a remote server, I have made changes to the config file and it still returns an error. I have also contacted the server administrator to...
8
by: pmud | last post by:
Hi, I am using a compare validator in asp.net application(c# code). This Custom validator is used for comparing a value enterd by the user against the primary key in the SQL database. IF the...
1
by: Ramanfromoz | last post by:
Hi, Developing a new we application. Everything okay on my local WIN XP PROFESSIONAL, IIS 5.0 running locally. The website is running smoothly. Now, the same code I am copying over to a...
1
by: thangchan | last post by:
Hi all, i am getting SQL update problem. as below ======================error messages ======================= Server Error in '/CMS' Application....
4
by: imaloner | last post by:
I am posting two threads because I have two different problems, but both have the same background information. Common Background Information: I am trying to rebuild code for a working,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.