1

I have such function in C++:

typedef boost::function<boost::shared_ptr<Object> (CL_DomElement*, std::string& desc)> Parser;
void registerParser(std::string type, Parser p);

// Later: exporting into python-module:
BOOST_PYTHON_MODULE(TypesManager)
{
    bp::def("RegisterParser", registerParser);
}

# Python code:
class TestObj(Object):
    @staticmethod
    def ParseTestObj(node, desc):
        pass

RegisterParser("test_obj", TestObj.ParseTestObj)

Object in python-code is exported class which is used in typedef (from c++ code).

Boost.Python.ArgumentError: Python argument types in
    RegisterParser(str, function)
did not match C++ signature:
    RegisterParser(TypesManager {lvalue}, std::string, boost::function<boost::shared_ptr<Object> ()(CL_DomElement*, std::string&)>)

What I do wrong?

1 Answer 1

1

I don't believe Boost Python understands how to convert a python function to a boost::function object. What I would suggest is to use a proxy to take the python callable object and mimic the C++ interface. Quick example mock-up (untested, of course):

typedef boost::function<boost::shared_ptr<Object> (CL_DomElement*, std::string& desc)> Parser;
void registerParser(std::string type, Parser p);

struct ParserProxy
{
    bp::object callable;

    ParserProxy(bp::object callable)
    : callable(callable)
    { }

    boost::shared_ptr<Object> operator()(CL_DomElement* elem, std::string& desc)
    {
        bp::object obj = callable(elem, desc);
        return bp::extract<boost::shared_ptr<Object> >(obj);
    }
};

void registerParserByProxy(std::string type, bp::object callable)
{
    registerParser(type, ParserProxy(callable));
}

// Later: exporting into python-module:
BOOST_PYTHON_MODULE(TypesManager)
{
        bp::def("RegisterParser", registerParserByProxy);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.