我正在使用CFFI從Python中調用返回結構的Python函數。該結構由time_t
元素定義。如何將結構聲明爲CFFI,以便我可以從Python訪問它?在Python中聲明包含time_t字段的結構CFFI
例如,我嘗試了以下(以獲得文件的修改時間):
import cffi
ffi = cffi.FFI()
ffi.cdef("""
// From POSIX
struct timespec {
time_t tv_sec;
long tv_nsec;
...;
};
struct stat {
struct timespec st_mtim;
...;
};
// From "man 2 lstat"
int lstat(const char *path, struct stat *buf);
""")
stat = ffi.verify("#include <sys/stat.h>")
這給出了一個錯誤:
cffi.api.CDefError: cannot parse " time_t tv_sec;"
:5: before: time_t
它註釋掉行time_t tv_sec;
之後編譯,但當然你不能訪問tv_sec
字段。據推測,CFFI的C語法分析器不支持typedefs。您不能只用time_t
替換實際的類型,因爲類型在不同的平臺上可能會有所不同。
見我非常類似的回答與工作代碼[獲取文件的修改時間納秒的精度] [1] [1]:http://stackoverflow.com/questions/19351867/get-file-modification-time-to-nanosecond-precision –