PyPy可以嵌入使用新的extern "Python“风格的cffi回调吗?PyPy的文档只显示了旧风格的cffi回调,但cffi文档建议不要使用它们。PyPy文档没有引用新样式回调,我无法让新样式回调工作。
嵌入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!发布于 2015-12-21 17:16:29
"extern Python“实际上并不意味着现在就可以用于嵌入您所指向的情况。为了更好地支持这种情况,cffi的开发人员需要付出更多的努力(包括我:-)。换句话说,未来的cffi版本应该提供一种替代的嵌入方式,比CPython和PyPy的自定义解决方案都更简单(分别是“使用CPython cffi”和“跟随https://pypy.readthedocs.org/en/latest/embedding.html")。它还应提供一个单一的共同解决办法。不过,现在还没有完成。
您可以在PyPy文档示例的基础上应用现有的(CFfI1.4) "extern“解决方案,但它需要一些重构--特别是,该示例使用”内联ABI模式“,而"extern”仅适用于“线外API模式”。如果我们将https://pypy.readthedocs.org/en/latest/embedding.html视为一种纯粹的ABI模式方法,那么使用ffi.callback()仍然是在CFFI中实现它的唯一方法。
更新:CFfI1.5完全支持在"extern“样式中嵌入(http://cffi.readthedocs.org/en/latest/embedding.html)。它现在可以在CPython上使用。PyPy需要主干版本(或者PyPy 4.1,它应该在2016年3月或4月发布)。
https://stackoverflow.com/questions/34392109
复制相似问题