|
This post has NOT been accepted by the mailing list yet.
Ok... I found how to deal with this situation. For those who read these lines and who are as lost as I was, here is a way to handle errors in this situation (adapted from the embedding.cpp boost example file). First I define this function (which I find convinient):
bool handled_exec(str code, object globals = object(), object locals = object())
{
return handle_exception(boost::bind(exec, code, globals, locals));
}
And then the following code can be used (no need to try/catch):
// Retrieve the main module
object main = import("__main__");
// Retrieve the main module's namespace
object global(main.attr("__dict__"));
string somePythonCode("3 * 4");
if(handled_exec(somePythonCode.c_str(), global, global))
{
if (PyErr_Occurred())
{
PyErr_Print();
}
else
{
BOOST_ERROR("A C++ exception was thrown for which "
"there was no exception translator registered.");
}
}
Happy coding,
Christian
|