【发布时间】:2019-12-03 17:16:03
【问题描述】:
我正在编写由 cffi 在 pypy3 中调用的 C 函数。但是,无论真正的返回值是什么,包装函数在 pypy3 中总是返回一个无意义的值。
printf() 函数的输出告诉我在 C 函数中一切正常,但是 pypy3 中的返回值发生了变化。
C函数是这样写的:
double test(){
return 5.12;
}
double test2(){
double tmp=test();
printf("!!!!!!!%f\n",tmp);
return tmp;
}
cffi构建脚本如下:
from cffi import FFI
ffibuilder = FFI()
ffibuilder.set_source("_faststr_cffi",
"""
#include <Python.h>
#include "stdint.h"
#include <string.h>
typedef uint32_t char32_t;
""",
sources=['faststr_purec.c']
) # library name, for the linker
ffibuilder.cdef("""
double test();
double test2();
""")
if __name__ == "__main__":
ffibuilder.compile(verbose=True)
我尝试在 pypy3 控制台中调用 test2():
>>>> from _faststr_cffi import lib
>>>> lib.test2()
!!!!!!!5.120000
16.0
printf 告诉我返回值应该是 5.120000,但它在 pypy3 中返回了 16.0。
我发现了一些线索:如果我在test2() printf函数中改变字符串,pypy3中test2的返回值就会改变。
更新:cpython 3.6.7 中的结果是一样的,所以这不是 pypy3 问题
【问题讨论】:
-
奇怪的问题,但总是写的字符数吗?因为这就是这里的样子
-
@EdwardMinnix 确实,这是一个很好的捕获,这导致了错误行为的原因的识别
标签: c python-3.x pypy python-cffi