Отправка функции Python в качестве аргумента Boost.Function

В моем мире все усложняется попыткой связать код Python с моим C++.

По сути, я хочу иметь возможность назначить функцию обратного вызова, которая будет использоваться после того, как HTTP-вызов получит ответ, и я хочу быть в состоянии сделать это из C++ или Python.

Другими словами, я хочу иметь возможность вызывать это из C++:

http.get_asyc("www.google.ca", [&](int a) { std::cout << "response recieved: " << a << std::endl; });

и это из Python:

def f(r):
    print str.format('response recieved: {}', r)

http.get_async('www.google.ca', f)

Я создал демо на Coliru, которое показывает именно то, что я пытаюсь достичь. Вот код и ошибка, которую я получаю:

C++

#include <boost/python.hpp>
#include <boost/function.hpp>

struct http_manager
{
    void get_async(std::string url, boost::function<void(int)> on_response)
    {
        if (on_response)
        {
            on_response(42);
        }
    }
} http;

BOOST_PYTHON_MODULE(example)
{
    boost::python::class_<http_manager>("HttpManager", boost::python::no_init)
        .def("get_async", &http_manager::get_async);

    boost::python::scope().attr("http") = boost::ref(http);
}

питон

import example
def f(r):
    print r
example.http.get_async('www.google.ca', f)

ошибка

Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
Boost.Python.ArgumentError: Python argument types in
    HttpManager.get_async(HttpManager, str, function)
did not match C++ signature:
    get_async(http_manager {lvalue}, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, boost::function<void (int)>)

Я не уверен, почему function не превращается в boost::function автоматически.

Я задавал неопределенно похожий вопрос о SO раньше и получил удивительный ответ. Мне также интересно, можно ли применить подобный метод в ответе, приведенном там, к этому варианту использования.

Большое спасибо за любую поддержку!

2 ответа

Решение

Когда вызывается функция, которая была открыта через Boost.Python, Boost.Python запросит свой реестр, чтобы найти подходящий конвертер from-Python для каждого аргумента вызывающей стороны на основе требуемого типа C++. Если найден конвертер, который знает, как преобразовать объект Python в объект C++, он будет использовать конвертер для создания объекта C++. Если подходящих конвертеров не найдено, Boost.Python вызовет ArgumentError исключение.

Преобразователи from-Python зарегистрированы:

  • автоматически для типов, поддерживаемых Boost.Python, таких как int а также std::string
  • неявно для типов, представленных boost::python::class<T>, По умолчанию результирующий класс Python будет содержать встроенный экземпляр T C++ объект, и зарегистрируйте to-Python и from-Python преобразователи для класса и типа Python T, используя встроенный экземпляр.
  • явно через boost::python::converter::registry::push_back()

Этапы тестирования на конвертируемость и конструирование объекта выполняются в два отдельных этапа. Поскольку конвертер from-Python не был зарегистрирован для boost::function<void(int)>, Boost.Python поднимет ArgumentError исключение. Boost.Python не будет пытаться создать boost::function<void(int)> объект, несмотря на boost::function<void(int)> быть конструируемым из boost::python::object,


Чтобы решить эту проблему, рассмотрите возможность использования функции shim для отсрочки создания boost::function<void(int)> пока после boost::python::object прошел через слой Boost.Python:

void http_manager_get_async_aux(
  http_manager& self, std::string url, boost::python::object on_response)
{
  return self.get_async(url, on_response);
}

...

BOOST_PYTHON_MODULE(example)
{
  namespace python = boost::python;
  python::class_<http_manager>("HttpManager", python::no_init)
    .def("get_async", &http_manager_get_async_aux);

  ...
}

Вот полный пример, демонстрирующий этот подход:

#include <boost/python.hpp>
#include <boost/function.hpp>

struct http_manager
{
  void get_async(std::string url, boost::function<void(int)> on_response)
  {
    if (on_response)
    {
      on_response(42);
    }
  }
} http;

void http_manager_get_async_aux(
  http_manager& self, std::string url, boost::python::object on_response)
{
  return self.get_async(url, on_response);
}

BOOST_PYTHON_MODULE(example)
{
  namespace python = boost::python;
  python::class_<http_manager>("HttpManager", python::no_init)
    .def("get_async", &http_manager_get_async_aux);

  python::scope().attr("http") = boost::ref(http);
}

Интерактивное использование:

>>> import example
>>> result = 0
>>> def f(r):
...     global result
...     result = r
...
>>> assert(result == 0)
>>> example.http.get_async('www.google.com', f)
>>> assert(result == 42)
>>> try:
...     example.http.get_async('www.google.com', 42)
...     assert(False)
... except TypeError:
...    pass
...

Альтернативный подход заключается в явной регистрации конвертера from-Python для boost::function<void(int)>, Преимущество этого заключается в том, что все функции, предоставляемые через Boost.Python, могут использовать конвертер (например, для каждой функции не нужно писать прокладку). Однако для каждого типа C++ необходимо зарегистрировать конвертирование. Вот пример, демонстрирующий явную регистрацию пользовательского конвертера для boost::function<void(int)> а также boost::function<void(std::string)>:

#include <boost/python.hpp>
#include <boost/function.hpp>

struct http_manager
{
  void get_async(std::string url, boost::function<void(int)> on_response)
  {
    if (on_response)
    {
      on_response(42);
    }
  }
} http;

/// @brief Type that allows for registration of conversions from
///        python iterable types.
struct function_converter
{
  /// @note Registers converter from a python callable type to the
  ///       provided type.
  template <typename FunctionSig>
  function_converter&
  from_python()
  {
    boost::python::converter::registry::push_back(
      &function_converter::convertible,
      &function_converter::construct<FunctionSig>,
      boost::python::type_id<boost::function<FunctionSig>>());

    // Support chaining.
    return *this;
  }

  /// @brief Check if PyObject is callable.
  static void* convertible(PyObject* object)
  {
    return PyCallable_Check(object) ? object : NULL;
  }

  /// @brief Convert callable PyObject to a C++ boost::function.
  template <typename FunctionSig>
  static void construct(
    PyObject* object,
    boost::python::converter::rvalue_from_python_stage1_data* data)
  {
    namespace python = boost::python;
    // Object is a borrowed reference, so create a handle indicting it is
    // borrowed for proper reference counting.
    python::handle<> handle(python::borrowed(object));

    // Obtain a handle to the memory block that the converter has allocated
    // for the C++ type.
    typedef boost::function<FunctionSig> functor_type;
    typedef python::converter::rvalue_from_python_storage<functor_type>
                                                                storage_type;
    void* storage = reinterpret_cast<storage_type*>(data)->storage.bytes;

    // Allocate the C++ type into the converter's memory block, and assign
    // its handle to the converter's convertible variable.
    new (storage) functor_type(python::object(handle));
    data->convertible = storage;
  }
};

BOOST_PYTHON_MODULE(example)
{
  namespace python = boost::python;
  python::class_<http_manager>("HttpManager", python::no_init)
    .def("get_async", &http_manager::get_async);

  python::scope().attr("http") = boost::ref(http);

  // Enable conversions for boost::function.
  function_converter()
    .from_python<void(int)>()
    // Chaining is supported, so the following would enable
    // another conversion.
    .from_python<void(std::string)>()
    ;
}

Одним из решений является добавление функции перегрузки:

void get_async(std::string url, boost::python::object obj)
{
    if (PyCallable_Check(obj.ptr()))
        get_async(url, static_cast<boost::function<void(int)>>(obj));
}

Затем выставьте только эту специфическую перегрузку:

.def("get_async", static_cast<void (http_manager::*)(std::string, boost::python::object)>(&http_manager::get_async))

Или, если вы не хотите загрязнять свой основной класс Python, тогда вы можете создать класс-оболочку. Вещи выглядят намного чище тогда тоже:

struct http_manager_wrapper : http_manager
{
    void get_async(std::string url, boost::python::object obj)
    {
        if (PyCallable_Check(obj.ptr()))
            http_manager::get_async(url, obj);
    }

} http_wrapper;

BOOST_PYTHON_MODULE(example)
{
    boost::python::class_<http_manager_wrapper>("HttpManager", boost::python::no_init)
        .def("get_async", &http_manager_wrapper::get_async);

    boost::python::scope().attr("http") = boost::ref(http_wrapper);
}

Обновление: Другой вариант - использовать вызываемый Python для повышения конвертора функций. Это решит проблему синглтона и не потребует изменений в основном классе.

struct http_manager
{
    void get_async(std::string url, boost::function<void(int)> on_response)
    {
        if (on_response)
        {
            on_response(42);
        }
    }
} http;

struct BoostFunc_from_Python_Callable
{
    BoostFunc_from_Python_Callable()
    {
        boost::python::converter::registry::push_back(&convertible, &construct, boost::python::type_id< boost::function< void(int) > >());
    }

    static void* convertible(PyObject* obj_ptr)
    {
        if (!PyCallable_Check(obj_ptr)) 
            return 0;
        return obj_ptr;
    }

    static void construct(PyObject* obj_ptr, boost::python::converter::rvalue_from_python_stage1_data* data)
    {
        boost::python::object callable(boost::python::handle<>(boost::python::borrowed(obj_ptr)));
        void* storage = ((boost::python::converter::rvalue_from_python_storage< boost::function< void(int) > >*) data)->storage.bytes;
        new (storage)boost::function< void(int) >(callable);
        data->convertible = storage;
    }
};

BOOST_PYTHON_MODULE(example)
{
    // Register function converter
    BoostFunc_from_Python_Callable();

    boost::python::class_<http_manager>("HttpManager", boost::python::no_init)
        .def("get_async", &http_manager::get_async);

    boost::python::scope().attr("http") = boost::ref(http);
}
Другие вопросы по тегам