boost::python Export Custom Exception

后端 未结 4 1211
天涯浪人
天涯浪人 2020-12-13 03:24

I am currently writing a C++ extension for Python using Boost.Python. A function in this extension may generate an exception containing information about the error (beyond

4条回答
  •  生来不讨喜
    2020-12-13 03:45

    Thanks to variadic templates and generalized lambda capture, we can collapse Jack Edmond's answer into something much more manageable and hide all of the cruft from the user:

    template 
    py::class_ exception_(Args&&... args) {
        py::class_ cls(std::forward(args)...);
        py::register_exception_translator([ptr=cls.ptr()](E const& e){
            PyErr_SetObject(ptr, py::object(e).ptr());
        });
        return cls;
    }
    

    To expose MyCPPException as an exception, you just need to change py::class_ in the bindings to exception_:

    exception_("MyCPPException", py::init())
        .add_property("message", &MyCPPException::getMessage)
        .add_property("extra_data", &MyCPPException::getExtraData)
    ;
    

    And now we're back to the niceties of Boost.Python: don't need to name the class_ instance, don't need this extra PyObject*, and don't need an extra function somewhere.

提交回复
热议问题