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

traceback over C API - still not working...

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 handling?
Sounds like da bomb!

The API should have two levels of error handling. First level should
expose a conventional error code mechanism for checking for bad params
etc. Only if the error code says that there was a python error during
evaluation/compilation should one need to use this mechanism.

Just my two cents.

Thanks for help.

void get_python_error_info(string& info)
{
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)
{
PyErr_Clear();
return;
}
PyObject* list = NULL;
if (trace)
{
py_ref fun(PyObject_GetAttrString(module.get(), "format_exception"));
if (fun)
list = PyObject_CallFunctionObjArgs(type, value, trace, NULL);
PyErr_Clear();
}
else
{
py_ref fun(PyObject_GetAttrString(module.get(), "format_exception_only"));
if (fun)
list = PyObject_CallFunctionObjArgs(type, value, NULL);
PyErr_Clear();
}
if (list && PySequence_Check(list))
{
Py_ssize_t len = PySequence_Size(list);

for (Py_ssize_t i=0; i<len; ++i)
{
// bla bla
}
}
Py_XDECREF(list);
PyErr_Clear();
}


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 20 '07 #1
1 1989
En Sat, 20 Oct 2007 14:47:52 -0300, Sami Vaisanen <en********@hotmail.com>
escribi�:
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?
It seems very unlikely, since the traceback module is written in Python.
It clearly returns a list.
py_ref fun(PyObject_GetAttrString(module.get(),
"format_exception"));
if (fun)
list = PyObject_CallFunctionObjArgs(type, value, trace,
NULL);
Of course, you get a list if you actually call the function...

--
Gabriel Genellina

Oct 20 '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):...
5
by: Thomas Guettler | last post by:
Hi! Has someone written a script which dumps a traceback in a more verbose style? This should include the information you see with cgitb. I like this very much. You see the values of the...
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...
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...
0
by: Echo | last post by:
I have been working on handling unhanded exceptions and making a detailed print out of the traceback after the exception. I found that traceback.extract_tb worked nice and was quite simple. ...
1
by: metaperl.etc | last post by:
In this traceback, the path to 3 different SQL Alchemy source files is a relative directory. However, no such directory is below my current working directory. This is problematic for two...
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.