python数组 swig_SWIG - 裹C字符串数组Python列表

I was wondering what is the correct way to wrap an array of strings in C to a Python list using SWIG.

The array is inside a struct :

typedef struct {

char** my_array;

char* some_string;

}Foo;

SWIG automatically wraps some_string to a python string.

What should I put in the SWIG interface file so that I can access my_array in Python as a regular Python string list ['string1', 'string2' ] ?

I have 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 my_array variable appears as SwigPyObject: _20afba0100000000_p_p_char.

I wonder if that is because the char** is inside a struct? Maybe I need to inform SWIG that?

Any ideas?

解决方案

I don't think there is a option to handle this conversion automatically in SWIG. You need use Typemap feature of SWIG and write type converter manually. Here you can find a conversion from Python list to char** http://www.swig.org/Doc1.3/Python.html#Python_nn59 so half of job is done. What you need to do right now is to check rest of documentation of Typemap and write converter from char** to Python list.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值