[C++-sig] problems with embedded python and pure virtual functions

Stefan Seefeld seefeld at sympatico.ca
Thu Jan 4 14:47:12 CET 2007


Birgir Sigurjonsson wrote:
> Hi, I am having problem calling a pure virtual function that
> is overritten in Python. My apology, if this post is to long
> but I am not sure how to formulate my question nor do I know
> what part of my code is needed to for you all to help me.
> 
> I can create my AbstractLoader and call its load function.
> But then load calls load_impl I get Abort in the console.
> 
> Therfor I think that there is something wrong with my
> wrapper and or the auto_ptr.
> 
> I have stepped through the code in debugger and here is the call stack:
> 
> ZN5boost6python23throw_error_already_setEv,                              FP=7fbfffe960

It appears the python runtime raises an exception at some point that you don't
handle, resulting in it getting propagated as a C++ exception of type
boost::python::error_already_set up to the toplevel, leading to the abort.

To handle it, wrap your function that runs the embedded python script
into something similar to this (warning: completely untested !) :

using boost::python;

void handle_error()
{
  PyObject* type, *value, *traceback;
  PyErr_Fetch(&type, &value, &traceback);
  handle<> ty(type), v(value), tr(traceback);
  str format("exception type: %sn");
  format += "exception value: %sn";
  format += "traceback:n%s" ;
  str msg = format % make_tuple(ty, v, tr);
  std::cerr << extract<char const *>(msg) << std::endl;
}

try {...}
catch (boost::python::error_already_set const &)
{
  handle_error();
}

That may give you a clue about what goes wrong.

HTH,
		Stefan

-- 

      ...ich hab' noch einen Koffer in Berlin...



More information about the Cplusplus-sig mailing list