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