Debian Codegraph @

lib_pypy/hpy/devel/src/runtime/ctx_module.c

raw symbols SCIP
1 #include <Python.h>
2 #include "hpy.h"
3 #include "hpy/runtime/ctx_type.h"
4
5 #ifndef HPY_ABI_CPYTHON
6 // for _h2py and _py2h
7 # include "handles.h"
8 #endif
9
10 #define NON_DEFAULT_MESSAGE \
11 "This is not allowed because custom " \
12 "HPy_mod_create slot cannot return a builtin module " \
13 "object and cannot make any use of any other data " \
14 "defined in the HPyModuleDef. Either do not define " \
15 "HPy_mod_create slot and let the runtime create a builtin " \
16 "module object from the provided HPyModuleDef, or do not define " \
17 "anything else but the HPy_mod_create slot."
18
19 static PyModuleDef empty_moduledef = {
20 PyModuleDef_HEAD_INIT
21 };
22
23 static PyModuleDef_Slot* create_mod_slots(HPyModuleDef *hpydef, bool *found_create)
24 {
25 size_t slots_count = 0;
26 bool found_non_create = false;
27 for (int i = 0; hpydef->defines[i] != NULL; i++) {
28 HPyDef *src = hpydef->defines[i];
29 if (src->kind != HPyDef_Kind_Slot) {
30 found_non_create = true;
31 continue;
32 }
33 slots_count++;
34 if (src->slot.slot == HPy_mod_create) {
35 if (*found_create) {
36 PyErr_SetString(PyExc_SystemError, "Multiple definitions "
37 "of the HPy_mod_create "
38 "slot in HPyModuleDef.defines.");
39 return NULL;
40 }
41 *found_create = true;
42 } else if (src->slot.slot != HPy_mod_exec) {
43 PyErr_Format(PyExc_SystemError, "Unsupported slot in "
44 "HPyModuleDef.defines (value: %d).",
45 (int) src->slot.slot);
46 return NULL;
47 } else {
48 assert(src->slot.slot == HPy_mod_exec);
49 found_non_create = true;
50 }
51 }
52 if (*found_create && found_non_create) {
53 PyErr_Format(PyExc_SystemError,
54 "HPyModuleDef defines a HPy_mod_create slot and some other "
55 "slots or methods. " NON_DEFAULT_MESSAGE);
56 return NULL;
57 }
58
59 PyModuleDef_Slot* m_slots = (PyModuleDef_Slot*)PyMem_Calloc(
60 slots_count + 1, sizeof(PyModuleDef_Slot));
61 m_slots[slots_count].slot = 0;
62 m_slots[slots_count].value = NULL;
63 size_t slot_index = 0;
64 for (int i = 0; hpydef->defines[i] != NULL; i++) {
65 HPyDef *src = hpydef->defines[i];
66 if (src->kind != HPyDef_Kind_Slot)
67 continue;
68 int py_slot;
69 if (src->slot.slot == HPy_mod_create) {
70 py_slot = Py_mod_create;
71 } else {
72 assert(src->slot.slot == HPy_mod_exec);
73 py_slot = Py_mod_exec;
74 }
75 m_slots[slot_index].slot = py_slot;
76 m_slots[slot_index].value = (void*) src->slot.cpy_trampoline;
77 slot_index++;
78 }
79 return m_slots;
80 }
81
82 _HPy_HIDDEN PyModuleDef*
83 _HPyModuleDef_CreatePyModuleDef(HPyModuleDef *hpydef)
84 {
85 PyModuleDef *def = (PyModuleDef*)PyMem_Malloc(sizeof(PyModuleDef));
86 if (def == NULL) {
87 PyErr_NoMemory();
88 return NULL;
89 }
90 memcpy(def, &empty_moduledef, sizeof(PyModuleDef));
91 def->m_doc = hpydef->doc;
92 if (hpydef->size < 0) {
93 PyErr_SetString(PyExc_SystemError, "HPy does not permit "
94 "HPyModuleDef.size < 0");
95 goto error;
96 } else if (hpydef->size > 0) {
97 // GH-328
98 PyErr_SetString(PyExc_SystemError, "Module state is not supported yet "
99 "in HPy, set HPyModuleDef.size = 0 "
100 "if module state is not needed");
101 goto error;
102 }
103
104 def->m_methods = create_method_defs(hpydef->defines, hpydef->legacy_methods);
105 if (def->m_methods == NULL) {
106 goto error;
107 }
108
109 if (hpydef->defines != NULL) {
110 bool found_create = false;
111 def->m_slots = create_mod_slots(hpydef, &found_create);
112 if (def->m_slots == NULL) {
113 goto error;
114 }
115 if (found_create && (hpydef->legacy_methods || hpydef->size > 0 ||
116 hpydef->doc != NULL || hpydef->globals != NULL)) {
117 PyErr_Format(PyExc_SystemError,
118 "HPyModuleDef defines a HPy_mod_create slot and some "
119 "of the other fields are not set to their default "
120 "value. " NON_DEFAULT_MESSAGE);
121 goto error;
122 }
123 }
124
125 return def;
126 error:
127 if (def != NULL) {
128 PyMem_Free(def);
129 }
130 return NULL;
131 }
132
133 _HPy_HIDDEN PyObject*
134 _HPyModuleDef_AsPyInit(HPyModuleDef *hpydef)
135 {
136 PyModuleDef *def = _HPyModuleDef_CreatePyModuleDef(hpydef);
137 if (def == NULL) {
138 return NULL;
139 }
140 return PyModuleDef_Init(def);
141 }
142
143 _HPy_HIDDEN void
144 _HPyModule_CheckCreateSlotResult(PyObject **result)
145 {
146 if (*result != NULL && PyModule_Check(*result)) {
147 PyErr_SetString(PyExc_SystemError,
148 "HPy_mod_create slot returned a builtin module object. "
149 "This is currently not supported.");
150 *result = NULL;
151 }
152 }