访问C结构数组的Python SWIG用 [英] Accessing C struct array to Python with SWIG

查看:375
本文介绍了访问C结构数组的Python SWIG用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图调入Python的现有的C code。在C code定义一个struct B 包含 A 所组成的结构数组。在C code也定义调用时把值到结构的功能。我可以访问该阵列成员变量,但它不是一个列表(或者一些支持索引)。相反,我得到一个对象,它是 B * 代理。

我发现这个问题,但它并不像它彻底解决。我也不能确定如何使Python类 B 的一个实例来代替 PyString_FromString()

下面是证明我的问题需要code和如何执行它:

example.h文件

  typedef结构A_S
{
  unsigned int类型的事情;
  unsigned int类型等;
} 在;typedef结构B_S
{
  unsigned int类型的大小;
  A_T项目[16];
} B_T;无符号整型富(B_T * B);

example.c

 的#includeexample.h文件无符号整型
美孚(B_T * B)
{
  B->大小= 1;
  B->项目[0] .thing = 1;
  B->项目[0]。其他= 2;
}

example.i

 %模块示例
%{
#定义SWIG_FILE_WITH_INIT
#包括example.h文件
%}%包括example.h文件

setup.py

 从distutils.core进口设置,扩展模块1 =扩展(示例,源= ['example.c','example.i'])
设置(NAME =样本,版本='0.1',ext_modules = [模块1])

script.py - 这使用库,并演示失败

 导入示例
B = example.B_t()
example.foo(二)
打印b.size
打印b.items
对于我的xrange(b.size):
    打印b.items [I]

如何运行的一切:

 蟒蛇setup.py build_ext --inplace
MV example.so _example.so
蟒蛇script.py


解决方案

您可以写一些C的辅助功能,如:

  INT get_thing(B_T * this_b_t,INT IDX);
INT get_other(B_T * this_b_t,INT IDX);
无效set_thing(B_T * this_b_t,INT IDX,诠释VAL);
无效set_other(B_T * this_b_t,INT IDX,诠释VAL);

包装这些,然后你可以使用您从 example.B_t()去访问值从数据结构的安排,例如内的指针。

 >>> B = example.B_t()
>>> a_thing = example.get_thing(B,0)
>>> example.set_thing(B,0999)

希望其明显的这些C函数的实现应该是什么 - 如果不是我可以提供一个例子...

授予这并不像能够访问C数组作为Python列表无缝 - 你可能能够使用typemaps来实现这一点,但我不记得你需要在这种情况下确切的语法 - 你要做一点RTFM的SWIG文档上

也可以复制在这里:<一href=\"http://stackoverflow.com/questions/7713318/nested-structure-array-access-in-python-using-swig?rq=1\">nested使用python的结构数组访问痛饮

I attempting to call into existing C code from Python. The C code defines a struct B that contains an struct array of As. The C code also defines a function that puts values into the structure when called. I can access the array member variable, but it is not an list (or something that supports indexing). Instead, I am getting an object that is a proxy for B*.

I found this question, but it doesn't look like it was completely resolved. I'm also not sure how to make an instance of the Python class B to replace the PyString_FromString().

Below is the code needed to demonstrate my issue and how to execute it:

example.h

typedef struct A_s
{
  unsigned int thing;
  unsigned int other;
} A_t;

typedef struct B_s
{
  unsigned int size;
  A_t items[16];
} B_t;

unsigned int foo(B_t* b);

example.c

#include "example.h"

unsigned int
foo(B_t* b)
{
  b->size = 1;
  b->items[0].thing = 1;
  b->items[0].other = 2;
}

example.i

%module example
%{
#define SWIG_FILE_WITH_INIT
#include "example.h"
%}

%include "example.h"

setup.py

from distutils.core import setup, Extension

module1 = Extension('example', sources=['example.c', 'example.i'])
setup(name='Example', version='0.1', ext_modules=[module1])

script.py - This uses library and demonstrates the failure.

import example
b = example.B_t()
example.foo(b)
print b.size
print b.items
for i in xrange(b.size):
    print b.items[i]

How to run everything:

python setup.py build_ext --inplace
mv example.so _example.so
python script.py

解决方案

you could write some C helper functions like:

int get_thing(B_t *this_b_t, int idx);
int get_other(B_t *this_b_t, int idx);
void set_thing(B_t *this_b_t, int idx, int val);
void set_other(B_t *this_b_t, int idx, int val);

wrap these and you can then use the pointer that you get from example.B_t() to access values from within your data-structure arrangement, e.g.

>>> b = example.B_t()
>>> a_thing = example.get_thing(b, 0)
>>> example.set_thing(b,0,999)

Hopefully its obvious what the implementation of these C functions should be - if not I could provide an example...

granted this isn't as seamless as being able to access C arrays as python lists - you might be able to use typemaps to achieve this but I can't remember the exact syntax you would need in this instance - you'd have to do a bit of RTFM on the SWIG documentation

also possible duplicate here: nested structure array access in python using SWIG

这篇关于访问C结构数组的Python SWIG用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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