Extending and Embedding Python, Release 3.9.5 21 2.1. Extending Python with C or C++ (continues on next page) (continued from previous page) return NULL ; sts =PySpam_System(command); return PyLong_FromLong(sts); } PyMODINIT_FUNC PyInit_spam( void ) { PyObject *m; static void *PySpam_API[PySpam_API_pointers]; PyObject *c_api_object; m =PyModule_Create(&spammodule); if (m==NULL ) return NULL ; /* Initialize the C API pointer array */ PySpam_API[PySpam_System_NUM] =( void *)PySpam_System; /* Create a Capsule containing the API pointer array's address */ c_api_object=PyCapsule_New(( void *)PySpam_API,"spam._C_API ",NULL ); if (PyModule_AddObject(m, "_C_API", c_api_object) <0) { Py_XDECREF(c_api_object); Py_DECREF(m); return NULL ; } return m; } #ifndef Py_SPAMMODULE_H #define Py_SPAMMODULE_H #ifdef __cplusplus extern "C"{ #endif /* Header file for spammodule */ /* C API functions */ #define PySpam_System_NUM 0 #define PySpam_System_RETURN int #define PySpam_System_PROTO (const char *command) The bulk of the work is in the header file$_{spammodule.h}$ , which looks like this: Note that$_{PySpam_API}$is declared$_{static}$; otherwise the pointer array would disappear when$_{PyInit_spam()}$ terminates! The #define is used to tell the header file that it is being included in the exporting module, not a client module. Finally, the module’s initialization function must take care of initializing the C API pointer array: two more lines must be added: In the beginning of the module, right after the line #include<Python.h> #define SPAM_MODULE #include"spammodule.h"