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