473,811 Members | 3,026 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

traceback over C API and PyObject_CallOb ject

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_CallFu nctionObjArgs but the result is the same.

Thanks

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

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

PyErr_Fetch(&ty pe, &value, &trace);

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

py_ref name(PyString_F romString("trac eback"));
py_ref module(PyImport _Import(name.ge t()));
if (module)
{
py_ref fun(PyObject_Ge tAttrString(mod ule.get(), "format_exc "));
if (fun)
{
PyErr_Restore(t ype, value, trace);
ref_type.releas e();
ref_value.relea se();
ref_trace.relea se();

py_ref ret(PyObject_Ca llObject(fun.ge t(), NULL));
if (ret && PyString_Check( ret.get()))
{
char* str = PyString_AsStri ng(ret.get());
message = str;
traceback = "traceback not available";
return;
}
}
}
message = "message not available";
traceback = "traceback not available";
}
Oct 19 '07 #1
1 4367
En Fri, 19 Oct 2007 13:53:03 -0300, Sami Vaisanen <en********@hot mail.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_CallFu nctionObjArgs but the result is the same.
Since you already know the three components (type, value, trace), I'd use
traceback.forma t_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_CallFu nctionObjArgs (remember the final NULL). Beware:
format_exceptio n 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_exce ption(string& message, string& traceback)
{
GIL g;
PyObject* type = NULL;
PyObject* value = NULL;
PyObject* trace = NULL;
PyErr_Fetch(&ty pe, &value, &trace);
py_ref ref_type(type);
py_ref ref_value(value );
py_ref ref_trace(trace );
py_ref name(PyString_F romString("trac eback"));
py_ref module(PyImport _Import(name.ge t()));
if (module)
{
py_ref fun(PyObject_Ge tAttrString(mod ule.get(), "format_exc "));
if (fun)
{
PyErr_Restore(t ype, value, trace);
ref_type.releas e();
ref_value.relea se();
ref_trace.relea se();
py_ref ret(PyObject_Ca llObject(fun.ge t(), NULL));
if (ret && PyString_Check( ret.get()))
{
char* str = PyString_AsStri ng(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
2052
by: Oliver Walczak | last post by:
This seems to be a quite difficult approach. Try this: ##################################################################### import traceback class MyTraceback: def __init__(self): self.clear() def clear(self): self.s = '' def write(self, s):
7
2237
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 'traceback\n',''.join(traceback.format_exception(tbt,tbv,tb))
1
2276
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 some methods to use on it. >From what I can tell the traceback that's passed in works differently from the module traceback. I just need to get the traceback to log it. I can use type and value,
0
2823
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
2563
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 source of both methods. One uses f_lineno (wrong)
5
3667
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 systems there might not be any command window or terminal window to show the traceback messages in). Do I want to do something like override the print_exc (or format_exc?) method of traceback to get the text of the message and call my dialog box...
8
2006
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 interactive interpreter does, but only show the part of the traceback relating to the code sent to exec. For example here is the code I'm using: try: exec code
1
2022
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 PySequence_List() gives me back -1. So wtf? I must say the API is crap on this part. Im trying to get error information regarding previous error and if all i get back is another error indicator, then what am I supposed to do? Recursive error...
2
1971
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 simple piece of code that I typed in to test this fact: --------------------------------------------------------------------------- import sys
0
9727
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10647
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10386
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10398
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7669
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4339
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3017
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.