Problem
I am trying to convert a list of lists, returned by a python function called inside a C++ code. Though the pybind11 library allows type conversion from python data types to C++ data types, my attempt to convert a list of lists returned by python to a std::list
of std::list
of strings of C++, fails every time.
Code
Here is the python function (The function returns a list of list containing string values):
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
And Here I am calling it in C++ using pybind11:
py::list obj = _tool.attr("return_sheet")();
data = py::cast<SheetData>(obj); // This is where the problem lies, This cast crashes the program
Where SheetData
is a typedef
for:
typedef std::list<std::list<std::string> > SheetData;
While debugging, I found out that the program is actually crashing at this line:
py::object dataTable = _tool.attr("return_sheet")(); // Where _tool.attr("return_sheet")() gives an py::object which is a list of list of str
Does someone know, how can I successfully convert a list of lists of python to std::list
of std::list
of C++?
EDIT
Here is the python program file I am embedding in c++ [xlanalyser.py] : https://pastebin.com/gARnkMTv
And Here is the c++ code [main.cpp] : https://pastebin.com/wDDUB1s4
Note: All other functions in xlanalyser.py do not cause a crash on embedding in c++ [ only the return_sheet() function causes the crash ]