【发布时间】:2012-07-06 11:51:09
【问题描述】:
我正在尝试从特定于 Windows 的程序中移植一些 Python ctypes 代码,以链接到我的库的 Linux 端口。描述我的问题的最短 Python 代码示例如下所示。当我尝试执行它时,我在 Python 中的 examine_arguments() 中收到分段错误。我在崩溃函数调用时在我的库中放置了 printf 语句,但它从未执行,这让我认为问题出在 ctypes 代码中。
import ctypes
avidll = ctypes.CDLL("libavxsynth.so")
class AVS_Value(ctypes.Structure, object):
def __init__(self, val=None):
self.type=ctypes.c_short(105) # 'i'
self.array_size = 5
self.d.i = 99
class U(ctypes.Union):
_fields_ = [("c", ctypes.c_void_p),
("b", ctypes.c_long),
("i", ctypes.c_int),
("f", ctypes.c_float),
("s", ctypes.c_char_p),
("a", ctypes.POINTER(AVS_Value))]
AVS_Value._fields_ = [("type", ctypes.c_short),
("array_size", ctypes.c_short),
("d", U)]
avs_create_script_environment = avidll.avs_create_script_environment
avs_create_script_environment.restype = ctypes.c_void_p
avs_create_script_environment.argtypes = [ctypes.c_int]
avs_set_var = avidll.avs_set_var
avs_set_var.restype = ctypes.c_int
avs_set_var.argtypes = [ctypes.c_void_p, ctypes.c_char_p, AVS_Value]
env = avs_create_script_environment(2)
val = AVS_Value()
res = avs_set_var(env, b'test', val)
我的库在其标题中包含以下内容,并且执行我上面描述的纯 C 程序(调用 create_script_environment 后跟 set_var)运行良好。查看我的库在控制台上的日志记录信息,当我尝试输入 avs_set_var 时发生崩溃。
typedef struct AVS_ScriptEnvironment AVS_ScriptEnvironment;
typedef struct AVS_Value AVS_Value;
struct AVS_Value {
short type; // 'a'rray, 'c'lip, 'b'ool, 'i'nt, 'f'loat, 's'tring, 'v'oid, or 'l'ong
// for some function e'rror
short array_size;
union {
void * clip; // do not use directly, use avs_take_clip
char boolean;
int integer;
float floating_pt;
const char * string;
const AVS_Value * array;
} d;
};
AVS_ScriptEnvironment * avs_create_script_environment(int version);
int avs_set_var(AVS_ScriptEnvironment *, const char* name, AVS_Value val);
我尝试从 GDB 回溯调用,但我不明白如何解释结果,也不太了解如何使用 GDB。
#0 0x00007ffff61d6490 in examine_argument () from /usr/lib/python2.7/lib-dynload/_ctypes.so
#1 0x00007ffff61d65ba in ffi_prep_cif_machdep () from /usr/lib/python2.7/lib-dynload/_ctypes.so
#2 0x00007ffff61d3447 in ffi_prep_cif () from /usr/lib/python2.7/lib-dynload/_ctypes.so
#3 0x00007ffff61c7275 in _ctypes_callproc () from /usr/lib/python2.7/lib-dynload/_ctypes.so
#4 0x00007ffff61c7aa2 in PyCFuncPtr_call.2798 () from /usr/lib/python2.7/lib-dynload/_ctypes.so
#5 0x00000000004c7c76 in PyObject_Call ()
#6 0x000000000042aa4a in PyEval_EvalFrameEx ()
#7 0x00000000004317f2 in PyEval_EvalCodeEx ()
#8 0x000000000054b171 in PyRun_FileExFlags ()
#9 0x000000000054b7d8 in PyRun_SimpleFileExFlags ()
#10 0x000000000054c5d6 in Py_Main ()
#11 0x00007ffff68e576d in __libc_start_main () from /lib/x86_64-linux-gnu/libc.so.6
#12 0x000000000041b931 in _start ()
我不知道如何解决这个问题。我查看了调用类型的详细信息,但我没有看到任何明显不正确的地方。我是否陷入任何特定于平台的类型用法?
编辑 ctypes 模块中的 32 位与 64 位架构似乎存在问题。当我用我的库的 32 位构建和 32 位 Python 再次测试时,它运行成功。在 64 位上,它在同一位置出现段错误。
【问题讨论】:
-
我复制了这段代码,重新排列了明显错误粘贴的位,对这两个函数做了一些虚拟实现,为 x86_64 编译它,并在 64 位 Python 2.7(Ubuntu Natty,gcc 4.5.2、Python 2.7.1、eglibc 2.13),一切都按预期工作。在这里将问题归咎于 ctypes 似乎为时过早。也许您可以提供一个更完整的突破性示例?
-
我清理了代码示例。似乎段错误与作为 AVS_Value 类的一部分的联合有关。当我用 ctypes.c_int 或 ctypes.c_void_p 之类的东西替换它时,程序不再出现段错误。我开始认为问题出在安装在 Ubuntu 上的 Python 上——我使用的是 Ubuntu 12.04、GCC 4.6.3、Python 2.7..
-
您的
boolean变量是char类型,但在Python 中您已将b声明为c_long。 -
否则代码看起来是正确的,但在我的虚拟实现中也可以正常工作,所以问题可能出在 C 代码中。