• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /* Module object implementation */
3 
4 #include "Python.h"
5 #include "pycore_call.h"          // _PyObject_CallNoArgs()
6 #include "pycore_fileutils.h"     // _Py_wgetcwd
7 #include "pycore_interp.h"        // PyInterpreterState.importlib
8 #include "pycore_modsupport.h"    // _PyModule_CreateInitialized()
9 #include "pycore_moduleobject.h"  // _PyModule_GetDef()
10 #include "pycore_object.h"        // _PyType_AllocNoTrack
11 #include "pycore_pyerrors.h"      // _PyErr_FormatFromCause()
12 #include "pycore_pystate.h"       // _PyInterpreterState_GET()
13 
14 #include "osdefs.h"               // MAXPATHLEN
15 
16 
17 static PyMemberDef module_members[] = {
18     {"__dict__", _Py_T_OBJECT, offsetof(PyModuleObject, md_dict), Py_READONLY},
19     {0}
20 };
21 
22 
23 PyTypeObject PyModuleDef_Type = {
24     PyVarObject_HEAD_INIT(&PyType_Type, 0)
25     "moduledef",                                /* tp_name */
26     sizeof(PyModuleDef),                        /* tp_basicsize */
27     0,                                          /* tp_itemsize */
28 };
29 
30 
31 int
_PyModule_IsExtension(PyObject * obj)32 _PyModule_IsExtension(PyObject *obj)
33 {
34     if (!PyModule_Check(obj)) {
35         return 0;
36     }
37     PyModuleObject *module = (PyModuleObject*)obj;
38 
39     PyModuleDef *def = module->md_def;
40     return (def != NULL && def->m_methods != NULL);
41 }
42 
43 
44 PyObject*
PyModuleDef_Init(PyModuleDef * def)45 PyModuleDef_Init(PyModuleDef* def)
46 {
47     assert(PyModuleDef_Type.tp_flags & Py_TPFLAGS_READY);
48     if (def->m_base.m_index == 0) {
49         Py_SET_REFCNT(def, 1);
50         Py_SET_TYPE(def, &PyModuleDef_Type);
51         def->m_base.m_index = _PyImport_GetNextModuleIndex();
52     }
53     return (PyObject*)def;
54 }
55 
56 static int
module_init_dict(PyModuleObject * mod,PyObject * md_dict,PyObject * name,PyObject * doc)57 module_init_dict(PyModuleObject *mod, PyObject *md_dict,
58                  PyObject *name, PyObject *doc)
59 {
60     assert(md_dict != NULL);
61     if (doc == NULL)
62         doc = Py_None;
63 
64     if (PyDict_SetItem(md_dict, &_Py_ID(__name__), name) != 0)
65         return -1;
66     if (PyDict_SetItem(md_dict, &_Py_ID(__doc__), doc) != 0)
67         return -1;
68     if (PyDict_SetItem(md_dict, &_Py_ID(__package__), Py_None) != 0)
69         return -1;
70     if (PyDict_SetItem(md_dict, &_Py_ID(__loader__), Py_None) != 0)
71         return -1;
72     if (PyDict_SetItem(md_dict, &_Py_ID(__spec__), Py_None) != 0)
73         return -1;
74     if (PyUnicode_CheckExact(name)) {
75         Py_XSETREF(mod->md_name, Py_NewRef(name));
76     }
77 
78     return 0;
79 }
80 
81 static PyModuleObject *
new_module_notrack(PyTypeObject * mt)82 new_module_notrack(PyTypeObject *mt)
83 {
84     PyModuleObject *m;
85     m = (PyModuleObject *)_PyType_AllocNoTrack(mt, 0);
86     if (m == NULL)
87         return NULL;
88     m->md_def = NULL;
89     m->md_state = NULL;
90     m->md_weaklist = NULL;
91     m->md_name = NULL;
92     m->md_dict = PyDict_New();
93     if (m->md_dict == NULL) {
94         Py_DECREF(m);
95         return NULL;
96     }
97     return m;
98 }
99 
100 static void
track_module(PyModuleObject * m)101 track_module(PyModuleObject *m)
102 {
103     _PyObject_SetDeferredRefcount(m->md_dict);
104     PyObject_GC_Track(m->md_dict);
105 
106     _PyObject_SetDeferredRefcount((PyObject *)m);
107     PyObject_GC_Track(m);
108 }
109 
110 static PyObject *
new_module(PyTypeObject * mt,PyObject * args,PyObject * kws)111 new_module(PyTypeObject *mt, PyObject *args, PyObject *kws)
112 {
113     PyModuleObject *m = new_module_notrack(mt);
114     if (m != NULL) {
115         track_module(m);
116     }
117     return (PyObject *)m;
118 }
119 
120 PyObject *
PyModule_NewObject(PyObject * name)121 PyModule_NewObject(PyObject *name)
122 {
123     PyModuleObject *m = new_module_notrack(&PyModule_Type);
124     if (m == NULL)
125         return NULL;
126     if (module_init_dict(m, m->md_dict, name, NULL) != 0)
127         goto fail;
128     track_module(m);
129     return (PyObject *)m;
130 
131  fail:
132     Py_DECREF(m);
133     return NULL;
134 }
135 
136 PyObject *
PyModule_New(const char * name)137 PyModule_New(const char *name)
138 {
139     PyObject *nameobj, *module;
140     nameobj = PyUnicode_FromString(name);
141     if (nameobj == NULL)
142         return NULL;
143     module = PyModule_NewObject(nameobj);
144     Py_DECREF(nameobj);
145     return module;
146 }
147 
148 /* Check API/ABI version
149  * Issues a warning on mismatch, which is usually not fatal.
150  * Returns 0 if an exception is raised.
151  */
152 static int
check_api_version(const char * name,int module_api_version)153 check_api_version(const char *name, int module_api_version)
154 {
155     if (module_api_version != PYTHON_API_VERSION && module_api_version != PYTHON_ABI_VERSION) {
156         int err;
157         err = PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
158             "Python C API version mismatch for module %.100s: "
159             "This Python has API version %d, module %.100s has version %d.",
160              name,
161              PYTHON_API_VERSION, name, module_api_version);
162         if (err)
163             return 0;
164     }
165     return 1;
166 }
167 
168 static int
_add_methods_to_object(PyObject * module,PyObject * name,PyMethodDef * functions)169 _add_methods_to_object(PyObject *module, PyObject *name, PyMethodDef *functions)
170 {
171     PyObject *func;
172     PyMethodDef *fdef;
173 
174     for (fdef = functions; fdef->ml_name != NULL; fdef++) {
175         if ((fdef->ml_flags & METH_CLASS) ||
176             (fdef->ml_flags & METH_STATIC)) {
177             PyErr_SetString(PyExc_ValueError,
178                             "module functions cannot set"
179                             " METH_CLASS or METH_STATIC");
180             return -1;
181         }
182         func = PyCFunction_NewEx(fdef, (PyObject*)module, name);
183         if (func == NULL) {
184             return -1;
185         }
186         _PyObject_SetDeferredRefcount(func);
187         if (PyObject_SetAttrString(module, fdef->ml_name, func) != 0) {
188             Py_DECREF(func);
189             return -1;
190         }
191         Py_DECREF(func);
192     }
193 
194     return 0;
195 }
196 
197 PyObject *
PyModule_Create2(PyModuleDef * module,int module_api_version)198 PyModule_Create2(PyModuleDef* module, int module_api_version)
199 {
200     if (!_PyImport_IsInitialized(_PyInterpreterState_GET())) {
201         PyErr_SetString(PyExc_SystemError,
202                         "Python import machinery not initialized");
203         return NULL;
204     }
205     return _PyModule_CreateInitialized(module, module_api_version);
206 }
207 
208 PyObject *
_PyModule_CreateInitialized(PyModuleDef * module,int module_api_version)209 _PyModule_CreateInitialized(PyModuleDef* module, int module_api_version)
210 {
211     const char* name;
212     PyModuleObject *m;
213 
214     if (!PyModuleDef_Init(module))
215         return NULL;
216     name = module->m_name;
217     if (!check_api_version(name, module_api_version)) {
218         return NULL;
219     }
220     if (module->m_slots) {
221         PyErr_Format(
222             PyExc_SystemError,
223             "module %s: PyModule_Create is incompatible with m_slots", name);
224         return NULL;
225     }
226     name = _PyImport_ResolveNameWithPackageContext(name);
227     if ((m = (PyModuleObject*)PyModule_New(name)) == NULL)
228         return NULL;
229 
230     if (module->m_size > 0) {
231         m->md_state = PyMem_Malloc(module->m_size);
232         if (!m->md_state) {
233             PyErr_NoMemory();
234             Py_DECREF(m);
235             return NULL;
236         }
237         memset(m->md_state, 0, module->m_size);
238     }
239 
240     if (module->m_methods != NULL) {
241         if (PyModule_AddFunctions((PyObject *) m, module->m_methods) != 0) {
242             Py_DECREF(m);
243             return NULL;
244         }
245     }
246     if (module->m_doc != NULL) {
247         if (PyModule_SetDocString((PyObject *) m, module->m_doc) != 0) {
248             Py_DECREF(m);
249             return NULL;
250         }
251     }
252     m->md_def = module;
253 #ifdef Py_GIL_DISABLED
254     m->md_gil = Py_MOD_GIL_USED;
255 #endif
256     return (PyObject*)m;
257 }
258 
259 PyObject *
PyModule_FromDefAndSpec2(PyModuleDef * def,PyObject * spec,int module_api_version)260 PyModule_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_multiple_interpreters_slot = 0;
267     void *multiple_interpreters = (void *)0;
268     int has_gil_slot = 0;
269     void *gil_slot = Py_MOD_GIL_USED;
270     int has_execution_slots = 0;
271     const char *name;
272     int ret;
273     PyInterpreterState *interp = _PyInterpreterState_GET();
274 
275     PyModuleDef_Init(def);
276 
277     nameobj = PyObject_GetAttrString(spec, "name");
278     if (nameobj == NULL) {
279         return NULL;
280     }
281     name = PyUnicode_AsUTF8(nameobj);
282     if (name == NULL) {
283         goto error;
284     }
285 
286     if (!check_api_version(name, module_api_version)) {
287         goto error;
288     }
289 
290     if (def->m_size < 0) {
291         PyErr_Format(
292             PyExc_SystemError,
293             "module %s: m_size may not be negative for multi-phase initialization",
294             name);
295         goto error;
296     }
297 
298     for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) {
299         switch (cur_slot->slot) {
300             case Py_mod_create:
301                 if (create) {
302                     PyErr_Format(
303                         PyExc_SystemError,
304                         "module %s has multiple create slots",
305                         name);
306                     goto error;
307                 }
308                 create = cur_slot->value;
309                 break;
310             case Py_mod_exec:
311                 has_execution_slots = 1;
312                 break;
313             case Py_mod_multiple_interpreters:
314                 if (has_multiple_interpreters_slot) {
315                     PyErr_Format(
316                         PyExc_SystemError,
317                         "module %s has more than one 'multiple interpreters' slots",
318                         name);
319                     goto error;
320                 }
321                 multiple_interpreters = cur_slot->value;
322                 has_multiple_interpreters_slot = 1;
323                 break;
324             case Py_mod_gil:
325                 if (has_gil_slot) {
326                     PyErr_Format(
327                        PyExc_SystemError,
328                        "module %s has more than one 'gil' slot",
329                        name);
330                     goto error;
331                 }
332                 gil_slot = cur_slot->value;
333                 has_gil_slot = 1;
334                 break;
335             default:
336                 assert(cur_slot->slot < 0 || cur_slot->slot > _Py_mod_LAST_SLOT);
337                 PyErr_Format(
338                     PyExc_SystemError,
339                     "module %s uses unknown slot ID %i",
340                     name, cur_slot->slot);
341                 goto error;
342         }
343     }
344 
345     /* By default, multi-phase init modules are expected
346        to work under multiple interpreters. */
347     if (!has_multiple_interpreters_slot) {
348         multiple_interpreters = Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED;
349     }
350     if (multiple_interpreters == Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED) {
351         if (!_Py_IsMainInterpreter(interp)
352             && _PyImport_CheckSubinterpIncompatibleExtensionAllowed(name) < 0)
353         {
354             goto error;
355         }
356     }
357     else if (multiple_interpreters != Py_MOD_PER_INTERPRETER_GIL_SUPPORTED
358              && interp->ceval.own_gil
359              && !_Py_IsMainInterpreter(interp)
360              && _PyImport_CheckSubinterpIncompatibleExtensionAllowed(name) < 0)
361     {
362         goto error;
363     }
364 
365     if (create) {
366         m = create(spec, def);
367         if (m == NULL) {
368             if (!PyErr_Occurred()) {
369                 PyErr_Format(
370                     PyExc_SystemError,
371                     "creation of module %s failed without setting an exception",
372                     name);
373             }
374             goto error;
375         } else {
376             if (PyErr_Occurred()) {
377                 _PyErr_FormatFromCause(
378                     PyExc_SystemError,
379                     "creation of module %s raised unreported exception",
380                     name);
381                 goto error;
382             }
383         }
384     } else {
385         m = PyModule_NewObject(nameobj);
386         if (m == NULL) {
387             goto error;
388         }
389     }
390 
391     if (PyModule_Check(m)) {
392         ((PyModuleObject*)m)->md_state = NULL;
393         ((PyModuleObject*)m)->md_def = def;
394 #ifdef Py_GIL_DISABLED
395         ((PyModuleObject*)m)->md_gil = gil_slot;
396 #else
397         (void)gil_slot;
398 #endif
399     } else {
400         if (def->m_size > 0 || def->m_traverse || def->m_clear || def->m_free) {
401             PyErr_Format(
402                 PyExc_SystemError,
403                 "module %s is not a module object, but requests module state",
404                 name);
405             goto error;
406         }
407         if (has_execution_slots) {
408             PyErr_Format(
409                 PyExc_SystemError,
410                 "module %s specifies execution slots, but did not create "
411                     "a ModuleType instance",
412                 name);
413             goto error;
414         }
415     }
416 
417     if (def->m_methods != NULL) {
418         ret = _add_methods_to_object(m, nameobj, def->m_methods);
419         if (ret != 0) {
420             goto error;
421         }
422     }
423 
424     if (def->m_doc != NULL) {
425         ret = PyModule_SetDocString(m, def->m_doc);
426         if (ret != 0) {
427             goto error;
428         }
429     }
430 
431     Py_DECREF(nameobj);
432     return m;
433 
434 error:
435     Py_DECREF(nameobj);
436     Py_XDECREF(m);
437     return NULL;
438 }
439 
440 #ifdef Py_GIL_DISABLED
441 int
PyUnstable_Module_SetGIL(PyObject * module,void * gil)442 PyUnstable_Module_SetGIL(PyObject *module, void *gil)
443 {
444     if (!PyModule_Check(module)) {
445         PyErr_BadInternalCall();
446         return -1;
447     }
448     ((PyModuleObject *)module)->md_gil = gil;
449     return 0;
450 }
451 #endif
452 
453 int
PyModule_ExecDef(PyObject * module,PyModuleDef * def)454 PyModule_ExecDef(PyObject *module, PyModuleDef *def)
455 {
456     PyModuleDef_Slot *cur_slot;
457     const char *name;
458     int ret;
459 
460     name = PyModule_GetName(module);
461     if (name == NULL) {
462         return -1;
463     }
464 
465     if (def->m_size >= 0) {
466         PyModuleObject *md = (PyModuleObject*)module;
467         if (md->md_state == NULL) {
468             /* Always set a state pointer; this serves as a marker to skip
469              * multiple initialization (importlib.reload() is no-op) */
470             md->md_state = PyMem_Malloc(def->m_size);
471             if (!md->md_state) {
472                 PyErr_NoMemory();
473                 return -1;
474             }
475             memset(md->md_state, 0, def->m_size);
476         }
477     }
478 
479     if (def->m_slots == NULL) {
480         return 0;
481     }
482 
483     for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) {
484         switch (cur_slot->slot) {
485             case Py_mod_create:
486                 /* handled in PyModule_FromDefAndSpec2 */
487                 break;
488             case Py_mod_exec:
489                 ret = ((int (*)(PyObject *))cur_slot->value)(module);
490                 if (ret != 0) {
491                     if (!PyErr_Occurred()) {
492                         PyErr_Format(
493                             PyExc_SystemError,
494                             "execution of module %s failed without setting an exception",
495                             name);
496                     }
497                     return -1;
498                 }
499                 if (PyErr_Occurred()) {
500                     _PyErr_FormatFromCause(
501                         PyExc_SystemError,
502                         "execution of module %s raised unreported exception",
503                         name);
504                     return -1;
505                 }
506                 break;
507             case Py_mod_multiple_interpreters:
508             case Py_mod_gil:
509                 /* handled in PyModule_FromDefAndSpec2 */
510                 break;
511             default:
512                 PyErr_Format(
513                     PyExc_SystemError,
514                     "module %s initialized with unknown slot %i",
515                     name, cur_slot->slot);
516                 return -1;
517         }
518     }
519     return 0;
520 }
521 
522 int
PyModule_AddFunctions(PyObject * m,PyMethodDef * functions)523 PyModule_AddFunctions(PyObject *m, PyMethodDef *functions)
524 {
525     int res;
526     PyObject *name = PyModule_GetNameObject(m);
527     if (name == NULL) {
528         return -1;
529     }
530 
531     res = _add_methods_to_object(m, name, functions);
532     Py_DECREF(name);
533     return res;
534 }
535 
536 int
PyModule_SetDocString(PyObject * m,const char * doc)537 PyModule_SetDocString(PyObject *m, const char *doc)
538 {
539     PyObject *v;
540 
541     v = PyUnicode_FromString(doc);
542     if (v == NULL || PyObject_SetAttr(m, &_Py_ID(__doc__), v) != 0) {
543         Py_XDECREF(v);
544         return -1;
545     }
546     Py_DECREF(v);
547     return 0;
548 }
549 
550 PyObject *
PyModule_GetDict(PyObject * m)551 PyModule_GetDict(PyObject *m)
552 {
553     if (!PyModule_Check(m)) {
554         PyErr_BadInternalCall();
555         return NULL;
556     }
557     return _PyModule_GetDict(m);  // borrowed reference
558 }
559 
560 PyObject*
PyModule_GetNameObject(PyObject * mod)561 PyModule_GetNameObject(PyObject *mod)
562 {
563     if (!PyModule_Check(mod)) {
564         PyErr_BadArgument();
565         return NULL;
566     }
567     PyObject *dict = ((PyModuleObject *)mod)->md_dict;  // borrowed reference
568     if (dict == NULL || !PyDict_Check(dict)) {
569         goto error;
570     }
571     PyObject *name;
572     if (PyDict_GetItemRef(dict, &_Py_ID(__name__), &name) <= 0) {
573         // error or not found
574         goto error;
575     }
576     if (!PyUnicode_Check(name)) {
577         Py_DECREF(name);
578         goto error;
579     }
580     return name;
581 
582 error:
583     if (!PyErr_Occurred()) {
584         PyErr_SetString(PyExc_SystemError, "nameless module");
585     }
586     return NULL;
587 }
588 
589 const char *
PyModule_GetName(PyObject * m)590 PyModule_GetName(PyObject *m)
591 {
592     PyObject *name = PyModule_GetNameObject(m);
593     if (name == NULL) {
594         return NULL;
595     }
596     assert(Py_REFCNT(name) >= 2);
597     Py_DECREF(name);   /* module dict has still a reference */
598     return PyUnicode_AsUTF8(name);
599 }
600 
601 PyObject*
PyModule_GetFilenameObject(PyObject * mod)602 PyModule_GetFilenameObject(PyObject *mod)
603 {
604     if (!PyModule_Check(mod)) {
605         PyErr_BadArgument();
606         return NULL;
607     }
608     PyObject *dict = ((PyModuleObject *)mod)->md_dict;  // borrowed reference
609     if (dict == NULL) {
610         goto error;
611     }
612     PyObject *fileobj;
613     if (PyDict_GetItemRef(dict, &_Py_ID(__file__), &fileobj) <= 0) {
614         // error or not found
615         goto error;
616     }
617     if (!PyUnicode_Check(fileobj)) {
618         Py_DECREF(fileobj);
619         goto error;
620     }
621     return fileobj;
622 
623 error:
624     if (!PyErr_Occurred()) {
625         PyErr_SetString(PyExc_SystemError, "module filename missing");
626     }
627     return NULL;
628 }
629 
630 const char *
PyModule_GetFilename(PyObject * m)631 PyModule_GetFilename(PyObject *m)
632 {
633     PyObject *fileobj;
634     const char *utf8;
635     fileobj = PyModule_GetFilenameObject(m);
636     if (fileobj == NULL)
637         return NULL;
638     utf8 = PyUnicode_AsUTF8(fileobj);
639     Py_DECREF(fileobj);   /* module dict has still a reference */
640     return utf8;
641 }
642 
643 PyModuleDef*
PyModule_GetDef(PyObject * m)644 PyModule_GetDef(PyObject* m)
645 {
646     if (!PyModule_Check(m)) {
647         PyErr_BadArgument();
648         return NULL;
649     }
650     return _PyModule_GetDef(m);
651 }
652 
653 void*
PyModule_GetState(PyObject * m)654 PyModule_GetState(PyObject* m)
655 {
656     if (!PyModule_Check(m)) {
657         PyErr_BadArgument();
658         return NULL;
659     }
660     return _PyModule_GetState(m);
661 }
662 
663 void
_PyModule_Clear(PyObject * m)664 _PyModule_Clear(PyObject *m)
665 {
666     PyObject *d = ((PyModuleObject *)m)->md_dict;
667     if (d != NULL)
668         _PyModule_ClearDict(d);
669 }
670 
671 void
_PyModule_ClearDict(PyObject * d)672 _PyModule_ClearDict(PyObject *d)
673 {
674     /* To make the execution order of destructors for global
675        objects a bit more predictable, we first zap all objects
676        whose name starts with a single underscore, before we clear
677        the entire dictionary.  We zap them by replacing them with
678        None, rather than deleting them from the dictionary, to
679        avoid rehashing the dictionary (to some extent). */
680 
681     Py_ssize_t pos;
682     PyObject *key, *value;
683 
684     int verbose = _Py_GetConfig()->verbose;
685 
686     /* First, clear only names starting with a single underscore */
687     pos = 0;
688     while (PyDict_Next(d, &pos, &key, &value)) {
689         if (value != Py_None && PyUnicode_Check(key)) {
690             if (PyUnicode_READ_CHAR(key, 0) == '_' &&
691                 PyUnicode_READ_CHAR(key, 1) != '_') {
692                 if (verbose > 1) {
693                     const char *s = PyUnicode_AsUTF8(key);
694                     if (s != NULL)
695                         PySys_WriteStderr("#   clear[1] %s\n", s);
696                     else
697                         PyErr_Clear();
698                 }
699                 if (PyDict_SetItem(d, key, Py_None) != 0) {
700                     PyErr_FormatUnraisable("Exception ignored on clearing module dict");
701                 }
702             }
703         }
704     }
705 
706     /* Next, clear all names except for __builtins__ */
707     pos = 0;
708     while (PyDict_Next(d, &pos, &key, &value)) {
709         if (value != Py_None && PyUnicode_Check(key)) {
710             if (PyUnicode_READ_CHAR(key, 0) != '_' ||
711                 !_PyUnicode_EqualToASCIIString(key, "__builtins__"))
712             {
713                 if (verbose > 1) {
714                     const char *s = PyUnicode_AsUTF8(key);
715                     if (s != NULL)
716                         PySys_WriteStderr("#   clear[2] %s\n", s);
717                     else
718                         PyErr_Clear();
719                 }
720                 if (PyDict_SetItem(d, key, Py_None) != 0) {
721                     PyErr_FormatUnraisable("Exception ignored on clearing module dict");
722                 }
723             }
724         }
725     }
726 
727     /* Note: we leave __builtins__ in place, so that destructors
728        of non-global objects defined in this module can still use
729        builtins, in particularly 'None'. */
730 
731 }
732 
733 /*[clinic input]
734 class module "PyModuleObject *" "&PyModule_Type"
735 [clinic start generated code]*/
736 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=3e35d4f708ecb6af]*/
737 
738 #include "clinic/moduleobject.c.h"
739 
740 /* Methods */
741 
742 /*[clinic input]
743 module.__init__
744     name: unicode
745     doc: object = None
746 
747 Create a module object.
748 
749 The name must be a string; the optional doc argument can have any type.
750 [clinic start generated code]*/
751 
752 static int
module___init___impl(PyModuleObject * self,PyObject * name,PyObject * doc)753 module___init___impl(PyModuleObject *self, PyObject *name, PyObject *doc)
754 /*[clinic end generated code: output=e7e721c26ce7aad7 input=57f9e177401e5e1e]*/
755 {
756     return module_init_dict(self, self->md_dict, name, doc);
757 }
758 
759 static void
module_dealloc(PyModuleObject * m)760 module_dealloc(PyModuleObject *m)
761 {
762     int verbose = _Py_GetConfig()->verbose;
763 
764     PyObject_GC_UnTrack(m);
765     if (verbose && m->md_name) {
766         PySys_FormatStderr("# destroy %U\n", m->md_name);
767     }
768     if (m->md_weaklist != NULL)
769         PyObject_ClearWeakRefs((PyObject *) m);
770     /* bpo-39824: Don't call m_free() if m_size > 0 and md_state=NULL */
771     if (m->md_def && m->md_def->m_free
772         && (m->md_def->m_size <= 0 || m->md_state != NULL))
773     {
774         m->md_def->m_free(m);
775     }
776     Py_XDECREF(m->md_dict);
777     Py_XDECREF(m->md_name);
778     if (m->md_state != NULL)
779         PyMem_Free(m->md_state);
780     Py_TYPE(m)->tp_free((PyObject *)m);
781 }
782 
783 static PyObject *
module_repr(PyModuleObject * m)784 module_repr(PyModuleObject *m)
785 {
786     PyInterpreterState *interp = _PyInterpreterState_GET();
787     return _PyImport_ImportlibModuleRepr(interp, (PyObject *)m);
788 }
789 
790 /* Check if the "_initializing" attribute of the module spec is set to true.
791  */
792 int
_PyModuleSpec_IsInitializing(PyObject * spec)793 _PyModuleSpec_IsInitializing(PyObject *spec)
794 {
795     if (spec == NULL) {
796         return 0;
797     }
798     PyObject *value;
799     int rc = PyObject_GetOptionalAttr(spec, &_Py_ID(_initializing), &value);
800     if (rc > 0) {
801         rc = PyObject_IsTrue(value);
802         Py_DECREF(value);
803     }
804     return rc;
805 }
806 
807 /* Check if the submodule name is in the "_uninitialized_submodules" attribute
808    of the module spec.
809  */
810 int
_PyModuleSpec_IsUninitializedSubmodule(PyObject * spec,PyObject * name)811 _PyModuleSpec_IsUninitializedSubmodule(PyObject *spec, PyObject *name)
812 {
813     if (spec == NULL) {
814          return 0;
815     }
816 
817     PyObject *value;
818     int rc = PyObject_GetOptionalAttr(spec, &_Py_ID(_uninitialized_submodules), &value);
819     if (rc > 0) {
820         rc = PySequence_Contains(value, name);
821         Py_DECREF(value);
822     }
823     return rc;
824 }
825 
826 int
_PyModuleSpec_GetFileOrigin(PyObject * spec,PyObject ** p_origin)827 _PyModuleSpec_GetFileOrigin(PyObject *spec, PyObject **p_origin)
828 {
829     PyObject *has_location = NULL;
830     int rc = PyObject_GetOptionalAttr(spec, &_Py_ID(has_location), &has_location);
831     if (rc <= 0) {
832         return rc;
833     }
834     // If origin is not a location, or doesn't exist, or is not a str, we could consider falling
835     // back to module.__file__. But the cases in which module.__file__ is not __spec__.origin
836     // are cases in which we probably shouldn't be guessing.
837     rc = PyObject_IsTrue(has_location);
838     Py_DECREF(has_location);
839     if (rc <= 0) {
840         return rc;
841     }
842     // has_location is true, so origin is a location
843     PyObject *origin = NULL;
844     rc = PyObject_GetOptionalAttr(spec, &_Py_ID(origin), &origin);
845     if (rc <= 0) {
846         return rc;
847     }
848     assert(origin != NULL);
849     if (!PyUnicode_Check(origin)) {
850         Py_DECREF(origin);
851         return 0;
852     }
853     *p_origin = origin;
854     return 1;
855 }
856 
857 int
_PyModule_IsPossiblyShadowing(PyObject * origin)858 _PyModule_IsPossiblyShadowing(PyObject *origin)
859 {
860     // origin must be a unicode subtype
861     // Returns 1 if the module at origin could be shadowing a module of the
862     // same name later in the module search path. The condition we check is basically:
863     // root = os.path.dirname(origin.removesuffix(os.sep + "__init__.py"))
864     // return not sys.flags.safe_path and root == (sys.path[0] or os.getcwd())
865     // Returns 0 otherwise (or if we aren't sure)
866     // Returns -1 if an error occurred that should be propagated
867     if (origin == NULL) {
868         return 0;
869     }
870 
871     // not sys.flags.safe_path
872     const PyConfig *config = _Py_GetConfig();
873     if (config->safe_path) {
874         return 0;
875     }
876 
877     // root = os.path.dirname(origin.removesuffix(os.sep + "__init__.py"))
878     wchar_t root[MAXPATHLEN + 1];
879     Py_ssize_t size = PyUnicode_AsWideChar(origin, root, MAXPATHLEN);
880     if (size < 0) {
881         return -1;
882     }
883     assert(size <= MAXPATHLEN);
884     root[size] = L'\0';
885 
886     wchar_t *sep = wcsrchr(root, SEP);
887     if (sep == NULL) {
888         return 0;
889     }
890     // If it's a package then we need to look one directory further up
891     if (wcscmp(sep + 1, L"__init__.py") == 0) {
892         *sep = L'\0';
893         sep = wcsrchr(root, SEP);
894         if (sep == NULL) {
895             return 0;
896         }
897     }
898     *sep = L'\0';
899 
900     // sys.path[0] or os.getcwd()
901     wchar_t *sys_path_0 = config->sys_path_0;
902     if (!sys_path_0) {
903         return 0;
904     }
905 
906     wchar_t sys_path_0_buf[MAXPATHLEN];
907     if (sys_path_0[0] == L'\0') {
908         // if sys.path[0] == "", treat it as if it were the current directory
909         if (!_Py_wgetcwd(sys_path_0_buf, MAXPATHLEN)) {
910             return -1;
911         }
912         sys_path_0 = sys_path_0_buf;
913     }
914 
915     int result = wcscmp(sys_path_0, root) == 0;
916     return result;
917 }
918 
919 PyObject*
_Py_module_getattro_impl(PyModuleObject * m,PyObject * name,int suppress)920 _Py_module_getattro_impl(PyModuleObject *m, PyObject *name, int suppress)
921 {
922     // When suppress=1, this function suppresses AttributeError.
923     PyObject *attr, *mod_name, *getattr;
924     attr = _PyObject_GenericGetAttrWithDict((PyObject *)m, name, NULL, suppress);
925     if (attr) {
926         return attr;
927     }
928     if (suppress == 1) {
929         if (PyErr_Occurred()) {
930             // pass up non-AttributeError exception
931             return NULL;
932         }
933     }
934     else {
935         if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
936             // pass up non-AttributeError exception
937             return NULL;
938         }
939         PyErr_Clear();
940     }
941     assert(m->md_dict != NULL);
942     if (PyDict_GetItemRef(m->md_dict, &_Py_ID(__getattr__), &getattr) < 0) {
943         return NULL;
944     }
945     if (getattr) {
946         PyObject *result = PyObject_CallOneArg(getattr, name);
947         if (result == NULL && suppress == 1 && PyErr_ExceptionMatches(PyExc_AttributeError)) {
948             // suppress AttributeError
949             PyErr_Clear();
950         }
951         Py_DECREF(getattr);
952         return result;
953     }
954 
955     // The attribute was not found.  We make a best effort attempt at a useful error message,
956     // but only if we're not suppressing AttributeError.
957     if (suppress == 1) {
958         return NULL;
959     }
960     if (PyDict_GetItemRef(m->md_dict, &_Py_ID(__name__), &mod_name) < 0) {
961         return NULL;
962     }
963     if (!mod_name || !PyUnicode_Check(mod_name)) {
964         Py_XDECREF(mod_name);
965         PyErr_Format(PyExc_AttributeError,
966                     "module has no attribute '%U'", name);
967         return NULL;
968     }
969     PyObject *spec;
970     if (PyDict_GetItemRef(m->md_dict, &_Py_ID(__spec__), &spec) < 0) {
971         Py_DECREF(mod_name);
972         return NULL;
973     }
974     if (spec == NULL) {
975         PyErr_Format(PyExc_AttributeError,
976                      "module '%U' has no attribute '%U'",
977                      mod_name, name);
978         Py_DECREF(mod_name);
979         return NULL;
980     }
981 
982     PyObject *origin = NULL;
983     if (_PyModuleSpec_GetFileOrigin(spec, &origin) < 0) {
984         goto done;
985     }
986 
987     int is_possibly_shadowing = _PyModule_IsPossiblyShadowing(origin);
988     if (is_possibly_shadowing < 0) {
989         goto done;
990     }
991     int is_possibly_shadowing_stdlib = 0;
992     if (is_possibly_shadowing) {
993         PyObject *stdlib_modules = PySys_GetObject("stdlib_module_names");
994         if (stdlib_modules && PyAnySet_Check(stdlib_modules)) {
995             is_possibly_shadowing_stdlib = PySet_Contains(stdlib_modules, mod_name);
996             if (is_possibly_shadowing_stdlib < 0) {
997                 goto done;
998             }
999         }
1000     }
1001 
1002     if (is_possibly_shadowing_stdlib) {
1003         assert(origin);
1004         PyErr_Format(PyExc_AttributeError,
1005                     "module '%U' has no attribute '%U' "
1006                     "(consider renaming '%U' since it has the same "
1007                     "name as the standard library module named '%U' "
1008                     "and prevents importing that standard library module)",
1009                     mod_name, name, origin, mod_name);
1010     }
1011     else {
1012         int rc = _PyModuleSpec_IsInitializing(spec);
1013         if (rc < 0) {
1014             goto done;
1015         }
1016         else if (rc > 0) {
1017             if (is_possibly_shadowing) {
1018                 assert(origin);
1019                 // For non-stdlib modules, only mention the possibility of
1020                 // shadowing if the module is being initialized.
1021                 PyErr_Format(PyExc_AttributeError,
1022                             "module '%U' has no attribute '%U' "
1023                             "(consider renaming '%U' if it has the same name "
1024                             "as a library you intended to import)",
1025                             mod_name, name, origin);
1026             }
1027             else if (origin) {
1028                 PyErr_Format(PyExc_AttributeError,
1029                             "partially initialized "
1030                             "module '%U' from '%U' has no attribute '%U' "
1031                             "(most likely due to a circular import)",
1032                             mod_name, origin, name);
1033             }
1034             else {
1035                 PyErr_Format(PyExc_AttributeError,
1036                             "partially initialized "
1037                             "module '%U' has no attribute '%U' "
1038                             "(most likely due to a circular import)",
1039                             mod_name, name);
1040             }
1041         }
1042         else {
1043             assert(rc == 0);
1044             rc = _PyModuleSpec_IsUninitializedSubmodule(spec, name);
1045             if (rc > 0) {
1046                 PyErr_Format(PyExc_AttributeError,
1047                             "cannot access submodule '%U' of module '%U' "
1048                             "(most likely due to a circular import)",
1049                             name, mod_name);
1050             }
1051             else if (rc == 0) {
1052                 PyErr_Format(PyExc_AttributeError,
1053                             "module '%U' has no attribute '%U'",
1054                             mod_name, name);
1055             }
1056         }
1057     }
1058 
1059 done:
1060     Py_XDECREF(origin);
1061     Py_DECREF(spec);
1062     Py_DECREF(mod_name);
1063     return NULL;
1064 }
1065 
1066 
1067 PyObject*
_Py_module_getattro(PyModuleObject * m,PyObject * name)1068 _Py_module_getattro(PyModuleObject *m, PyObject *name)
1069 {
1070     return _Py_module_getattro_impl(m, name, 0);
1071 }
1072 
1073 static int
module_traverse(PyModuleObject * m,visitproc visit,void * arg)1074 module_traverse(PyModuleObject *m, visitproc visit, void *arg)
1075 {
1076     /* bpo-39824: Don't call m_traverse() if m_size > 0 and md_state=NULL */
1077     if (m->md_def && m->md_def->m_traverse
1078         && (m->md_def->m_size <= 0 || m->md_state != NULL))
1079     {
1080         int res = m->md_def->m_traverse((PyObject*)m, visit, arg);
1081         if (res)
1082             return res;
1083     }
1084     Py_VISIT(m->md_dict);
1085     return 0;
1086 }
1087 
1088 static int
module_clear(PyModuleObject * m)1089 module_clear(PyModuleObject *m)
1090 {
1091     /* bpo-39824: Don't call m_clear() if m_size > 0 and md_state=NULL */
1092     if (m->md_def && m->md_def->m_clear
1093         && (m->md_def->m_size <= 0 || m->md_state != NULL))
1094     {
1095         int res = m->md_def->m_clear((PyObject*)m);
1096         if (PyErr_Occurred()) {
1097             PyErr_FormatUnraisable("Exception ignored in m_clear of module%s%V",
1098                                    m->md_name ? " " : "",
1099                                    m->md_name, "");
1100         }
1101         if (res)
1102             return res;
1103     }
1104     Py_CLEAR(m->md_dict);
1105     return 0;
1106 }
1107 
1108 static PyObject *
module_dir(PyObject * self,PyObject * args)1109 module_dir(PyObject *self, PyObject *args)
1110 {
1111     PyObject *result = NULL;
1112     PyObject *dict = PyObject_GetAttr(self, &_Py_ID(__dict__));
1113 
1114     if (dict != NULL) {
1115         if (PyDict_Check(dict)) {
1116             PyObject *dirfunc = PyDict_GetItemWithError(dict, &_Py_ID(__dir__));
1117             if (dirfunc) {
1118                 result = _PyObject_CallNoArgs(dirfunc);
1119             }
1120             else if (!PyErr_Occurred()) {
1121                 result = PyDict_Keys(dict);
1122             }
1123         }
1124         else {
1125             PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary");
1126         }
1127     }
1128 
1129     Py_XDECREF(dict);
1130     return result;
1131 }
1132 
1133 static PyMethodDef module_methods[] = {
1134     {"__dir__", module_dir, METH_NOARGS,
1135      PyDoc_STR("__dir__() -> list\nspecialized dir() implementation")},
1136     {0}
1137 };
1138 
1139 static PyObject *
module_get_annotations(PyModuleObject * m,void * Py_UNUSED (ignored))1140 module_get_annotations(PyModuleObject *m, void *Py_UNUSED(ignored))
1141 {
1142     PyObject *dict = PyObject_GetAttr((PyObject *)m, &_Py_ID(__dict__));
1143     if (dict == NULL) {
1144         return NULL;
1145     }
1146     if (!PyDict_Check(dict)) {
1147         PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary");
1148         Py_DECREF(dict);
1149         return NULL;
1150     }
1151 
1152     PyObject *annotations;
1153     if (PyDict_GetItemRef(dict, &_Py_ID(__annotations__), &annotations) == 0) {
1154         annotations = PyDict_New();
1155         if (annotations) {
1156             int result = PyDict_SetItem(
1157                     dict, &_Py_ID(__annotations__), annotations);
1158             if (result) {
1159                 Py_CLEAR(annotations);
1160             }
1161         }
1162     }
1163     Py_DECREF(dict);
1164     return annotations;
1165 }
1166 
1167 static int
module_set_annotations(PyModuleObject * m,PyObject * value,void * Py_UNUSED (ignored))1168 module_set_annotations(PyModuleObject *m, PyObject *value, void *Py_UNUSED(ignored))
1169 {
1170     int ret = -1;
1171     PyObject *dict = PyObject_GetAttr((PyObject *)m, &_Py_ID(__dict__));
1172     if (dict == NULL) {
1173         return -1;
1174     }
1175     if (!PyDict_Check(dict)) {
1176         PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary");
1177         goto exit;
1178     }
1179 
1180     if (value != NULL) {
1181         /* set */
1182         ret = PyDict_SetItem(dict, &_Py_ID(__annotations__), value);
1183     }
1184     else {
1185         /* delete */
1186         ret = PyDict_Pop(dict, &_Py_ID(__annotations__), NULL);
1187         if (ret == 0) {
1188             PyErr_SetObject(PyExc_AttributeError, &_Py_ID(__annotations__));
1189             ret = -1;
1190         }
1191         else if (ret > 0) {
1192             ret = 0;
1193         }
1194     }
1195 
1196 exit:
1197     Py_DECREF(dict);
1198     return ret;
1199 }
1200 
1201 
1202 static PyGetSetDef module_getsets[] = {
1203     {"__annotations__", (getter)module_get_annotations, (setter)module_set_annotations},
1204     {NULL}
1205 };
1206 
1207 PyTypeObject PyModule_Type = {
1208     PyVarObject_HEAD_INIT(&PyType_Type, 0)
1209     "module",                                   /* tp_name */
1210     sizeof(PyModuleObject),                     /* tp_basicsize */
1211     0,                                          /* tp_itemsize */
1212     (destructor)module_dealloc,                 /* tp_dealloc */
1213     0,                                          /* tp_vectorcall_offset */
1214     0,                                          /* tp_getattr */
1215     0,                                          /* tp_setattr */
1216     0,                                          /* tp_as_async */
1217     (reprfunc)module_repr,                      /* tp_repr */
1218     0,                                          /* tp_as_number */
1219     0,                                          /* tp_as_sequence */
1220     0,                                          /* tp_as_mapping */
1221     0,                                          /* tp_hash */
1222     0,                                          /* tp_call */
1223     0,                                          /* tp_str */
1224     (getattrofunc)_Py_module_getattro,          /* tp_getattro */
1225     PyObject_GenericSetAttr,                    /* tp_setattro */
1226     0,                                          /* tp_as_buffer */
1227     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1228         Py_TPFLAGS_BASETYPE,                    /* tp_flags */
1229     module___init____doc__,                     /* tp_doc */
1230     (traverseproc)module_traverse,              /* tp_traverse */
1231     (inquiry)module_clear,                      /* tp_clear */
1232     0,                                          /* tp_richcompare */
1233     offsetof(PyModuleObject, md_weaklist),      /* tp_weaklistoffset */
1234     0,                                          /* tp_iter */
1235     0,                                          /* tp_iternext */
1236     module_methods,                             /* tp_methods */
1237     module_members,                             /* tp_members */
1238     module_getsets,                             /* tp_getset */
1239     0,                                          /* tp_base */
1240     0,                                          /* tp_dict */
1241     0,                                          /* tp_descr_get */
1242     0,                                          /* tp_descr_set */
1243     offsetof(PyModuleObject, md_dict),          /* tp_dictoffset */
1244     module___init__,                            /* tp_init */
1245     0,                                          /* tp_alloc */
1246     new_module,                                 /* tp_new */
1247     PyObject_GC_Del,                            /* tp_free */
1248 };
1249