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