可以嵌入 PyPy 使用新的 extern "Python" 样式 cffi 回调吗?PyPy 的文档仅显示旧式 cffi 回调,但 cffi 文档建议不要使用它们。PyPy 文档没有提及新样式回调,我无法让新样式回调工作。
# file "interface.py"
import cffi
# add new extern "Python" declaration
ffi = cffi.FFI() ffi.cdef('''
struct API {
double (*add_numbers)(double x, double y);
extern "Python" void add_numbers2(double, double);
}; ''')
# Better define callbacks at module scope, it's important to
# keep this object alive.
@ffi.callback("double (double, double)")
def add_numbers(x, y):
return x + y
# new function
@ffi.def_extern()
def add_numbers2(x, y):
return x + y
def fill_api(ptr):
global api
api = ffi.cast("struct API*", ptr)
api.add_numbers = add_numbers
运行编译后的 C 时出错(C 的源代码与 PyPy 文档相同):
debug: OperationError:
debug: operator-type: CDefError
debug: operator-value: cannot parse "extern "Python" void add_numbers2(double, double);"
:6:5: before: extern
Error calling pypy_execute_source_ptr!