This presentation aims to show how libraries like Python PIL, ScyPy, NumPy do interfaces with C libraries. Python allows this communication natively. We’ll see the concepts, the Python compiler resources, code API and some code examples.
❖ Why a C module ❖ How to create a module ❖ How to install a module ❖ Code API and built in properties ❖ How to use a module ❖ Experiments ❖ Analysis Agenda
Why a C module? ❖ Heavy computation (numpy) ❖ Increase performance (Scipy) ❖ Don't want to recreate mature a stable C libraries (PIL) ❖ Get control over the memory allocation (embedded systems) ❖ Embed Python in some application (GoDot game engine)
Python.h ❖ Functions and Variables have prefixes ➢ Py - Used by modules ➢ _Py - Internal Python interpreter use https://docs.python.org/3/c-api/intro.html ❖ Avoid using those prefixes ➢ Confuses the Interpreter ➢ Legibility ➢ Conflicts in future versions of Python
Objects ❖ Retorn PyObject * ➢ Reference to an opaque object ➢ Every Python object pointer can best cast to a PyObject ❖ Most python objects are allocated in the Heap ➢ Pymalloc != malloc static PyObject * https://docs.python.org/3/c-api/module.html
Objects PyObject* Py_BuildValue (const char *format, ...) return Py_BuildValue("s", "Hello Pythonist"); format C type c char f float i int d double format C type u Py_UNICODE* O PyObject* [...] ... {...} ...
How to create a module #include static PyObject * hello (PyObject *self) { return Py_BuildValue("s", "Hello Pythonist"); } static char docstring[] = "Hello world module for Python written in C";
Function entry type struct PyMethodDef { char *ml_name; /* Module name called by Python */ PyCFunction ml_meth; /* Function reference */ int ml_flags; /* Function parameters type */ char *ml_doc; /* Function description */ }; https://docs.python.org/3/c-api/structures.html#c.PyMethodDef
Extension Extension('module', ['hello.c']) ❖ Describes C/C++ extensions in setup.py ❖ Only a Python class with attributes ❖ When Extensions object exist, calls build_ext function cpython/Lib/distutils/extension.py
Symbol table $> objdump -t /path/to/lib/python2.7/site-packages/module.so | grep text 0000000000000690 l d .text 0000000000000000 .text 0000000000000690 l F .text 0000000000000000 deregister_tm_clones 00000000000006d0 l F .text 0000000000000000 register_tm_clones 0000000000000720 l F .text 0000000000000000 __do_global_dtors_aux 0000000000000760 l F .text 0000000000000000 frame_dummy 0000000000000790 l F .text 0000000000000015 hello 00000000000007b0 g F .text 000000000000001d initmodule $> man objdump
Experiments ❖ 1000 lists with 1000 random integers ranging from 1 to 1000 ❖ Compute times of execution of the module and of standard library ❖ Plot histograms with mean times