尝试使用Python CType连接到库时,我遇到了一个奇怪的段错误。我在Kubuntu Trusty 64位上运行Python 3.4.0-0ubuntu2。
当我在struct
内使用4个匿名union
时,会发生段错误,但是当我使用3个或更少时,则不会发生,这是最好奇的。只有当我在return
中尝试union
fn2
返回到Python时,它才会发生,但是当我从发送时却不会Python fn1
。
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;
}
main.py:
from ctypes import *
def _makeFields(s):
return tuple([(c, c_int) for c in s])
Array3 = c_int * 3
class _RGB(Structure): _fields_ = _makeFields("rgb")
class _XYZ(Structure): _fields_ = _makeFields("xyz")
class _LAB(Structure): _fields_ = _makeFields("laB") # cap B to silence error
class Triplet(Union):
_anonymous_ = ["rgb", "xyz", "laB"]
_fields_ = [("data", Array3),
("rgb", _RGB),
("xyz", _XYZ),
("laB", _LAB)]
_lib = CDLL("libuniontest.so")
_lib.fn1.argtypes = [Triplet]
_lib.fn2.restype = Triplet
_lib.fn2.argtypes = [Triplet]
t = Triplet(Array3(99, 129, 39))
_lib.fn1(t)
tt = _lib.fn2(t)
print(tuple(tt.data))
生成文件:
test:
$(CC) -fPIC -shared -o libuniontest.so lib.c
sudo cp libuniontest.so /usr/local/lib/
sudo ldconfig
python3 main.py
减少struct
成员的数量也不是问题。 (完全删除struct
成员会导致another problem。)现在,为了演示segfault,只需添加一个匿名struct
:
:
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)]
此段错误的原因是什么?如何解决?感谢。