لفت نظري مكتبة CFFI كبديل رائع ل swig و ctypes -ربما ليس في كل الأحيان- ولكنها قطعا تستحق التجربة
أنظر لبعض الأمثلة
مثال على استدعاء printf
>>> from cffi import FFI
>>> ffi = FFI()
>>> ffi.cdef("""
... int printf(const char *format, ...); // copy-pasted from the man page
... """)
>>> C = ffi.dlopen(None) # loads the entire C namespace
>>> arg = ffi.new("char[]", "world") # equivalent to C code: char arg[] = "world";
>>> C.printf("hi there, %s!\n", arg) # call printf
hi there, world!مثال اخر على passwd struct
from cffi import FFI
ffi = FFI()
ffi.cdef(""" // some declarations from the man page
struct passwd {
char *pw_name;
...;
};
struct passwd *getpwuid(int uid);
""")
C = ffi.verify(""" // passed to the real C compiler
#include <sys/types.h>
#include <pwd.h>
""")
p = C.getpwuid(0)
assert ffi.string(p.pw_name) == 'root' # on Python 3: b'root'مرفق في الdemos أمثلة لمكتبة curses وغيرها