[英]Accessing C struct array to Python with SWIG
I attempting to call into existing C code from Python. 我试图从Python调用现有的C代码。 The C code defines a struct
B
that contains an struct array of A
s. C代码定义了一个结构
B
,它包含一个结构数组A
s。 The C code also defines a function that puts values into the structure when called. C代码还定义了一个函数,在调用时将值放入结构中。 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*
. 相反,我得到的对象是
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()
. 我也不确定如何创建Python类
B
的实例来替换PyString_FromString()
。
Below is the code needed to demonstrate my issue and how to execute it: 以下是演示我的问题以及如何执行它所需的代码:
example.h 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 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 example.i
%module example
%{
#define SWIG_FILE_WITH_INIT
#include "example.h"
%}
%include "example.h"
setup.py 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. script.py - 这使用库并演示失败。
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: 你可以编写一些C辅助函数,如:
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, eg 包装这些,然后您可以使用从
example.B_t()
获得的指针来访问数据结构排列中的值,例如
>>> 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... 希望显而易见的是这些C函数的实现应该是什么 - 如果不是我可以提供一个例子......
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 授予它并不像能够作为python列表访问C数组那样无缝 - 您可能能够使用类型映射来实现这一点,但我不记得在这个实例中您需要的确切语法 - 您必须这样做SWIG文档中的一些RTFM
also possible duplicate here: nested structure array access in python using SWIG 也可能在这里重复: 使用SWIG在python中嵌套结构数组访问
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.