我正在尝试建立一个测试环境来验证我正在使用的C库。该库嵌入在运行自定义linux的设备上。经过一番网上阅读后,我决定使用python和ctypes从python调用我的库函数。几乎我所有的功能都可以很好地工作,但是在使用回调函数时会卡住。
我的回调函数在库中定义为“弱”。我想知道是否可以使用python ctypes重载弱C函数吗?
以我的尝试为例:
libtest.c
#include <stdio.h>
#include <stdlib.h>
#include "libtest.h"
int multiply(int a, int b)
{
custom_callback(a);
return a*b;
}
int __attribute__((weak)) custom_callback(int a)
{
printf("Callback not redefined, a = %d\n", a);
return 0;
}
main.py
from ctypes import *
lib = cdll.LoadLibrary("libtest.so")
CALLBACKFUNC = CFUNCTYPE(c_int, c_int)
def py_callback_func(a):
print("py redifinition", a)
return 0
lib.custom_callback = CALLBACKFUNC(py_callback_func)
lib.multiply(c_int(5), c_int(10))
然后我用python3运行main.py
$ python3 main.py
Callback not redefined, a = 5
我希望输出为py redifinition 5
我做错什么了吗?还是ctypes根本无法重新定义一个弱声明的C函数?
答案 0 :(得分:2)
您正在感到困惑:弱符号在 link 时间处理,而您在 运行 时间。链接器可能无法了解您的 Python 回调函数。尽管它不是官方消息来源,但请查看[Wikipedia]: Weak symbol了解更多详细信息。
还有[Python 3]: ctypes - A foreign function library for Python。
这是一个小演示。
libtest.c :
#include <stdio.h>
int __attribute__((weak)) custom_callback(int a) {
printf("Callback not redefined, a = %d\n", a);
return 0;
}
int multiply(int a, int b) {
custom_callback(a);
return a * b;
}
callback.c :
#include <stdio.h>
int custom_callback(int a) {
printf("C callback redefinition, a = %d\n", a);
return 0;
}
code.py :
#!/usr/bin/env python3
import sys
import ctypes
DLL_NAME = "./libtest.so"
CALLBACKFUNCTYPE = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int)
def py_callback_func(a):
print("PY callback redefinition", a)
return 0
def main(argv):
dll_name = argv[0] if argv else DLL_NAME
dll = ctypes.CDLL(dll_name)
multiply = dll.multiply
multiply.argtypes = [ctypes.c_int, ctypes.c_int]
multiply.restype = ctypes.c_int
#dll.custom_callback(3)
#dll.custom_callback = CALLBACKFUNCTYPE(py_callback_func)
#dll.custom_callback(3)
res = multiply(5, 10)
if __name__ == "__main__":
print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
main(sys.argv[1:])
print("Done.")
输出:
[cfati@cfati-ubtu16x64-0:~/Work/Dev/StackOverflow/q055357490]> ~/sopr.sh *** Set shorter prompt to better fit when pasted in StackOverflow (or other) pages *** [prompt]> ls callback.c code.py libtest.c [prompt]> gcc -o libtest0.so -fPIC -shared libtest.c [prompt]> gcc -o libtest1.so -fPIC -shared libtest.c callback.c [prompt]> ls callback.c code.py libtest0.so libtest1.so libtest.c [prompt]> [prompt]> python3 code.py ./libtest0.so Python 3.5.2 (default, Nov 12 2018, 13:43:14) [GCC 5.4.0 20160609] on linux Callback not redefined, a = 5 [prompt]> [prompt]> python3 code.py ./libtest1.so Python 3.5.2 (default, Nov 12 2018, 13:43:14) [GCC 5.4.0 20160609] on linux C callback redefinition, a = 5 Done.
可见,__attribute__((weak))
效果 可见。另外,关于注释的代码(尝试设置 .dll 函数):它仅在 ctypes 代理级别下运行,而不会更改 .dll 代码(这很荒谬)。
关于您的问题,有两种解决方法。
这里是使用带有此类回调函数的指针的。指针可以通过设置函数从外部设置(也可以导出)。
libtest.c :
#include <stdio.h>
#define EXEC_CALLBACK(X) \
if (pCallback) { \
pCallback(X); \
} else { \
fallbackCallback(X); \
}
typedef int (*CustomCallbackPtr)(int a);
static CustomCallbackPtr pCallback = NULL;
static int fallbackCallback(int a) {
printf("Callback not redefined, a = %d\n", a);
return 0;
}
int multiply(int a, int b) {
EXEC_CALLBACK(a);
return a * b;
}
void setCallback(CustomCallbackPtr ptr) {
pCallback = ptr;
}
code.py :
#!/usr/bin/env python3
import sys
import ctypes
DLL_NAME = "./libtest.so"
CALLBACKFUNCTYPE = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int)
def py_callback_func(a):
print("PY callback redefinition", a)
return 0
def main():
dll = ctypes.CDLL(DLL_NAME)
multiply = dll.multiply
multiply.argtypes = [ctypes.c_int, ctypes.c_int]
multiply.restype = ctypes.c_int
set_callback = dll.setCallback
set_callback.argtypes = [CALLBACKFUNCTYPE]
multiply(5, 10)
set_callback(CALLBACKFUNCTYPE(py_callback_func))
multiply(5, 10)
set_callback(ctypes.cast(ctypes.c_void_p(), CALLBACKFUNCTYPE))
multiply(5, 10)
if __name__ == "__main__":
print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
main()
print("Done.")
输出:
[cfati@cfati-ubtu16x64-0:~/Work/Dev/StackOverflow/q055357490]> ~/sopr.sh *** Set shorter prompt to better fit when pasted in StackOverflow (or other) pages *** [prompt]> ls code.py libtest.c [prompt]> gcc -o libtest.so -fPIC -shared libtest.c [prompt]> python3 code.py Python 3.5.2 (default, Nov 12 2018, 13:43:14) [GCC 5.4.0 20160609] on linux Callback not redefined, a = 5 PY callback redefinition 5 Callback not redefined, a = 5 Done.