Extending and Embedding Python, Release 3.9.5 11 2.1. Extending Python with C or C++ (continues on next page) There are two more things to do before you can use your new extension: compiling and linking it with the Python system. If you use dynamic loading, the details may depend on the style of dynamic loading your system uses; see the chapters about building extension modules (chapter Building C and C++ Extensions ) and additional information that pertains only to building on Windows (chapter Building C and C++ Extensions on Windows ) for more information about this. If you can’t use dynamic loading, or if you want to make your module a permanent part of the Python interpreter, you will have to change the configuration setup and rebuild the interpreter. Luckily, this is very simple on Unix: just place your file ($_{spammodule.c}$ for example) in the$_{Modules/}$directory of an unpacked source distribution, add a line to the file$_{Modules/Setup.local}$ describing your file: spam spammodule.o and rebuild the interpreter by running make in the toplevel directory. You can also run make in the Modules/ subdirectory, but then you must first rebuild Makefile there by running ‘ make Makefile’. (This is necessary each time you change the$_{Setup}$file.) If your module requires additional libraries to link with, these can be listed on the line in the configuration file as well, for instance: spam spammodule.o -lX11 So far we have concentrated on making C functions callable from Python. The reverse is also useful: calling Python functions from C. This is especially the case for libraries that support so-called “callback” functions. If a C interface makes use of callbacks, the equivalent Python often needs to provide a callback mechanism to the Python program- mer; the implementation will require calling the Python callback functions from a C callback. Other uses are also imaginable. Fortunately, the Python interpreter is easily called recursively, and there is a standard interface to call a Python function. (I won’t dwell on how to call the Python parser with a particular string as input — if you’re interested, have a look at the implementation of the$_{-c}$command line option in$_{Modules/main.c}$ from the Python source code.) Calling a Python function is easy. First, the Python program must somehow pass you the Python function object. You should provide a function (or some other interface) to do this. When this function is called, save a pointer to the Python function object (be careful to$_{Py_INCREF()}$it!) in a global variable — or wherever you see fit. For example, the following function might be part of a module definition: 2.1.6 Calling Python Functions from C static PyObject *my_callback =NULL ; static PyObject * my_set_callback(PyObject *dummy, PyObject*args) { PyObject *result=NULL ; PyObject *temp; if (PyArg_ParseTuple(args, "O:set_callback ",&temp)) { if (!PyCallable_Check(temp)) { PyErr_SetString(PyExc_TypeError, "parameter must be callable "); return NULL ; } Py_XINCREF(temp); /* Add a reference to new callback */ Py_XDECREF(my_callback); /* Dispose of previous callback */ my_callback =temp; /* Remember new callback */ /* Boilerplate to return "None" */ Py_INCREF(Py_None); result=Py_None; 2.1.5 Compilation and Linkage