Re: traceback over C API and PyObject_CallObject

2007-10-19 Thread Gabriel Genellina
En Fri, 19 Oct 2007 13:53:03 -0300, Sami Vaisanen [EMAIL PROTECTED]  
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

-- 
http://mail.python.org/mailman/listinfo/python-list


traceback over C API and PyObject_CallObject

2007-10-19 Thread Sami Vaisanen
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;
}


-- 
http://mail.python.org/mailman/listinfo/python-list