mirror of
https://github.com/ralsina/tartrazine.git
synced 2025-06-19 22:53:05 -03:00
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:
33
python/enry_build.py
Normal file
33
python/enry_build.py
Normal 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)
|
Reference in New Issue
Block a user