|
| 1 | +// C extension made of two C files (extension.c and static_inline.c) to detect |
| 2 | +// when a pythoncapi_compat.h function is implemented without "static inline". |
| 3 | + |
| 4 | +#ifndef MODULE_NAME |
| 5 | +# error "MODULE_NAME macro is not set" |
| 6 | +#endif |
| 7 | + |
| 8 | +#include "pythoncapi_compat.h" |
| 9 | + |
| 10 | +#define _STR(NAME) #NAME |
| 11 | +#define STR(NAME) _STR(NAME) |
| 12 | +#define _CONCAT(a, b) a ## b |
| 13 | +#define CONCAT(a, b) _CONCAT(a, b) |
| 14 | + |
| 15 | + |
| 16 | +// No method and no module exec method needed, most basic C extension |
| 17 | + |
| 18 | + |
| 19 | +// Use PySlot API on Python 3.15 and newer and on limited C API 3.15 and newer |
| 20 | +#if (PY_VERSION_HEX >= _Py_PACK_VERSION(3, 15) \ |
| 21 | + && (!defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= _Py_PACK_VERSION(3, 15))) |
| 22 | + |
| 23 | +PyABIInfo_VAR(abi_info); |
| 24 | + |
| 25 | +static PySlot module_slots[] = { |
| 26 | + PySlot_PTR_STATIC(Py_mod_abi, &abi_info), |
| 27 | + PySlot_PTR_STATIC(Py_mod_name, (void*)STR(MODULE_NAME)), |
| 28 | + PySlot_PTR_STATIC(Py_mod_state_size, 0), |
| 29 | + PySlot_PTR_STATIC(Py_mod_gil, Py_MOD_GIL_NOT_USED), |
| 30 | + {0, 0, {0}, {0}} |
| 31 | +}; |
| 32 | + |
| 33 | +#define EXPORT_FUNC CONCAT(PyModExport_, MODULE_NAME) |
| 34 | + |
| 35 | +PyMODEXPORT_FUNC |
| 36 | +EXPORT_FUNC(void) |
| 37 | +{ |
| 38 | + return module_slots; |
| 39 | +} |
| 40 | + |
| 41 | +#ifdef MS_WINDOWS |
| 42 | +# define INIT_FUNC CONCAT(PyInit_, MODULE_NAME) |
| 43 | + |
| 44 | +// Workaround for https://github.com/pypa/distutils/issues/387 |
| 45 | +// https://docs.python.org/3.16/howto/abi3t-migration.html#module-export-hook |
| 46 | +PyMODINIT_FUNC |
| 47 | +INIT_FUNC(void) |
| 48 | +{ |
| 49 | + PyErr_SetString(PyExc_SystemError, |
| 50 | + "PyInit_* called for module with PyModExport_*"); |
| 51 | + return NULL; |
| 52 | +} |
| 53 | +#endif |
| 54 | + |
| 55 | +#else |
| 56 | + |
| 57 | +static struct PyModuleDef module_def = { |
| 58 | + PyModuleDef_HEAD_INIT, |
| 59 | + STR(MODULE_NAME), // m_name |
| 60 | + _Py_NULL, // m_doc |
| 61 | + 0, // m_size |
| 62 | + _Py_NULL, // m_methods |
| 63 | + _Py_NULL, // m_slots |
| 64 | + _Py_NULL, // m_traverse |
| 65 | + _Py_NULL, // m_clear |
| 66 | + _Py_NULL, // m_free |
| 67 | +}; |
| 68 | + |
| 69 | + |
| 70 | +#define INIT_FUNC CONCAT(PyInit_, MODULE_NAME) |
| 71 | + |
| 72 | +PyMODINIT_FUNC |
| 73 | +INIT_FUNC(void) |
| 74 | +{ |
| 75 | + return PyModule_Create(&module_def); |
| 76 | +} |
| 77 | +#endif |
0 commit comments