blob: 0a593261c41349d964b1e661cd7a66573292cd89 [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 Stinner621cebe2018-11-12 16:53:38 +01005#include "pycore_pystate.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +00006#include "structmember.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00007
Martin v. Löwis1a214512008-06-11 05:26:20 +00008static Py_ssize_t max_module_number;
9
Hai Shi46874c22020-01-30 17:20:25 -060010_Py_IDENTIFIER(__doc__);
11_Py_IDENTIFIER(__name__);
12_Py_IDENTIFIER(__spec__);
13
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000014typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000015 PyObject_HEAD
16 PyObject *md_dict;
17 struct PyModuleDef *md_def;
18 void *md_state;
Antoine Pitroudcedaf62013-07-31 23:14:08 +020019 PyObject *md_weaklist;
20 PyObject *md_name; /* for logging purposes after md_dict is cleared */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000021} PyModuleObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000022
Neil Schemenauerf23473f2001-10-21 22:28:58 +000023static PyMemberDef module_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000024 {"__dict__", T_OBJECT, offsetof(PyModuleObject, md_dict), READONLY},
25 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +000026};
27
Marcel Plchc2b0b122018-03-17 06:41:20 +010028
29/* Helper for sanity check for traverse not handling m_state == NULL
30 * Issue #32374 */
31#ifdef Py_DEBUG
32static int
33bad_traverse_test(PyObject *self, void *arg) {
34 assert(self != NULL);
35 return 0;
36}
37#endif
38
Nick Coghland5cacbb2015-05-23 22:24:10 +100039PyTypeObject PyModuleDef_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 PyVarObject_HEAD_INIT(&PyType_Type, 0)
41 "moduledef", /* tp_name */
Peter Eisentraut0e0bc4e2018-09-10 18:46:08 +020042 sizeof(struct PyModuleDef), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 0, /* tp_itemsize */
Martin v. Löwis1a214512008-06-11 05:26:20 +000044};
45
46
Nick Coghland5cacbb2015-05-23 22:24:10 +100047PyObject*
48PyModuleDef_Init(struct PyModuleDef* def)
49{
50 if (PyType_Ready(&PyModuleDef_Type) < 0)
51 return NULL;
52 if (def->m_base.m_index == 0) {
53 max_module_number++;
Victor Stinnerc86a1122020-02-07 01:24:29 +010054 Py_SET_REFCNT(def, 1);
Victor Stinnerd2ec81a2020-02-07 09:17:07 +010055 Py_SET_TYPE(def, &PyModuleDef_Type);
Nick Coghland5cacbb2015-05-23 22:24:10 +100056 def->m_base.m_index = max_module_number;
57 }
58 return (PyObject*)def;
59}
60
Brett Cannon4c14b5d2013-05-04 13:56:58 -040061static int
Antoine Pitroudcedaf62013-07-31 23:14:08 +020062module_init_dict(PyModuleObject *mod, PyObject *md_dict,
63 PyObject *name, PyObject *doc)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040064{
Benjamin Peterson027ce162014-04-24 19:39:18 -040065 _Py_IDENTIFIER(__package__);
66 _Py_IDENTIFIER(__loader__);
Serhiy Storchaka009b8112015-03-18 21:53:15 +020067
Brett Cannon4c14b5d2013-05-04 13:56:58 -040068 if (md_dict == NULL)
69 return -1;
70 if (doc == NULL)
71 doc = Py_None;
72
Benjamin Peterson027ce162014-04-24 19:39:18 -040073 if (_PyDict_SetItemId(md_dict, &PyId___name__, name) != 0)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040074 return -1;
Benjamin Peterson027ce162014-04-24 19:39:18 -040075 if (_PyDict_SetItemId(md_dict, &PyId___doc__, doc) != 0)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040076 return -1;
Benjamin Peterson027ce162014-04-24 19:39:18 -040077 if (_PyDict_SetItemId(md_dict, &PyId___package__, Py_None) != 0)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040078 return -1;
Benjamin Peterson027ce162014-04-24 19:39:18 -040079 if (_PyDict_SetItemId(md_dict, &PyId___loader__, Py_None) != 0)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040080 return -1;
Benjamin Peterson027ce162014-04-24 19:39:18 -040081 if (_PyDict_SetItemId(md_dict, &PyId___spec__, Py_None) != 0)
Eric Snowb523f842013-11-22 09:05:39 -070082 return -1;
Antoine Pitroudcedaf62013-07-31 23:14:08 +020083 if (PyUnicode_CheckExact(name)) {
84 Py_INCREF(name);
Serhiy Storchaka48842712016-04-06 09:45:48 +030085 Py_XSETREF(mod->md_name, name);
Antoine Pitroudcedaf62013-07-31 23:14:08 +020086 }
Brett Cannon4c14b5d2013-05-04 13:56:58 -040087
88 return 0;
89}
90
91
Guido van Rossumc0b618a1997-05-02 03:12:38 +000092PyObject *
Victor Stinner0639b562011-03-04 12:57:07 +000093PyModule_NewObject(PyObject *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000094{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 PyModuleObject *m;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 m = PyObject_GC_New(PyModuleObject, &PyModule_Type);
97 if (m == NULL)
98 return NULL;
99 m->md_def = NULL;
100 m->md_state = NULL;
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200101 m->md_weaklist = NULL;
102 m->md_name = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 m->md_dict = PyDict_New();
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200104 if (module_init_dict(m, m->md_dict, name, NULL) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 PyObject_GC_Track(m);
107 return (PyObject *)m;
Guido van Rossumc45611d1993-11-17 22:58:56 +0000108
109 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 Py_DECREF(m);
111 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112}
113
Martin v. Löwis1a214512008-06-11 05:26:20 +0000114PyObject *
Victor Stinner0639b562011-03-04 12:57:07 +0000115PyModule_New(const char *name)
116{
117 PyObject *nameobj, *module;
118 nameobj = PyUnicode_FromString(name);
119 if (nameobj == NULL)
120 return NULL;
121 module = PyModule_NewObject(nameobj);
122 Py_DECREF(nameobj);
123 return module;
124}
125
Nick Coghland5cacbb2015-05-23 22:24:10 +1000126/* Check API/ABI version
127 * Issues a warning on mismatch, which is usually not fatal.
128 * Returns 0 if an exception is raised.
129 */
130static int
131check_api_version(const char *name, int module_api_version)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000132{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000133 if (module_api_version != PYTHON_API_VERSION && module_api_version != PYTHON_ABI_VERSION) {
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000134 int err;
135 err = PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
136 "Python C API version mismatch for module %.100s: "
137 "This Python has API version %d, module %.100s has version %d.",
138 name,
139 PYTHON_API_VERSION, name, module_api_version);
140 if (err)
Nick Coghland5cacbb2015-05-23 22:24:10 +1000141 return 0;
142 }
143 return 1;
144}
145
Nick Coghlan8682f572016-08-21 17:41:56 +1000146static int
147_add_methods_to_object(PyObject *module, PyObject *name, PyMethodDef *functions)
148{
149 PyObject *func;
150 PyMethodDef *fdef;
151
152 for (fdef = functions; fdef->ml_name != NULL; fdef++) {
153 if ((fdef->ml_flags & METH_CLASS) ||
154 (fdef->ml_flags & METH_STATIC)) {
155 PyErr_SetString(PyExc_ValueError,
156 "module functions cannot set"
157 " METH_CLASS or METH_STATIC");
158 return -1;
159 }
160 func = PyCFunction_NewEx(fdef, (PyObject*)module, name);
161 if (func == NULL) {
162 return -1;
163 }
164 if (PyObject_SetAttrString(module, fdef->ml_name, func) != 0) {
165 Py_DECREF(func);
166 return -1;
167 }
168 Py_DECREF(func);
169 }
170
171 return 0;
172}
173
Nick Coghland5cacbb2015-05-23 22:24:10 +1000174PyObject *
175PyModule_Create2(struct PyModuleDef* module, int module_api_version)
176{
Victor Stinnera94c6b62020-01-27 22:37:05 +0100177 if (!_PyImport_IsInitialized(_PyInterpreterState_Get())) {
178 PyErr_SetString(PyExc_SystemError,
179 "Python import machinery not initialized");
180 return NULL;
181 }
Eric Snowd393c1b2017-09-14 12:18:12 -0600182 return _PyModule_CreateInitialized(module, module_api_version);
183}
184
185PyObject *
186_PyModule_CreateInitialized(struct PyModuleDef* module, int module_api_version)
187{
Nick Coghland5cacbb2015-05-23 22:24:10 +1000188 const char* name;
189 PyModuleObject *m;
Eric Snowd393c1b2017-09-14 12:18:12 -0600190
Nick Coghland5cacbb2015-05-23 22:24:10 +1000191 if (!PyModuleDef_Init(module))
192 return NULL;
193 name = module->m_name;
194 if (!check_api_version(name, module_api_version)) {
195 return NULL;
196 }
197 if (module->m_slots) {
198 PyErr_Format(
199 PyExc_SystemError,
200 "module %s: PyModule_Create is incompatible with m_slots", name);
201 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 }
203 /* Make sure name is fully qualified.
Martin v. Löwis1a214512008-06-11 05:26:20 +0000204
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 This is a bit of a hack: when the shared library is loaded,
206 the module name is "package.module", but the module calls
207 PyModule_Create*() with just "module" for the name. The shared
208 library loader squirrels away the true name of the module in
209 _Py_PackageContext, and PyModule_Create*() will substitute this
210 (if the name actually matches).
211 */
212 if (_Py_PackageContext != NULL) {
Serhiy Storchakab57d9ea2016-11-21 10:25:54 +0200213 const char *p = strrchr(_Py_PackageContext, '.');
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 if (p != NULL && strcmp(module->m_name, p+1) == 0) {
215 name = _Py_PackageContext;
216 _Py_PackageContext = NULL;
217 }
218 }
219 if ((m = (PyModuleObject*)PyModule_New(name)) == NULL)
220 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 if (module->m_size > 0) {
223 m->md_state = PyMem_MALLOC(module->m_size);
224 if (!m->md_state) {
225 PyErr_NoMemory();
226 Py_DECREF(m);
227 return NULL;
228 }
229 memset(m->md_state, 0, module->m_size);
230 }
231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 if (module->m_methods != NULL) {
Nick Coghland5cacbb2015-05-23 22:24:10 +1000233 if (PyModule_AddFunctions((PyObject *) m, module->m_methods) != 0) {
Meador Inge29e49d62012-07-19 13:45:43 -0500234 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 return NULL;
Meador Inge29e49d62012-07-19 13:45:43 -0500236 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 }
238 if (module->m_doc != NULL) {
Nick Coghland5cacbb2015-05-23 22:24:10 +1000239 if (PyModule_SetDocString((PyObject *) m, module->m_doc) != 0) {
Meador Inge29e49d62012-07-19 13:45:43 -0500240 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 return NULL;
242 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 }
244 m->md_def = module;
245 return (PyObject*)m;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000246}
247
Nick Coghland5cacbb2015-05-23 22:24:10 +1000248PyObject *
249PyModule_FromDefAndSpec2(struct PyModuleDef* def, PyObject *spec, int module_api_version)
250{
251 PyModuleDef_Slot* cur_slot;
252 PyObject *(*create)(PyObject *, PyModuleDef*) = NULL;
253 PyObject *nameobj;
254 PyObject *m = NULL;
255 int has_execution_slots = 0;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200256 const char *name;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000257 int ret;
258
259 PyModuleDef_Init(def);
260
261 nameobj = PyObject_GetAttrString(spec, "name");
262 if (nameobj == NULL) {
263 return NULL;
264 }
265 name = PyUnicode_AsUTF8(nameobj);
266 if (name == NULL) {
267 goto error;
268 }
269
270 if (!check_api_version(name, module_api_version)) {
271 goto error;
272 }
273
274 if (def->m_size < 0) {
275 PyErr_Format(
276 PyExc_SystemError,
277 "module %s: m_size may not be negative for multi-phase initialization",
278 name);
279 goto error;
280 }
281
282 for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) {
283 if (cur_slot->slot == Py_mod_create) {
284 if (create) {
285 PyErr_Format(
286 PyExc_SystemError,
287 "module %s has multiple create slots",
288 name);
289 goto error;
290 }
291 create = cur_slot->value;
292 } else if (cur_slot->slot < 0 || cur_slot->slot > _Py_mod_LAST_SLOT) {
293 PyErr_Format(
294 PyExc_SystemError,
295 "module %s uses unknown slot ID %i",
296 name, cur_slot->slot);
297 goto error;
298 } else {
299 has_execution_slots = 1;
300 }
301 }
302
303 if (create) {
304 m = create(spec, def);
305 if (m == NULL) {
306 if (!PyErr_Occurred()) {
307 PyErr_Format(
308 PyExc_SystemError,
309 "creation of module %s failed without setting an exception",
310 name);
311 }
312 goto error;
313 } else {
314 if (PyErr_Occurred()) {
315 PyErr_Format(PyExc_SystemError,
316 "creation of module %s raised unreported exception",
317 name);
318 goto error;
319 }
320 }
321 } else {
Nick Coghlan8682f572016-08-21 17:41:56 +1000322 m = PyModule_NewObject(nameobj);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000323 if (m == NULL) {
324 goto error;
325 }
326 }
327
328 if (PyModule_Check(m)) {
329 ((PyModuleObject*)m)->md_state = NULL;
330 ((PyModuleObject*)m)->md_def = def;
331 } else {
332 if (def->m_size > 0 || def->m_traverse || def->m_clear || def->m_free) {
333 PyErr_Format(
334 PyExc_SystemError,
335 "module %s is not a module object, but requests module state",
336 name);
337 goto error;
338 }
339 if (has_execution_slots) {
340 PyErr_Format(
341 PyExc_SystemError,
342 "module %s specifies execution slots, but did not create "
343 "a ModuleType instance",
344 name);
345 goto error;
346 }
347 }
348
349 if (def->m_methods != NULL) {
Nick Coghlan8682f572016-08-21 17:41:56 +1000350 ret = _add_methods_to_object(m, nameobj, def->m_methods);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000351 if (ret != 0) {
352 goto error;
353 }
354 }
355
356 if (def->m_doc != NULL) {
357 ret = PyModule_SetDocString(m, def->m_doc);
358 if (ret != 0) {
359 goto error;
360 }
361 }
362
Marcel Plchc2b0b122018-03-17 06:41:20 +0100363 /* Sanity check for traverse not handling m_state == NULL
364 * This doesn't catch all possible cases, but in many cases it should
365 * make many cases of invalid code crash or raise Valgrind issues
366 * sooner than they would otherwise.
367 * Issue #32374 */
368#ifdef Py_DEBUG
369 if (def->m_traverse != NULL) {
370 def->m_traverse(m, bad_traverse_test, NULL);
371 }
372#endif
Nick Coghlana48db2b2015-05-24 01:03:46 +1000373 Py_DECREF(nameobj);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000374 return m;
375
376error:
377 Py_DECREF(nameobj);
378 Py_XDECREF(m);
379 return NULL;
380}
381
382int
383PyModule_ExecDef(PyObject *module, PyModuleDef *def)
384{
385 PyModuleDef_Slot *cur_slot;
386 const char *name;
387 int ret;
388
389 name = PyModule_GetName(module);
390 if (name == NULL) {
391 return -1;
392 }
393
Nick Coghlan8682f572016-08-21 17:41:56 +1000394 if (def->m_size >= 0) {
Nick Coghland5cacbb2015-05-23 22:24:10 +1000395 PyModuleObject *md = (PyModuleObject*)module;
396 if (md->md_state == NULL) {
397 /* Always set a state pointer; this serves as a marker to skip
398 * multiple initialization (importlib.reload() is no-op) */
399 md->md_state = PyMem_MALLOC(def->m_size);
400 if (!md->md_state) {
401 PyErr_NoMemory();
402 return -1;
403 }
404 memset(md->md_state, 0, def->m_size);
405 }
406 }
407
408 if (def->m_slots == NULL) {
409 return 0;
410 }
411
412 for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) {
413 switch (cur_slot->slot) {
414 case Py_mod_create:
Serhiy Storchaka333ad922016-09-26 23:14:44 +0300415 /* handled in PyModule_FromDefAndSpec2 */
Nick Coghland5cacbb2015-05-23 22:24:10 +1000416 break;
417 case Py_mod_exec:
418 ret = ((int (*)(PyObject *))cur_slot->value)(module);
419 if (ret != 0) {
420 if (!PyErr_Occurred()) {
421 PyErr_Format(
422 PyExc_SystemError,
423 "execution of module %s failed without setting an exception",
424 name);
425 }
426 return -1;
427 }
428 if (PyErr_Occurred()) {
429 PyErr_Format(
430 PyExc_SystemError,
431 "execution of module %s raised unreported exception",
432 name);
433 return -1;
434 }
435 break;
436 default:
437 PyErr_Format(
438 PyExc_SystemError,
439 "module %s initialized with unknown slot %i",
440 name, cur_slot->slot);
441 return -1;
442 }
443 }
444 return 0;
445}
446
447int
448PyModule_AddFunctions(PyObject *m, PyMethodDef *functions)
449{
Nick Coghlan8682f572016-08-21 17:41:56 +1000450 int res;
451 PyObject *name = PyModule_GetNameObject(m);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000452 if (name == NULL) {
453 return -1;
454 }
455
Nick Coghlan8682f572016-08-21 17:41:56 +1000456 res = _add_methods_to_object(m, name, functions);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000457 Py_DECREF(name);
Nick Coghlan8682f572016-08-21 17:41:56 +1000458 return res;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000459}
460
461int
462PyModule_SetDocString(PyObject *m, const char *doc)
463{
464 PyObject *v;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000465
466 v = PyUnicode_FromString(doc);
467 if (v == NULL || _PyObject_SetAttrId(m, &PyId___doc__, v) != 0) {
468 Py_XDECREF(v);
469 return -1;
470 }
471 Py_DECREF(v);
472 return 0;
473}
Martin v. Löwis1a214512008-06-11 05:26:20 +0000474
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000475PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000476PyModule_GetDict(PyObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000477{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 PyObject *d;
479 if (!PyModule_Check(m)) {
480 PyErr_BadInternalCall();
481 return NULL;
482 }
483 d = ((PyModuleObject *)m) -> md_dict;
Berker Peksag7fbce562016-08-19 12:00:13 +0300484 assert(d != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000486}
487
Victor Stinnerbd475112011-02-23 00:21:43 +0000488PyObject*
489PyModule_GetNameObject(PyObject *m)
Guido van Rossum0558a201990-10-26 15:00:11 +0000490{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 PyObject *d;
Victor Stinnerbd475112011-02-23 00:21:43 +0000492 PyObject *name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 if (!PyModule_Check(m)) {
494 PyErr_BadArgument();
495 return NULL;
496 }
497 d = ((PyModuleObject *)m)->md_dict;
498 if (d == NULL ||
Benjamin Peterson027ce162014-04-24 19:39:18 -0400499 (name = _PyDict_GetItemId(d, &PyId___name__)) == NULL ||
Victor Stinnerbd475112011-02-23 00:21:43 +0000500 !PyUnicode_Check(name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 {
502 PyErr_SetString(PyExc_SystemError, "nameless module");
503 return NULL;
504 }
Victor Stinnerbd475112011-02-23 00:21:43 +0000505 Py_INCREF(name);
506 return name;
507}
508
509const char *
510PyModule_GetName(PyObject *m)
511{
512 PyObject *name = PyModule_GetNameObject(m);
513 if (name == NULL)
514 return NULL;
515 Py_DECREF(name); /* module dict has still a reference */
Serhiy Storchaka06515832016-11-20 09:13:07 +0200516 return PyUnicode_AsUTF8(name);
Guido van Rossum0558a201990-10-26 15:00:11 +0000517}
518
Victor Stinner6c00c142010-08-17 23:37:11 +0000519PyObject*
520PyModule_GetFilenameObject(PyObject *m)
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000521{
Benjamin Peterson027ce162014-04-24 19:39:18 -0400522 _Py_IDENTIFIER(__file__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 PyObject *d;
524 PyObject *fileobj;
525 if (!PyModule_Check(m)) {
526 PyErr_BadArgument();
527 return NULL;
528 }
529 d = ((PyModuleObject *)m)->md_dict;
530 if (d == NULL ||
Benjamin Peterson027ce162014-04-24 19:39:18 -0400531 (fileobj = _PyDict_GetItemId(d, &PyId___file__)) == NULL ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 !PyUnicode_Check(fileobj))
533 {
534 PyErr_SetString(PyExc_SystemError, "module filename missing");
535 return NULL;
536 }
Victor Stinner6c00c142010-08-17 23:37:11 +0000537 Py_INCREF(fileobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 return fileobj;
Victor Stinner8124feb2010-05-07 00:50:12 +0000539}
540
541const char *
542PyModule_GetFilename(PyObject *m)
543{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 PyObject *fileobj;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200545 const char *utf8;
Victor Stinner6c00c142010-08-17 23:37:11 +0000546 fileobj = PyModule_GetFilenameObject(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 if (fileobj == NULL)
548 return NULL;
Serhiy Storchaka06515832016-11-20 09:13:07 +0200549 utf8 = PyUnicode_AsUTF8(fileobj);
Victor Stinnerbd475112011-02-23 00:21:43 +0000550 Py_DECREF(fileobj); /* module dict has still a reference */
Victor Stinner6c00c142010-08-17 23:37:11 +0000551 return utf8;
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000552}
553
Martin v. Löwis1a214512008-06-11 05:26:20 +0000554PyModuleDef*
555PyModule_GetDef(PyObject* m)
556{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 if (!PyModule_Check(m)) {
558 PyErr_BadArgument();
559 return NULL;
560 }
561 return ((PyModuleObject *)m)->md_def;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000562}
563
564void*
565PyModule_GetState(PyObject* m)
566{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 if (!PyModule_Check(m)) {
568 PyErr_BadArgument();
569 return NULL;
570 }
571 return ((PyModuleObject *)m)->md_state;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000572}
573
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000574void
Fred Drakeee238b92000-07-09 06:03:25 +0000575_PyModule_Clear(PyObject *m)
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000576{
Serhiy Storchaka87a5c512014-02-10 18:21:34 +0200577 PyObject *d = ((PyModuleObject *)m)->md_dict;
578 if (d != NULL)
579 _PyModule_ClearDict(d);
580}
581
582void
583_PyModule_ClearDict(PyObject *d)
584{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 /* To make the execution order of destructors for global
586 objects a bit more predictable, we first zap all objects
587 whose name starts with a single underscore, before we clear
588 the entire dictionary. We zap them by replacing them with
589 None, rather than deleting them from the dictionary, to
590 avoid rehashing the dictionary (to some extent). */
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 Py_ssize_t pos;
593 PyObject *key, *value;
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000594
Victor Stinner331a6a52019-05-27 16:39:22 +0200595 int verbose = _PyInterpreterState_GET_UNSAFE()->config.verbose;
Victor Stinnerc96be812019-05-14 17:34:56 +0200596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 /* First, clear only names starting with a single underscore */
598 pos = 0;
599 while (PyDict_Next(d, &pos, &key, &value)) {
600 if (value != Py_None && PyUnicode_Check(key)) {
Brett Cannon62228db2012-04-29 14:38:11 -0400601 if (PyUnicode_READ_CHAR(key, 0) == '_' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200602 PyUnicode_READ_CHAR(key, 1) != '_') {
Victor Stinnerc96be812019-05-14 17:34:56 +0200603 if (verbose > 1) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200604 const char *s = PyUnicode_AsUTF8(key);
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000605 if (s != NULL)
606 PySys_WriteStderr("# clear[1] %s\n", s);
607 else
608 PyErr_Clear();
609 }
Serhiy Storchakac1a68322018-04-29 22:16:30 +0300610 if (PyDict_SetItem(d, key, Py_None) != 0) {
611 PyErr_WriteUnraisable(NULL);
612 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 }
614 }
615 }
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000616
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 /* Next, clear all names except for __builtins__ */
618 pos = 0;
619 while (PyDict_Next(d, &pos, &key, &value)) {
620 if (value != Py_None && PyUnicode_Check(key)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200621 if (PyUnicode_READ_CHAR(key, 0) != '_' ||
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200622 !_PyUnicode_EqualToASCIIString(key, "__builtins__"))
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000623 {
Victor Stinnerc96be812019-05-14 17:34:56 +0200624 if (verbose > 1) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200625 const char *s = PyUnicode_AsUTF8(key);
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000626 if (s != NULL)
627 PySys_WriteStderr("# clear[2] %s\n", s);
628 else
629 PyErr_Clear();
630 }
Serhiy Storchakac1a68322018-04-29 22:16:30 +0300631 if (PyDict_SetItem(d, key, Py_None) != 0) {
632 PyErr_WriteUnraisable(NULL);
633 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 }
635 }
636 }
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 /* Note: we leave __builtins__ in place, so that destructors
639 of non-global objects defined in this module can still use
640 builtins, in particularly 'None'. */
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000641
642}
643
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200644/*[clinic input]
645class module "PyModuleObject *" "&PyModule_Type"
646[clinic start generated code]*/
647/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3e35d4f708ecb6af]*/
648
649#include "clinic/moduleobject.c.h"
650
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000651/* Methods */
652
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200653/*[clinic input]
654module.__init__
655 name: unicode
656 doc: object = None
657
658Create a module object.
659
660The name must be a string; the optional doc argument can have any type.
661[clinic start generated code]*/
662
Tim Peters6d6c1a32001-08-02 04:15:00 +0000663static int
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200664module___init___impl(PyModuleObject *self, PyObject *name, PyObject *doc)
665/*[clinic end generated code: output=e7e721c26ce7aad7 input=57f9e177401e5e1e]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +0000666{
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200667 PyObject *dict = self->md_dict;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 if (dict == NULL) {
669 dict = PyDict_New();
670 if (dict == NULL)
671 return -1;
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200672 self->md_dict = dict;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 }
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200674 if (module_init_dict(self, dict, name, doc) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 return -1;
676 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000677}
678
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000679static void
Fred Drakeee238b92000-07-09 06:03:25 +0000680module_dealloc(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000681{
Victor Stinner331a6a52019-05-27 16:39:22 +0200682 int verbose = _PyInterpreterState_GET_UNSAFE()->config.verbose;
Victor Stinnerc96be812019-05-14 17:34:56 +0200683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 PyObject_GC_UnTrack(m);
Victor Stinnerc96be812019-05-14 17:34:56 +0200685 if (verbose && m->md_name) {
Serhiy Storchakad7c38732019-10-08 13:46:17 +0300686 PySys_FormatStderr("# destroy %U\n", m->md_name);
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200687 }
688 if (m->md_weaklist != NULL)
689 PyObject_ClearWeakRefs((PyObject *) m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 if (m->md_def && m->md_def->m_free)
691 m->md_def->m_free(m);
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200692 Py_XDECREF(m->md_dict);
693 Py_XDECREF(m->md_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 if (m->md_state != NULL)
695 PyMem_FREE(m->md_state);
696 Py_TYPE(m)->tp_free((PyObject *)m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000697}
698
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000699static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000700module_repr(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000701{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200702 PyInterpreterState *interp = _PyInterpreterState_Get();
Barry Warsaw2907fe62001-08-16 20:39:24 +0000703
Eric Snowb523f842013-11-22 09:05:39 -0700704 return PyObject_CallMethod(interp->importlib, "_module_repr", "O", m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000705}
706
Serhiy Storchaka3e429dc2018-10-30 13:19:51 +0200707/* Check if the "_initializing" attribute of the module spec is set to true.
708 Clear the exception and return 0 if spec is NULL.
709 */
710int
711_PyModuleSpec_IsInitializing(PyObject *spec)
712{
713 if (spec != NULL) {
714 _Py_IDENTIFIER(_initializing);
715 PyObject *value = _PyObject_GetAttrId(spec, &PyId__initializing);
716 if (value != NULL) {
717 int initializing = PyObject_IsTrue(value);
718 Py_DECREF(value);
719 if (initializing >= 0) {
720 return initializing;
721 }
722 }
723 }
724 PyErr_Clear();
725 return 0;
726}
727
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700728static PyObject*
Benjamin Peterson1184e262014-04-24 19:29:23 -0400729module_getattro(PyModuleObject *m, PyObject *name)
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700730{
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100731 PyObject *attr, *mod_name, *getattr;
Benjamin Peterson1184e262014-04-24 19:29:23 -0400732 attr = PyObject_GenericGetAttr((PyObject *)m, name);
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100733 if (attr || !PyErr_ExceptionMatches(PyExc_AttributeError)) {
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700734 return attr;
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100735 }
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700736 PyErr_Clear();
Benjamin Peterson1184e262014-04-24 19:29:23 -0400737 if (m->md_dict) {
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100738 _Py_IDENTIFIER(__getattr__);
739 getattr = _PyDict_GetItemId(m->md_dict, &PyId___getattr__);
740 if (getattr) {
Jeroen Demeyer196a5302019-07-04 12:31:34 +0200741 return _PyObject_CallOneArg(getattr, name);
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100742 }
Benjamin Peterson027ce162014-04-24 19:39:18 -0400743 mod_name = _PyDict_GetItemId(m->md_dict, &PyId___name__);
Oren Milman6db70332017-09-19 14:23:01 +0300744 if (mod_name && PyUnicode_Check(mod_name)) {
Serhiy Storchaka3e429dc2018-10-30 13:19:51 +0200745 Py_INCREF(mod_name);
746 PyObject *spec = _PyDict_GetItemId(m->md_dict, &PyId___spec__);
747 Py_XINCREF(spec);
748 if (_PyModuleSpec_IsInitializing(spec)) {
749 PyErr_Format(PyExc_AttributeError,
750 "partially initialized "
751 "module '%U' has no attribute '%U' "
752 "(most likely due to a circular import)",
753 mod_name, name);
754 }
755 else {
756 PyErr_Format(PyExc_AttributeError,
757 "module '%U' has no attribute '%U'",
758 mod_name, name);
759 }
760 Py_XDECREF(spec);
761 Py_DECREF(mod_name);
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700762 return NULL;
763 }
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700764 }
765 PyErr_Format(PyExc_AttributeError,
766 "module has no attribute '%U'", name);
767 return NULL;
768}
769
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000770static int
771module_traverse(PyModuleObject *m, visitproc visit, void *arg)
772{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 if (m->md_def && m->md_def->m_traverse) {
774 int res = m->md_def->m_traverse((PyObject*)m, visit, arg);
775 if (res)
776 return res;
777 }
778 Py_VISIT(m->md_dict);
779 return 0;
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000780}
781
Martin v. Löwis1a214512008-06-11 05:26:20 +0000782static int
783module_clear(PyModuleObject *m)
784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 if (m->md_def && m->md_def->m_clear) {
786 int res = m->md_def->m_clear((PyObject*)m);
Serhiy Storchakad7c38732019-10-08 13:46:17 +0300787 if (PyErr_Occurred()) {
788 PySys_FormatStderr("Exception ignored in m_clear of module%s%V\n",
789 m->md_name ? " " : "",
790 m->md_name, "");
791 PyErr_WriteUnraisable(NULL);
792 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 if (res)
794 return res;
795 }
796 Py_CLEAR(m->md_dict);
797 return 0;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000798}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500800static PyObject *
801module_dir(PyObject *self, PyObject *args)
802{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200803 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200804 _Py_IDENTIFIER(__dir__);
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500805 PyObject *result = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200806 PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500807
808 if (dict != NULL) {
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100809 if (PyDict_Check(dict)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200810 PyObject *dirfunc = _PyDict_GetItemIdWithError(dict, &PyId___dir__);
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100811 if (dirfunc) {
812 result = _PyObject_CallNoArg(dirfunc);
813 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200814 else if (!PyErr_Occurred()) {
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100815 result = PyDict_Keys(dict);
816 }
817 }
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500818 else {
819 const char *name = PyModule_GetName(self);
820 if (name)
821 PyErr_Format(PyExc_TypeError,
822 "%.200s.__dict__ is not a dictionary",
823 name);
824 }
825 }
826
827 Py_XDECREF(dict);
828 return result;
829}
830
831static PyMethodDef module_methods[] = {
832 {"__dir__", module_dir, METH_NOARGS,
Benjamin Petersonc7284122011-05-24 12:46:15 -0500833 PyDoc_STR("__dir__() -> list\nspecialized dir() implementation")},
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500834 {0}
835};
836
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000837PyTypeObject PyModule_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 PyVarObject_HEAD_INIT(&PyType_Type, 0)
839 "module", /* tp_name */
Peter Eisentraut0e0bc4e2018-09-10 18:46:08 +0200840 sizeof(PyModuleObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 0, /* tp_itemsize */
842 (destructor)module_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200843 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 0, /* tp_getattr */
845 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200846 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 (reprfunc)module_repr, /* tp_repr */
848 0, /* tp_as_number */
849 0, /* tp_as_sequence */
850 0, /* tp_as_mapping */
851 0, /* tp_hash */
852 0, /* tp_call */
853 0, /* tp_str */
Benjamin Peterson1184e262014-04-24 19:29:23 -0400854 (getattrofunc)module_getattro, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 PyObject_GenericSetAttr, /* tp_setattro */
856 0, /* tp_as_buffer */
857 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
858 Py_TPFLAGS_BASETYPE, /* tp_flags */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200859 module___init____doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 (traverseproc)module_traverse, /* tp_traverse */
861 (inquiry)module_clear, /* tp_clear */
862 0, /* tp_richcompare */
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200863 offsetof(PyModuleObject, md_weaklist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 0, /* tp_iter */
865 0, /* tp_iternext */
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500866 module_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 module_members, /* tp_members */
868 0, /* tp_getset */
869 0, /* tp_base */
870 0, /* tp_dict */
871 0, /* tp_descr_get */
872 0, /* tp_descr_set */
873 offsetof(PyModuleObject, md_dict), /* tp_dictoffset */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200874 module___init__, /* tp_init */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 PyType_GenericAlloc, /* tp_alloc */
876 PyType_GenericNew, /* tp_new */
877 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000878};