python: initial impl of bindings using cFFI

A PoC that exposes single function
`enry.language_by_extension()` and a small
number of helpers to deal with string
coversion between Go<->C<->Python.

Signed-off-by: Alexander Bezzubov <bzz@apache.org>
This commit is contained in:
Alexander Bezzubov
2019-06-01 19:47:13 +02:00
parent 6a6a3cc26e
commit ee7a0f1139
5 changed files with 111 additions and 0 deletions

33
python/enry_build.py Normal file
View File

@ -0,0 +1,33 @@
from cffi import FFI
ffibuilder = FFI()
# cdef() expects a single string declaring the C types, functions and
# globals needed to use the shared object. It must be in valid C syntax.
ffibuilder.cdef("""
typedef struct { const char *p; ptrdiff_t n; } _GoString_;
typedef _GoString_ GoString;
typedef unsigned char GoUint8;
/* Return type for GetLanguageByExtension */
struct GetLanguageByExtension_return {
GoString r0; /* language */
GoUint8 r1; /* safe */
};
extern struct GetLanguageByExtension_return GetLanguageByExtension(GoString p0);
""")
# set_source() gives the name of the python extension module to
# produce, and some C source code as a string. This C code needs
# to make the declarated functions, types and globals available,
# so it is often just the "#include".
ffibuilder.set_source("_c_enry",
"""
#include "../.shared/libenry.h" // the C header of the library
""",
libraries=['enry'],
library_dirs=['../.shared'
]) # library name, for the linker
if __name__ == "__main__":
ffibuilder.compile(verbose=True)