联合体中结构的swig/python数组
我是swig/python的初学者,曾试图在python中访问C-structure数组,但出现以下错误: TypeError:“bar”对象不支持索引 以下是我尝试做的一个简化版本: foo.h:联合体中结构的swig/python数组,python,arrays,swig,Python,Arrays,Swig,我是swig/python的初学者,曾试图在python中访问C-structure数组,但出现以下错误: TypeError:“bar”对象不支持索引 以下是我尝试做的一个简化版本: foo.h: #include <inttypes.h> typedef struct bar { uint8_t val; }bar; typedef struct foo { union { bar b[2]; } u; }foo; int fill_foo(fo
#include <inttypes.h>
typedef struct bar {
uint8_t val;
}bar;
typedef struct foo {
union {
bar b[2];
} u;
}foo;
int fill_foo(foo *);
foo_测试i:
%module foo_test
%{
#include "foo.h"
%}
%include "foo.h"
foo.py:
import foo_test
f = foo_test.foo()
foo_test.fill_foo(f)
print f.u.b[0]
我读过一些关于c阵列和swig的其他帖子,但我不清楚如何解决这个特殊情况。如果有人能帮我,我会很高兴的。经过一番探索,我发现我需要扩展structbar来解决这个问题。我能够使用下面添加到foo_test.I中的代码来实现上述示例代码
%extend bar {
const bar __getitem__(int i) {
return $self[i];
}
}
但缺点是,我需要为用作数组的每个结构添加这样的扩展。仍然没有弄清楚如何对所有结构数组通用地解决这个问题。它是一个指针,不能使用它的常规索引。如果数组只有两个大元素,为什么要使用数组?@Natecat数组的两个元素只是一个示例/简化。我的实际代码使用更大的数组。
%extend bar {
const bar __getitem__(int i) {
return $self[i];
}
}