SWIG - 裹C字符串数组Python列表 [英] SWIG - Wrap C string array to python list

查看:578
本文介绍了SWIG - 裹C字符串数组Python列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么是使用痛饮包装字符串数组在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自动换行的 some_string 以字符串形式。

我应该把在痛饮接口文件,这样我可以访问的 my_array 在Python作为一个常规的Python字符串列表['字符串1','字符串2']?

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' ] ?

我已经使用类型映射为sugested:

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]));
  }
}

但是,这仍然没有奏效。在Python中,my_array变量出现SwigPyObject:_20afba0100000000_p_p_char

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?

任何想法?

推荐答案

我不认为这是在痛饮自动处理这种转换一个选项。你需要使用痛饮的类型映射功能和手动编写类型转换器。在这里您可以找到Python列表转换成char ** HTTP:// WWW。 swig.org/Doc1.3/Python.html#Python_nn59 ,以便工作的一半就完成了。你现在需要做的是检查类型映射的文档休息和写入字符转换为** Python列表。

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.

这篇关于SWIG - 裹C字符串数组Python列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆