LCOV - code coverage report
Current view: top level - Objects - moduleobject.c (source / functions) Hit Total Coverage
Test: CPython lcov report Lines: 361 467 77.3 %
Date: 2022-07-07 18:19:46 Functions: 34 35 97.1 %

          Line data    Source code
       1             : 
       2             : /* Module object implementation */
       3             : 
       4             : #include "Python.h"
       5             : #include "pycore_call.h"          // _PyObject_CallNoArgs()
       6             : #include "pycore_interp.h"        // PyInterpreterState.importlib
       7             : #include "pycore_object.h"        // _PyType_AllocNoTrack
       8             : #include "pycore_pystate.h"       // _PyInterpreterState_GET()
       9             : #include "pycore_moduleobject.h"  // _PyModule_GetDef()
      10             : #include "structmember.h"         // PyMemberDef
      11             : 
      12             : static Py_ssize_t max_module_number;
      13             : 
      14             : static PyMemberDef module_members[] = {
      15             :     {"__dict__", T_OBJECT, offsetof(PyModuleObject, md_dict), READONLY},
      16             :     {0}
      17             : };
      18             : 
      19             : 
      20             : PyTypeObject 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             : 
      28             : int
      29          40 : _PyModule_IsExtension(PyObject *obj)
      30             : {
      31          40 :     if (!PyModule_Check(obj)) {
      32           0 :         return 0;
      33             :     }
      34          40 :     PyModuleObject *module = (PyModuleObject*)obj;
      35             : 
      36          40 :     PyModuleDef *def = module->md_def;
      37          40 :     return (def != NULL && def->m_methods != NULL);
      38             : }
      39             : 
      40             : 
      41             : PyObject*
      42      171572 : PyModuleDef_Init(PyModuleDef* def)
      43             : {
      44      171572 :     assert(PyModuleDef_Type.tp_flags & Py_TPFLAGS_READY);
      45      171572 :     if (def->m_base.m_index == 0) {
      46       89356 :         max_module_number++;
      47       89356 :         Py_SET_REFCNT(def, 1);
      48       89356 :         Py_SET_TYPE(def, &PyModuleDef_Type);
      49       89356 :         def->m_base.m_index = max_module_number;
      50             :     }
      51      171572 :     return (PyObject*)def;
      52             : }
      53             : 
      54             : static int
      55      312026 : module_init_dict(PyModuleObject *mod, PyObject *md_dict,
      56             :                  PyObject *name, PyObject *doc)
      57             : {
      58      312026 :     assert(md_dict != NULL);
      59      312026 :     if (doc == NULL)
      60       98786 :         doc = Py_None;
      61             : 
      62      312026 :     if (PyDict_SetItem(md_dict, &_Py_ID(__name__), name) != 0)
      63           0 :         return -1;
      64      312026 :     if (PyDict_SetItem(md_dict, &_Py_ID(__doc__), doc) != 0)
      65           0 :         return -1;
      66      312026 :     if (PyDict_SetItem(md_dict, &_Py_ID(__package__), Py_None) != 0)
      67           0 :         return -1;
      68      312026 :     if (PyDict_SetItem(md_dict, &_Py_ID(__loader__), Py_None) != 0)
      69           0 :         return -1;
      70      312026 :     if (PyDict_SetItem(md_dict, &_Py_ID(__spec__), Py_None) != 0)
      71           0 :         return -1;
      72      312026 :     if (PyUnicode_CheckExact(name)) {
      73      312026 :         Py_INCREF(name);
      74      312026 :         Py_XSETREF(mod->md_name, name);
      75             :     }
      76             : 
      77      312026 :     return 0;
      78             : }
      79             : 
      80             : static PyModuleObject *
      81      312028 : new_module_notrack(PyTypeObject *mt)
      82             : {
      83             :     PyModuleObject *m;
      84      312028 :     m = (PyModuleObject *)_PyType_AllocNoTrack(mt, 0);
      85      312028 :     if (m == NULL)
      86           0 :         return NULL;
      87      312028 :     m->md_def = NULL;
      88      312028 :     m->md_state = NULL;
      89      312028 :     m->md_weaklist = NULL;
      90      312028 :     m->md_name = NULL;
      91      312028 :     m->md_dict = PyDict_New();
      92      312028 :     if (m->md_dict != NULL) {
      93      312028 :         return m;
      94             :     }
      95           0 :     Py_DECREF(m);
      96           0 :     return NULL;
      97             : }
      98             : 
      99             : static PyObject *
     100      213242 : new_module(PyTypeObject *mt, PyObject *args, PyObject *kws)
     101             : {
     102      213242 :     PyObject *m = (PyObject *)new_module_notrack(mt);
     103      213242 :     if (m != NULL) {
     104      213242 :         PyObject_GC_Track(m);
     105             :     }
     106      213242 :     return m;
     107             : }
     108             : 
     109             : PyObject *
     110       98786 : PyModule_NewObject(PyObject *name)
     111             : {
     112       98786 :     PyModuleObject *m = new_module_notrack(&PyModule_Type);
     113       98786 :     if (m == NULL)
     114           0 :         return NULL;
     115       98786 :     if (module_init_dict(m, m->md_dict, name, NULL) != 0)
     116           0 :         goto fail;
     117       98786 :     PyObject_GC_Track(m);
     118       98786 :     return (PyObject *)m;
     119             : 
     120           0 :  fail:
     121           0 :     Py_DECREF(m);
     122           0 :     return NULL;
     123             : }
     124             : 
     125             : PyObject *
     126       13350 : PyModule_New(const char *name)
     127             : {
     128             :     PyObject *nameobj, *module;
     129       13350 :     nameobj = PyUnicode_FromString(name);
     130       13350 :     if (nameobj == NULL)
     131           0 :         return NULL;
     132       13350 :     module = PyModule_NewObject(nameobj);
     133       13350 :     Py_DECREF(nameobj);
     134       13350 :     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             :  */
     141             : static int
     142       92421 : check_api_version(const char *name, int module_api_version)
     143             : {
     144       92421 :     if (module_api_version != PYTHON_API_VERSION && module_api_version != PYTHON_ABI_VERSION) {
     145             :         int err;
     146           0 :         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           0 :         if (err)
     152           0 :             return 0;
     153             :     }
     154       92421 :     return 1;
     155             : }
     156             : 
     157             : static int
     158       87157 : _add_methods_to_object(PyObject *module, PyObject *name, PyMethodDef *functions)
     159             : {
     160             :     PyObject *func;
     161             :     PyMethodDef *fdef;
     162             : 
     163     2005080 :     for (fdef = functions; fdef->ml_name != NULL; fdef++) {
     164     1917920 :         if ((fdef->ml_flags & METH_CLASS) ||
     165     1917920 :             (fdef->ml_flags & METH_STATIC)) {
     166           0 :             PyErr_SetString(PyExc_ValueError,
     167             :                             "module functions cannot set"
     168             :                             " METH_CLASS or METH_STATIC");
     169           0 :             return -1;
     170             :         }
     171     1917920 :         func = PyCFunction_NewEx(fdef, (PyObject*)module, name);
     172     1917920 :         if (func == NULL) {
     173           0 :             return -1;
     174             :         }
     175     1917920 :         if (PyObject_SetAttrString(module, fdef->ml_name, func) != 0) {
     176           0 :             Py_DECREF(func);
     177           0 :             return -1;
     178             :         }
     179     1917920 :         Py_DECREF(func);
     180             :     }
     181             : 
     182       87157 :     return 0;
     183             : }
     184             : 
     185             : PyObject *
     186        7004 : PyModule_Create2(PyModuleDef* module, int module_api_version)
     187             : {
     188        7004 :     if (!_PyImport_IsInitialized(_PyInterpreterState_GET())) {
     189           0 :         PyErr_SetString(PyExc_SystemError,
     190             :                         "Python import machinery not initialized");
     191           0 :         return NULL;
     192             :     }
     193        7004 :     return _PyModule_CreateInitialized(module, module_api_version);
     194             : }
     195             : 
     196             : PyObject *
     197       13272 : _PyModule_CreateInitialized(PyModuleDef* module, int module_api_version)
     198             : {
     199             :     const char* name;
     200             :     PyModuleObject *m;
     201             : 
     202       13272 :     if (!PyModuleDef_Init(module))
     203           0 :         return NULL;
     204       13272 :     name = module->m_name;
     205       13272 :     if (!check_api_version(name, module_api_version)) {
     206           0 :         return NULL;
     207             :     }
     208       13272 :     if (module->m_slots) {
     209           0 :         PyErr_Format(
     210             :             PyExc_SystemError,
     211             :             "module %s: PyModule_Create is incompatible with m_slots", name);
     212           0 :         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       13272 :     if (_Py_PackageContext != NULL) {
     224        3847 :         const char *p = strrchr(_Py_PackageContext, '.');
     225        3847 :         if (p != NULL && strcmp(module->m_name, p+1) == 0) {
     226           0 :             name = _Py_PackageContext;
     227           0 :             _Py_PackageContext = NULL;
     228             :         }
     229             :     }
     230       13272 :     if ((m = (PyModuleObject*)PyModule_New(name)) == NULL)
     231           0 :         return NULL;
     232             : 
     233       13272 :     if (module->m_size > 0) {
     234        3990 :         m->md_state = PyMem_Malloc(module->m_size);
     235        3990 :         if (!m->md_state) {
     236           0 :             PyErr_NoMemory();
     237           0 :             Py_DECREF(m);
     238           0 :             return NULL;
     239             :         }
     240        3990 :         memset(m->md_state, 0, module->m_size);
     241             :     }
     242             : 
     243       13272 :     if (module->m_methods != NULL) {
     244       13267 :         if (PyModule_AddFunctions((PyObject *) m, module->m_methods) != 0) {
     245           0 :             Py_DECREF(m);
     246           0 :             return NULL;
     247             :         }
     248             :     }
     249       13272 :     if (module->m_doc != NULL) {
     250       11977 :         if (PyModule_SetDocString((PyObject *) m, module->m_doc) != 0) {
     251           0 :             Py_DECREF(m);
     252           0 :             return NULL;
     253             :         }
     254             :     }
     255       13272 :     m->md_def = module;
     256       13272 :     return (PyObject*)m;
     257             : }
     258             : 
     259             : PyObject *
     260       79149 : PyModule_FromDefAndSpec2(PyModuleDef* def, PyObject *spec, int module_api_version)
     261             : {
     262             :     PyModuleDef_Slot* cur_slot;
     263       79149 :     PyObject *(*create)(PyObject *, PyModuleDef*) = NULL;
     264             :     PyObject *nameobj;
     265       79149 :     PyObject *m = NULL;
     266       79149 :     int has_execution_slots = 0;
     267             :     const char *name;
     268             :     int ret;
     269             : 
     270       79149 :     PyModuleDef_Init(def);
     271             : 
     272       79149 :     nameobj = PyObject_GetAttrString(spec, "name");
     273       79149 :     if (nameobj == NULL) {
     274           0 :         return NULL;
     275             :     }
     276       79149 :     name = PyUnicode_AsUTF8(nameobj);
     277       79149 :     if (name == NULL) {
     278           0 :         goto error;
     279             :     }
     280             : 
     281       79149 :     if (!check_api_version(name, module_api_version)) {
     282           0 :         goto error;
     283             :     }
     284             : 
     285       79149 :     if (def->m_size < 0) {
     286           2 :         PyErr_Format(
     287             :             PyExc_SystemError,
     288             :             "module %s: m_size may not be negative for multi-phase initialization",
     289             :             name);
     290           2 :         goto error;
     291             :     }
     292             : 
     293      154358 :     for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) {
     294       75215 :         if (cur_slot->slot == Py_mod_create) {
     295          14 :             if (create) {
     296           0 :                 PyErr_Format(
     297             :                     PyExc_SystemError,
     298             :                     "module %s has multiple create slots",
     299             :                     name);
     300           0 :                 goto error;
     301             :             }
     302          14 :             create = cur_slot->value;
     303       75201 :         } else if (cur_slot->slot < 0 || cur_slot->slot > _Py_mod_LAST_SLOT) {
     304           4 :             PyErr_Format(
     305             :                 PyExc_SystemError,
     306             :                 "module %s uses unknown slot ID %i",
     307             :                 name, cur_slot->slot);
     308           4 :             goto error;
     309             :         } else {
     310       75197 :             has_execution_slots = 1;
     311             :         }
     312             :     }
     313             : 
     314       79143 :     if (create) {
     315          14 :         m = create(spec, def);
     316          14 :         if (m == NULL) {
     317           8 :             if (!PyErr_Occurred()) {
     318           2 :                 PyErr_Format(
     319             :                     PyExc_SystemError,
     320             :                     "creation of module %s failed without setting an exception",
     321             :                     name);
     322             :             }
     323           8 :             goto error;
     324             :         } else {
     325           6 :             if (PyErr_Occurred()) {
     326           2 :                 PyErr_Format(PyExc_SystemError,
     327             :                             "creation of module %s raised unreported exception",
     328             :                             name);
     329           2 :                 goto error;
     330             :             }
     331             :         }
     332             :     } else {
     333       79129 :         m = PyModule_NewObject(nameobj);
     334       79129 :         if (m == NULL) {
     335           0 :             goto error;
     336             :         }
     337             :     }
     338             : 
     339       79133 :     if (PyModule_Check(m)) {
     340       79129 :         ((PyModuleObject*)m)->md_state = NULL;
     341       79129 :         ((PyModuleObject*)m)->md_def = def;
     342             :     } else {
     343           4 :         if (def->m_size > 0 || def->m_traverse || def->m_clear || def->m_free) {
     344           0 :             PyErr_Format(
     345             :                 PyExc_SystemError,
     346             :                 "module %s is not a module object, but requests module state",
     347             :                 name);
     348           0 :             goto error;
     349             :         }
     350           4 :         if (has_execution_slots) {
     351           0 :             PyErr_Format(
     352             :                 PyExc_SystemError,
     353             :                 "module %s specifies execution slots, but did not create "
     354             :                     "a ModuleType instance",
     355             :                 name);
     356           0 :             goto error;
     357             :         }
     358             :     }
     359             : 
     360       79133 :     if (def->m_methods != NULL) {
     361       73890 :         ret = _add_methods_to_object(m, nameobj, def->m_methods);
     362       73890 :         if (ret != 0) {
     363           0 :             goto error;
     364             :         }
     365             :     }
     366             : 
     367       79133 :     if (def->m_doc != NULL) {
     368       67163 :         ret = PyModule_SetDocString(m, def->m_doc);
     369       67163 :         if (ret != 0) {
     370           0 :             goto error;
     371             :         }
     372             :     }
     373             : 
     374       79133 :     Py_DECREF(nameobj);
     375       79133 :     return m;
     376             : 
     377          16 : error:
     378          16 :     Py_DECREF(nameobj);
     379          16 :     Py_XDECREF(m);
     380          16 :     return NULL;
     381             : }
     382             : 
     383             : int
     384       82146 : PyModule_ExecDef(PyObject *module, PyModuleDef *def)
     385             : {
     386             :     PyModuleDef_Slot *cur_slot;
     387             :     const char *name;
     388             :     int ret;
     389             : 
     390       82146 :     name = PyModule_GetName(module);
     391       82146 :     if (name == NULL) {
     392           0 :         return -1;
     393             :     }
     394             : 
     395       82146 :     if (def->m_size >= 0) {
     396       79148 :         PyModuleObject *md = (PyModuleObject*)module;
     397       79148 :         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       79148 :             md->md_state = PyMem_Malloc(def->m_size);
     401       79148 :             if (!md->md_state) {
     402           0 :                 PyErr_NoMemory();
     403           0 :                 return -1;
     404             :             }
     405       79148 :             memset(md->md_state, 0, def->m_size);
     406             :         }
     407             :     }
     408             : 
     409       82146 :     if (def->m_slots == NULL) {
     410        6811 :         return 0;
     411             :     }
     412             : 
     413      150524 :     for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) {
     414       75195 :         switch (cur_slot->slot) {
     415           0 :             case Py_mod_create:
     416             :                 /* handled in PyModule_FromDefAndSpec2 */
     417           0 :                 break;
     418       75195 :             case Py_mod_exec:
     419       75195 :                 ret = ((int (*)(PyObject *))cur_slot->value)(module);
     420       75195 :                 if (ret != 0) {
     421           4 :                     if (!PyErr_Occurred()) {
     422           2 :                         PyErr_Format(
     423             :                             PyExc_SystemError,
     424             :                             "execution of module %s failed without setting an exception",
     425             :                             name);
     426             :                     }
     427           4 :                     return -1;
     428             :                 }
     429       75191 :                 if (PyErr_Occurred()) {
     430           2 :                     PyErr_Format(
     431             :                         PyExc_SystemError,
     432             :                         "execution of module %s raised unreported exception",
     433             :                         name);
     434           2 :                     return -1;
     435             :                 }
     436       75189 :                 break;
     437           0 :             default:
     438           0 :                 PyErr_Format(
     439             :                     PyExc_SystemError,
     440             :                     "module %s initialized with unknown slot %i",
     441             :                     name, cur_slot->slot);
     442           0 :                 return -1;
     443             :         }
     444             :     }
     445       75329 :     return 0;
     446             : }
     447             : 
     448             : int
     449       13267 : PyModule_AddFunctions(PyObject *m, PyMethodDef *functions)
     450             : {
     451             :     int res;
     452       13267 :     PyObject *name = PyModule_GetNameObject(m);
     453       13267 :     if (name == NULL) {
     454           0 :         return -1;
     455             :     }
     456             : 
     457       13267 :     res = _add_methods_to_object(m, name, functions);
     458       13267 :     Py_DECREF(name);
     459       13267 :     return res;
     460             : }
     461             : 
     462             : int
     463       79140 : PyModule_SetDocString(PyObject *m, const char *doc)
     464             : {
     465             :     PyObject *v;
     466             : 
     467       79140 :     v = PyUnicode_FromString(doc);
     468       79140 :     if (v == NULL || PyObject_SetAttr(m, &_Py_ID(__doc__), v) != 0) {
     469           0 :         Py_XDECREF(v);
     470           0 :         return -1;
     471             :     }
     472       79140 :     Py_DECREF(v);
     473       79140 :     return 0;
     474             : }
     475             : 
     476             : PyObject *
     477     2348800 : PyModule_GetDict(PyObject *m)
     478             : {
     479     2348800 :     if (!PyModule_Check(m)) {
     480           0 :         PyErr_BadInternalCall();
     481           0 :         return NULL;
     482             :     }
     483     2348800 :     return _PyModule_GetDict(m);
     484             : }
     485             : 
     486             : PyObject*
     487       95413 : PyModule_GetNameObject(PyObject *m)
     488             : {
     489             :     PyObject *d;
     490             :     PyObject *name;
     491       95413 :     if (!PyModule_Check(m)) {
     492           0 :         PyErr_BadArgument();
     493           0 :         return NULL;
     494             :     }
     495       95413 :     d = ((PyModuleObject *)m)->md_dict;
     496      190826 :     if (d == NULL || !PyDict_Check(d) ||
     497      190826 :         (name = PyDict_GetItemWithError(d, &_Py_ID(__name__))) == NULL ||
     498       95413 :         !PyUnicode_Check(name))
     499             :     {
     500           0 :         if (!PyErr_Occurred()) {
     501           0 :             PyErr_SetString(PyExc_SystemError, "nameless module");
     502             :         }
     503           0 :         return NULL;
     504             :     }
     505       95413 :     Py_INCREF(name);
     506       95413 :     return name;
     507             : }
     508             : 
     509             : const char *
     510       82146 : PyModule_GetName(PyObject *m)
     511             : {
     512       82146 :     PyObject *name = PyModule_GetNameObject(m);
     513       82146 :     if (name == NULL) {
     514           0 :         return NULL;
     515             :     }
     516       82146 :     assert(Py_REFCNT(name) >= 2);
     517       82146 :     Py_DECREF(name);   /* module dict has still a reference */
     518       82146 :     return PyUnicode_AsUTF8(name);
     519             : }
     520             : 
     521             : PyObject*
     522        4768 : PyModule_GetFilenameObject(PyObject *m)
     523             : {
     524             :     PyObject *d;
     525             :     PyObject *fileobj;
     526        4768 :     if (!PyModule_Check(m)) {
     527           1 :         PyErr_BadArgument();
     528           1 :         return NULL;
     529             :     }
     530        4767 :     d = ((PyModuleObject *)m)->md_dict;
     531        9534 :     if (d == NULL ||
     532        6405 :         (fileobj = PyDict_GetItemWithError(d, &_Py_ID(__file__))) == NULL ||
     533        1638 :         !PyUnicode_Check(fileobj))
     534             :     {
     535        3129 :         if (!PyErr_Occurred()) {
     536        3129 :             PyErr_SetString(PyExc_SystemError, "module filename missing");
     537             :         }
     538        3129 :         return NULL;
     539             :     }
     540        1638 :     Py_INCREF(fileobj);
     541        1638 :     return fileobj;
     542             : }
     543             : 
     544             : const char *
     545           0 : PyModule_GetFilename(PyObject *m)
     546             : {
     547             :     PyObject *fileobj;
     548             :     const char *utf8;
     549           0 :     fileobj = PyModule_GetFilenameObject(m);
     550           0 :     if (fileobj == NULL)
     551           0 :         return NULL;
     552           0 :     utf8 = PyUnicode_AsUTF8(fileobj);
     553           0 :     Py_DECREF(fileobj);   /* module dict has still a reference */
     554           0 :     return utf8;
     555             : }
     556             : 
     557             : PyModuleDef*
     558      119809 : PyModule_GetDef(PyObject* m)
     559             : {
     560      119809 :     if (!PyModule_Check(m)) {
     561           0 :         PyErr_BadArgument();
     562           0 :         return NULL;
     563             :     }
     564      119809 :     return _PyModule_GetDef(m);
     565             : }
     566             : 
     567             : void*
     568      793061 : PyModule_GetState(PyObject* m)
     569             : {
     570      793061 :     if (!PyModule_Check(m)) {
     571           0 :         PyErr_BadArgument();
     572           0 :         return NULL;
     573             :     }
     574      793061 :     return _PyModule_GetState(m);
     575             : }
     576             : 
     577             : void
     578      134109 : _PyModule_Clear(PyObject *m)
     579             : {
     580      134109 :     PyObject *d = ((PyModuleObject *)m)->md_dict;
     581      134109 :     if (d != NULL)
     582      134109 :         _PyModule_ClearDict(d);
     583      134109 : }
     584             : 
     585             : void
     586      140349 : _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      140349 :     int verbose = _Py_GetConfig()->verbose;
     599             : 
     600             :     /* First, clear only names starting with a single underscore */
     601      140349 :     pos = 0;
     602     8759540 :     while (PyDict_Next(d, &pos, &key, &value)) {
     603     8619190 :         if (value != Py_None && PyUnicode_Check(key)) {
     604    10160100 :             if (PyUnicode_READ_CHAR(key, 0) == '_' &&
     605     1890770 :                 PyUnicode_READ_CHAR(key, 1) != '_') {
     606      921057 :                 if (verbose > 1) {
     607         438 :                     const char *s = PyUnicode_AsUTF8(key);
     608         438 :                     if (s != NULL)
     609         438 :                         PySys_WriteStderr("#   clear[1] %s\n", s);
     610             :                     else
     611           0 :                         PyErr_Clear();
     612             :                 }
     613      921057 :                 if (PyDict_SetItem(d, key, Py_None) != 0) {
     614           0 :                     PyErr_WriteUnraisable(NULL);
     615             :                 }
     616             :             }
     617             :         }
     618             :     }
     619             : 
     620             :     /* Next, clear all names except for __builtins__ */
     621      140349 :     pos = 0;
     622     8759540 :     while (PyDict_Next(d, &pos, &key, &value)) {
     623     8619190 :         if (value != Py_None && PyUnicode_Check(key)) {
     624     8317950 :             if (PyUnicode_READ_CHAR(key, 0) != '_' ||
     625      969713 :                 !_PyUnicode_EqualToASCIIString(key, "__builtins__"))
     626             :             {
     627     7273680 :                 if (verbose > 1) {
     628        3693 :                     const char *s = PyUnicode_AsUTF8(key);
     629        3693 :                     if (s != NULL)
     630        3693 :                         PySys_WriteStderr("#   clear[2] %s\n", s);
     631             :                     else
     632           0 :                         PyErr_Clear();
     633             :                 }
     634     7273680 :                 if (PyDict_SetItem(d, key, Py_None) != 0) {
     635           0 :                     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      140349 : }
     646             : 
     647             : /*[clinic input]
     648             : class 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]
     657             : module.__init__
     658             :     name: unicode
     659             :     doc: object = None
     660             : 
     661             : Create a module object.
     662             : 
     663             : The name must be a string; the optional doc argument can have any type.
     664             : [clinic start generated code]*/
     665             : 
     666             : static int
     667      213240 : module___init___impl(PyModuleObject *self, PyObject *name, PyObject *doc)
     668             : /*[clinic end generated code: output=e7e721c26ce7aad7 input=57f9e177401e5e1e]*/
     669             : {
     670      213240 :     PyObject *dict = self->md_dict;
     671      213240 :     if (dict == NULL) {
     672           0 :         dict = PyDict_New();
     673           0 :         if (dict == NULL)
     674           0 :             return -1;
     675           0 :         self->md_dict = dict;
     676             :     }
     677      213240 :     if (module_init_dict(self, dict, name, doc) < 0)
     678           0 :         return -1;
     679      213240 :     return 0;
     680             : }
     681             : 
     682             : static void
     683      308515 : module_dealloc(PyModuleObject *m)
     684             : {
     685      308515 :     int verbose = _Py_GetConfig()->verbose;
     686             : 
     687      308515 :     PyObject_GC_UnTrack(m);
     688      308515 :     if (verbose && m->md_name) {
     689         617 :         PySys_FormatStderr("# destroy %U\n", m->md_name);
     690             :     }
     691      308515 :     if (m->md_weaklist != NULL)
     692       87439 :         PyObject_ClearWeakRefs((PyObject *) m);
     693             :     /* bpo-39824: Don't call m_free() if m_size > 0 and md_state=NULL */
     694      308515 :     if (m->md_def && m->md_def->m_free
     695       42143 :         && (m->md_def->m_size <= 0 || m->md_state != NULL))
     696             :     {
     697       42143 :         m->md_def->m_free(m);
     698             :     }
     699      308515 :     Py_XDECREF(m->md_dict);
     700      308515 :     Py_XDECREF(m->md_name);
     701      308515 :     if (m->md_state != NULL)
     702       82280 :         PyMem_Free(m->md_state);
     703      308515 :     Py_TYPE(m)->tp_free((PyObject *)m);
     704      308515 : }
     705             : 
     706             : static PyObject *
     707        7100 : module_repr(PyModuleObject *m)
     708             : {
     709        7100 :     PyInterpreterState *interp = _PyInterpreterState_GET();
     710             : 
     711        7100 :     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             :  */
     717             : int
     718     3990200 : _PyModuleSpec_IsInitializing(PyObject *spec)
     719             : {
     720     3990200 :     if (spec != NULL) {
     721     3990120 :         PyObject *value = PyObject_GetAttr(spec, &_Py_ID(_initializing));
     722     3990120 :         if (value != NULL) {
     723     2882100 :             int initializing = PyObject_IsTrue(value);
     724     2882100 :             Py_DECREF(value);
     725     2882100 :             if (initializing >= 0) {
     726     2882100 :                 return initializing;
     727             :             }
     728             :         }
     729             :     }
     730     1108090 :     PyErr_Clear();
     731     1108090 :     return 0;
     732             : }
     733             : 
     734             : /* Check if the submodule name is in the "_uninitialized_submodules" attribute
     735             :    of the module spec.
     736             :  */
     737             : int
     738     1954390 : _PyModuleSpec_IsUninitializedSubmodule(PyObject *spec, PyObject *name)
     739             : {
     740     1954390 :     if (spec == NULL) {
     741          30 :          return 0;
     742             :     }
     743             : 
     744     1954360 :     PyObject *value = PyObject_GetAttr(spec, &_Py_ID(_uninitialized_submodules));
     745     1954360 :     if (value == NULL) {
     746       38033 :         return 0;
     747             :     }
     748             : 
     749     1916330 :     int is_uninitialized = PySequence_Contains(value, name);
     750     1916330 :     Py_DECREF(value);
     751     1916330 :     if (is_uninitialized == -1) {
     752           0 :         return 0;
     753             :     }
     754     1916330 :     return is_uninitialized;
     755             : }
     756             : 
     757             : static PyObject*
     758    22021000 : module_getattro(PyModuleObject *m, PyObject *name)
     759             : {
     760             :     PyObject *attr, *mod_name, *getattr;
     761    22021000 :     attr = PyObject_GenericGetAttr((PyObject *)m, name);
     762    22021000 :     if (attr || !PyErr_ExceptionMatches(PyExc_AttributeError)) {
     763    20041600 :         return attr;
     764             :     }
     765     1979430 :     PyErr_Clear();
     766     1979430 :     assert(m->md_dict != NULL);
     767     1979430 :     getattr = PyDict_GetItemWithError(m->md_dict, &_Py_ID(__getattr__));
     768     1979430 :     if (getattr) {
     769        5237 :         return PyObject_CallOneArg(getattr, name);
     770             :     }
     771     1974190 :     if (PyErr_Occurred()) {
     772           0 :         return NULL;
     773             :     }
     774     1974190 :     mod_name = PyDict_GetItemWithError(m->md_dict, &_Py_ID(__name__));
     775     1974190 :     if (mod_name && PyUnicode_Check(mod_name)) {
     776     1974160 :         Py_INCREF(mod_name);
     777     1974160 :         PyObject *spec = PyDict_GetItemWithError(m->md_dict, &_Py_ID(__spec__));
     778     1974160 :         if (spec == NULL && PyErr_Occurred()) {
     779           0 :             Py_DECREF(mod_name);
     780           0 :             return NULL;
     781             :         }
     782     1974160 :         Py_XINCREF(spec);
     783     1974160 :         if (_PyModuleSpec_IsInitializing(spec)) {
     784       19766 :             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     1954390 :         else if (_PyModuleSpec_IsUninitializedSubmodule(spec, name)) {
     791          14 :             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     1954380 :             PyErr_Format(PyExc_AttributeError,
     798             :                             "module '%U' has no attribute '%U'",
     799             :                             mod_name, name);
     800             :         }
     801     1974160 :         Py_XDECREF(spec);
     802     1974160 :         Py_DECREF(mod_name);
     803     1974160 :         return NULL;
     804             :     }
     805          37 :     else if (PyErr_Occurred()) {
     806           0 :         return NULL;
     807             :     }
     808          37 :     PyErr_Format(PyExc_AttributeError,
     809             :                 "module has no attribute '%U'", name);
     810          37 :     return NULL;
     811             : }
     812             : 
     813             : static int
     814     8736420 : module_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     8736420 :     if (m->md_def && m->md_def->m_traverse
     818     1045250 :         && (m->md_def->m_size <= 0 || m->md_state != NULL))
     819             :     {
     820     1038950 :         int res = m->md_def->m_traverse((PyObject*)m, visit, arg);
     821     1038950 :         if (res)
     822           0 :             return res;
     823             :     }
     824     8736420 :     Py_VISIT(m->md_dict);
     825     8736390 :     return 0;
     826             : }
     827             : 
     828             : static int
     829      116530 : module_clear(PyModuleObject *m)
     830             : {
     831             :     /* bpo-39824: Don't call m_clear() if m_size > 0 and md_state=NULL */
     832      116530 :     if (m->md_def && m->md_def->m_clear
     833       31755 :         && (m->md_def->m_size <= 0 || m->md_state != NULL))
     834             :     {
     835       31755 :         int res = m->md_def->m_clear((PyObject*)m);
     836       31755 :         if (PyErr_Occurred()) {
     837           0 :             PySys_FormatStderr("Exception ignored in m_clear of module%s%V\n",
     838           0 :                                m->md_name ? " " : "",
     839             :                                m->md_name, "");
     840           0 :             PyErr_WriteUnraisable(NULL);
     841             :         }
     842       31755 :         if (res)
     843           0 :             return res;
     844             :     }
     845      116530 :     Py_CLEAR(m->md_dict);
     846      116530 :     return 0;
     847             : }
     848             : 
     849             : static PyObject *
     850        6281 : module_dir(PyObject *self, PyObject *args)
     851             : {
     852        6281 :     PyObject *result = NULL;
     853        6281 :     PyObject *dict = PyObject_GetAttr(self, &_Py_ID(__dict__));
     854             : 
     855        6281 :     if (dict != NULL) {
     856        6281 :         if (PyDict_Check(dict)) {
     857        6279 :             PyObject *dirfunc = PyDict_GetItemWithError(dict, &_Py_ID(__dir__));
     858        6279 :             if (dirfunc) {
     859           9 :                 result = _PyObject_CallNoArgs(dirfunc);
     860             :             }
     861        6270 :             else if (!PyErr_Occurred()) {
     862        6270 :                 result = PyDict_Keys(dict);
     863             :             }
     864             :         }
     865             :         else {
     866           2 :             PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary");
     867             :         }
     868             :     }
     869             : 
     870        6281 :     Py_XDECREF(dict);
     871        6281 :     return result;
     872             : }
     873             : 
     874             : static PyMethodDef module_methods[] = {
     875             :     {"__dir__", module_dir, METH_NOARGS,
     876             :      PyDoc_STR("__dir__() -> list\nspecialized dir() implementation")},
     877             :     {0}
     878             : };
     879             : 
     880             : static PyObject *
     881          46 : module_get_annotations(PyModuleObject *m, void *Py_UNUSED(ignored))
     882             : {
     883          46 :     PyObject *dict = PyObject_GetAttr((PyObject *)m, &_Py_ID(__dict__));
     884             : 
     885          46 :     if ((dict == NULL) || !PyDict_Check(dict)) {
     886           0 :         PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary");
     887           0 :         Py_XDECREF(dict);
     888           0 :         return NULL;
     889             :     }
     890             : 
     891             :     PyObject *annotations;
     892             :     /* there's no _PyDict_GetItemId without WithError, so let's LBYL. */
     893          46 :     if (PyDict_Contains(dict, &_Py_ID(__annotations__))) {
     894          38 :         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          38 :         if (annotations) {
     901          38 :             Py_INCREF(annotations);
     902             :         }
     903             :     } else {
     904           8 :         annotations = PyDict_New();
     905           8 :         if (annotations) {
     906           8 :             int result = PyDict_SetItem(
     907             :                     dict, &_Py_ID(__annotations__), annotations);
     908           8 :             if (result) {
     909           0 :                 Py_CLEAR(annotations);
     910             :             }
     911             :         }
     912             :     }
     913          46 :     Py_DECREF(dict);
     914          46 :     return annotations;
     915             : }
     916             : 
     917             : static int
     918          12 : module_set_annotations(PyModuleObject *m, PyObject *value, void *Py_UNUSED(ignored))
     919             : {
     920          12 :     int ret = -1;
     921          12 :     PyObject *dict = PyObject_GetAttr((PyObject *)m, &_Py_ID(__dict__));
     922             : 
     923          12 :     if ((dict == NULL) || !PyDict_Check(dict)) {
     924           0 :         PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary");
     925           0 :         goto exit;
     926             :     }
     927             : 
     928          12 :     if (value != NULL) {
     929             :         /* set */
     930           5 :         ret = PyDict_SetItem(dict, &_Py_ID(__annotations__), value);
     931           5 :         goto exit;
     932             :     }
     933             : 
     934             :     /* delete */
     935           7 :     if (!PyDict_Contains(dict, &_Py_ID(__annotations__))) {
     936           1 :         PyErr_Format(PyExc_AttributeError, "__annotations__");
     937           1 :         goto exit;
     938             :     }
     939             : 
     940           6 :     ret = PyDict_DelItem(dict, &_Py_ID(__annotations__));
     941             : 
     942          12 : exit:
     943          12 :     Py_XDECREF(dict);
     944          12 :     return ret;
     945             : }
     946             : 
     947             : 
     948             : static PyGetSetDef module_getsets[] = {
     949             :     {"__annotations__", (getter)module_get_annotations, (setter)module_set_annotations},
     950             :     {NULL}
     951             : };
     952             : 
     953             : PyTypeObject 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             : };

Generated by: LCOV version 1.14