blob: c3ceb788e8e690905232433e286d9272db0a893c [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Module object implementation */
3
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Victor Stinner4a21e572020-04-15 02:35:41 +02005#include "pycore_interp.h" // PyInterpreterState.importlib
6#include "pycore_pystate.h" // _PyInterpreterState_GET()
7#include "structmember.h" // PyMemberDef
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008
Martin v. Löwis1a214512008-06-11 05:26:20 +00009static Py_ssize_t max_module_number;
10
Hai Shi46874c22020-01-30 17:20:25 -060011_Py_IDENTIFIER(__doc__);
12_Py_IDENTIFIER(__name__);
13_Py_IDENTIFIER(__spec__);
14
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000015typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000016 PyObject_HEAD
17 PyObject *md_dict;
18 struct PyModuleDef *md_def;
19 void *md_state;
Antoine Pitroudcedaf62013-07-31 23:14:08 +020020 PyObject *md_weaklist;
21 PyObject *md_name; /* for logging purposes after md_dict is cleared */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000022} PyModuleObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000023
Neil Schemenauerf23473f2001-10-21 22:28:58 +000024static PyMemberDef module_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000025 {"__dict__", T_OBJECT, offsetof(PyModuleObject, md_dict), READONLY},
26 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +000027};
28
Marcel Plchc2b0b122018-03-17 06:41:20 +010029
Nick Coghland5cacbb2015-05-23 22:24:10 +100030PyTypeObject PyModuleDef_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000031 PyVarObject_HEAD_INIT(&PyType_Type, 0)
32 "moduledef", /* tp_name */
Peter Eisentraut0e0bc4e2018-09-10 18:46:08 +020033 sizeof(struct PyModuleDef), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000034 0, /* tp_itemsize */
Martin v. Löwis1a214512008-06-11 05:26:20 +000035};
36
37
Nick Coghland5cacbb2015-05-23 22:24:10 +100038PyObject*
39PyModuleDef_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 Stinnerc86a1122020-02-07 01:24:29 +010045 Py_SET_REFCNT(def, 1);
Victor Stinnerd2ec81a2020-02-07 09:17:07 +010046 Py_SET_TYPE(def, &PyModuleDef_Type);
Nick Coghland5cacbb2015-05-23 22:24:10 +100047 def->m_base.m_index = max_module_number;
48 }
49 return (PyObject*)def;
50}
51
Brett Cannon4c14b5d2013-05-04 13:56:58 -040052static int
Antoine Pitroudcedaf62013-07-31 23:14:08 +020053module_init_dict(PyModuleObject *mod, PyObject *md_dict,
54 PyObject *name, PyObject *doc)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040055{
Benjamin Peterson027ce162014-04-24 19:39:18 -040056 _Py_IDENTIFIER(__package__);
57 _Py_IDENTIFIER(__loader__);
Serhiy Storchaka009b8112015-03-18 21:53:15 +020058
Brett Cannon4c14b5d2013-05-04 13:56:58 -040059 if (md_dict == NULL)
60 return -1;
61 if (doc == NULL)
62 doc = Py_None;
63
Benjamin Peterson027ce162014-04-24 19:39:18 -040064 if (_PyDict_SetItemId(md_dict, &PyId___name__, name) != 0)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040065 return -1;
Benjamin Peterson027ce162014-04-24 19:39:18 -040066 if (_PyDict_SetItemId(md_dict, &PyId___doc__, doc) != 0)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040067 return -1;
Benjamin Peterson027ce162014-04-24 19:39:18 -040068 if (_PyDict_SetItemId(md_dict, &PyId___package__, Py_None) != 0)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040069 return -1;
Benjamin Peterson027ce162014-04-24 19:39:18 -040070 if (_PyDict_SetItemId(md_dict, &PyId___loader__, Py_None) != 0)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040071 return -1;
Benjamin Peterson027ce162014-04-24 19:39:18 -040072 if (_PyDict_SetItemId(md_dict, &PyId___spec__, Py_None) != 0)
Eric Snowb523f842013-11-22 09:05:39 -070073 return -1;
Antoine Pitroudcedaf62013-07-31 23:14:08 +020074 if (PyUnicode_CheckExact(name)) {
75 Py_INCREF(name);
Serhiy Storchaka48842712016-04-06 09:45:48 +030076 Py_XSETREF(mod->md_name, name);
Antoine Pitroudcedaf62013-07-31 23:14:08 +020077 }
Brett Cannon4c14b5d2013-05-04 13:56:58 -040078
79 return 0;
80}
81
82
Guido van Rossumc0b618a1997-05-02 03:12:38 +000083PyObject *
Victor Stinner0639b562011-03-04 12:57:07 +000084PyModule_NewObject(PyObject *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000085{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 PyModuleObject *m;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 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 Pitroudcedaf62013-07-31 23:14:08 +020092 m->md_weaklist = NULL;
93 m->md_name = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 m->md_dict = PyDict_New();
Antoine Pitroudcedaf62013-07-31 23:14:08 +020095 if (module_init_dict(m, m->md_dict, name, NULL) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 PyObject_GC_Track(m);
98 return (PyObject *)m;
Guido van Rossumc45611d1993-11-17 22:58:56 +000099
100 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 Py_DECREF(m);
102 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000103}
104
Martin v. Löwis1a214512008-06-11 05:26:20 +0000105PyObject *
Victor Stinner0639b562011-03-04 12:57:07 +0000106PyModule_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 Coghland5cacbb2015-05-23 22:24:10 +1000117/* Check API/ABI version
118 * Issues a warning on mismatch, which is usually not fatal.
119 * Returns 0 if an exception is raised.
120 */
121static int
122check_api_version(const char *name, int module_api_version)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000123{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000124 if (module_api_version != PYTHON_API_VERSION && module_api_version != PYTHON_ABI_VERSION) {
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000125 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 Coghland5cacbb2015-05-23 22:24:10 +1000132 return 0;
133 }
134 return 1;
135}
136
Nick Coghlan8682f572016-08-21 17:41:56 +1000137static 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 Coghland5cacbb2015-05-23 22:24:10 +1000165PyObject *
166PyModule_Create2(struct PyModuleDef* module, int module_api_version)
167{
Victor Stinner81a7be32020-04-14 15:14:01 +0200168 if (!_PyImport_IsInitialized(_PyInterpreterState_GET())) {
Victor Stinnera94c6b62020-01-27 22:37:05 +0100169 PyErr_SetString(PyExc_SystemError,
170 "Python import machinery not initialized");
171 return NULL;
172 }
Eric Snowd393c1b2017-09-14 12:18:12 -0600173 return _PyModule_CreateInitialized(module, module_api_version);
174}
175
176PyObject *
177_PyModule_CreateInitialized(struct PyModuleDef* module, int module_api_version)
178{
Nick Coghland5cacbb2015-05-23 22:24:10 +1000179 const char* name;
180 PyModuleObject *m;
Eric Snowd393c1b2017-09-14 12:18:12 -0600181
Nick Coghland5cacbb2015-05-23 22:24:10 +1000182 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 Pitrouf95a1b32010-05-09 15:52:27 +0000193 }
194 /* Make sure name is fully qualified.
Martin v. Löwis1a214512008-06-11 05:26:20 +0000195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 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 Storchakab57d9ea2016-11-21 10:25:54 +0200204 const char *p = strrchr(_Py_PackageContext, '.');
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 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öwis1a214512008-06-11 05:26:20 +0000212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 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 Pitrouf95a1b32010-05-09 15:52:27 +0000223 if (module->m_methods != NULL) {
Nick Coghland5cacbb2015-05-23 22:24:10 +1000224 if (PyModule_AddFunctions((PyObject *) m, module->m_methods) != 0) {
Meador Inge29e49d62012-07-19 13:45:43 -0500225 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 return NULL;
Meador Inge29e49d62012-07-19 13:45:43 -0500227 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 }
229 if (module->m_doc != NULL) {
Nick Coghland5cacbb2015-05-23 22:24:10 +1000230 if (PyModule_SetDocString((PyObject *) m, module->m_doc) != 0) {
Meador Inge29e49d62012-07-19 13:45:43 -0500231 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 return NULL;
233 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 }
235 m->md_def = module;
236 return (PyObject*)m;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000237}
238
Nick Coghland5cacbb2015-05-23 22:24:10 +1000239PyObject *
240PyModule_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 Storchaka85b0f5b2016-11-20 10:16:47 +0200247 const char *name;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000248 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 Coghlan8682f572016-08-21 17:41:56 +1000313 m = PyModule_NewObject(nameobj);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000314 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 Coghlan8682f572016-08-21 17:41:56 +1000341 ret = _add_methods_to_object(m, nameobj, def->m_methods);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000342 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 Coghlana48db2b2015-05-24 01:03:46 +1000354 Py_DECREF(nameobj);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000355 return m;
356
357error:
358 Py_DECREF(nameobj);
359 Py_XDECREF(m);
360 return NULL;
361}
362
363int
364PyModule_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 Coghlan8682f572016-08-21 17:41:56 +1000375 if (def->m_size >= 0) {
Nick Coghland5cacbb2015-05-23 22:24:10 +1000376 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 Storchaka333ad922016-09-26 23:14:44 +0300396 /* handled in PyModule_FromDefAndSpec2 */
Nick Coghland5cacbb2015-05-23 22:24:10 +1000397 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
428int
429PyModule_AddFunctions(PyObject *m, PyMethodDef *functions)
430{
Nick Coghlan8682f572016-08-21 17:41:56 +1000431 int res;
432 PyObject *name = PyModule_GetNameObject(m);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000433 if (name == NULL) {
434 return -1;
435 }
436
Nick Coghlan8682f572016-08-21 17:41:56 +1000437 res = _add_methods_to_object(m, name, functions);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000438 Py_DECREF(name);
Nick Coghlan8682f572016-08-21 17:41:56 +1000439 return res;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000440}
441
442int
443PyModule_SetDocString(PyObject *m, const char *doc)
444{
445 PyObject *v;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000446
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öwis1a214512008-06-11 05:26:20 +0000455
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000456PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000457PyModule_GetDict(PyObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000458{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 PyObject *d;
460 if (!PyModule_Check(m)) {
461 PyErr_BadInternalCall();
462 return NULL;
463 }
464 d = ((PyModuleObject *)m) -> md_dict;
Berker Peksag7fbce562016-08-19 12:00:13 +0300465 assert(d != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000467}
468
Victor Stinnerbd475112011-02-23 00:21:43 +0000469PyObject*
470PyModule_GetNameObject(PyObject *m)
Guido van Rossum0558a201990-10-26 15:00:11 +0000471{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 PyObject *d;
Victor Stinnerbd475112011-02-23 00:21:43 +0000473 PyObject *name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 if (!PyModule_Check(m)) {
475 PyErr_BadArgument();
476 return NULL;
477 }
478 d = ((PyModuleObject *)m)->md_dict;
479 if (d == NULL ||
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200480 (name = _PyDict_GetItemIdWithError(d, &PyId___name__)) == NULL ||
Victor Stinnerbd475112011-02-23 00:21:43 +0000481 !PyUnicode_Check(name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 {
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200483 if (!PyErr_Occurred()) {
484 PyErr_SetString(PyExc_SystemError, "nameless module");
485 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486 return NULL;
487 }
Victor Stinnerbd475112011-02-23 00:21:43 +0000488 Py_INCREF(name);
489 return name;
490}
491
492const char *
493PyModule_GetName(PyObject *m)
494{
495 PyObject *name = PyModule_GetNameObject(m);
496 if (name == NULL)
497 return NULL;
498 Py_DECREF(name); /* module dict has still a reference */
Serhiy Storchaka06515832016-11-20 09:13:07 +0200499 return PyUnicode_AsUTF8(name);
Guido van Rossum0558a201990-10-26 15:00:11 +0000500}
501
Victor Stinner6c00c142010-08-17 23:37:11 +0000502PyObject*
503PyModule_GetFilenameObject(PyObject *m)
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000504{
Benjamin Peterson027ce162014-04-24 19:39:18 -0400505 _Py_IDENTIFIER(__file__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 PyObject *d;
507 PyObject *fileobj;
508 if (!PyModule_Check(m)) {
509 PyErr_BadArgument();
510 return NULL;
511 }
512 d = ((PyModuleObject *)m)->md_dict;
513 if (d == NULL ||
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200514 (fileobj = _PyDict_GetItemIdWithError(d, &PyId___file__)) == NULL ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 !PyUnicode_Check(fileobj))
516 {
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200517 if (!PyErr_Occurred()) {
518 PyErr_SetString(PyExc_SystemError, "module filename missing");
519 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 return NULL;
521 }
Victor Stinner6c00c142010-08-17 23:37:11 +0000522 Py_INCREF(fileobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 return fileobj;
Victor Stinner8124feb2010-05-07 00:50:12 +0000524}
525
526const char *
527PyModule_GetFilename(PyObject *m)
528{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 PyObject *fileobj;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200530 const char *utf8;
Victor Stinner6c00c142010-08-17 23:37:11 +0000531 fileobj = PyModule_GetFilenameObject(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 if (fileobj == NULL)
533 return NULL;
Serhiy Storchaka06515832016-11-20 09:13:07 +0200534 utf8 = PyUnicode_AsUTF8(fileobj);
Victor Stinnerbd475112011-02-23 00:21:43 +0000535 Py_DECREF(fileobj); /* module dict has still a reference */
Victor Stinner6c00c142010-08-17 23:37:11 +0000536 return utf8;
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000537}
538
Martin v. Löwis1a214512008-06-11 05:26:20 +0000539PyModuleDef*
540PyModule_GetDef(PyObject* m)
541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 if (!PyModule_Check(m)) {
543 PyErr_BadArgument();
544 return NULL;
545 }
546 return ((PyModuleObject *)m)->md_def;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000547}
548
549void*
550PyModule_GetState(PyObject* m)
551{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 if (!PyModule_Check(m)) {
553 PyErr_BadArgument();
554 return NULL;
555 }
556 return ((PyModuleObject *)m)->md_state;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000557}
558
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000559void
Fred Drakeee238b92000-07-09 06:03:25 +0000560_PyModule_Clear(PyObject *m)
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000561{
Serhiy Storchaka87a5c512014-02-10 18:21:34 +0200562 PyObject *d = ((PyModuleObject *)m)->md_dict;
563 if (d != NULL)
564 _PyModule_ClearDict(d);
565}
566
567void
568_PyModule_ClearDict(PyObject *d)
569{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 /* To make the execution order of destructors for global
571 objects a bit more predictable, we first zap all objects
572 whose name starts with a single underscore, before we clear
573 the entire dictionary. We zap them by replacing them with
574 None, rather than deleting them from the dictionary, to
575 avoid rehashing the dictionary (to some extent). */
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 Py_ssize_t pos;
578 PyObject *key, *value;
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000579
Victor Stinnerda7933e2020-04-13 03:04:28 +0200580 int verbose = _Py_GetConfig()->verbose;
Victor Stinnerc96be812019-05-14 17:34:56 +0200581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 /* First, clear only names starting with a single underscore */
583 pos = 0;
584 while (PyDict_Next(d, &pos, &key, &value)) {
585 if (value != Py_None && PyUnicode_Check(key)) {
Brett Cannon62228db2012-04-29 14:38:11 -0400586 if (PyUnicode_READ_CHAR(key, 0) == '_' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200587 PyUnicode_READ_CHAR(key, 1) != '_') {
Victor Stinnerc96be812019-05-14 17:34:56 +0200588 if (verbose > 1) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200589 const char *s = PyUnicode_AsUTF8(key);
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000590 if (s != NULL)
591 PySys_WriteStderr("# clear[1] %s\n", s);
592 else
593 PyErr_Clear();
594 }
Serhiy Storchakac1a68322018-04-29 22:16:30 +0300595 if (PyDict_SetItem(d, key, Py_None) != 0) {
596 PyErr_WriteUnraisable(NULL);
597 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 }
599 }
600 }
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000601
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 /* Next, clear all names except for __builtins__ */
603 pos = 0;
604 while (PyDict_Next(d, &pos, &key, &value)) {
605 if (value != Py_None && PyUnicode_Check(key)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200606 if (PyUnicode_READ_CHAR(key, 0) != '_' ||
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200607 !_PyUnicode_EqualToASCIIString(key, "__builtins__"))
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000608 {
Victor Stinnerc96be812019-05-14 17:34:56 +0200609 if (verbose > 1) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200610 const char *s = PyUnicode_AsUTF8(key);
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000611 if (s != NULL)
612 PySys_WriteStderr("# clear[2] %s\n", s);
613 else
614 PyErr_Clear();
615 }
Serhiy Storchakac1a68322018-04-29 22:16:30 +0300616 if (PyDict_SetItem(d, key, Py_None) != 0) {
617 PyErr_WriteUnraisable(NULL);
618 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 }
620 }
621 }
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 /* Note: we leave __builtins__ in place, so that destructors
624 of non-global objects defined in this module can still use
625 builtins, in particularly 'None'. */
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000626
627}
628
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200629/*[clinic input]
630class module "PyModuleObject *" "&PyModule_Type"
631[clinic start generated code]*/
632/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3e35d4f708ecb6af]*/
633
634#include "clinic/moduleobject.c.h"
635
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000636/* Methods */
637
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200638/*[clinic input]
639module.__init__
640 name: unicode
641 doc: object = None
642
643Create a module object.
644
645The name must be a string; the optional doc argument can have any type.
646[clinic start generated code]*/
647
Tim Peters6d6c1a32001-08-02 04:15:00 +0000648static int
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200649module___init___impl(PyModuleObject *self, PyObject *name, PyObject *doc)
650/*[clinic end generated code: output=e7e721c26ce7aad7 input=57f9e177401e5e1e]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +0000651{
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200652 PyObject *dict = self->md_dict;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 if (dict == NULL) {
654 dict = PyDict_New();
655 if (dict == NULL)
656 return -1;
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200657 self->md_dict = dict;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 }
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200659 if (module_init_dict(self, dict, name, doc) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 return -1;
661 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000662}
663
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000664static void
Fred Drakeee238b92000-07-09 06:03:25 +0000665module_dealloc(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000666{
Victor Stinnerda7933e2020-04-13 03:04:28 +0200667 int verbose = _Py_GetConfig()->verbose;
Victor Stinnerc96be812019-05-14 17:34:56 +0200668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 PyObject_GC_UnTrack(m);
Victor Stinnerc96be812019-05-14 17:34:56 +0200670 if (verbose && m->md_name) {
Serhiy Storchakad7c38732019-10-08 13:46:17 +0300671 PySys_FormatStderr("# destroy %U\n", m->md_name);
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200672 }
673 if (m->md_weaklist != NULL)
674 PyObject_ClearWeakRefs((PyObject *) m);
Victor Stinner5b1ef202020-03-17 18:09:46 +0100675 /* bpo-39824: Don't call m_free() if m_size > 0 and md_state=NULL */
676 if (m->md_def && m->md_def->m_free
677 && (m->md_def->m_size <= 0 || m->md_state != NULL))
678 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 m->md_def->m_free(m);
Victor Stinner5b1ef202020-03-17 18:09:46 +0100680 }
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200681 Py_XDECREF(m->md_dict);
682 Py_XDECREF(m->md_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 if (m->md_state != NULL)
684 PyMem_FREE(m->md_state);
685 Py_TYPE(m)->tp_free((PyObject *)m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000686}
687
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000688static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000689module_repr(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000690{
Victor Stinner81a7be32020-04-14 15:14:01 +0200691 PyInterpreterState *interp = _PyInterpreterState_GET();
Barry Warsaw2907fe62001-08-16 20:39:24 +0000692
Eric Snowb523f842013-11-22 09:05:39 -0700693 return PyObject_CallMethod(interp->importlib, "_module_repr", "O", m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000694}
695
Serhiy Storchaka3e429dc2018-10-30 13:19:51 +0200696/* Check if the "_initializing" attribute of the module spec is set to true.
697 Clear the exception and return 0 if spec is NULL.
698 */
699int
700_PyModuleSpec_IsInitializing(PyObject *spec)
701{
702 if (spec != NULL) {
703 _Py_IDENTIFIER(_initializing);
704 PyObject *value = _PyObject_GetAttrId(spec, &PyId__initializing);
705 if (value != NULL) {
706 int initializing = PyObject_IsTrue(value);
707 Py_DECREF(value);
708 if (initializing >= 0) {
709 return initializing;
710 }
711 }
712 }
713 PyErr_Clear();
714 return 0;
715}
716
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700717static PyObject*
Benjamin Peterson1184e262014-04-24 19:29:23 -0400718module_getattro(PyModuleObject *m, PyObject *name)
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700719{
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100720 PyObject *attr, *mod_name, *getattr;
Benjamin Peterson1184e262014-04-24 19:29:23 -0400721 attr = PyObject_GenericGetAttr((PyObject *)m, name);
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100722 if (attr || !PyErr_ExceptionMatches(PyExc_AttributeError)) {
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700723 return attr;
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100724 }
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700725 PyErr_Clear();
Benjamin Peterson1184e262014-04-24 19:29:23 -0400726 if (m->md_dict) {
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100727 _Py_IDENTIFIER(__getattr__);
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200728 getattr = _PyDict_GetItemIdWithError(m->md_dict, &PyId___getattr__);
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100729 if (getattr) {
Petr Viktorinffd97532020-02-11 17:46:57 +0100730 return PyObject_CallOneArg(getattr, name);
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100731 }
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200732 if (PyErr_Occurred()) {
733 return NULL;
734 }
735 mod_name = _PyDict_GetItemIdWithError(m->md_dict, &PyId___name__);
Oren Milman6db70332017-09-19 14:23:01 +0300736 if (mod_name && PyUnicode_Check(mod_name)) {
Serhiy Storchaka3e429dc2018-10-30 13:19:51 +0200737 Py_INCREF(mod_name);
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200738 PyObject *spec = _PyDict_GetItemIdWithError(m->md_dict, &PyId___spec__);
739 if (spec == NULL && PyErr_Occurred()) {
740 Py_DECREF(mod_name);
741 return NULL;
742 }
Serhiy Storchaka3e429dc2018-10-30 13:19:51 +0200743 Py_XINCREF(spec);
744 if (_PyModuleSpec_IsInitializing(spec)) {
745 PyErr_Format(PyExc_AttributeError,
746 "partially initialized "
747 "module '%U' has no attribute '%U' "
748 "(most likely due to a circular import)",
749 mod_name, name);
750 }
751 else {
752 PyErr_Format(PyExc_AttributeError,
753 "module '%U' has no attribute '%U'",
754 mod_name, name);
755 }
756 Py_XDECREF(spec);
757 Py_DECREF(mod_name);
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700758 return NULL;
759 }
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200760 else if (PyErr_Occurred()) {
761 return NULL;
762 }
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700763 }
764 PyErr_Format(PyExc_AttributeError,
765 "module has no attribute '%U'", name);
766 return NULL;
767}
768
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000769static int
770module_traverse(PyModuleObject *m, visitproc visit, void *arg)
771{
Victor Stinner5b1ef202020-03-17 18:09:46 +0100772 /* bpo-39824: Don't call m_traverse() if m_size > 0 and md_state=NULL */
773 if (m->md_def && m->md_def->m_traverse
774 && (m->md_def->m_size <= 0 || m->md_state != NULL))
775 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 int res = m->md_def->m_traverse((PyObject*)m, visit, arg);
777 if (res)
778 return res;
779 }
780 Py_VISIT(m->md_dict);
781 return 0;
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000782}
783
Martin v. Löwis1a214512008-06-11 05:26:20 +0000784static int
785module_clear(PyModuleObject *m)
786{
Victor Stinner5b1ef202020-03-17 18:09:46 +0100787 /* bpo-39824: Don't call m_clear() if m_size > 0 and md_state=NULL */
788 if (m->md_def && m->md_def->m_clear
789 && (m->md_def->m_size <= 0 || m->md_state != NULL))
790 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 int res = m->md_def->m_clear((PyObject*)m);
Serhiy Storchakad7c38732019-10-08 13:46:17 +0300792 if (PyErr_Occurred()) {
793 PySys_FormatStderr("Exception ignored in m_clear of module%s%V\n",
794 m->md_name ? " " : "",
795 m->md_name, "");
796 PyErr_WriteUnraisable(NULL);
797 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 if (res)
799 return res;
800 }
801 Py_CLEAR(m->md_dict);
802 return 0;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000803}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500805static PyObject *
806module_dir(PyObject *self, PyObject *args)
807{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200808 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200809 _Py_IDENTIFIER(__dir__);
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500810 PyObject *result = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200811 PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500812
813 if (dict != NULL) {
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100814 if (PyDict_Check(dict)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200815 PyObject *dirfunc = _PyDict_GetItemIdWithError(dict, &PyId___dir__);
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100816 if (dirfunc) {
817 result = _PyObject_CallNoArg(dirfunc);
818 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200819 else if (!PyErr_Occurred()) {
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100820 result = PyDict_Keys(dict);
821 }
822 }
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500823 else {
824 const char *name = PyModule_GetName(self);
825 if (name)
826 PyErr_Format(PyExc_TypeError,
827 "%.200s.__dict__ is not a dictionary",
828 name);
829 }
830 }
831
832 Py_XDECREF(dict);
833 return result;
834}
835
836static PyMethodDef module_methods[] = {
837 {"__dir__", module_dir, METH_NOARGS,
Benjamin Petersonc7284122011-05-24 12:46:15 -0500838 PyDoc_STR("__dir__() -> list\nspecialized dir() implementation")},
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500839 {0}
840};
841
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000842PyTypeObject PyModule_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 PyVarObject_HEAD_INIT(&PyType_Type, 0)
844 "module", /* tp_name */
Peter Eisentraut0e0bc4e2018-09-10 18:46:08 +0200845 sizeof(PyModuleObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 0, /* tp_itemsize */
847 (destructor)module_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200848 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 0, /* tp_getattr */
850 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200851 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 (reprfunc)module_repr, /* tp_repr */
853 0, /* tp_as_number */
854 0, /* tp_as_sequence */
855 0, /* tp_as_mapping */
856 0, /* tp_hash */
857 0, /* tp_call */
858 0, /* tp_str */
Benjamin Peterson1184e262014-04-24 19:29:23 -0400859 (getattrofunc)module_getattro, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 PyObject_GenericSetAttr, /* tp_setattro */
861 0, /* tp_as_buffer */
862 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
863 Py_TPFLAGS_BASETYPE, /* tp_flags */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200864 module___init____doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 (traverseproc)module_traverse, /* tp_traverse */
866 (inquiry)module_clear, /* tp_clear */
867 0, /* tp_richcompare */
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200868 offsetof(PyModuleObject, md_weaklist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 0, /* tp_iter */
870 0, /* tp_iternext */
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500871 module_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 module_members, /* tp_members */
873 0, /* tp_getset */
874 0, /* tp_base */
875 0, /* tp_dict */
876 0, /* tp_descr_get */
877 0, /* tp_descr_set */
878 offsetof(PyModuleObject, md_dict), /* tp_dictoffset */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200879 module___init__, /* tp_init */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 PyType_GenericAlloc, /* tp_alloc */
881 PyType_GenericNew, /* tp_new */
882 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000883};