给出以下test.pyx
文件:
import os;
cpdef get_stat(filename):
cdef os.stat_result stat_info
stat_info=os.stat(filename)
return stat_info
我无法使用cython对其进行分析:
Error compiling Cython file (`cython -a test.pyx`):
------------------------------------------------------------
...
import os;
cpdef get_stat(filename):
cdef os.stat_result stat_info
^
------------------------------------------------------------
test.pyx:4:9: 'os' is not a cimported module
使用cimport os;
代替import os;
失败,并显示以下信息:
Error compiling Cython file:
------------------------------------------------------------
...
cimport os;
^
------------------------------------------------------------
test.pyx:1:8: 'os.pxd' not found
如何用Cython编译得到这么简单的内容?
答案 0 :(得分:1)
@ead正确指出,要与纯Python代码连接,必须在Python对象中使用cdef object
:
# test.pyx
import os;
cpdef get_stat(filename):
cdef object stat_info
stat_info=os.stat(filename)
return stat_info
使用一个简单的文件对其进行测试:
get_stat('.vimrc')
#> os.stat_result(st_mode=33206, st_ino=45317471250423239, st_dev=920636878, \
#> st_nlink=1, st_uid=0, st_gid=0, st_size=1140, st_atime=1512123416, \
#> st_mtime=1512123416, st_ctime=1500548526)