Debian Codegraph @

src/c/cffi1_module.c

raw
1
2 #include "parse_c_type.c"
3 #include "realize_c_type.c"
4
5 #define CFFI_VERSION_MIN 0x2601
6 #define CFFI_VERSION_CHAR16CHAR32 0x2801
7 #define CFFI_VERSION_MAX 0x28FF
8
9 typedef struct FFIObject_s FFIObject;
10 typedef struct LibObject_s LibObject;
11
12 static PyTypeObject FFI_Type; /* forward */
13 static PyTypeObject Lib_Type; /* forward */
14
15 #include "ffi_obj.c"
16 #include "cglob.c"
17 #include "lib_obj.c"
18 #include "cdlopen.c"
19 #include "commontypes.c"
20 #include "call_python.c"
21
22
23 static int init_ffi_lib(PyObject *m)
24 {
25 PyObject *x;
26 int i, res;
27 static char init_done = 0;
28
29 if (!init_done) {
30 if (init_global_types_dict(FFI_Type.tp_dict) < 0)
31 return -1;
32
33 FFIError = PyErr_NewException("ffi.error", NULL, NULL);
34 if (FFIError == NULL)
35 return -1;
36 if (PyDict_SetItemString(FFI_Type.tp_dict, "error", FFIError) < 0)
37 return -1;
38 if (PyDict_SetItemString(FFI_Type.tp_dict, "CType",
39 (PyObject *)&CTypeDescr_Type) < 0)
40 return -1;
41 if (PyDict_SetItemString(FFI_Type.tp_dict, "CData",
42 (PyObject *)&CData_Type) < 0)
43 return -1;
44 if (PyDict_SetItemString(FFI_Type.tp_dict, "buffer",
45 (PyObject *)&MiniBuffer_Type) < 0)
46 return -1;
47
48 for (i = 0; all_dlopen_flags[i].name != NULL; i++) {
49 x = PyLong_FromLong(all_dlopen_flags[i].value);
50 if (x == NULL)
51 return -1;
52 res = PyDict_SetItemString(FFI_Type.tp_dict,
53 all_dlopen_flags[i].name, x);
54 Py_DECREF(x);
55 if (res < 0)
56 return -1;
57 }
58 init_done = 1;
59 }
60 return 0;
61 }
62
63 static int make_included_tuples(char *module_name,
64 const char *const *ctx_includes,
65 PyObject **included_ffis,
66 PyObject **included_libs)
67 {
68 Py_ssize_t num = 0;
69 const char *const *p_include;
70
71 if (ctx_includes == NULL)
72 return 0;
73
74 for (p_include = ctx_includes; *p_include; p_include++) {
75 num++;
76 }
77 *included_ffis = PyTuple_New(num);
78 *included_libs = PyTuple_New(num);
79 if (*included_ffis == NULL || *included_libs == NULL)
80 goto error;
81
82 num = 0;
83 for (p_include = ctx_includes; *p_include; p_include++) {
84 PyObject *included_ffi, *included_lib;
85 PyObject *m = PyImport_ImportModule(*p_include);
86 if (m == NULL)
87 goto import_error;
88
89 included_ffi = PyObject_GetAttrString(m, "ffi");
90 PyTuple_SET_ITEM(*included_ffis, num, included_ffi);
91
92 included_lib = (included_ffi == NULL) ? NULL :
93 PyObject_GetAttrString(m, "lib");
94 PyTuple_SET_ITEM(*included_libs, num, included_lib);
95
96 Py_DECREF(m);
97 if (included_lib == NULL)
98 goto import_error;
99
100 if (!FFIObject_Check(included_ffi) ||
101 !LibObject_Check(included_lib))
102 goto import_error;
103 num++;
104 }
105 return 0;
106
107 import_error:
108 PyErr_Format(PyExc_ImportError,
109 "while loading %.200s: failed to import ffi, lib from %.200s",
110 module_name, *p_include);
111 error:
112 Py_XDECREF(*included_ffis); *included_ffis = NULL;
113 Py_XDECREF(*included_libs); *included_libs = NULL;
114 return -1;
115 }
116
117 static PyObject *_my_Py_InitModule(char *module_name)
118 {
119 struct PyModuleDef *module_def, local_module_def = {
120 PyModuleDef_HEAD_INIT,
121 module_name,
122 NULL,
123 -1,
124 NULL, NULL, NULL, NULL, NULL
125 };
126 /* note: the 'module_def' is allocated dynamically and leaks,
127 but anyway the C extension module can never be unloaded */
128 module_def = PyMem_Malloc(sizeof(struct PyModuleDef));
129 if (module_def == NULL)
130 return PyErr_NoMemory();
131 *module_def = local_module_def;
132 PyObject *m = PyModule_Create(module_def);
133 # ifdef Py_GIL_DISABLED
134 if (m != NULL) {
135 PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED);
136 }
137 # endif
138 return m;
139 }
140
141 static PyObject *b_init_cffi_1_0_external_module(PyObject *self, PyObject *arg)
142 {
143 PyObject *m, *modules_dict;
144 FFIObject *ffi;
145 LibObject *lib;
146 Py_ssize_t version, num_exports;
147 char *module_name, *exports, *module_name_with_lib;
148 void **raw;
149 const struct _cffi_type_context_s *ctx;
150
151 raw = (void **)PyLong_AsVoidPtr(arg);
152 if (raw == NULL)
153 return NULL;
154
155 module_name = (char *)raw[0];
156 version = (Py_ssize_t)raw[1];
157 exports = (char *)raw[2];
158 ctx = (const struct _cffi_type_context_s *)raw[3];
159
160 if (version < CFFI_VERSION_MIN || version > CFFI_VERSION_MAX) {
161 if (!PyErr_Occurred())
162 PyErr_Format(PyExc_ImportError,
163 "cffi extension module '%s' uses an unknown version tag %p. "
164 "This module might need a more recent version of cffi "
165 "than the one currently installed, which is %s",
166 module_name, (void *)version, CFFI_VERSION);
167 return NULL;
168 }
169
170 /* initialize the exports array */
171 num_exports = 25;
172 if (ctx->flags & 1) /* set to mean that 'extern "Python"' is used */
173 num_exports = 26;
174 if (version >= CFFI_VERSION_CHAR16CHAR32)
175 num_exports = 28;
176 memcpy(exports, (char *)cffi_exports, num_exports * sizeof(void *));
177
178 /* make the module object */
179 m = _my_Py_InitModule(module_name);
180 if (m == NULL)
181 return NULL;
182
183 /* build the FFI and Lib object inside this new module */
184 ffi = ffi_internal_new(&FFI_Type, ctx);
185 Py_XINCREF(ffi); /* make the ffi object really immortal */
186 if (ffi == NULL || PyModule_AddObject(m, "ffi", (PyObject *)ffi) < 0)
187 return NULL;
188
189 lib = lib_internal_new(ffi, module_name, NULL, 0);
190 if (lib == NULL || PyModule_AddObject(m, "lib", (PyObject *)lib) < 0)
191 return NULL;
192
193 if (make_included_tuples(module_name, ctx->includes,
194 &ffi->types_builder.included_ffis,
195 &lib->l_types_builder->included_libs) < 0)
196 return NULL;
197
198 /* add manually 'module_name.lib' in sys.modules:
199 see test_import_from_lib */
200 modules_dict = PySys_GetObject("modules");
201 if (!modules_dict)
202 return NULL;
203 module_name_with_lib = alloca(strlen(module_name) + 5);
204 strcpy(module_name_with_lib, module_name);
205 strcat(module_name_with_lib, ".lib");
206 if (PyDict_SetItemString(modules_dict, module_name_with_lib,
207 (PyObject *)lib) < 0)
208 return NULL;
209
210 /* add manually 'module_name' in sys.modules: it seems that
211 Py_InitModule() is not enough to do that */
212 if (PyDict_SetItemString(modules_dict, module_name, m) < 0)
213 return NULL;
214
215 return m;
216 }