pybind11:Python 到 C++ 数据类型转换不起作用

pybind11: Python to C++ Datatype Conversion Not Working

本文关键字:数据 类型转换 不起作用 C++ Python pybind11      更新时间:2023-10-16

>问题

我正在尝试转换列表列表,该列表由C++代码中调用的python函数返回。尽管pybind11库允许从 python 数据类型转换为C++数据类型的类型,但我尝试将 python 返回的列表列表转换为std::listC++字符串的std::list,每次都失败。

法典

下面是 python 函数(该函数返回包含字符串值的列表列表):

def return_sheet(self):
"""Returns the sheet in a list of lists
"""
dataTable = []
for r in range(self._isheet.nrows):
datalist = []
for c in range(self._isheet.ncols):
datalist.append(self._isheet.cell_value(r,c))
dataTable.append(datalist)
return dataTable

在这里,我使用pybind11在C++中调用它:

py::list obj = _tool.attr("return_sheet")();
data = py::cast<SheetData>(obj); // This is where the problem lies, This cast crashes the program

其中SheetData是以下typedef

typedef std::list<std::list<std::string> > SheetData;

在调试时,我发现程序实际上在以下行崩溃:

py::object dataTable = _tool.attr("return_sheet")(); // Where _tool.attr("return_sheet")() gives an py::object which is a list of list of str

有人知道,我怎样才能成功地将 python 列表转换为std::listC++std::list

编辑

这是我嵌入在c ++ [xlanalyser.py]中的python程序文件: https://pastebin.com/gARnkMTv

这是 c++ 代码 [main.cpp] : https://pastebin.com/wDDUB1s4

注意:xlanalyser.py 中的所有其他函数在嵌入 c++ 时不会导致崩溃 [ 只有 return_sheet() 函数会导致崩溃 ]

您可以使用Python/C API 作为解决方法(检查函数CastToSheetData)。我在下面包括完整的示例:

program.py

def return_matrix():
dataTable = []
for r in range(0,2):
datalist = []
for c in range(0,2):
datalist.append(str(r+c))
dataTable.append(datalist)
return dataTable

主.cpp

#include <pybind11/embed.h>
#include <iostream>
#include <list>
#include <string>
typedef std::list<std::list<std::string> > SheetData;
namespace py = pybind11;
SheetData CastToSheetData(PyObject *obj)
{
SheetData data;
PyObject *iter = PyObject_GetIter(obj);
if (!iter)
return data;
while (true) {
std::list<std::string> aux_list;
PyObject *next = PyIter_Next(iter);
if (!next) {
// nothing left in the iterator
break;
}
PyObject *iter2 = PyObject_GetIter(next);
if (!iter2)
continue;
while(true) {
PyObject *next2 = PyIter_Next(iter2);
if (!next2) {
// nothing left in the iterator
break;
}
PyObject* pyStrObj = PyUnicode_AsUTF8String(next2); 
char* zStr = PyBytes_AsString(pyStrObj); 
std::string foo(strdup(zStr));
aux_list.push_back(foo);
Py_DECREF(pyStrObj);
}
data.push_back(aux_list);
}
return data;
}

int main()
{
py::scoped_interpreter guard{};
py::module calc = py::module::import("program");
py::object result = calc.attr("return_matrix")();
SheetData data = CastToSheetData(result.ptr());
for (auto l : data)
{
std::cout << "[ ";
for(auto s : l)
std::cout << s <<  " ";
std::cout << "]" << std::endl;
}
return 0;
}

输出:

[ 0 1 ]
[ 1 2 ]

也许,最好的方法是使用类似于load方法中CastToSheetData函数中的代码来创建自定义type_caster