我在尝试使用Python CTypes接口到库时遇到了一个奇怪的错误。我正在Kubuntu Trusty 64位上运行Python 3.4.0-0ubuntu2。你知道吗
segfault发生在我在一个union
中使用4个匿名struct
时,而不是在我使用3个或更少的时候,这是最奇怪的。它也只在我尝试return
将union
返回到fn2
中的Python时发生,而不是在fn1
中从Python发送时发生。你知道吗
带有3 struct
的代码不存在以下错误:
库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;
}
你知道吗主.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
:
在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;
在主.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)]
出现此故障的原因是什么?如何修复?谢谢。你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐