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