SWIG-将C字符串数组包装到python列表中

Fab*_*ral 6 c python swig

我想知道使用SWIG将C中的字符串数组包装到Python列表的正确方法是什么。

该数组在struct内部:

typedef struct {
   char** my_array;
   char* some_string; 
}Foo;
Run Code Online (Sandbox Code Playgroud)

SWIG自动将some_string包装为python字符串。

我应该在SWIG接口文件中放入什么,以便可以在Python中作为常规Python字符串列表['string1','string2'] 访问my_array

我已将typemap用作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]));
  }
}
Run Code Online (Sandbox Code Playgroud)

但这仍然行不通。在Python中,my_array变量显示为SwigPyObject:_20afba0100000000_p_p_char。

我想知道是否是因为char **在结构内部?也许我需要告知SWIG?

有任何想法吗?

use*_*005 -1

很抱歉有点偏离主题,但如果它适合您,我强烈建议您使用 ctypes 而不是 swig。这是我之前在 ctypes 上下文中问过的一个相关问题:Passing a list of strings to from python/ctypes to C function waiting char **