使用带有4个结构(但不少于4个)的Python CTypes联合时会出现错误

使用带有4个结构(但不少于4个)的Python CTypes联合时会出现错误,python,struct,segmentation-fault,ctypes,unions,Python,Struct,Segmentation Fault,Ctypes,Unions,我在尝试使用Python CTypes与库进行接口时遇到了一个奇怪的错误。我正在Kubuntu Trusty 64位上运行Python 3.4.0-0ubuntu2 segfault发生在我在联合中使用4个匿名structs时,而不是在我使用3个或更少时,这是最奇怪的。仅当我试图在fn2中将联合返回到Python时,它才会发生,但当我在fn1中从Python发送它时,它就不会发生 3structs不分段的代码: lib.c: #include <stdio.h> typedef u

我在尝试使用Python CTypes与库进行接口时遇到了一个奇怪的错误。我正在Kubuntu Trusty 64位上运行Python 3.4.0-0ubuntu2

segfault发生在我在
联合中使用4个匿名
struct
s时,而不是在我使用3个或更少时,这是最奇怪的。仅当我试图在
fn2
联合返回到Python时,它才会发生,但当我在
fn1
中从Python发送它时,它就不会发生

3
struct
s不分段的代码:

lib.c:

#include <stdio.h>

typedef union {
    int data[3];
    struct { int r, g, b; };
    struct { int x, y, z; };
    struct { int l, a, B; }; // cap B to silence error
} Triplet;

void fn1(Triplet t)
{
    fprintf(stderr, "%d, %d, %d\n", t.r, t.g, t.b);
}

Triplet fn2(Triplet t)
{
    Triplet temp = {{t.r + 1, t.g + 1, t.b + 1}};
    return temp;
}
生成文件:

test:
    $(CC) -fPIC -shared -o libuniontest.so lib.c
    sudo cp libuniontest.so /usr/local/lib/
    sudo ldconfig
    python3 main.py
减少
struct
成员的数量也不是问题。(完全删除
struct
成员会导致出现错误。)现在,为了演示segfault,只需再添加一个匿名
struct

在lib.c中:

typedef union {
    int data[3];
    struct { int r, g, b; };
    struct { int x, y, z; };
    struct { int l, a, B; }; // cap B to silence error
    struct { int L, c, h; }; // cap L to silence error
} Triplet;
在main.py中:

class _RGB(Structure): _fields_ = _makeFields("rgb")
class _XYZ(Structure): _fields_ = _makeFields("xyz")
class _LAB(Structure): _fields_ = _makeFields("laB") # cap B to silence error
class _LCH(Structure): _fields_ = _makeFields("Lch") # cap L to silence error

class Triplet(Union):
    _anonymous_ = ["rgb", "xyz", "laB", "Lch"]
    _fields_ = [("data", Array3),
                ("rgb", _RGB),
                ("xyz", _XYZ),
                ("laB", _LAB),
                ("Lch", _LCH)]
此故障的原因是什么?如何修复?谢谢

class _RGB(Structure): _fields_ = _makeFields("rgb")
class _XYZ(Structure): _fields_ = _makeFields("xyz")
class _LAB(Structure): _fields_ = _makeFields("laB") # cap B to silence error
class _LCH(Structure): _fields_ = _makeFields("Lch") # cap L to silence error

class Triplet(Union):
    _anonymous_ = ["rgb", "xyz", "laB", "Lch"]
    _fields_ = [("data", Array3),
                ("rgb", _RGB),
                ("xyz", _XYZ),
                ("laB", _LAB),
                ("Lch", _LCH)]