boost-python纯虚拟检测缺少的实现

时间:2019-02-07 17:54:47

标签: c++ boost-python pure-virtual

我有一个C ++类,带有使用boost-python暴露给python的纯虚拟方法。我正在从C ++调用虚拟函数,并假设该虚拟函数是在python中实现的。如果实现了该功能,一切都会正常,但如果不是,我会收到一个令人讨厌的异常。

我试图找到一种方法来检测该方法是否在加载类时实际上在不调用的情况下实现了

大致代码如下:

#include <boost/python.hpp>

using namespace boost::python;

public Foo {
public:
   void func() = 0;
}

class PyFoo : public Foo, public boost::python::wrapper<Foo> {
public:
    void func() override {
        get_override("func")();
    }
};

BOOST_PYTHON_MODULE(example)
{
    using namespace boost::python;
    class_<PyFoo>, boost::noncopyable>("Foo")
        .def("func", pure_virtual(&PyFoo::func))
        ;
}

void create {
    object main_module = import("__main__");
    object main_namespace = main_module.attr("__dict__");

    std::string overrideCommand(
    R"(
    import example

    class MyFoo(example.Foo):
        def __init__(self):
            example.Foo.__init__(self)

    # virtual function in C++. (Should be defined)
    #    def func(self):
    #        print('func called')
    )");

    boost::python::exec(overrideCommand.c_str(), main_namespace);
    result = eval("MyFoo()", main_namespace);

    // Can I detect if 'result' has func implemented? If I call it and it
    // is not defined death results. I have tried:
    object attr = result.attr("func");

    // but attr always seems to be set even if there is no function,
    // I think from the base class Foo.

    // This is the call:
    Foo& t = extract<Foo&>(result);
    t.func();
}

2 个答案:

答案 0 :(得分:0)

您可以使用PyCallable_Check

if (!PyCallable_Check(result.func()))
{
    PyErr_SetString(PyExc_TypeError, error_msg.str().c_str());
    python::throw_error_already_set();
}

答案 1 :(得分:0)

我找到了解决方案。可行不优雅。我添加了方法:

bool isThere()  {
    auto obj = get_override("func");
    return PyCallable_Check(obj.ptr());
}

对FooPy。然后:

FooPy& t = extract<FooPy&>(result);
t.isThere();