-1

I have a requirement to create, but not throw, a python RuntimeError.

I'm using Boost python, the code so far:

static boost::python::object builtins = boost::python::import("builtins");                    
static boost::python::object object = boost::python::extract<boost::python::object>(builtins.attr("RuntimeError"));

PyObject* pyobj = PyObject_CallMethod(object.ptr(), "__init__", "s", "Test error.");

My understanding is that the second statement gives me the class. And them I'm attempting to call the string constructor on that class in the third statement.

That fails, with nullptr returned.

Any ideas?

P45 Imminent
  • 8,319
  • 4
  • 35
  • 78

1 Answers1

0

I figured it out! You need to use PyObject_CallObject, which as far as I can tell, doesn't have a Boost wrapper.

For a std::string message,

static boost::python::object builtins = boost::python::import("builtins");
static boost::python::object cls = boost::python::extract<boost::python::object>(builtins.attr("RuntimeError"));
boost::python::object args = boost::python::make_tuple(message);
PyObject* pyobj = PyObject_CallObject(cls.ptr(), args.ptr());                    
return boost::python::object(boost::python::handle<>(pyobj));
P45 Imminent
  • 8,319
  • 4
  • 35
  • 78