Inlinec - a new way to use C code in Python scripts

project inlinec A new way of inline-integration of C code into Python scripts is proposed. C functions are defined directly in the same file as the Python code, distinguished by the "@inlinec" decorator. The summary script is executed as is by the cpython interpreter and parsed using the mechanism provided by Python codecs, which makes it possible to connect the parser to convert the script before parsing it by the interpreter (as a rule, the codecs module is used for transparent text conversion, but it also allows you to arbitrarily transform the contents of the script).

The parser is connected as a module ("from inlinec import inlinec"), which performs primary processing and on the fly translates the definitions of C-functions, allocated using the @inlinec annotations, into ctypes-bindings and replaces the body of the C-function with a call to these bindings. After such a transformation, the Python interpreter receives the already correct converted source text of the script, the C functions in which are called using ctypes. A similar method is also used in the project Pyxl4, which allows you to mix HTML and Python code in one file.

# coding: inlinec
from inlinec import inlinec

@inlinec
deftest():
#include
void test() {
printf("Hello world");
}

The development is still being presented as an experimental prototype, which notes such shortcomings as the lack of support for passing pointers to the function (except for strings), the need to run
"gcc -E" for code preprocessing, saving intermediate *.so, *.o and *.c files in the current directory, no caching of the converted version and extra parsing steps (large delays at each start).

Source: opennet.ru

Add a comment