I was wondering how to correctly transfer an array of strings in C to a Python list using SWIG.
The array is inside the structure:
typedef struct { char** my_array; char* some_string; }Foo;
SWIG automatically wraps some_string in a python string.
What should I add to the SWIG interface file so that I can access my_array in Python as a regular Python string list ['string1', 'string2']?
I used typemap as sugested:
%typemap(python,out) char** { int len,i; len = 0; while ($1[len]) len++; $result = PyList_New(len); for (i = 0; i < len; i++) { PyList_SetItem($result,i,PyString_FromString($1[i])); } }
But that still didn't work. In Python, the variable my_array appears as SwigPyObject: _20afba0100000000_p_p_char.
Interestingly, this is because char ** is inside the structure? Maybe I need to tell SWIG what?
Any ideas?
source share