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