[英]swig/python array of structure in a union
我是swig / python世界的初學者,試圖在python中訪問C結構數組,但出現以下錯誤:
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(foo *);
foo.c:
#include "foo.h"
int
fill_foo(foo *var)
{
var->u.b[0].val = 10;
var->u.b[1].val = 20;
return 0;
}
foo_test.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的文章,但是我不清楚我如何解決這個特殊情況。 如果有人能幫助我會很高興。
經過一番摸索后,我發現我需要擴展結構欄來解決此問題。 我可以將上面的示例代碼與foo_test.i的以下新增功能一起使用。
%extend bar {
const bar __getitem__(int i) {
return $self[i];
}
}
一個缺點是,我需要為用作數組的每個結構添加這樣的擴展。 仍未弄清楚如何針對所有結構數組通用地解決此問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.