1 | n/a | |
---|
2 | n/a | /* Module object implementation */ |
---|
3 | n/a | |
---|
4 | n/a | #include "Python.h" |
---|
5 | n/a | #include "structmember.h" |
---|
6 | n/a | |
---|
7 | n/a | static Py_ssize_t max_module_number; |
---|
8 | n/a | |
---|
9 | n/a | typedef struct { |
---|
10 | n/a | PyObject_HEAD |
---|
11 | n/a | PyObject *md_dict; |
---|
12 | n/a | struct PyModuleDef *md_def; |
---|
13 | n/a | void *md_state; |
---|
14 | n/a | PyObject *md_weaklist; |
---|
15 | n/a | PyObject *md_name; /* for logging purposes after md_dict is cleared */ |
---|
16 | n/a | } PyModuleObject; |
---|
17 | n/a | |
---|
18 | n/a | static PyMemberDef module_members[] = { |
---|
19 | n/a | {"__dict__", T_OBJECT, offsetof(PyModuleObject, md_dict), READONLY}, |
---|
20 | n/a | {0} |
---|
21 | n/a | }; |
---|
22 | n/a | |
---|
23 | n/a | PyTypeObject PyModuleDef_Type = { |
---|
24 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
---|
25 | n/a | "moduledef", /* tp_name */ |
---|
26 | n/a | sizeof(struct PyModuleDef), /* tp_size */ |
---|
27 | n/a | 0, /* tp_itemsize */ |
---|
28 | n/a | }; |
---|
29 | n/a | |
---|
30 | n/a | |
---|
31 | n/a | PyObject* |
---|
32 | n/a | PyModuleDef_Init(struct PyModuleDef* def) |
---|
33 | n/a | { |
---|
34 | n/a | if (PyType_Ready(&PyModuleDef_Type) < 0) |
---|
35 | n/a | return NULL; |
---|
36 | n/a | if (def->m_base.m_index == 0) { |
---|
37 | n/a | max_module_number++; |
---|
38 | n/a | Py_REFCNT(def) = 1; |
---|
39 | n/a | Py_TYPE(def) = &PyModuleDef_Type; |
---|
40 | n/a | def->m_base.m_index = max_module_number; |
---|
41 | n/a | } |
---|
42 | n/a | return (PyObject*)def; |
---|
43 | n/a | } |
---|
44 | n/a | |
---|
45 | n/a | static int |
---|
46 | n/a | module_init_dict(PyModuleObject *mod, PyObject *md_dict, |
---|
47 | n/a | PyObject *name, PyObject *doc) |
---|
48 | n/a | { |
---|
49 | n/a | _Py_IDENTIFIER(__name__); |
---|
50 | n/a | _Py_IDENTIFIER(__doc__); |
---|
51 | n/a | _Py_IDENTIFIER(__package__); |
---|
52 | n/a | _Py_IDENTIFIER(__loader__); |
---|
53 | n/a | _Py_IDENTIFIER(__spec__); |
---|
54 | n/a | |
---|
55 | n/a | if (md_dict == NULL) |
---|
56 | n/a | return -1; |
---|
57 | n/a | if (doc == NULL) |
---|
58 | n/a | doc = Py_None; |
---|
59 | n/a | |
---|
60 | n/a | if (_PyDict_SetItemId(md_dict, &PyId___name__, name) != 0) |
---|
61 | n/a | return -1; |
---|
62 | n/a | if (_PyDict_SetItemId(md_dict, &PyId___doc__, doc) != 0) |
---|
63 | n/a | return -1; |
---|
64 | n/a | if (_PyDict_SetItemId(md_dict, &PyId___package__, Py_None) != 0) |
---|
65 | n/a | return -1; |
---|
66 | n/a | if (_PyDict_SetItemId(md_dict, &PyId___loader__, Py_None) != 0) |
---|
67 | n/a | return -1; |
---|
68 | n/a | if (_PyDict_SetItemId(md_dict, &PyId___spec__, Py_None) != 0) |
---|
69 | n/a | return -1; |
---|
70 | n/a | if (PyUnicode_CheckExact(name)) { |
---|
71 | n/a | Py_INCREF(name); |
---|
72 | n/a | Py_XSETREF(mod->md_name, name); |
---|
73 | n/a | } |
---|
74 | n/a | |
---|
75 | n/a | return 0; |
---|
76 | n/a | } |
---|
77 | n/a | |
---|
78 | n/a | |
---|
79 | n/a | PyObject * |
---|
80 | n/a | PyModule_NewObject(PyObject *name) |
---|
81 | n/a | { |
---|
82 | n/a | PyModuleObject *m; |
---|
83 | n/a | m = PyObject_GC_New(PyModuleObject, &PyModule_Type); |
---|
84 | n/a | if (m == NULL) |
---|
85 | n/a | return NULL; |
---|
86 | n/a | m->md_def = NULL; |
---|
87 | n/a | m->md_state = NULL; |
---|
88 | n/a | m->md_weaklist = NULL; |
---|
89 | n/a | m->md_name = NULL; |
---|
90 | n/a | m->md_dict = PyDict_New(); |
---|
91 | n/a | if (module_init_dict(m, m->md_dict, name, NULL) != 0) |
---|
92 | n/a | goto fail; |
---|
93 | n/a | PyObject_GC_Track(m); |
---|
94 | n/a | return (PyObject *)m; |
---|
95 | n/a | |
---|
96 | n/a | fail: |
---|
97 | n/a | Py_DECREF(m); |
---|
98 | n/a | return NULL; |
---|
99 | n/a | } |
---|
100 | n/a | |
---|
101 | n/a | PyObject * |
---|
102 | n/a | PyModule_New(const char *name) |
---|
103 | n/a | { |
---|
104 | n/a | PyObject *nameobj, *module; |
---|
105 | n/a | nameobj = PyUnicode_FromString(name); |
---|
106 | n/a | if (nameobj == NULL) |
---|
107 | n/a | return NULL; |
---|
108 | n/a | module = PyModule_NewObject(nameobj); |
---|
109 | n/a | Py_DECREF(nameobj); |
---|
110 | n/a | return module; |
---|
111 | n/a | } |
---|
112 | n/a | |
---|
113 | n/a | /* Check API/ABI version |
---|
114 | n/a | * Issues a warning on mismatch, which is usually not fatal. |
---|
115 | n/a | * Returns 0 if an exception is raised. |
---|
116 | n/a | */ |
---|
117 | n/a | static int |
---|
118 | n/a | check_api_version(const char *name, int module_api_version) |
---|
119 | n/a | { |
---|
120 | n/a | if (module_api_version != PYTHON_API_VERSION && module_api_version != PYTHON_ABI_VERSION) { |
---|
121 | n/a | int err; |
---|
122 | n/a | err = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
---|
123 | n/a | "Python C API version mismatch for module %.100s: " |
---|
124 | n/a | "This Python has API version %d, module %.100s has version %d.", |
---|
125 | n/a | name, |
---|
126 | n/a | PYTHON_API_VERSION, name, module_api_version); |
---|
127 | n/a | if (err) |
---|
128 | n/a | return 0; |
---|
129 | n/a | } |
---|
130 | n/a | return 1; |
---|
131 | n/a | } |
---|
132 | n/a | |
---|
133 | n/a | static int |
---|
134 | n/a | _add_methods_to_object(PyObject *module, PyObject *name, PyMethodDef *functions) |
---|
135 | n/a | { |
---|
136 | n/a | PyObject *func; |
---|
137 | n/a | PyMethodDef *fdef; |
---|
138 | n/a | |
---|
139 | n/a | for (fdef = functions; fdef->ml_name != NULL; fdef++) { |
---|
140 | n/a | if ((fdef->ml_flags & METH_CLASS) || |
---|
141 | n/a | (fdef->ml_flags & METH_STATIC)) { |
---|
142 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
143 | n/a | "module functions cannot set" |
---|
144 | n/a | " METH_CLASS or METH_STATIC"); |
---|
145 | n/a | return -1; |
---|
146 | n/a | } |
---|
147 | n/a | func = PyCFunction_NewEx(fdef, (PyObject*)module, name); |
---|
148 | n/a | if (func == NULL) { |
---|
149 | n/a | return -1; |
---|
150 | n/a | } |
---|
151 | n/a | if (PyObject_SetAttrString(module, fdef->ml_name, func) != 0) { |
---|
152 | n/a | Py_DECREF(func); |
---|
153 | n/a | return -1; |
---|
154 | n/a | } |
---|
155 | n/a | Py_DECREF(func); |
---|
156 | n/a | } |
---|
157 | n/a | |
---|
158 | n/a | return 0; |
---|
159 | n/a | } |
---|
160 | n/a | |
---|
161 | n/a | PyObject * |
---|
162 | n/a | PyModule_Create2(struct PyModuleDef* module, int module_api_version) |
---|
163 | n/a | { |
---|
164 | n/a | const char* name; |
---|
165 | n/a | PyModuleObject *m; |
---|
166 | n/a | PyInterpreterState *interp = PyThreadState_Get()->interp; |
---|
167 | n/a | if (interp->modules == NULL) |
---|
168 | n/a | Py_FatalError("Python import machinery not initialized"); |
---|
169 | n/a | if (!PyModuleDef_Init(module)) |
---|
170 | n/a | return NULL; |
---|
171 | n/a | name = module->m_name; |
---|
172 | n/a | if (!check_api_version(name, module_api_version)) { |
---|
173 | n/a | return NULL; |
---|
174 | n/a | } |
---|
175 | n/a | if (module->m_slots) { |
---|
176 | n/a | PyErr_Format( |
---|
177 | n/a | PyExc_SystemError, |
---|
178 | n/a | "module %s: PyModule_Create is incompatible with m_slots", name); |
---|
179 | n/a | return NULL; |
---|
180 | n/a | } |
---|
181 | n/a | /* Make sure name is fully qualified. |
---|
182 | n/a | |
---|
183 | n/a | This is a bit of a hack: when the shared library is loaded, |
---|
184 | n/a | the module name is "package.module", but the module calls |
---|
185 | n/a | PyModule_Create*() with just "module" for the name. The shared |
---|
186 | n/a | library loader squirrels away the true name of the module in |
---|
187 | n/a | _Py_PackageContext, and PyModule_Create*() will substitute this |
---|
188 | n/a | (if the name actually matches). |
---|
189 | n/a | */ |
---|
190 | n/a | if (_Py_PackageContext != NULL) { |
---|
191 | n/a | const char *p = strrchr(_Py_PackageContext, '.'); |
---|
192 | n/a | if (p != NULL && strcmp(module->m_name, p+1) == 0) { |
---|
193 | n/a | name = _Py_PackageContext; |
---|
194 | n/a | _Py_PackageContext = NULL; |
---|
195 | n/a | } |
---|
196 | n/a | } |
---|
197 | n/a | if ((m = (PyModuleObject*)PyModule_New(name)) == NULL) |
---|
198 | n/a | return NULL; |
---|
199 | n/a | |
---|
200 | n/a | if (module->m_size > 0) { |
---|
201 | n/a | m->md_state = PyMem_MALLOC(module->m_size); |
---|
202 | n/a | if (!m->md_state) { |
---|
203 | n/a | PyErr_NoMemory(); |
---|
204 | n/a | Py_DECREF(m); |
---|
205 | n/a | return NULL; |
---|
206 | n/a | } |
---|
207 | n/a | memset(m->md_state, 0, module->m_size); |
---|
208 | n/a | } |
---|
209 | n/a | |
---|
210 | n/a | if (module->m_methods != NULL) { |
---|
211 | n/a | if (PyModule_AddFunctions((PyObject *) m, module->m_methods) != 0) { |
---|
212 | n/a | Py_DECREF(m); |
---|
213 | n/a | return NULL; |
---|
214 | n/a | } |
---|
215 | n/a | } |
---|
216 | n/a | if (module->m_doc != NULL) { |
---|
217 | n/a | if (PyModule_SetDocString((PyObject *) m, module->m_doc) != 0) { |
---|
218 | n/a | Py_DECREF(m); |
---|
219 | n/a | return NULL; |
---|
220 | n/a | } |
---|
221 | n/a | } |
---|
222 | n/a | m->md_def = module; |
---|
223 | n/a | return (PyObject*)m; |
---|
224 | n/a | } |
---|
225 | n/a | |
---|
226 | n/a | PyObject * |
---|
227 | n/a | PyModule_FromDefAndSpec2(struct PyModuleDef* def, PyObject *spec, int module_api_version) |
---|
228 | n/a | { |
---|
229 | n/a | PyModuleDef_Slot* cur_slot; |
---|
230 | n/a | PyObject *(*create)(PyObject *, PyModuleDef*) = NULL; |
---|
231 | n/a | PyObject *nameobj; |
---|
232 | n/a | PyObject *m = NULL; |
---|
233 | n/a | int has_execution_slots = 0; |
---|
234 | n/a | const char *name; |
---|
235 | n/a | int ret; |
---|
236 | n/a | |
---|
237 | n/a | PyModuleDef_Init(def); |
---|
238 | n/a | |
---|
239 | n/a | nameobj = PyObject_GetAttrString(spec, "name"); |
---|
240 | n/a | if (nameobj == NULL) { |
---|
241 | n/a | return NULL; |
---|
242 | n/a | } |
---|
243 | n/a | name = PyUnicode_AsUTF8(nameobj); |
---|
244 | n/a | if (name == NULL) { |
---|
245 | n/a | goto error; |
---|
246 | n/a | } |
---|
247 | n/a | |
---|
248 | n/a | if (!check_api_version(name, module_api_version)) { |
---|
249 | n/a | goto error; |
---|
250 | n/a | } |
---|
251 | n/a | |
---|
252 | n/a | if (def->m_size < 0) { |
---|
253 | n/a | PyErr_Format( |
---|
254 | n/a | PyExc_SystemError, |
---|
255 | n/a | "module %s: m_size may not be negative for multi-phase initialization", |
---|
256 | n/a | name); |
---|
257 | n/a | goto error; |
---|
258 | n/a | } |
---|
259 | n/a | |
---|
260 | n/a | for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) { |
---|
261 | n/a | if (cur_slot->slot == Py_mod_create) { |
---|
262 | n/a | if (create) { |
---|
263 | n/a | PyErr_Format( |
---|
264 | n/a | PyExc_SystemError, |
---|
265 | n/a | "module %s has multiple create slots", |
---|
266 | n/a | name); |
---|
267 | n/a | goto error; |
---|
268 | n/a | } |
---|
269 | n/a | create = cur_slot->value; |
---|
270 | n/a | } else if (cur_slot->slot < 0 || cur_slot->slot > _Py_mod_LAST_SLOT) { |
---|
271 | n/a | PyErr_Format( |
---|
272 | n/a | PyExc_SystemError, |
---|
273 | n/a | "module %s uses unknown slot ID %i", |
---|
274 | n/a | name, cur_slot->slot); |
---|
275 | n/a | goto error; |
---|
276 | n/a | } else { |
---|
277 | n/a | has_execution_slots = 1; |
---|
278 | n/a | } |
---|
279 | n/a | } |
---|
280 | n/a | |
---|
281 | n/a | if (create) { |
---|
282 | n/a | m = create(spec, def); |
---|
283 | n/a | if (m == NULL) { |
---|
284 | n/a | if (!PyErr_Occurred()) { |
---|
285 | n/a | PyErr_Format( |
---|
286 | n/a | PyExc_SystemError, |
---|
287 | n/a | "creation of module %s failed without setting an exception", |
---|
288 | n/a | name); |
---|
289 | n/a | } |
---|
290 | n/a | goto error; |
---|
291 | n/a | } else { |
---|
292 | n/a | if (PyErr_Occurred()) { |
---|
293 | n/a | PyErr_Format(PyExc_SystemError, |
---|
294 | n/a | "creation of module %s raised unreported exception", |
---|
295 | n/a | name); |
---|
296 | n/a | goto error; |
---|
297 | n/a | } |
---|
298 | n/a | } |
---|
299 | n/a | } else { |
---|
300 | n/a | m = PyModule_NewObject(nameobj); |
---|
301 | n/a | if (m == NULL) { |
---|
302 | n/a | goto error; |
---|
303 | n/a | } |
---|
304 | n/a | } |
---|
305 | n/a | |
---|
306 | n/a | if (PyModule_Check(m)) { |
---|
307 | n/a | ((PyModuleObject*)m)->md_state = NULL; |
---|
308 | n/a | ((PyModuleObject*)m)->md_def = def; |
---|
309 | n/a | } else { |
---|
310 | n/a | if (def->m_size > 0 || def->m_traverse || def->m_clear || def->m_free) { |
---|
311 | n/a | PyErr_Format( |
---|
312 | n/a | PyExc_SystemError, |
---|
313 | n/a | "module %s is not a module object, but requests module state", |
---|
314 | n/a | name); |
---|
315 | n/a | goto error; |
---|
316 | n/a | } |
---|
317 | n/a | if (has_execution_slots) { |
---|
318 | n/a | PyErr_Format( |
---|
319 | n/a | PyExc_SystemError, |
---|
320 | n/a | "module %s specifies execution slots, but did not create " |
---|
321 | n/a | "a ModuleType instance", |
---|
322 | n/a | name); |
---|
323 | n/a | goto error; |
---|
324 | n/a | } |
---|
325 | n/a | } |
---|
326 | n/a | |
---|
327 | n/a | if (def->m_methods != NULL) { |
---|
328 | n/a | ret = _add_methods_to_object(m, nameobj, def->m_methods); |
---|
329 | n/a | if (ret != 0) { |
---|
330 | n/a | goto error; |
---|
331 | n/a | } |
---|
332 | n/a | } |
---|
333 | n/a | |
---|
334 | n/a | if (def->m_doc != NULL) { |
---|
335 | n/a | ret = PyModule_SetDocString(m, def->m_doc); |
---|
336 | n/a | if (ret != 0) { |
---|
337 | n/a | goto error; |
---|
338 | n/a | } |
---|
339 | n/a | } |
---|
340 | n/a | |
---|
341 | n/a | Py_DECREF(nameobj); |
---|
342 | n/a | return m; |
---|
343 | n/a | |
---|
344 | n/a | error: |
---|
345 | n/a | Py_DECREF(nameobj); |
---|
346 | n/a | Py_XDECREF(m); |
---|
347 | n/a | return NULL; |
---|
348 | n/a | } |
---|
349 | n/a | |
---|
350 | n/a | int |
---|
351 | n/a | PyModule_ExecDef(PyObject *module, PyModuleDef *def) |
---|
352 | n/a | { |
---|
353 | n/a | PyModuleDef_Slot *cur_slot; |
---|
354 | n/a | const char *name; |
---|
355 | n/a | int ret; |
---|
356 | n/a | |
---|
357 | n/a | name = PyModule_GetName(module); |
---|
358 | n/a | if (name == NULL) { |
---|
359 | n/a | return -1; |
---|
360 | n/a | } |
---|
361 | n/a | |
---|
362 | n/a | if (def->m_size >= 0) { |
---|
363 | n/a | PyModuleObject *md = (PyModuleObject*)module; |
---|
364 | n/a | if (md->md_state == NULL) { |
---|
365 | n/a | /* Always set a state pointer; this serves as a marker to skip |
---|
366 | n/a | * multiple initialization (importlib.reload() is no-op) */ |
---|
367 | n/a | md->md_state = PyMem_MALLOC(def->m_size); |
---|
368 | n/a | if (!md->md_state) { |
---|
369 | n/a | PyErr_NoMemory(); |
---|
370 | n/a | return -1; |
---|
371 | n/a | } |
---|
372 | n/a | memset(md->md_state, 0, def->m_size); |
---|
373 | n/a | } |
---|
374 | n/a | } |
---|
375 | n/a | |
---|
376 | n/a | if (def->m_slots == NULL) { |
---|
377 | n/a | return 0; |
---|
378 | n/a | } |
---|
379 | n/a | |
---|
380 | n/a | for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) { |
---|
381 | n/a | switch (cur_slot->slot) { |
---|
382 | n/a | case Py_mod_create: |
---|
383 | n/a | /* handled in PyModule_FromDefAndSpec2 */ |
---|
384 | n/a | break; |
---|
385 | n/a | case Py_mod_exec: |
---|
386 | n/a | ret = ((int (*)(PyObject *))cur_slot->value)(module); |
---|
387 | n/a | if (ret != 0) { |
---|
388 | n/a | if (!PyErr_Occurred()) { |
---|
389 | n/a | PyErr_Format( |
---|
390 | n/a | PyExc_SystemError, |
---|
391 | n/a | "execution of module %s failed without setting an exception", |
---|
392 | n/a | name); |
---|
393 | n/a | } |
---|
394 | n/a | return -1; |
---|
395 | n/a | } |
---|
396 | n/a | if (PyErr_Occurred()) { |
---|
397 | n/a | PyErr_Format( |
---|
398 | n/a | PyExc_SystemError, |
---|
399 | n/a | "execution of module %s raised unreported exception", |
---|
400 | n/a | name); |
---|
401 | n/a | return -1; |
---|
402 | n/a | } |
---|
403 | n/a | break; |
---|
404 | n/a | default: |
---|
405 | n/a | PyErr_Format( |
---|
406 | n/a | PyExc_SystemError, |
---|
407 | n/a | "module %s initialized with unknown slot %i", |
---|
408 | n/a | name, cur_slot->slot); |
---|
409 | n/a | return -1; |
---|
410 | n/a | } |
---|
411 | n/a | } |
---|
412 | n/a | return 0; |
---|
413 | n/a | } |
---|
414 | n/a | |
---|
415 | n/a | int |
---|
416 | n/a | PyModule_AddFunctions(PyObject *m, PyMethodDef *functions) |
---|
417 | n/a | { |
---|
418 | n/a | int res; |
---|
419 | n/a | PyObject *name = PyModule_GetNameObject(m); |
---|
420 | n/a | if (name == NULL) { |
---|
421 | n/a | return -1; |
---|
422 | n/a | } |
---|
423 | n/a | |
---|
424 | n/a | res = _add_methods_to_object(m, name, functions); |
---|
425 | n/a | Py_DECREF(name); |
---|
426 | n/a | return res; |
---|
427 | n/a | } |
---|
428 | n/a | |
---|
429 | n/a | int |
---|
430 | n/a | PyModule_SetDocString(PyObject *m, const char *doc) |
---|
431 | n/a | { |
---|
432 | n/a | PyObject *v; |
---|
433 | n/a | _Py_IDENTIFIER(__doc__); |
---|
434 | n/a | |
---|
435 | n/a | v = PyUnicode_FromString(doc); |
---|
436 | n/a | if (v == NULL || _PyObject_SetAttrId(m, &PyId___doc__, v) != 0) { |
---|
437 | n/a | Py_XDECREF(v); |
---|
438 | n/a | return -1; |
---|
439 | n/a | } |
---|
440 | n/a | Py_DECREF(v); |
---|
441 | n/a | return 0; |
---|
442 | n/a | } |
---|
443 | n/a | |
---|
444 | n/a | PyObject * |
---|
445 | n/a | PyModule_GetDict(PyObject *m) |
---|
446 | n/a | { |
---|
447 | n/a | PyObject *d; |
---|
448 | n/a | if (!PyModule_Check(m)) { |
---|
449 | n/a | PyErr_BadInternalCall(); |
---|
450 | n/a | return NULL; |
---|
451 | n/a | } |
---|
452 | n/a | d = ((PyModuleObject *)m) -> md_dict; |
---|
453 | n/a | assert(d != NULL); |
---|
454 | n/a | return d; |
---|
455 | n/a | } |
---|
456 | n/a | |
---|
457 | n/a | PyObject* |
---|
458 | n/a | PyModule_GetNameObject(PyObject *m) |
---|
459 | n/a | { |
---|
460 | n/a | _Py_IDENTIFIER(__name__); |
---|
461 | n/a | PyObject *d; |
---|
462 | n/a | PyObject *name; |
---|
463 | n/a | if (!PyModule_Check(m)) { |
---|
464 | n/a | PyErr_BadArgument(); |
---|
465 | n/a | return NULL; |
---|
466 | n/a | } |
---|
467 | n/a | d = ((PyModuleObject *)m)->md_dict; |
---|
468 | n/a | if (d == NULL || |
---|
469 | n/a | (name = _PyDict_GetItemId(d, &PyId___name__)) == NULL || |
---|
470 | n/a | !PyUnicode_Check(name)) |
---|
471 | n/a | { |
---|
472 | n/a | PyErr_SetString(PyExc_SystemError, "nameless module"); |
---|
473 | n/a | return NULL; |
---|
474 | n/a | } |
---|
475 | n/a | Py_INCREF(name); |
---|
476 | n/a | return name; |
---|
477 | n/a | } |
---|
478 | n/a | |
---|
479 | n/a | const char * |
---|
480 | n/a | PyModule_GetName(PyObject *m) |
---|
481 | n/a | { |
---|
482 | n/a | PyObject *name = PyModule_GetNameObject(m); |
---|
483 | n/a | if (name == NULL) |
---|
484 | n/a | return NULL; |
---|
485 | n/a | Py_DECREF(name); /* module dict has still a reference */ |
---|
486 | n/a | return PyUnicode_AsUTF8(name); |
---|
487 | n/a | } |
---|
488 | n/a | |
---|
489 | n/a | PyObject* |
---|
490 | n/a | PyModule_GetFilenameObject(PyObject *m) |
---|
491 | n/a | { |
---|
492 | n/a | _Py_IDENTIFIER(__file__); |
---|
493 | n/a | PyObject *d; |
---|
494 | n/a | PyObject *fileobj; |
---|
495 | n/a | if (!PyModule_Check(m)) { |
---|
496 | n/a | PyErr_BadArgument(); |
---|
497 | n/a | return NULL; |
---|
498 | n/a | } |
---|
499 | n/a | d = ((PyModuleObject *)m)->md_dict; |
---|
500 | n/a | if (d == NULL || |
---|
501 | n/a | (fileobj = _PyDict_GetItemId(d, &PyId___file__)) == NULL || |
---|
502 | n/a | !PyUnicode_Check(fileobj)) |
---|
503 | n/a | { |
---|
504 | n/a | PyErr_SetString(PyExc_SystemError, "module filename missing"); |
---|
505 | n/a | return NULL; |
---|
506 | n/a | } |
---|
507 | n/a | Py_INCREF(fileobj); |
---|
508 | n/a | return fileobj; |
---|
509 | n/a | } |
---|
510 | n/a | |
---|
511 | n/a | const char * |
---|
512 | n/a | PyModule_GetFilename(PyObject *m) |
---|
513 | n/a | { |
---|
514 | n/a | PyObject *fileobj; |
---|
515 | n/a | const char *utf8; |
---|
516 | n/a | fileobj = PyModule_GetFilenameObject(m); |
---|
517 | n/a | if (fileobj == NULL) |
---|
518 | n/a | return NULL; |
---|
519 | n/a | utf8 = PyUnicode_AsUTF8(fileobj); |
---|
520 | n/a | Py_DECREF(fileobj); /* module dict has still a reference */ |
---|
521 | n/a | return utf8; |
---|
522 | n/a | } |
---|
523 | n/a | |
---|
524 | n/a | PyModuleDef* |
---|
525 | n/a | PyModule_GetDef(PyObject* m) |
---|
526 | n/a | { |
---|
527 | n/a | if (!PyModule_Check(m)) { |
---|
528 | n/a | PyErr_BadArgument(); |
---|
529 | n/a | return NULL; |
---|
530 | n/a | } |
---|
531 | n/a | return ((PyModuleObject *)m)->md_def; |
---|
532 | n/a | } |
---|
533 | n/a | |
---|
534 | n/a | void* |
---|
535 | n/a | PyModule_GetState(PyObject* m) |
---|
536 | n/a | { |
---|
537 | n/a | if (!PyModule_Check(m)) { |
---|
538 | n/a | PyErr_BadArgument(); |
---|
539 | n/a | return NULL; |
---|
540 | n/a | } |
---|
541 | n/a | return ((PyModuleObject *)m)->md_state; |
---|
542 | n/a | } |
---|
543 | n/a | |
---|
544 | n/a | void |
---|
545 | n/a | _PyModule_Clear(PyObject *m) |
---|
546 | n/a | { |
---|
547 | n/a | PyObject *d = ((PyModuleObject *)m)->md_dict; |
---|
548 | n/a | if (d != NULL) |
---|
549 | n/a | _PyModule_ClearDict(d); |
---|
550 | n/a | } |
---|
551 | n/a | |
---|
552 | n/a | void |
---|
553 | n/a | _PyModule_ClearDict(PyObject *d) |
---|
554 | n/a | { |
---|
555 | n/a | /* To make the execution order of destructors for global |
---|
556 | n/a | objects a bit more predictable, we first zap all objects |
---|
557 | n/a | whose name starts with a single underscore, before we clear |
---|
558 | n/a | the entire dictionary. We zap them by replacing them with |
---|
559 | n/a | None, rather than deleting them from the dictionary, to |
---|
560 | n/a | avoid rehashing the dictionary (to some extent). */ |
---|
561 | n/a | |
---|
562 | n/a | Py_ssize_t pos; |
---|
563 | n/a | PyObject *key, *value; |
---|
564 | n/a | |
---|
565 | n/a | /* First, clear only names starting with a single underscore */ |
---|
566 | n/a | pos = 0; |
---|
567 | n/a | while (PyDict_Next(d, &pos, &key, &value)) { |
---|
568 | n/a | if (value != Py_None && PyUnicode_Check(key)) { |
---|
569 | n/a | if (PyUnicode_READ_CHAR(key, 0) == '_' && |
---|
570 | n/a | PyUnicode_READ_CHAR(key, 1) != '_') { |
---|
571 | n/a | if (Py_VerboseFlag > 1) { |
---|
572 | n/a | const char *s = PyUnicode_AsUTF8(key); |
---|
573 | n/a | if (s != NULL) |
---|
574 | n/a | PySys_WriteStderr("# clear[1] %s\n", s); |
---|
575 | n/a | else |
---|
576 | n/a | PyErr_Clear(); |
---|
577 | n/a | } |
---|
578 | n/a | if (PyDict_SetItem(d, key, Py_None) != 0) |
---|
579 | n/a | PyErr_Clear(); |
---|
580 | n/a | } |
---|
581 | n/a | } |
---|
582 | n/a | } |
---|
583 | n/a | |
---|
584 | n/a | /* Next, clear all names except for __builtins__ */ |
---|
585 | n/a | pos = 0; |
---|
586 | n/a | while (PyDict_Next(d, &pos, &key, &value)) { |
---|
587 | n/a | if (value != Py_None && PyUnicode_Check(key)) { |
---|
588 | n/a | if (PyUnicode_READ_CHAR(key, 0) != '_' || |
---|
589 | n/a | !_PyUnicode_EqualToASCIIString(key, "__builtins__")) |
---|
590 | n/a | { |
---|
591 | n/a | if (Py_VerboseFlag > 1) { |
---|
592 | n/a | const char *s = PyUnicode_AsUTF8(key); |
---|
593 | n/a | if (s != NULL) |
---|
594 | n/a | PySys_WriteStderr("# clear[2] %s\n", s); |
---|
595 | n/a | else |
---|
596 | n/a | PyErr_Clear(); |
---|
597 | n/a | } |
---|
598 | n/a | if (PyDict_SetItem(d, key, Py_None) != 0) |
---|
599 | n/a | PyErr_Clear(); |
---|
600 | n/a | } |
---|
601 | n/a | } |
---|
602 | n/a | } |
---|
603 | n/a | |
---|
604 | n/a | /* Note: we leave __builtins__ in place, so that destructors |
---|
605 | n/a | of non-global objects defined in this module can still use |
---|
606 | n/a | builtins, in particularly 'None'. */ |
---|
607 | n/a | |
---|
608 | n/a | } |
---|
609 | n/a | |
---|
610 | n/a | /* Methods */ |
---|
611 | n/a | |
---|
612 | n/a | static int |
---|
613 | n/a | module_init(PyModuleObject *m, PyObject *args, PyObject *kwds) |
---|
614 | n/a | { |
---|
615 | n/a | static char *kwlist[] = {"name", "doc", NULL}; |
---|
616 | n/a | PyObject *dict, *name = Py_None, *doc = Py_None; |
---|
617 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "U|O:module.__init__", |
---|
618 | n/a | kwlist, &name, &doc)) |
---|
619 | n/a | return -1; |
---|
620 | n/a | dict = m->md_dict; |
---|
621 | n/a | if (dict == NULL) { |
---|
622 | n/a | dict = PyDict_New(); |
---|
623 | n/a | if (dict == NULL) |
---|
624 | n/a | return -1; |
---|
625 | n/a | m->md_dict = dict; |
---|
626 | n/a | } |
---|
627 | n/a | if (module_init_dict(m, dict, name, doc) < 0) |
---|
628 | n/a | return -1; |
---|
629 | n/a | return 0; |
---|
630 | n/a | } |
---|
631 | n/a | |
---|
632 | n/a | static void |
---|
633 | n/a | module_dealloc(PyModuleObject *m) |
---|
634 | n/a | { |
---|
635 | n/a | PyObject_GC_UnTrack(m); |
---|
636 | n/a | if (Py_VerboseFlag && m->md_name) { |
---|
637 | n/a | PySys_FormatStderr("# destroy %S\n", m->md_name); |
---|
638 | n/a | } |
---|
639 | n/a | if (m->md_weaklist != NULL) |
---|
640 | n/a | PyObject_ClearWeakRefs((PyObject *) m); |
---|
641 | n/a | if (m->md_def && m->md_def->m_free) |
---|
642 | n/a | m->md_def->m_free(m); |
---|
643 | n/a | Py_XDECREF(m->md_dict); |
---|
644 | n/a | Py_XDECREF(m->md_name); |
---|
645 | n/a | if (m->md_state != NULL) |
---|
646 | n/a | PyMem_FREE(m->md_state); |
---|
647 | n/a | Py_TYPE(m)->tp_free((PyObject *)m); |
---|
648 | n/a | } |
---|
649 | n/a | |
---|
650 | n/a | static PyObject * |
---|
651 | n/a | module_repr(PyModuleObject *m) |
---|
652 | n/a | { |
---|
653 | n/a | PyThreadState *tstate = PyThreadState_GET(); |
---|
654 | n/a | PyInterpreterState *interp = tstate->interp; |
---|
655 | n/a | |
---|
656 | n/a | return PyObject_CallMethod(interp->importlib, "_module_repr", "O", m); |
---|
657 | n/a | } |
---|
658 | n/a | |
---|
659 | n/a | static PyObject* |
---|
660 | n/a | module_getattro(PyModuleObject *m, PyObject *name) |
---|
661 | n/a | { |
---|
662 | n/a | PyObject *attr, *mod_name; |
---|
663 | n/a | attr = PyObject_GenericGetAttr((PyObject *)m, name); |
---|
664 | n/a | if (attr || !PyErr_ExceptionMatches(PyExc_AttributeError)) |
---|
665 | n/a | return attr; |
---|
666 | n/a | PyErr_Clear(); |
---|
667 | n/a | if (m->md_dict) { |
---|
668 | n/a | _Py_IDENTIFIER(__name__); |
---|
669 | n/a | mod_name = _PyDict_GetItemId(m->md_dict, &PyId___name__); |
---|
670 | n/a | if (mod_name) { |
---|
671 | n/a | PyErr_Format(PyExc_AttributeError, |
---|
672 | n/a | "module '%U' has no attribute '%U'", mod_name, name); |
---|
673 | n/a | return NULL; |
---|
674 | n/a | } |
---|
675 | n/a | else if (PyErr_Occurred()) { |
---|
676 | n/a | PyErr_Clear(); |
---|
677 | n/a | } |
---|
678 | n/a | } |
---|
679 | n/a | PyErr_Format(PyExc_AttributeError, |
---|
680 | n/a | "module has no attribute '%U'", name); |
---|
681 | n/a | return NULL; |
---|
682 | n/a | } |
---|
683 | n/a | |
---|
684 | n/a | static int |
---|
685 | n/a | module_traverse(PyModuleObject *m, visitproc visit, void *arg) |
---|
686 | n/a | { |
---|
687 | n/a | if (m->md_def && m->md_def->m_traverse) { |
---|
688 | n/a | int res = m->md_def->m_traverse((PyObject*)m, visit, arg); |
---|
689 | n/a | if (res) |
---|
690 | n/a | return res; |
---|
691 | n/a | } |
---|
692 | n/a | Py_VISIT(m->md_dict); |
---|
693 | n/a | return 0; |
---|
694 | n/a | } |
---|
695 | n/a | |
---|
696 | n/a | static int |
---|
697 | n/a | module_clear(PyModuleObject *m) |
---|
698 | n/a | { |
---|
699 | n/a | if (m->md_def && m->md_def->m_clear) { |
---|
700 | n/a | int res = m->md_def->m_clear((PyObject*)m); |
---|
701 | n/a | if (res) |
---|
702 | n/a | return res; |
---|
703 | n/a | } |
---|
704 | n/a | Py_CLEAR(m->md_dict); |
---|
705 | n/a | return 0; |
---|
706 | n/a | } |
---|
707 | n/a | |
---|
708 | n/a | static PyObject * |
---|
709 | n/a | module_dir(PyObject *self, PyObject *args) |
---|
710 | n/a | { |
---|
711 | n/a | _Py_IDENTIFIER(__dict__); |
---|
712 | n/a | PyObject *result = NULL; |
---|
713 | n/a | PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__); |
---|
714 | n/a | |
---|
715 | n/a | if (dict != NULL) { |
---|
716 | n/a | if (PyDict_Check(dict)) |
---|
717 | n/a | result = PyDict_Keys(dict); |
---|
718 | n/a | else { |
---|
719 | n/a | const char *name = PyModule_GetName(self); |
---|
720 | n/a | if (name) |
---|
721 | n/a | PyErr_Format(PyExc_TypeError, |
---|
722 | n/a | "%.200s.__dict__ is not a dictionary", |
---|
723 | n/a | name); |
---|
724 | n/a | } |
---|
725 | n/a | } |
---|
726 | n/a | |
---|
727 | n/a | Py_XDECREF(dict); |
---|
728 | n/a | return result; |
---|
729 | n/a | } |
---|
730 | n/a | |
---|
731 | n/a | static PyMethodDef module_methods[] = { |
---|
732 | n/a | {"__dir__", module_dir, METH_NOARGS, |
---|
733 | n/a | PyDoc_STR("__dir__() -> list\nspecialized dir() implementation")}, |
---|
734 | n/a | {0} |
---|
735 | n/a | }; |
---|
736 | n/a | |
---|
737 | n/a | PyDoc_STRVAR(module_doc, |
---|
738 | n/a | "module(name[, doc])\n\ |
---|
739 | n/a | \n\ |
---|
740 | n/a | Create a module object.\n\ |
---|
741 | n/a | The name must be a string; the optional doc argument can have any type."); |
---|
742 | n/a | |
---|
743 | n/a | PyTypeObject PyModule_Type = { |
---|
744 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
---|
745 | n/a | "module", /* tp_name */ |
---|
746 | n/a | sizeof(PyModuleObject), /* tp_size */ |
---|
747 | n/a | 0, /* tp_itemsize */ |
---|
748 | n/a | (destructor)module_dealloc, /* tp_dealloc */ |
---|
749 | n/a | 0, /* tp_print */ |
---|
750 | n/a | 0, /* tp_getattr */ |
---|
751 | n/a | 0, /* tp_setattr */ |
---|
752 | n/a | 0, /* tp_reserved */ |
---|
753 | n/a | (reprfunc)module_repr, /* tp_repr */ |
---|
754 | n/a | 0, /* tp_as_number */ |
---|
755 | n/a | 0, /* tp_as_sequence */ |
---|
756 | n/a | 0, /* tp_as_mapping */ |
---|
757 | n/a | 0, /* tp_hash */ |
---|
758 | n/a | 0, /* tp_call */ |
---|
759 | n/a | 0, /* tp_str */ |
---|
760 | n/a | (getattrofunc)module_getattro, /* tp_getattro */ |
---|
761 | n/a | PyObject_GenericSetAttr, /* tp_setattro */ |
---|
762 | n/a | 0, /* tp_as_buffer */ |
---|
763 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
---|
764 | n/a | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
---|
765 | n/a | module_doc, /* tp_doc */ |
---|
766 | n/a | (traverseproc)module_traverse, /* tp_traverse */ |
---|
767 | n/a | (inquiry)module_clear, /* tp_clear */ |
---|
768 | n/a | 0, /* tp_richcompare */ |
---|
769 | n/a | offsetof(PyModuleObject, md_weaklist), /* tp_weaklistoffset */ |
---|
770 | n/a | 0, /* tp_iter */ |
---|
771 | n/a | 0, /* tp_iternext */ |
---|
772 | n/a | module_methods, /* tp_methods */ |
---|
773 | n/a | module_members, /* tp_members */ |
---|
774 | n/a | 0, /* tp_getset */ |
---|
775 | n/a | 0, /* tp_base */ |
---|
776 | n/a | 0, /* tp_dict */ |
---|
777 | n/a | 0, /* tp_descr_get */ |
---|
778 | n/a | 0, /* tp_descr_set */ |
---|
779 | n/a | offsetof(PyModuleObject, md_dict), /* tp_dictoffset */ |
---|
780 | n/a | (initproc)module_init, /* tp_init */ |
---|
781 | n/a | PyType_GenericAlloc, /* tp_alloc */ |
---|
782 | n/a | PyType_GenericNew, /* tp_new */ |
---|
783 | n/a | PyObject_GC_Del, /* tp_free */ |
---|
784 | n/a | }; |
---|