pol*_*rto 11 c python struct cython unions
在Cython glue声明中,我如何表示struct包含匿名联合的C 类型?例如,如果我有一个C头文件mystruct.h包含
struct mystruct
{
union {
double da;
uint64_t ia;
};
};
Run Code Online (Sandbox Code Playgroud)
然后,在相应的.pyd文件中
cdef extern from "mystruct.h":
struct mystruct:
# what goes here???
Run Code Online (Sandbox Code Playgroud)
我试过这个:
cdef extern from "mystruct.h":
struct mystruct:
union {double da; uint64_t ia;};
Run Code Online (Sandbox Code Playgroud)
但这只给了我"C变量声明中的语法错误"就union行了.
Hal*_*own 11
对于那些通过谷歌来到这里的人,我找到了解决方案.如果你有一个结构:
typedef struct {
union {
int a;
struct {
int b;
int c;
};
}
} outer;
Run Code Online (Sandbox Code Playgroud)
您可以在Cython声明中将其全部展平,如下所示:
ctypedef struct outer:
int a
int b
int c
Run Code Online (Sandbox Code Playgroud)
Cython没有生成任何代码来对结构的内存布局做出任何假设; 你只是通过告诉它生成什么语法来调用它来告诉它你所调用的事实结构.因此,如果您的结构具有int可以作为访问的大小成员((outer) x).a,那么您可以抛出a结构定义,它将起作用.它是在文本替换而不是内存布局上运行的,因此它不关心这些东西是匿名联合或结构还是你有什么.
您无法根据我的知识嵌套声明,并且Cython不支持匿名联合AFAIK.
请尝试以下方法:
cdef union mystruct_union:
double lower_d
uint64_t lower
cdef struct mystruct:
mystruct_union un
Run Code Online (Sandbox Code Playgroud)
现在以un.lower_dand和方式访问union成员un.lower.