Branch data Line data Source code
1 : :
2 : : /* Module object implementation */
3 : :
4 : : #include "Python.h"
5 : : #include "pycore_call.h" // _PyObject_CallNoArgs()
6 : : #include "pycore_interp.h" // PyInterpreterState.importlib
7 : : #include "pycore_object.h" // _PyType_AllocNoTrack
8 : : #include "pycore_pystate.h" // _PyInterpreterState_GET()
9 : : #include "pycore_moduleobject.h" // _PyModule_GetDef()
10 : : #include "structmember.h" // PyMemberDef
11 : :
12 : :
13 : : static PyMemberDef module_members[] = {
14 : : {"__dict__", T_OBJECT, offsetof(PyModuleObject, md_dict), READONLY},
15 : : {0}
16 : : };
17 : :
18 : :
19 : : PyTypeObject PyModuleDef_Type = {
20 : : PyVarObject_HEAD_INIT(&PyType_Type, 0)
21 : : "moduledef", /* tp_name */
22 : : sizeof(PyModuleDef), /* tp_basicsize */
23 : : 0, /* tp_itemsize */
24 : : };
25 : :
26 : :
27 : : int
28 : 0 : _PyModule_IsExtension(PyObject *obj)
29 : : {
30 [ # # ]: 0 : if (!PyModule_Check(obj)) {
31 : 0 : return 0;
32 : : }
33 : 0 : PyModuleObject *module = (PyModuleObject*)obj;
34 : :
35 : 0 : PyModuleDef *def = module->md_def;
36 [ # # # # ]: 0 : return (def != NULL && def->m_methods != NULL);
37 : : }
38 : :
39 : :
40 : : PyObject*
41 : 932 : PyModuleDef_Init(PyModuleDef* def)
42 : : {
43 : : assert(PyModuleDef_Type.tp_flags & Py_TPFLAGS_READY);
44 [ + + ]: 932 : if (def->m_base.m_index == 0) {
45 : 481 : Py_SET_REFCNT(def, 1);
46 : 481 : Py_SET_TYPE(def, &PyModuleDef_Type);
47 : 481 : def->m_base.m_index = _PyImport_GetNextModuleIndex();
48 : : }
49 : 932 : return (PyObject*)def;
50 : : }
51 : :
52 : : static int
53 : 1212 : module_init_dict(PyModuleObject *mod, PyObject *md_dict,
54 : : PyObject *name, PyObject *doc)
55 : : {
56 : : assert(md_dict != NULL);
57 [ + + ]: 1212 : if (doc == NULL)
58 : 572 : doc = Py_None;
59 : :
60 [ - + ]: 1212 : if (PyDict_SetItem(md_dict, &_Py_ID(__name__), name) != 0)
61 : 0 : return -1;
62 [ - + ]: 1212 : if (PyDict_SetItem(md_dict, &_Py_ID(__doc__), doc) != 0)
63 : 0 : return -1;
64 [ - + ]: 1212 : if (PyDict_SetItem(md_dict, &_Py_ID(__package__), Py_None) != 0)
65 : 0 : return -1;
66 [ - + ]: 1212 : if (PyDict_SetItem(md_dict, &_Py_ID(__loader__), Py_None) != 0)
67 : 0 : return -1;
68 [ - + ]: 1212 : if (PyDict_SetItem(md_dict, &_Py_ID(__spec__), Py_None) != 0)
69 : 0 : return -1;
70 [ + - ]: 1212 : if (PyUnicode_CheckExact(name)) {
71 : 1212 : Py_XSETREF(mod->md_name, Py_NewRef(name));
72 : : }
73 : :
74 : 1212 : return 0;
75 : : }
76 : :
77 : : static PyModuleObject *
78 : 1212 : new_module_notrack(PyTypeObject *mt)
79 : : {
80 : : PyModuleObject *m;
81 : 1212 : m = (PyModuleObject *)_PyType_AllocNoTrack(mt, 0);
82 [ - + ]: 1212 : if (m == NULL)
83 : 0 : return NULL;
84 : 1212 : m->md_def = NULL;
85 : 1212 : m->md_state = NULL;
86 : 1212 : m->md_weaklist = NULL;
87 : 1212 : m->md_name = NULL;
88 : 1212 : m->md_dict = PyDict_New();
89 [ + - ]: 1212 : if (m->md_dict != NULL) {
90 : 1212 : return m;
91 : : }
92 : 0 : Py_DECREF(m);
93 : 0 : return NULL;
94 : : }
95 : :
96 : : static PyObject *
97 : 640 : new_module(PyTypeObject *mt, PyObject *args, PyObject *kws)
98 : : {
99 : 640 : PyObject *m = (PyObject *)new_module_notrack(mt);
100 [ + - ]: 640 : if (m != NULL) {
101 : 640 : PyObject_GC_Track(m);
102 : : }
103 : 640 : return m;
104 : : }
105 : :
106 : : PyObject *
107 : 572 : PyModule_NewObject(PyObject *name)
108 : : {
109 : 572 : PyModuleObject *m = new_module_notrack(&PyModule_Type);
110 [ - + ]: 572 : if (m == NULL)
111 : 0 : return NULL;
112 [ - + ]: 572 : if (module_init_dict(m, m->md_dict, name, NULL) != 0)
113 : 0 : goto fail;
114 : 572 : PyObject_GC_Track(m);
115 : 572 : return (PyObject *)m;
116 : :
117 : 0 : fail:
118 : 0 : Py_DECREF(m);
119 : 0 : return NULL;
120 : : }
121 : :
122 : : PyObject *
123 : 108 : PyModule_New(const char *name)
124 : : {
125 : : PyObject *nameobj, *module;
126 : 108 : nameobj = PyUnicode_FromString(name);
127 [ - + ]: 108 : if (nameobj == NULL)
128 : 0 : return NULL;
129 : 108 : module = PyModule_NewObject(nameobj);
130 : 108 : Py_DECREF(nameobj);
131 : 108 : return module;
132 : : }
133 : :
134 : : /* Check API/ABI version
135 : : * Issues a warning on mismatch, which is usually not fatal.
136 : : * Returns 0 if an exception is raised.
137 : : */
138 : : static int
139 : 518 : check_api_version(const char *name, int module_api_version)
140 : : {
141 [ - + - - ]: 518 : if (module_api_version != PYTHON_API_VERSION && module_api_version != PYTHON_ABI_VERSION) {
142 : : int err;
143 : 0 : err = PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
144 : : "Python C API version mismatch for module %.100s: "
145 : : "This Python has API version %d, module %.100s has version %d.",
146 : : name,
147 : : PYTHON_API_VERSION, name, module_api_version);
148 [ # # ]: 0 : if (err)
149 : 0 : return 0;
150 : : }
151 : 518 : return 1;
152 : : }
153 : :
154 : : static int
155 : 533 : _add_methods_to_object(PyObject *module, PyObject *name, PyMethodDef *functions)
156 : : {
157 : : PyObject *func;
158 : : PyMethodDef *fdef;
159 : :
160 [ + + ]: 12989 : for (fdef = functions; fdef->ml_name != NULL; fdef++) {
161 [ + - ]: 12456 : if ((fdef->ml_flags & METH_CLASS) ||
162 [ - + ]: 12456 : (fdef->ml_flags & METH_STATIC)) {
163 : 0 : PyErr_SetString(PyExc_ValueError,
164 : : "module functions cannot set"
165 : : " METH_CLASS or METH_STATIC");
166 : 0 : return -1;
167 : : }
168 : 12456 : func = PyCFunction_NewEx(fdef, (PyObject*)module, name);
169 [ - + ]: 12456 : if (func == NULL) {
170 : 0 : return -1;
171 : : }
172 [ - + ]: 12456 : if (PyObject_SetAttrString(module, fdef->ml_name, func) != 0) {
173 : 0 : Py_DECREF(func);
174 : 0 : return -1;
175 : : }
176 : 12456 : Py_DECREF(func);
177 : : }
178 : :
179 : 533 : return 0;
180 : : }
181 : :
182 : : PyObject *
183 : 46 : PyModule_Create2(PyModuleDef* module, int module_api_version)
184 : : {
185 [ - + ]: 46 : if (!_PyImport_IsInitialized(_PyInterpreterState_GET())) {
186 : 0 : PyErr_SetString(PyExc_SystemError,
187 : : "Python import machinery not initialized");
188 : 0 : return NULL;
189 : : }
190 : 46 : return _PyModule_CreateInitialized(module, module_api_version);
191 : : }
192 : :
193 : : PyObject *
194 : 104 : _PyModule_CreateInitialized(PyModuleDef* module, int module_api_version)
195 : : {
196 : : const char* name;
197 : : PyModuleObject *m;
198 : :
199 [ - + ]: 104 : if (!PyModuleDef_Init(module))
200 : 0 : return NULL;
201 : 104 : name = module->m_name;
202 [ - + ]: 104 : if (!check_api_version(name, module_api_version)) {
203 : 0 : return NULL;
204 : : }
205 [ - + ]: 104 : if (module->m_slots) {
206 : 0 : PyErr_Format(
207 : : PyExc_SystemError,
208 : : "module %s: PyModule_Create is incompatible with m_slots", name);
209 : 0 : return NULL;
210 : : }
211 : 104 : name = _PyImport_ResolveNameWithPackageContext(name);
212 [ - + ]: 104 : if ((m = (PyModuleObject*)PyModule_New(name)) == NULL)
213 : 0 : return NULL;
214 : :
215 [ + + ]: 104 : if (module->m_size > 0) {
216 : 29 : m->md_state = PyMem_Malloc(module->m_size);
217 [ - + ]: 29 : if (!m->md_state) {
218 : 0 : PyErr_NoMemory();
219 : 0 : Py_DECREF(m);
220 : 0 : return NULL;
221 : : }
222 : 29 : memset(m->md_state, 0, module->m_size);
223 : : }
224 : :
225 [ + + ]: 104 : if (module->m_methods != NULL) {
226 [ - + ]: 103 : if (PyModule_AddFunctions((PyObject *) m, module->m_methods) != 0) {
227 : 0 : Py_DECREF(m);
228 : 0 : return NULL;
229 : : }
230 : : }
231 [ + + ]: 104 : if (module->m_doc != NULL) {
232 [ - + ]: 96 : if (PyModule_SetDocString((PyObject *) m, module->m_doc) != 0) {
233 : 0 : Py_DECREF(m);
234 : 0 : return NULL;
235 : : }
236 : : }
237 : 104 : m->md_def = module;
238 : 104 : return (PyObject*)m;
239 : : }
240 : :
241 : : PyObject *
242 : 414 : PyModule_FromDefAndSpec2(PyModuleDef* def, PyObject *spec, int module_api_version)
243 : : {
244 : : PyModuleDef_Slot* cur_slot;
245 : 414 : PyObject *(*create)(PyObject *, PyModuleDef*) = NULL;
246 : : PyObject *nameobj;
247 : 414 : PyObject *m = NULL;
248 : 414 : int has_execution_slots = 0;
249 : : const char *name;
250 : : int ret;
251 : :
252 : 414 : PyModuleDef_Init(def);
253 : :
254 : 414 : nameobj = PyObject_GetAttrString(spec, "name");
255 [ - + ]: 414 : if (nameobj == NULL) {
256 : 0 : return NULL;
257 : : }
258 : 414 : name = PyUnicode_AsUTF8(nameobj);
259 [ - + ]: 414 : if (name == NULL) {
260 : 0 : goto error;
261 : : }
262 : :
263 [ - + ]: 414 : if (!check_api_version(name, module_api_version)) {
264 : 0 : goto error;
265 : : }
266 : :
267 [ - + ]: 414 : if (def->m_size < 0) {
268 : 0 : PyErr_Format(
269 : : PyExc_SystemError,
270 : : "module %s: m_size may not be negative for multi-phase initialization",
271 : : name);
272 : 0 : goto error;
273 : : }
274 : :
275 [ + + + + ]: 809 : for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) {
276 [ - + - ]: 395 : switch (cur_slot->slot) {
277 : 0 : case Py_mod_create:
278 [ # # ]: 0 : if (create) {
279 : 0 : PyErr_Format(
280 : : PyExc_SystemError,
281 : : "module %s has multiple create slots",
282 : : name);
283 : 0 : goto error;
284 : : }
285 : 0 : create = cur_slot->value;
286 : 0 : break;
287 : 395 : case Py_mod_exec:
288 : 395 : has_execution_slots = 1;
289 : 395 : break;
290 : 0 : default:
291 : : assert(cur_slot->slot < 0 || cur_slot->slot > _Py_mod_LAST_SLOT);
292 : 0 : PyErr_Format(
293 : : PyExc_SystemError,
294 : : "module %s uses unknown slot ID %i",
295 : : name, cur_slot->slot);
296 : 0 : goto error;
297 : : }
298 : : }
299 : :
300 [ - + ]: 414 : if (create) {
301 : 0 : m = create(spec, def);
302 [ # # ]: 0 : if (m == NULL) {
303 [ # # ]: 0 : if (!PyErr_Occurred()) {
304 : 0 : PyErr_Format(
305 : : PyExc_SystemError,
306 : : "creation of module %s failed without setting an exception",
307 : : name);
308 : : }
309 : 0 : goto error;
310 : : } else {
311 [ # # ]: 0 : if (PyErr_Occurred()) {
312 : 0 : _PyErr_FormatFromCause(
313 : : PyExc_SystemError,
314 : : "creation of module %s raised unreported exception",
315 : : name);
316 : 0 : goto error;
317 : : }
318 : : }
319 : : } else {
320 : 414 : m = PyModule_NewObject(nameobj);
321 [ - + ]: 414 : if (m == NULL) {
322 : 0 : goto error;
323 : : }
324 : : }
325 : :
326 [ + - ]: 414 : if (PyModule_Check(m)) {
327 : 414 : ((PyModuleObject*)m)->md_state = NULL;
328 : 414 : ((PyModuleObject*)m)->md_def = def;
329 : : } else {
330 [ # # # # : 0 : if (def->m_size > 0 || def->m_traverse || def->m_clear || def->m_free) {
# # # # ]
331 : 0 : PyErr_Format(
332 : : PyExc_SystemError,
333 : : "module %s is not a module object, but requests module state",
334 : : name);
335 : 0 : goto error;
336 : : }
337 [ # # ]: 0 : if (has_execution_slots) {
338 : 0 : PyErr_Format(
339 : : PyExc_SystemError,
340 : : "module %s specifies execution slots, but did not create "
341 : : "a ModuleType instance",
342 : : name);
343 : 0 : goto error;
344 : : }
345 : : }
346 : :
347 [ + + ]: 414 : if (def->m_methods != NULL) {
348 : 402 : ret = _add_methods_to_object(m, nameobj, def->m_methods);
349 [ - + ]: 402 : if (ret != 0) {
350 : 0 : goto error;
351 : : }
352 : : }
353 : :
354 [ + + ]: 414 : if (def->m_doc != NULL) {
355 : 346 : ret = PyModule_SetDocString(m, def->m_doc);
356 [ - + ]: 346 : if (ret != 0) {
357 : 0 : goto error;
358 : : }
359 : : }
360 : :
361 : 414 : Py_DECREF(nameobj);
362 : 414 : return m;
363 : :
364 : 0 : error:
365 : 0 : Py_DECREF(nameobj);
366 : 0 : Py_XDECREF(m);
367 : 0 : return NULL;
368 : : }
369 : :
370 : : int
371 : 434 : PyModule_ExecDef(PyObject *module, PyModuleDef *def)
372 : : {
373 : : PyModuleDef_Slot *cur_slot;
374 : : const char *name;
375 : : int ret;
376 : :
377 : 434 : name = PyModule_GetName(module);
378 [ - + ]: 434 : if (name == NULL) {
379 : 0 : return -1;
380 : : }
381 : :
382 [ + + ]: 434 : if (def->m_size >= 0) {
383 : 417 : PyModuleObject *md = (PyModuleObject*)module;
384 [ + - ]: 417 : if (md->md_state == NULL) {
385 : : /* Always set a state pointer; this serves as a marker to skip
386 : : * multiple initialization (importlib.reload() is no-op) */
387 : 417 : md->md_state = PyMem_Malloc(def->m_size);
388 [ - + ]: 417 : if (!md->md_state) {
389 : 0 : PyErr_NoMemory();
390 : 0 : return -1;
391 : : }
392 : 417 : memset(md->md_state, 0, def->m_size);
393 : : }
394 : : }
395 : :
396 [ + + ]: 434 : if (def->m_slots == NULL) {
397 : 30 : return 0;
398 : : }
399 : :
400 [ + - + + ]: 799 : for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) {
401 [ - + - ]: 395 : switch (cur_slot->slot) {
402 : 0 : case Py_mod_create:
403 : : /* handled in PyModule_FromDefAndSpec2 */
404 : 0 : break;
405 : 395 : case Py_mod_exec:
406 : 395 : ret = ((int (*)(PyObject *))cur_slot->value)(module);
407 [ - + ]: 395 : if (ret != 0) {
408 [ # # ]: 0 : if (!PyErr_Occurred()) {
409 : 0 : PyErr_Format(
410 : : PyExc_SystemError,
411 : : "execution of module %s failed without setting an exception",
412 : : name);
413 : : }
414 : 0 : return -1;
415 : : }
416 [ - + ]: 395 : if (PyErr_Occurred()) {
417 : 0 : _PyErr_FormatFromCause(
418 : : PyExc_SystemError,
419 : : "execution of module %s raised unreported exception",
420 : : name);
421 : 0 : return -1;
422 : : }
423 : 395 : break;
424 : 0 : default:
425 : 0 : PyErr_Format(
426 : : PyExc_SystemError,
427 : : "module %s initialized with unknown slot %i",
428 : : name, cur_slot->slot);
429 : 0 : return -1;
430 : : }
431 : : }
432 : 404 : return 0;
433 : : }
434 : :
435 : : int
436 : 131 : PyModule_AddFunctions(PyObject *m, PyMethodDef *functions)
437 : : {
438 : : int res;
439 : 131 : PyObject *name = PyModule_GetNameObject(m);
440 [ - + ]: 131 : if (name == NULL) {
441 : 0 : return -1;
442 : : }
443 : :
444 : 131 : res = _add_methods_to_object(m, name, functions);
445 : 131 : Py_DECREF(name);
446 : 131 : return res;
447 : : }
448 : :
449 : : int
450 : 442 : PyModule_SetDocString(PyObject *m, const char *doc)
451 : : {
452 : : PyObject *v;
453 : :
454 : 442 : v = PyUnicode_FromString(doc);
455 [ + - - + ]: 442 : if (v == NULL || PyObject_SetAttr(m, &_Py_ID(__doc__), v) != 0) {
456 : 0 : Py_XDECREF(v);
457 : 0 : return -1;
458 : : }
459 : 442 : Py_DECREF(v);
460 : 442 : return 0;
461 : : }
462 : :
463 : : PyObject *
464 : 11253 : PyModule_GetDict(PyObject *m)
465 : : {
466 [ - + ]: 11253 : if (!PyModule_Check(m)) {
467 : 0 : PyErr_BadInternalCall();
468 : 0 : return NULL;
469 : : }
470 : 11253 : return _PyModule_GetDict(m);
471 : : }
472 : :
473 : : PyObject*
474 : 565 : PyModule_GetNameObject(PyObject *m)
475 : : {
476 : : PyObject *d;
477 : : PyObject *name;
478 [ - + ]: 565 : if (!PyModule_Check(m)) {
479 : 0 : PyErr_BadArgument();
480 : 0 : return NULL;
481 : : }
482 : 565 : d = ((PyModuleObject *)m)->md_dict;
483 [ + - + - : 1130 : if (d == NULL || !PyDict_Check(d) ||
+ - ]
484 [ - + ]: 1130 : (name = PyDict_GetItemWithError(d, &_Py_ID(__name__))) == NULL ||
485 : 565 : !PyUnicode_Check(name))
486 : : {
487 [ # # ]: 0 : if (!PyErr_Occurred()) {
488 : 0 : PyErr_SetString(PyExc_SystemError, "nameless module");
489 : : }
490 : 0 : return NULL;
491 : : }
492 : 565 : return Py_NewRef(name);
493 : : }
494 : :
495 : : const char *
496 : 434 : PyModule_GetName(PyObject *m)
497 : : {
498 : 434 : PyObject *name = PyModule_GetNameObject(m);
499 [ - + ]: 434 : if (name == NULL) {
500 : 0 : return NULL;
501 : : }
502 : : assert(Py_REFCNT(name) >= 2);
503 : 434 : Py_DECREF(name); /* module dict has still a reference */
504 : 434 : return PyUnicode_AsUTF8(name);
505 : : }
506 : :
507 : : PyObject*
508 : 28 : PyModule_GetFilenameObject(PyObject *m)
509 : : {
510 : : PyObject *d;
511 : : PyObject *fileobj;
512 [ - + ]: 28 : if (!PyModule_Check(m)) {
513 : 0 : PyErr_BadArgument();
514 : 0 : return NULL;
515 : : }
516 : 28 : d = ((PyModuleObject *)m)->md_dict;
517 [ + - + + ]: 56 : if (d == NULL ||
518 [ - + ]: 31 : (fileobj = PyDict_GetItemWithError(d, &_Py_ID(__file__))) == NULL ||
519 : 3 : !PyUnicode_Check(fileobj))
520 : : {
521 [ + - ]: 25 : if (!PyErr_Occurred()) {
522 : 25 : PyErr_SetString(PyExc_SystemError, "module filename missing");
523 : : }
524 : 25 : return NULL;
525 : : }
526 : 3 : return Py_NewRef(fileobj);
527 : : }
528 : :
529 : : const char *
530 : 0 : PyModule_GetFilename(PyObject *m)
531 : : {
532 : : PyObject *fileobj;
533 : : const char *utf8;
534 : 0 : fileobj = PyModule_GetFilenameObject(m);
535 [ # # ]: 0 : if (fileobj == NULL)
536 : 0 : return NULL;
537 : 0 : utf8 = PyUnicode_AsUTF8(fileobj);
538 : 0 : Py_DECREF(fileobj); /* module dict has still a reference */
539 : 0 : return utf8;
540 : : }
541 : :
542 : : PyModuleDef*
543 : 712 : PyModule_GetDef(PyObject* m)
544 : : {
545 [ - + ]: 712 : if (!PyModule_Check(m)) {
546 : 0 : PyErr_BadArgument();
547 : 0 : return NULL;
548 : : }
549 : 712 : return _PyModule_GetDef(m);
550 : : }
551 : :
552 : : void*
553 : 1373 : PyModule_GetState(PyObject* m)
554 : : {
555 [ - + ]: 1373 : if (!PyModule_Check(m)) {
556 : 0 : PyErr_BadArgument();
557 : 0 : return NULL;
558 : : }
559 : 1373 : return _PyModule_GetState(m);
560 : : }
561 : :
562 : : void
563 : 491 : _PyModule_Clear(PyObject *m)
564 : : {
565 : 491 : PyObject *d = ((PyModuleObject *)m)->md_dict;
566 [ + - ]: 491 : if (d != NULL)
567 : 491 : _PyModule_ClearDict(d);
568 : 491 : }
569 : :
570 : : void
571 : 541 : _PyModule_ClearDict(PyObject *d)
572 : : {
573 : : /* To make the execution order of destructors for global
574 : : objects a bit more predictable, we first zap all objects
575 : : whose name starts with a single underscore, before we clear
576 : : the entire dictionary. We zap them by replacing them with
577 : : None, rather than deleting them from the dictionary, to
578 : : avoid rehashing the dictionary (to some extent). */
579 : :
580 : : Py_ssize_t pos;
581 : : PyObject *key, *value;
582 : :
583 : 541 : int verbose = _Py_GetConfig()->verbose;
584 : :
585 : : /* First, clear only names starting with a single underscore */
586 : 541 : pos = 0;
587 [ + + ]: 36739 : while (PyDict_Next(d, &pos, &key, &value)) {
588 [ + + + - ]: 36198 : if (value != Py_None && PyUnicode_Check(key)) {
589 [ + + + + ]: 43336 : if (PyUnicode_READ_CHAR(key, 0) == '_' &&
590 : 7979 : PyUnicode_READ_CHAR(key, 1) != '_') {
591 [ - + ]: 4388 : if (verbose > 1) {
592 : 0 : const char *s = PyUnicode_AsUTF8(key);
593 [ # # ]: 0 : if (s != NULL)
594 : 0 : PySys_WriteStderr("# clear[1] %s\n", s);
595 : : else
596 : 0 : PyErr_Clear();
597 : : }
598 [ - + ]: 4388 : if (PyDict_SetItem(d, key, Py_None) != 0) {
599 : 0 : PyErr_WriteUnraisable(NULL);
600 : : }
601 : : }
602 : : }
603 : : }
604 : :
605 : : /* Next, clear all names except for __builtins__ */
606 : 541 : pos = 0;
607 [ + + ]: 36739 : while (PyDict_Next(d, &pos, &key, &value)) {
608 [ + + + - ]: 36198 : if (value != Py_None && PyUnicode_Check(key)) {
609 [ + + + + ]: 34560 : if (PyUnicode_READ_CHAR(key, 0) != '_' ||
610 : 3591 : !_PyUnicode_EqualToASCIIString(key, "__builtins__"))
611 : : {
612 [ - + ]: 30733 : if (verbose > 1) {
613 : 0 : const char *s = PyUnicode_AsUTF8(key);
614 [ # # ]: 0 : if (s != NULL)
615 : 0 : PySys_WriteStderr("# clear[2] %s\n", s);
616 : : else
617 : 0 : PyErr_Clear();
618 : : }
619 [ - + ]: 30733 : if (PyDict_SetItem(d, key, Py_None) != 0) {
620 : 0 : PyErr_WriteUnraisable(NULL);
621 : : }
622 : : }
623 : : }
624 : : }
625 : :
626 : : /* Note: we leave __builtins__ in place, so that destructors
627 : : of non-global objects defined in this module can still use
628 : : builtins, in particularly 'None'. */
629 : :
630 : 541 : }
631 : :
632 : : /*[clinic input]
633 : : class module "PyModuleObject *" "&PyModule_Type"
634 : : [clinic start generated code]*/
635 : : /*[clinic end generated code: output=da39a3ee5e6b4b0d input=3e35d4f708ecb6af]*/
636 : :
637 : : #include "clinic/moduleobject.c.h"
638 : :
639 : : /* Methods */
640 : :
641 : : /*[clinic input]
642 : : module.__init__
643 : : name: unicode
644 : : doc: object = None
645 : :
646 : : Create a module object.
647 : :
648 : : The name must be a string; the optional doc argument can have any type.
649 : : [clinic start generated code]*/
650 : :
651 : : static int
652 : 640 : module___init___impl(PyModuleObject *self, PyObject *name, PyObject *doc)
653 : : /*[clinic end generated code: output=e7e721c26ce7aad7 input=57f9e177401e5e1e]*/
654 : : {
655 : 640 : PyObject *dict = self->md_dict;
656 [ - + ]: 640 : if (dict == NULL) {
657 : 0 : dict = PyDict_New();
658 [ # # ]: 0 : if (dict == NULL)
659 : 0 : return -1;
660 : 0 : self->md_dict = dict;
661 : : }
662 [ - + ]: 640 : if (module_init_dict(self, dict, name, doc) < 0)
663 : 0 : return -1;
664 : 640 : return 0;
665 : : }
666 : :
667 : : static void
668 : 1189 : module_dealloc(PyModuleObject *m)
669 : : {
670 : 1189 : int verbose = _Py_GetConfig()->verbose;
671 : :
672 : 1189 : PyObject_GC_UnTrack(m);
673 [ - + - - ]: 1189 : if (verbose && m->md_name) {
674 : 0 : PySys_FormatStderr("# destroy %U\n", m->md_name);
675 : : }
676 [ + + ]: 1189 : if (m->md_weaklist != NULL)
677 : 373 : PyObject_ClearWeakRefs((PyObject *) m);
678 : : /* bpo-39824: Don't call m_free() if m_size > 0 and md_state=NULL */
679 [ + + + + ]: 1189 : if (m->md_def && m->md_def->m_free
680 [ + - + - ]: 222 : && (m->md_def->m_size <= 0 || m->md_state != NULL))
681 : : {
682 : 222 : m->md_def->m_free(m);
683 : : }
684 : 1189 : Py_XDECREF(m->md_dict);
685 : 1189 : Py_XDECREF(m->md_name);
686 [ + + ]: 1189 : if (m->md_state != NULL)
687 : 442 : PyMem_Free(m->md_state);
688 : 1189 : Py_TYPE(m)->tp_free((PyObject *)m);
689 : 1189 : }
690 : :
691 : : static PyObject *
692 : 0 : module_repr(PyModuleObject *m)
693 : : {
694 : 0 : PyInterpreterState *interp = _PyInterpreterState_GET();
695 : 0 : return _PyImport_ImportlibModuleRepr(interp, (PyObject *)m);
696 : : }
697 : :
698 : : /* Check if the "_initializing" attribute of the module spec is set to true.
699 : : Clear the exception and return 0 if spec is NULL.
700 : : */
701 : : int
702 : 8075 : _PyModuleSpec_IsInitializing(PyObject *spec)
703 : : {
704 [ + - ]: 8075 : if (spec != NULL) {
705 : 8075 : PyObject *value = PyObject_GetAttr(spec, &_Py_ID(_initializing));
706 [ + + ]: 8075 : if (value != NULL) {
707 : 4314 : int initializing = PyObject_IsTrue(value);
708 : 4314 : Py_DECREF(value);
709 [ + - ]: 4314 : if (initializing >= 0) {
710 : 4314 : return initializing;
711 : : }
712 : : }
713 : : }
714 : 3761 : PyErr_Clear();
715 : 3761 : return 0;
716 : : }
717 : :
718 : : /* Check if the submodule name is in the "_uninitialized_submodules" attribute
719 : : of the module spec.
720 : : */
721 : : int
722 : 4404 : _PyModuleSpec_IsUninitializedSubmodule(PyObject *spec, PyObject *name)
723 : : {
724 [ - + ]: 4404 : if (spec == NULL) {
725 : 0 : return 0;
726 : : }
727 : :
728 : 4404 : PyObject *value = PyObject_GetAttr(spec, &_Py_ID(_uninitialized_submodules));
729 [ + + ]: 4404 : if (value == NULL) {
730 : 300 : return 0;
731 : : }
732 : :
733 : 4104 : int is_uninitialized = PySequence_Contains(value, name);
734 : 4104 : Py_DECREF(value);
735 [ - + ]: 4104 : if (is_uninitialized == -1) {
736 : 0 : return 0;
737 : : }
738 : 4104 : return is_uninitialized;
739 : : }
740 : :
741 : : static PyObject*
742 : 51744 : module_getattro(PyModuleObject *m, PyObject *name)
743 : : {
744 : : PyObject *attr, *mod_name, *getattr;
745 : 51744 : attr = PyObject_GenericGetAttr((PyObject *)m, name);
746 [ + + - + ]: 51744 : if (attr || !PyErr_ExceptionMatches(PyExc_AttributeError)) {
747 : 47275 : return attr;
748 : : }
749 : 4469 : PyErr_Clear();
750 : : assert(m->md_dict != NULL);
751 : 4469 : getattr = PyDict_GetItemWithError(m->md_dict, &_Py_ID(__getattr__));
752 [ + + ]: 4469 : if (getattr) {
753 : 2 : return PyObject_CallOneArg(getattr, name);
754 : : }
755 [ - + ]: 4467 : if (PyErr_Occurred()) {
756 : 0 : return NULL;
757 : : }
758 : 4467 : mod_name = PyDict_GetItemWithError(m->md_dict, &_Py_ID(__name__));
759 [ + - + - ]: 4467 : if (mod_name && PyUnicode_Check(mod_name)) {
760 : 4467 : Py_INCREF(mod_name);
761 : 4467 : PyObject *spec = PyDict_GetItemWithError(m->md_dict, &_Py_ID(__spec__));
762 [ - + - - ]: 4467 : if (spec == NULL && PyErr_Occurred()) {
763 : 0 : Py_DECREF(mod_name);
764 : 0 : return NULL;
765 : : }
766 : 4467 : Py_XINCREF(spec);
767 [ + + ]: 4467 : if (_PyModuleSpec_IsInitializing(spec)) {
768 : 63 : PyErr_Format(PyExc_AttributeError,
769 : : "partially initialized "
770 : : "module '%U' has no attribute '%U' "
771 : : "(most likely due to a circular import)",
772 : : mod_name, name);
773 : : }
774 [ - + ]: 4404 : else if (_PyModuleSpec_IsUninitializedSubmodule(spec, name)) {
775 : 0 : PyErr_Format(PyExc_AttributeError,
776 : : "cannot access submodule '%U' of module '%U' "
777 : : "(most likely due to a circular import)",
778 : : name, mod_name);
779 : : }
780 : : else {
781 : 4404 : PyErr_Format(PyExc_AttributeError,
782 : : "module '%U' has no attribute '%U'",
783 : : mod_name, name);
784 : : }
785 : 4467 : Py_XDECREF(spec);
786 : 4467 : Py_DECREF(mod_name);
787 : 4467 : return NULL;
788 : : }
789 [ # # ]: 0 : else if (PyErr_Occurred()) {
790 : 0 : return NULL;
791 : : }
792 : 0 : PyErr_Format(PyExc_AttributeError,
793 : : "module has no attribute '%U'", name);
794 : 0 : return NULL;
795 : : }
796 : :
797 : : static int
798 : 12628 : module_traverse(PyModuleObject *m, visitproc visit, void *arg)
799 : : {
800 : : /* bpo-39824: Don't call m_traverse() if m_size > 0 and md_state=NULL */
801 [ + + + + ]: 12628 : if (m->md_def && m->md_def->m_traverse
802 [ + + + - ]: 2318 : && (m->md_def->m_size <= 0 || m->md_state != NULL))
803 : : {
804 : 2318 : int res = m->md_def->m_traverse((PyObject*)m, visit, arg);
805 [ - + ]: 2318 : if (res)
806 : 0 : return res;
807 : : }
808 [ + - - + ]: 12628 : Py_VISIT(m->md_dict);
809 : 12628 : return 0;
810 : : }
811 : :
812 : : static int
813 : 474 : module_clear(PyModuleObject *m)
814 : : {
815 : : /* bpo-39824: Don't call m_clear() if m_size > 0 and md_state=NULL */
816 [ + + + + ]: 474 : if (m->md_def && m->md_def->m_clear
817 [ + - + - ]: 208 : && (m->md_def->m_size <= 0 || m->md_state != NULL))
818 : : {
819 : 208 : int res = m->md_def->m_clear((PyObject*)m);
820 [ - + ]: 208 : if (PyErr_Occurred()) {
821 : 0 : PySys_FormatStderr("Exception ignored in m_clear of module%s%V\n",
822 [ # # ]: 0 : m->md_name ? " " : "",
823 : : m->md_name, "");
824 : 0 : PyErr_WriteUnraisable(NULL);
825 : : }
826 [ - + ]: 208 : if (res)
827 : 0 : return res;
828 : : }
829 [ + - ]: 474 : Py_CLEAR(m->md_dict);
830 : 474 : return 0;
831 : : }
832 : :
833 : : static PyObject *
834 : 28 : module_dir(PyObject *self, PyObject *args)
835 : : {
836 : 28 : PyObject *result = NULL;
837 : 28 : PyObject *dict = PyObject_GetAttr(self, &_Py_ID(__dict__));
838 : :
839 [ + - ]: 28 : if (dict != NULL) {
840 [ + - ]: 28 : if (PyDict_Check(dict)) {
841 : 28 : PyObject *dirfunc = PyDict_GetItemWithError(dict, &_Py_ID(__dir__));
842 [ - + ]: 28 : if (dirfunc) {
843 : 0 : result = _PyObject_CallNoArgs(dirfunc);
844 : : }
845 [ + - ]: 28 : else if (!PyErr_Occurred()) {
846 : 28 : result = PyDict_Keys(dict);
847 : : }
848 : : }
849 : : else {
850 : 0 : PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary");
851 : : }
852 : : }
853 : :
854 : 28 : Py_XDECREF(dict);
855 : 28 : return result;
856 : : }
857 : :
858 : : static PyMethodDef module_methods[] = {
859 : : {"__dir__", module_dir, METH_NOARGS,
860 : : PyDoc_STR("__dir__() -> list\nspecialized dir() implementation")},
861 : : {0}
862 : : };
863 : :
864 : : static PyObject *
865 : 0 : module_get_annotations(PyModuleObject *m, void *Py_UNUSED(ignored))
866 : : {
867 : 0 : PyObject *dict = PyObject_GetAttr((PyObject *)m, &_Py_ID(__dict__));
868 : :
869 [ # # # # ]: 0 : if ((dict == NULL) || !PyDict_Check(dict)) {
870 : 0 : PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary");
871 : 0 : Py_XDECREF(dict);
872 : 0 : return NULL;
873 : : }
874 : :
875 : : PyObject *annotations;
876 : : /* there's no _PyDict_GetItemId without WithError, so let's LBYL. */
877 [ # # ]: 0 : if (PyDict_Contains(dict, &_Py_ID(__annotations__))) {
878 : 0 : annotations = PyDict_GetItemWithError(dict, &_Py_ID(__annotations__));
879 : : /*
880 : : ** _PyDict_GetItemIdWithError could still fail,
881 : : ** for instance with a well-timed Ctrl-C or a MemoryError.
882 : : ** so let's be totally safe.
883 : : */
884 [ # # ]: 0 : if (annotations) {
885 : 0 : Py_INCREF(annotations);
886 : : }
887 : : } else {
888 : 0 : annotations = PyDict_New();
889 [ # # ]: 0 : if (annotations) {
890 : 0 : int result = PyDict_SetItem(
891 : : dict, &_Py_ID(__annotations__), annotations);
892 [ # # ]: 0 : if (result) {
893 [ # # ]: 0 : Py_CLEAR(annotations);
894 : : }
895 : : }
896 : : }
897 : 0 : Py_DECREF(dict);
898 : 0 : return annotations;
899 : : }
900 : :
901 : : static int
902 : 0 : module_set_annotations(PyModuleObject *m, PyObject *value, void *Py_UNUSED(ignored))
903 : : {
904 : 0 : int ret = -1;
905 : 0 : PyObject *dict = PyObject_GetAttr((PyObject *)m, &_Py_ID(__dict__));
906 : :
907 [ # # # # ]: 0 : if ((dict == NULL) || !PyDict_Check(dict)) {
908 : 0 : PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary");
909 : 0 : goto exit;
910 : : }
911 : :
912 [ # # ]: 0 : if (value != NULL) {
913 : : /* set */
914 : 0 : ret = PyDict_SetItem(dict, &_Py_ID(__annotations__), value);
915 : 0 : goto exit;
916 : : }
917 : :
918 : : /* delete */
919 [ # # ]: 0 : if (!PyDict_Contains(dict, &_Py_ID(__annotations__))) {
920 : 0 : PyErr_Format(PyExc_AttributeError, "__annotations__");
921 : 0 : goto exit;
922 : : }
923 : :
924 : 0 : ret = PyDict_DelItem(dict, &_Py_ID(__annotations__));
925 : :
926 : 0 : exit:
927 : 0 : Py_XDECREF(dict);
928 : 0 : return ret;
929 : : }
930 : :
931 : :
932 : : static PyGetSetDef module_getsets[] = {
933 : : {"__annotations__", (getter)module_get_annotations, (setter)module_set_annotations},
934 : : {NULL}
935 : : };
936 : :
937 : : PyTypeObject PyModule_Type = {
938 : : PyVarObject_HEAD_INIT(&PyType_Type, 0)
939 : : "module", /* tp_name */
940 : : sizeof(PyModuleObject), /* tp_basicsize */
941 : : 0, /* tp_itemsize */
942 : : (destructor)module_dealloc, /* tp_dealloc */
943 : : 0, /* tp_vectorcall_offset */
944 : : 0, /* tp_getattr */
945 : : 0, /* tp_setattr */
946 : : 0, /* tp_as_async */
947 : : (reprfunc)module_repr, /* tp_repr */
948 : : 0, /* tp_as_number */
949 : : 0, /* tp_as_sequence */
950 : : 0, /* tp_as_mapping */
951 : : 0, /* tp_hash */
952 : : 0, /* tp_call */
953 : : 0, /* tp_str */
954 : : (getattrofunc)module_getattro, /* tp_getattro */
955 : : PyObject_GenericSetAttr, /* tp_setattro */
956 : : 0, /* tp_as_buffer */
957 : : Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
958 : : Py_TPFLAGS_BASETYPE, /* tp_flags */
959 : : module___init____doc__, /* tp_doc */
960 : : (traverseproc)module_traverse, /* tp_traverse */
961 : : (inquiry)module_clear, /* tp_clear */
962 : : 0, /* tp_richcompare */
963 : : offsetof(PyModuleObject, md_weaklist), /* tp_weaklistoffset */
964 : : 0, /* tp_iter */
965 : : 0, /* tp_iternext */
966 : : module_methods, /* tp_methods */
967 : : module_members, /* tp_members */
968 : : module_getsets, /* tp_getset */
969 : : 0, /* tp_base */
970 : : 0, /* tp_dict */
971 : : 0, /* tp_descr_get */
972 : : 0, /* tp_descr_set */
973 : : offsetof(PyModuleObject, md_dict), /* tp_dictoffset */
974 : : module___init__, /* tp_init */
975 : : 0, /* tp_alloc */
976 : : new_module, /* tp_new */
977 : : PyObject_GC_Del, /* tp_free */
978 : : };
|