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