blob: ee4ed97588e29e6c41036b9604896582aa3267c2 [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 ||
Benjamin Peterson027ce162014-04-24 19:39:18 -0400480 (name = _PyDict_GetItemId(d, &PyId___name__)) == NULL ||
Victor Stinnerbd475112011-02-23 00:21:43 +0000481 !PyUnicode_Check(name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 {
483 PyErr_SetString(PyExc_SystemError, "nameless module");
484 return NULL;
485 }
Victor Stinnerbd475112011-02-23 00:21:43 +0000486 Py_INCREF(name);
487 return name;
488}
489
490const char *
491PyModule_GetName(PyObject *m)
492{
493 PyObject *name = PyModule_GetNameObject(m);
494 if (name == NULL)
495 return NULL;
496 Py_DECREF(name); /* module dict has still a reference */
Serhiy Storchaka06515832016-11-20 09:13:07 +0200497 return PyUnicode_AsUTF8(name);
Guido van Rossum0558a201990-10-26 15:00:11 +0000498}
499
Victor Stinner6c00c142010-08-17 23:37:11 +0000500PyObject*
501PyModule_GetFilenameObject(PyObject *m)
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000502{
Benjamin Peterson027ce162014-04-24 19:39:18 -0400503 _Py_IDENTIFIER(__file__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 PyObject *d;
505 PyObject *fileobj;
506 if (!PyModule_Check(m)) {
507 PyErr_BadArgument();
508 return NULL;
509 }
510 d = ((PyModuleObject *)m)->md_dict;
511 if (d == NULL ||
Benjamin Peterson027ce162014-04-24 19:39:18 -0400512 (fileobj = _PyDict_GetItemId(d, &PyId___file__)) == NULL ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 !PyUnicode_Check(fileobj))
514 {
515 PyErr_SetString(PyExc_SystemError, "module filename missing");
516 return NULL;
517 }
Victor Stinner6c00c142010-08-17 23:37:11 +0000518 Py_INCREF(fileobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 return fileobj;
Victor Stinner8124feb2010-05-07 00:50:12 +0000520}
521
522const char *
523PyModule_GetFilename(PyObject *m)
524{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 PyObject *fileobj;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200526 const char *utf8;
Victor Stinner6c00c142010-08-17 23:37:11 +0000527 fileobj = PyModule_GetFilenameObject(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 if (fileobj == NULL)
529 return NULL;
Serhiy Storchaka06515832016-11-20 09:13:07 +0200530 utf8 = PyUnicode_AsUTF8(fileobj);
Victor Stinnerbd475112011-02-23 00:21:43 +0000531 Py_DECREF(fileobj); /* module dict has still a reference */
Victor Stinner6c00c142010-08-17 23:37:11 +0000532 return utf8;
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000533}
534
Martin v. Löwis1a214512008-06-11 05:26:20 +0000535PyModuleDef*
536PyModule_GetDef(PyObject* m)
537{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 if (!PyModule_Check(m)) {
539 PyErr_BadArgument();
540 return NULL;
541 }
542 return ((PyModuleObject *)m)->md_def;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000543}
544
545void*
546PyModule_GetState(PyObject* m)
547{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 if (!PyModule_Check(m)) {
549 PyErr_BadArgument();
550 return NULL;
551 }
552 return ((PyModuleObject *)m)->md_state;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000553}
554
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000555void
Fred Drakeee238b92000-07-09 06:03:25 +0000556_PyModule_Clear(PyObject *m)
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000557{
Serhiy Storchaka87a5c512014-02-10 18:21:34 +0200558 PyObject *d = ((PyModuleObject *)m)->md_dict;
559 if (d != NULL)
560 _PyModule_ClearDict(d);
561}
562
563void
564_PyModule_ClearDict(PyObject *d)
565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 /* To make the execution order of destructors for global
567 objects a bit more predictable, we first zap all objects
568 whose name starts with a single underscore, before we clear
569 the entire dictionary. We zap them by replacing them with
570 None, rather than deleting them from the dictionary, to
571 avoid rehashing the dictionary (to some extent). */
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 Py_ssize_t pos;
574 PyObject *key, *value;
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000575
Victor Stinnerda7933e2020-04-13 03:04:28 +0200576 int verbose = _Py_GetConfig()->verbose;
Victor Stinnerc96be812019-05-14 17:34:56 +0200577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 /* First, clear only names starting with a single underscore */
579 pos = 0;
580 while (PyDict_Next(d, &pos, &key, &value)) {
581 if (value != Py_None && PyUnicode_Check(key)) {
Brett Cannon62228db2012-04-29 14:38:11 -0400582 if (PyUnicode_READ_CHAR(key, 0) == '_' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200583 PyUnicode_READ_CHAR(key, 1) != '_') {
Victor Stinnerc96be812019-05-14 17:34:56 +0200584 if (verbose > 1) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200585 const char *s = PyUnicode_AsUTF8(key);
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000586 if (s != NULL)
587 PySys_WriteStderr("# clear[1] %s\n", s);
588 else
589 PyErr_Clear();
590 }
Serhiy Storchakac1a68322018-04-29 22:16:30 +0300591 if (PyDict_SetItem(d, key, Py_None) != 0) {
592 PyErr_WriteUnraisable(NULL);
593 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 }
595 }
596 }
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598 /* Next, clear all names except for __builtins__ */
599 pos = 0;
600 while (PyDict_Next(d, &pos, &key, &value)) {
601 if (value != Py_None && PyUnicode_Check(key)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200602 if (PyUnicode_READ_CHAR(key, 0) != '_' ||
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200603 !_PyUnicode_EqualToASCIIString(key, "__builtins__"))
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000604 {
Victor Stinnerc96be812019-05-14 17:34:56 +0200605 if (verbose > 1) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200606 const char *s = PyUnicode_AsUTF8(key);
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000607 if (s != NULL)
608 PySys_WriteStderr("# clear[2] %s\n", s);
609 else
610 PyErr_Clear();
611 }
Serhiy Storchakac1a68322018-04-29 22:16:30 +0300612 if (PyDict_SetItem(d, key, Py_None) != 0) {
613 PyErr_WriteUnraisable(NULL);
614 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 }
616 }
617 }
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 /* Note: we leave __builtins__ in place, so that destructors
620 of non-global objects defined in this module can still use
621 builtins, in particularly 'None'. */
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000622
623}
624
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200625/*[clinic input]
626class module "PyModuleObject *" "&PyModule_Type"
627[clinic start generated code]*/
628/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3e35d4f708ecb6af]*/
629
630#include "clinic/moduleobject.c.h"
631
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000632/* Methods */
633
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200634/*[clinic input]
635module.__init__
636 name: unicode
637 doc: object = None
638
639Create a module object.
640
641The name must be a string; the optional doc argument can have any type.
642[clinic start generated code]*/
643
Tim Peters6d6c1a32001-08-02 04:15:00 +0000644static int
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200645module___init___impl(PyModuleObject *self, PyObject *name, PyObject *doc)
646/*[clinic end generated code: output=e7e721c26ce7aad7 input=57f9e177401e5e1e]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +0000647{
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200648 PyObject *dict = self->md_dict;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 if (dict == NULL) {
650 dict = PyDict_New();
651 if (dict == NULL)
652 return -1;
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200653 self->md_dict = dict;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 }
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200655 if (module_init_dict(self, dict, name, doc) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 return -1;
657 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000658}
659
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000660static void
Fred Drakeee238b92000-07-09 06:03:25 +0000661module_dealloc(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000662{
Victor Stinnerda7933e2020-04-13 03:04:28 +0200663 int verbose = _Py_GetConfig()->verbose;
Victor Stinnerc96be812019-05-14 17:34:56 +0200664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 PyObject_GC_UnTrack(m);
Victor Stinnerc96be812019-05-14 17:34:56 +0200666 if (verbose && m->md_name) {
Serhiy Storchakad7c38732019-10-08 13:46:17 +0300667 PySys_FormatStderr("# destroy %U\n", m->md_name);
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200668 }
669 if (m->md_weaklist != NULL)
670 PyObject_ClearWeakRefs((PyObject *) m);
Victor Stinner5b1ef202020-03-17 18:09:46 +0100671 /* bpo-39824: Don't call m_free() if m_size > 0 and md_state=NULL */
672 if (m->md_def && m->md_def->m_free
673 && (m->md_def->m_size <= 0 || m->md_state != NULL))
674 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000675 m->md_def->m_free(m);
Victor Stinner5b1ef202020-03-17 18:09:46 +0100676 }
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200677 Py_XDECREF(m->md_dict);
678 Py_XDECREF(m->md_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000679 if (m->md_state != NULL)
680 PyMem_FREE(m->md_state);
681 Py_TYPE(m)->tp_free((PyObject *)m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000682}
683
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000684static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000685module_repr(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000686{
Victor Stinner81a7be32020-04-14 15:14:01 +0200687 PyInterpreterState *interp = _PyInterpreterState_GET();
Barry Warsaw2907fe62001-08-16 20:39:24 +0000688
Eric Snowb523f842013-11-22 09:05:39 -0700689 return PyObject_CallMethod(interp->importlib, "_module_repr", "O", m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000690}
691
Serhiy Storchaka3e429dc2018-10-30 13:19:51 +0200692/* Check if the "_initializing" attribute of the module spec is set to true.
693 Clear the exception and return 0 if spec is NULL.
694 */
695int
696_PyModuleSpec_IsInitializing(PyObject *spec)
697{
698 if (spec != NULL) {
699 _Py_IDENTIFIER(_initializing);
700 PyObject *value = _PyObject_GetAttrId(spec, &PyId__initializing);
701 if (value != NULL) {
702 int initializing = PyObject_IsTrue(value);
703 Py_DECREF(value);
704 if (initializing >= 0) {
705 return initializing;
706 }
707 }
708 }
709 PyErr_Clear();
710 return 0;
711}
712
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700713static PyObject*
Benjamin Peterson1184e262014-04-24 19:29:23 -0400714module_getattro(PyModuleObject *m, PyObject *name)
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700715{
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100716 PyObject *attr, *mod_name, *getattr;
Benjamin Peterson1184e262014-04-24 19:29:23 -0400717 attr = PyObject_GenericGetAttr((PyObject *)m, name);
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100718 if (attr || !PyErr_ExceptionMatches(PyExc_AttributeError)) {
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700719 return attr;
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100720 }
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700721 PyErr_Clear();
Benjamin Peterson1184e262014-04-24 19:29:23 -0400722 if (m->md_dict) {
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100723 _Py_IDENTIFIER(__getattr__);
724 getattr = _PyDict_GetItemId(m->md_dict, &PyId___getattr__);
725 if (getattr) {
Petr Viktorinffd97532020-02-11 17:46:57 +0100726 return PyObject_CallOneArg(getattr, name);
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100727 }
Benjamin Peterson027ce162014-04-24 19:39:18 -0400728 mod_name = _PyDict_GetItemId(m->md_dict, &PyId___name__);
Oren Milman6db70332017-09-19 14:23:01 +0300729 if (mod_name && PyUnicode_Check(mod_name)) {
Serhiy Storchaka3e429dc2018-10-30 13:19:51 +0200730 Py_INCREF(mod_name);
731 PyObject *spec = _PyDict_GetItemId(m->md_dict, &PyId___spec__);
732 Py_XINCREF(spec);
733 if (_PyModuleSpec_IsInitializing(spec)) {
734 PyErr_Format(PyExc_AttributeError,
735 "partially initialized "
736 "module '%U' has no attribute '%U' "
737 "(most likely due to a circular import)",
738 mod_name, name);
739 }
740 else {
741 PyErr_Format(PyExc_AttributeError,
742 "module '%U' has no attribute '%U'",
743 mod_name, name);
744 }
745 Py_XDECREF(spec);
746 Py_DECREF(mod_name);
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700747 return NULL;
748 }
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700749 }
750 PyErr_Format(PyExc_AttributeError,
751 "module has no attribute '%U'", name);
752 return NULL;
753}
754
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000755static int
756module_traverse(PyModuleObject *m, visitproc visit, void *arg)
757{
Victor Stinner5b1ef202020-03-17 18:09:46 +0100758 /* bpo-39824: Don't call m_traverse() if m_size > 0 and md_state=NULL */
759 if (m->md_def && m->md_def->m_traverse
760 && (m->md_def->m_size <= 0 || m->md_state != NULL))
761 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 int res = m->md_def->m_traverse((PyObject*)m, visit, arg);
763 if (res)
764 return res;
765 }
766 Py_VISIT(m->md_dict);
767 return 0;
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000768}
769
Martin v. Löwis1a214512008-06-11 05:26:20 +0000770static int
771module_clear(PyModuleObject *m)
772{
Victor Stinner5b1ef202020-03-17 18:09:46 +0100773 /* bpo-39824: Don't call m_clear() if m_size > 0 and md_state=NULL */
774 if (m->md_def && m->md_def->m_clear
775 && (m->md_def->m_size <= 0 || m->md_state != NULL))
776 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 int res = m->md_def->m_clear((PyObject*)m);
Serhiy Storchakad7c38732019-10-08 13:46:17 +0300778 if (PyErr_Occurred()) {
779 PySys_FormatStderr("Exception ignored in m_clear of module%s%V\n",
780 m->md_name ? " " : "",
781 m->md_name, "");
782 PyErr_WriteUnraisable(NULL);
783 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 if (res)
785 return res;
786 }
787 Py_CLEAR(m->md_dict);
788 return 0;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000789}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500791static PyObject *
792module_dir(PyObject *self, PyObject *args)
793{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200794 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200795 _Py_IDENTIFIER(__dir__);
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500796 PyObject *result = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200797 PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500798
799 if (dict != NULL) {
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100800 if (PyDict_Check(dict)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200801 PyObject *dirfunc = _PyDict_GetItemIdWithError(dict, &PyId___dir__);
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100802 if (dirfunc) {
803 result = _PyObject_CallNoArg(dirfunc);
804 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200805 else if (!PyErr_Occurred()) {
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100806 result = PyDict_Keys(dict);
807 }
808 }
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500809 else {
810 const char *name = PyModule_GetName(self);
811 if (name)
812 PyErr_Format(PyExc_TypeError,
813 "%.200s.__dict__ is not a dictionary",
814 name);
815 }
816 }
817
818 Py_XDECREF(dict);
819 return result;
820}
821
822static PyMethodDef module_methods[] = {
823 {"__dir__", module_dir, METH_NOARGS,
Benjamin Petersonc7284122011-05-24 12:46:15 -0500824 PyDoc_STR("__dir__() -> list\nspecialized dir() implementation")},
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500825 {0}
826};
827
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000828PyTypeObject PyModule_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 PyVarObject_HEAD_INIT(&PyType_Type, 0)
830 "module", /* tp_name */
Peter Eisentraut0e0bc4e2018-09-10 18:46:08 +0200831 sizeof(PyModuleObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 0, /* tp_itemsize */
833 (destructor)module_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200834 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 0, /* tp_getattr */
836 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200837 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 (reprfunc)module_repr, /* tp_repr */
839 0, /* tp_as_number */
840 0, /* tp_as_sequence */
841 0, /* tp_as_mapping */
842 0, /* tp_hash */
843 0, /* tp_call */
844 0, /* tp_str */
Benjamin Peterson1184e262014-04-24 19:29:23 -0400845 (getattrofunc)module_getattro, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 PyObject_GenericSetAttr, /* tp_setattro */
847 0, /* tp_as_buffer */
848 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
849 Py_TPFLAGS_BASETYPE, /* tp_flags */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200850 module___init____doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 (traverseproc)module_traverse, /* tp_traverse */
852 (inquiry)module_clear, /* tp_clear */
853 0, /* tp_richcompare */
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200854 offsetof(PyModuleObject, md_weaklist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 0, /* tp_iter */
856 0, /* tp_iternext */
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500857 module_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 module_members, /* tp_members */
859 0, /* tp_getset */
860 0, /* tp_base */
861 0, /* tp_dict */
862 0, /* tp_descr_get */
863 0, /* tp_descr_set */
864 offsetof(PyModuleObject, md_dict), /* tp_dictoffset */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200865 module___init__, /* tp_init */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 PyType_GenericAlloc, /* tp_alloc */
867 PyType_GenericNew, /* tp_new */
868 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000869};