Pybind11: передача строкового аргумента конструктору - PullRequest
0 голосов
/ 10 мая 2018

В библиотеке C ++, которую я не могу изменять, у меня есть конструктор, который выглядит следующим образом:

Dfa(const int n_state, const int dim_alf, const string *alf);

Если я просто свяжусь с

.def(py::init<const int, const int, const std::string*>())

успешно компилируется. Проблема в том, что я не могу передать строку * через python, потому что, например, если я пытаюсь выполнить на python

alph=['x','y']
z=Dfa(3,2,alph)

Возвращает следующую ошибку:

TypeError: __init__(): incompatible constructor arguments. The
following argument types are supported:
gi_gipy.Dfa(arg0: int, arg1: int, arg2: unicode)

Пользователь "R zu" любезно предложил мне написать обертку, но я не могу понять, как. Учитывая то, что в Python это что-то вроде: ['x','y'], в с ++ принимается как std::list<std::string> Я попытался написать следующий код:

.def(py::init([](int n_state,int dim_alf, std::list<std::string> alph){
         std::string* alfabeto=new std::string[dim_alf];
         std::list<string>::iterator it=alph.begin();
         for(int i=0;it!=alph.end();++i,++it)  alfabeto[i]=*it;
         Dfa::Dfa(n_state,dim_alf,alfabeto);
}))

но он возвращает мне 2 ошибки:

cannot pass expression of type 'void' to variadic function
construct<Class>(v_h, func(std::forward<Args>(args)...)

и

static_assert failed "pybind11::init(): init function must return a compatible pointer,
  holder, or value"
static_assert(!std::is_same<Class, Class>::value /* always false */

Ясно, что я немного запутался в том, как преодолеть эту проблему, и думаю, что это связано с использованием указателя на строку в качестве параметра для конструктора. Повторяю, я не могу изменить библиотеку, я могу только создать соответствующую привязку. Спасибо за внимание

1 Ответ

0 голосов
/ 12 мая 2018

main.cpp:

#include <iostream>
#include <list>
#include "pybind11/pybind11.h"
#include <pybind11/stl.h>
namespace py = pybind11;

class Dfa{
public:
    Dfa(const int n_state, const std::size_t size, const std::string* alpha)
            : alpha_(*alpha) {
        std::cout << "n_state: " << n_state << "\n";
        std::cout << "size: " << size << "\n";
        std::cout << "*alpha: " << *alpha << "\n";
    }
    // copy the std::string, not the reference or pointer.
    std::string alpha_; 
};

Dfa make_dfa(int n_state, std::string alpha) {
    // Copies the python unicode str to a local std::string
    // Modifying the local copy won't change the python
    // str.
    return Dfa(n_state, alpha.size(), &alpha);
    // Problem: Once the program leaves this function,
    // This local std::string is destroyed.
    // If the Dfa class just copies the pointer to this
    // std::string instead of the std::string, the Dfa
    // class will use a destroyed string.
    // Unless the Dfa object copies the string, this will
    // cause big trouble.
}

void print_char_list(std::list<char> alpha) {
    for (auto c: alpha) std::cout << c << ", ";
    std::cout << "\n";
    std::cout << "length of list is: " << alpha.size() << "\n";
}

PYBIND11_MODULE(_cpp, m) {
    py::class_<Dfa>(m, "Dfa")
            .def_readwrite("alpha", &Dfa::alpha_);;
    m.def("make_dfa", &make_dfa, "Create a Dfa object");
    m.def("print_char_list", &print_char_list, "Print a list of chars");
}

CMakeLists.txt:

cmake_minimum_required(VERSION 3.9)
project(test_pybind11)

set(CMAKE_CXX_STANDARD 11)

# Find packages.
set(PYTHON_VERSION 3)
find_package( PythonInterp ${PYTHON_VERSION} REQUIRED )
find_package( PythonLibs ${PYTHON_VERSION} REQUIRED )

# Download pybind11
set(pybind11_url https://github.com/pybind/pybind11/archive/stable.zip)

set(downloaded_file ${CMAKE_BINARY_DIR}/pybind11-stable.zip)
file(DOWNLOAD ${pybind11_url} ${downloaded_file})
execute_process(COMMAND ${CMAKE_COMMAND} -E tar xzf ${downloaded_file}
        SHOW_PROGRESS)
file(REMOVE ${downloaded_file})

set(pybind11_dir ${CMAKE_BINARY_DIR}/pybind11-stable)
add_subdirectory(${pybind11_dir})
include_directories(${pybind11_dir}/include)

# Make python module
pybind11_add_module(_cpp main.cpp)

Тест Python 3:

>>> import _cpp
>>> s = "xyz"
>>> d = _cpp.make_dfa(1, s)
n_state: 1
size: 3
*alpha: xyz
>>> print(d.alpha)
xyz
>>> d.alpha = "abc"
>>> d.alpha
'abc'
>>> _cpp.print_char_list(['x', 'y', 'z'])
x, y, z, 
length of list is: 3
...