| 1 |
#include <Python.h> |
| 2 |
|
| 3 |
static int {modname}_exec(PyObject *module) |
| 4 |
{{ |
| 5 |
PyObject *tmp; |
| 6 |
// Use ImportModuleLevel with a non-empty fromlist so Python returns the |
| 7 |
// leaf submodule via sys.modules lookup, avoiding the top-down getattr |
| 8 |
// walk done by PyImport_ImportModule. That walk fails when the shim runs |
| 9 |
// during a parent package's __init__.py, because the parent's submodule |
| 10 |
// attribute isn't set until after __init__ returns. |
| 11 |
static PyObject *fromlist = NULL; |
| 12 |
if (!fromlist) {{ |
| 13 |
fromlist = Py_BuildValue("(s)", "*"); |
| 14 |
if (!fromlist) return -1; |
| 15 |
}} |
| 16 |
tmp = PyImport_ImportModuleLevel("{libname}", NULL, NULL, fromlist, 0); |
| 17 |
if (!tmp) return -1; |
| 18 |
// Populate cross-group export tables lazily, just before we instantiate |
| 19 |
// the per-module real init. Deferring this out of the shared lib's own |
| 20 |
// PyInit keeps separate-mode compiled modules from recursively triggering |
| 21 |
// sibling-package __init__.py mid-bootstrap. |
| 22 |
// PyObject_GetOptionalAttrString would be cleaner, but it's 3.13+ and the |
| 23 |
// shim deliberately #includes only <Python.h>, so we can't pull in the |
| 24 |
// pythoncapi_compat.h shim from lib-rt here. AttributeError + PyErr_Clear |
| 25 |
// is the portable form. |
| 26 |
PyObject *deps_capsule = PyObject_GetAttrString(tmp, "ensure_deps"); |
| 27 |
if (deps_capsule != NULL) {{ |
| 28 |
int (*deps_func)(void) = (int (*)(void))PyCapsule_GetPointer( |
| 29 |
deps_capsule, "{libname}.ensure_deps"); |
| 30 |
Py_DECREF(deps_capsule); |
| 31 |
if (deps_func == NULL) {{ |
| 32 |
Py_DECREF(tmp); |
| 33 |
return -1; |
| 34 |
}} |
| 35 |
if (deps_func() < 0) {{ |
| 36 |
Py_DECREF(tmp); |
| 37 |
return -1; |
| 38 |
}} |
| 39 |
}} else {{ |
| 40 |
PyErr_Clear(); |
| 41 |
}} |
| 42 |
PyObject *capsule = PyObject_GetAttrString(tmp, "exec_{full_modname}"); |
| 43 |
Py_DECREF(tmp); |
| 44 |
if (capsule == NULL) return -1; |
| 45 |
void *exec_func = PyCapsule_GetPointer(capsule, "{libname}.exec_{full_modname}"); |
| 46 |
Py_DECREF(capsule); |
| 47 |
if (!exec_func) return -1; |
| 48 |
if (((int (*)(PyObject *))exec_func)(module) != 0) return -1; |
| 49 |
return 0; |
| 50 |
}} |
| 51 |
|
| 52 |
static PyModuleDef_Slot {modname}_slots[] = {{ |
| 53 |
{{Py_mod_exec, {modname}_exec}}, |
| 54 |
{{Py_mod_multiple_interpreters, Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED}}, |
| 55 |
{{Py_mod_gil, Py_MOD_GIL_NOT_USED}}, |
| 56 |
{{0, NULL}}, |
| 57 |
}}; |
| 58 |
|
| 59 |
static struct PyModuleDef {modname}_module = {{ |
| 60 |
PyModuleDef_HEAD_INIT, |
| 61 |
.m_name = "{modname}", |
| 62 |
.m_doc = NULL, |
| 63 |
.m_methods = NULL, |
| 64 |
.m_size = 0, |
| 65 |
.m_slots = {modname}_slots, |
| 66 |
}}; |
| 67 |
|
| 68 |
PyMODINIT_FUNC |
| 69 |
PyInit_{modname}(void) |
| 70 |
{{ |
| 71 |
return PyModuleDef_Init(&{modname}_module); |
| 72 |
}} |
| 73 |
|
| 74 |
// distutils sometimes spuriously tells cl to export CPyInit___init__, |
| 75 |
// so provide that so it chills out |
| 76 |
PyMODINIT_FUNC PyInit___init__(void) {{ return PyInit_{modname}(); }} |