blob: f02ca75c9ee04f8b280a6272fd5bccafad5c9947 [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
Nick Coghland5cacbb2015-05-23 22:24:10 +100029PyTypeObject PyModuleDef_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000030 PyVarObject_HEAD_INIT(&PyType_Type, 0)
31 "moduledef", /* tp_name */
Peter Eisentraut0e0bc4e2018-09-10 18:46:08 +020032 sizeof(struct PyModuleDef), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000033 0, /* tp_itemsize */
Martin v. Löwis1a214512008-06-11 05:26:20 +000034};
35
36
Nick Coghland5cacbb2015-05-23 22:24:10 +100037PyObject*
38PyModuleDef_Init(struct PyModuleDef* def)
39{
40 if (PyType_Ready(&PyModuleDef_Type) < 0)
41 return NULL;
42 if (def->m_base.m_index == 0) {
43 max_module_number++;
Victor Stinnerc86a1122020-02-07 01:24:29 +010044 Py_SET_REFCNT(def, 1);
Victor Stinnerd2ec81a2020-02-07 09:17:07 +010045 Py_SET_TYPE(def, &PyModuleDef_Type);
Nick Coghland5cacbb2015-05-23 22:24:10 +100046 def->m_base.m_index = max_module_number;
47 }
48 return (PyObject*)def;
49}
50
Brett Cannon4c14b5d2013-05-04 13:56:58 -040051static int
Antoine Pitroudcedaf62013-07-31 23:14:08 +020052module_init_dict(PyModuleObject *mod, PyObject *md_dict,
53 PyObject *name, PyObject *doc)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040054{
Benjamin Peterson027ce162014-04-24 19:39:18 -040055 _Py_IDENTIFIER(__package__);
56 _Py_IDENTIFIER(__loader__);
Serhiy Storchaka009b8112015-03-18 21:53:15 +020057
Brett Cannon4c14b5d2013-05-04 13:56:58 -040058 if (md_dict == NULL)
59 return -1;
60 if (doc == NULL)
61 doc = Py_None;
62
Benjamin Peterson027ce162014-04-24 19:39:18 -040063 if (_PyDict_SetItemId(md_dict, &PyId___name__, name) != 0)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040064 return -1;
Benjamin Peterson027ce162014-04-24 19:39:18 -040065 if (_PyDict_SetItemId(md_dict, &PyId___doc__, doc) != 0)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040066 return -1;
Benjamin Peterson027ce162014-04-24 19:39:18 -040067 if (_PyDict_SetItemId(md_dict, &PyId___package__, Py_None) != 0)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040068 return -1;
Benjamin Peterson027ce162014-04-24 19:39:18 -040069 if (_PyDict_SetItemId(md_dict, &PyId___loader__, Py_None) != 0)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040070 return -1;
Benjamin Peterson027ce162014-04-24 19:39:18 -040071 if (_PyDict_SetItemId(md_dict, &PyId___spec__, Py_None) != 0)
Eric Snowb523f842013-11-22 09:05:39 -070072 return -1;
Antoine Pitroudcedaf62013-07-31 23:14:08 +020073 if (PyUnicode_CheckExact(name)) {
74 Py_INCREF(name);
Serhiy Storchaka48842712016-04-06 09:45:48 +030075 Py_XSETREF(mod->md_name, name);
Antoine Pitroudcedaf62013-07-31 23:14:08 +020076 }
Brett Cannon4c14b5d2013-05-04 13:56:58 -040077
78 return 0;
79}
80
81
Guido van Rossumc0b618a1997-05-02 03:12:38 +000082PyObject *
Victor Stinner0639b562011-03-04 12:57:07 +000083PyModule_NewObject(PyObject *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 PyModuleObject *m;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 m = PyObject_GC_New(PyModuleObject, &PyModule_Type);
87 if (m == NULL)
88 return NULL;
89 m->md_def = NULL;
90 m->md_state = NULL;
Antoine Pitroudcedaf62013-07-31 23:14:08 +020091 m->md_weaklist = NULL;
92 m->md_name = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 m->md_dict = PyDict_New();
Antoine Pitroudcedaf62013-07-31 23:14:08 +020094 if (module_init_dict(m, m->md_dict, name, NULL) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 PyObject_GC_Track(m);
97 return (PyObject *)m;
Guido van Rossumc45611d1993-11-17 22:58:56 +000098
99 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 Py_DECREF(m);
101 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102}
103
Martin v. Löwis1a214512008-06-11 05:26:20 +0000104PyObject *
Victor Stinner0639b562011-03-04 12:57:07 +0000105PyModule_New(const char *name)
106{
107 PyObject *nameobj, *module;
108 nameobj = PyUnicode_FromString(name);
109 if (nameobj == NULL)
110 return NULL;
111 module = PyModule_NewObject(nameobj);
112 Py_DECREF(nameobj);
113 return module;
114}
115
Nick Coghland5cacbb2015-05-23 22:24:10 +1000116/* Check API/ABI version
117 * Issues a warning on mismatch, which is usually not fatal.
118 * Returns 0 if an exception is raised.
119 */
120static int
121check_api_version(const char *name, int module_api_version)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000122{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000123 if (module_api_version != PYTHON_API_VERSION && module_api_version != PYTHON_ABI_VERSION) {
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000124 int err;
125 err = PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
126 "Python C API version mismatch for module %.100s: "
127 "This Python has API version %d, module %.100s has version %d.",
128 name,
129 PYTHON_API_VERSION, name, module_api_version);
130 if (err)
Nick Coghland5cacbb2015-05-23 22:24:10 +1000131 return 0;
132 }
133 return 1;
134}
135
Nick Coghlan8682f572016-08-21 17:41:56 +1000136static int
137_add_methods_to_object(PyObject *module, PyObject *name, PyMethodDef *functions)
138{
139 PyObject *func;
140 PyMethodDef *fdef;
141
142 for (fdef = functions; fdef->ml_name != NULL; fdef++) {
143 if ((fdef->ml_flags & METH_CLASS) ||
144 (fdef->ml_flags & METH_STATIC)) {
145 PyErr_SetString(PyExc_ValueError,
146 "module functions cannot set"
147 " METH_CLASS or METH_STATIC");
148 return -1;
149 }
150 func = PyCFunction_NewEx(fdef, (PyObject*)module, name);
151 if (func == NULL) {
152 return -1;
153 }
154 if (PyObject_SetAttrString(module, fdef->ml_name, func) != 0) {
155 Py_DECREF(func);
156 return -1;
157 }
158 Py_DECREF(func);
159 }
160
161 return 0;
162}
163
Nick Coghland5cacbb2015-05-23 22:24:10 +1000164PyObject *
165PyModule_Create2(struct PyModuleDef* module, int module_api_version)
166{
Victor Stinnerff4584c2020-03-13 18:03:56 +0100167 if (!_PyImport_IsInitialized(_PyInterpreterState_GET_UNSAFE())) {
Victor Stinnera94c6b62020-01-27 22:37:05 +0100168 PyErr_SetString(PyExc_SystemError,
169 "Python import machinery not initialized");
170 return NULL;
171 }
Eric Snowd393c1b2017-09-14 12:18:12 -0600172 return _PyModule_CreateInitialized(module, module_api_version);
173}
174
175PyObject *
176_PyModule_CreateInitialized(struct PyModuleDef* module, int module_api_version)
177{
Nick Coghland5cacbb2015-05-23 22:24:10 +1000178 const char* name;
179 PyModuleObject *m;
Eric Snowd393c1b2017-09-14 12:18:12 -0600180
Nick Coghland5cacbb2015-05-23 22:24:10 +1000181 if (!PyModuleDef_Init(module))
182 return NULL;
183 name = module->m_name;
184 if (!check_api_version(name, module_api_version)) {
185 return NULL;
186 }
187 if (module->m_slots) {
188 PyErr_Format(
189 PyExc_SystemError,
190 "module %s: PyModule_Create is incompatible with m_slots", name);
191 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192 }
193 /* Make sure name is fully qualified.
Martin v. Löwis1a214512008-06-11 05:26:20 +0000194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 This is a bit of a hack: when the shared library is loaded,
196 the module name is "package.module", but the module calls
197 PyModule_Create*() with just "module" for the name. The shared
198 library loader squirrels away the true name of the module in
199 _Py_PackageContext, and PyModule_Create*() will substitute this
200 (if the name actually matches).
201 */
202 if (_Py_PackageContext != NULL) {
Serhiy Storchakab57d9ea2016-11-21 10:25:54 +0200203 const char *p = strrchr(_Py_PackageContext, '.');
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 if (p != NULL && strcmp(module->m_name, p+1) == 0) {
205 name = _Py_PackageContext;
206 _Py_PackageContext = NULL;
207 }
208 }
209 if ((m = (PyModuleObject*)PyModule_New(name)) == NULL)
210 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 if (module->m_size > 0) {
213 m->md_state = PyMem_MALLOC(module->m_size);
214 if (!m->md_state) {
215 PyErr_NoMemory();
216 Py_DECREF(m);
217 return NULL;
218 }
219 memset(m->md_state, 0, module->m_size);
220 }
221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 if (module->m_methods != NULL) {
Nick Coghland5cacbb2015-05-23 22:24:10 +1000223 if (PyModule_AddFunctions((PyObject *) m, module->m_methods) != 0) {
Meador Inge29e49d62012-07-19 13:45:43 -0500224 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 return NULL;
Meador Inge29e49d62012-07-19 13:45:43 -0500226 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 }
228 if (module->m_doc != NULL) {
Nick Coghland5cacbb2015-05-23 22:24:10 +1000229 if (PyModule_SetDocString((PyObject *) m, module->m_doc) != 0) {
Meador Inge29e49d62012-07-19 13:45:43 -0500230 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 return NULL;
232 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 }
234 m->md_def = module;
235 return (PyObject*)m;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000236}
237
Nick Coghland5cacbb2015-05-23 22:24:10 +1000238PyObject *
239PyModule_FromDefAndSpec2(struct PyModuleDef* def, PyObject *spec, int module_api_version)
240{
241 PyModuleDef_Slot* cur_slot;
242 PyObject *(*create)(PyObject *, PyModuleDef*) = NULL;
243 PyObject *nameobj;
244 PyObject *m = NULL;
245 int has_execution_slots = 0;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200246 const char *name;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000247 int ret;
248
249 PyModuleDef_Init(def);
250
251 nameobj = PyObject_GetAttrString(spec, "name");
252 if (nameobj == NULL) {
253 return NULL;
254 }
255 name = PyUnicode_AsUTF8(nameobj);
256 if (name == NULL) {
257 goto error;
258 }
259
260 if (!check_api_version(name, module_api_version)) {
261 goto error;
262 }
263
264 if (def->m_size < 0) {
265 PyErr_Format(
266 PyExc_SystemError,
267 "module %s: m_size may not be negative for multi-phase initialization",
268 name);
269 goto error;
270 }
271
272 for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) {
273 if (cur_slot->slot == Py_mod_create) {
274 if (create) {
275 PyErr_Format(
276 PyExc_SystemError,
277 "module %s has multiple create slots",
278 name);
279 goto error;
280 }
281 create = cur_slot->value;
282 } else if (cur_slot->slot < 0 || cur_slot->slot > _Py_mod_LAST_SLOT) {
283 PyErr_Format(
284 PyExc_SystemError,
285 "module %s uses unknown slot ID %i",
286 name, cur_slot->slot);
287 goto error;
288 } else {
289 has_execution_slots = 1;
290 }
291 }
292
293 if (create) {
294 m = create(spec, def);
295 if (m == NULL) {
296 if (!PyErr_Occurred()) {
297 PyErr_Format(
298 PyExc_SystemError,
299 "creation of module %s failed without setting an exception",
300 name);
301 }
302 goto error;
303 } else {
304 if (PyErr_Occurred()) {
305 PyErr_Format(PyExc_SystemError,
306 "creation of module %s raised unreported exception",
307 name);
308 goto error;
309 }
310 }
311 } else {
Nick Coghlan8682f572016-08-21 17:41:56 +1000312 m = PyModule_NewObject(nameobj);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000313 if (m == NULL) {
314 goto error;
315 }
316 }
317
318 if (PyModule_Check(m)) {
319 ((PyModuleObject*)m)->md_state = NULL;
320 ((PyModuleObject*)m)->md_def = def;
321 } else {
322 if (def->m_size > 0 || def->m_traverse || def->m_clear || def->m_free) {
323 PyErr_Format(
324 PyExc_SystemError,
325 "module %s is not a module object, but requests module state",
326 name);
327 goto error;
328 }
329 if (has_execution_slots) {
330 PyErr_Format(
331 PyExc_SystemError,
332 "module %s specifies execution slots, but did not create "
333 "a ModuleType instance",
334 name);
335 goto error;
336 }
337 }
338
339 if (def->m_methods != NULL) {
Nick Coghlan8682f572016-08-21 17:41:56 +1000340 ret = _add_methods_to_object(m, nameobj, def->m_methods);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000341 if (ret != 0) {
342 goto error;
343 }
344 }
345
346 if (def->m_doc != NULL) {
347 ret = PyModule_SetDocString(m, def->m_doc);
348 if (ret != 0) {
349 goto error;
350 }
351 }
352
Nick Coghlana48db2b2015-05-24 01:03:46 +1000353 Py_DECREF(nameobj);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000354 return m;
355
356error:
357 Py_DECREF(nameobj);
358 Py_XDECREF(m);
359 return NULL;
360}
361
362int
363PyModule_ExecDef(PyObject *module, PyModuleDef *def)
364{
365 PyModuleDef_Slot *cur_slot;
366 const char *name;
367 int ret;
368
369 name = PyModule_GetName(module);
370 if (name == NULL) {
371 return -1;
372 }
373
Nick Coghlan8682f572016-08-21 17:41:56 +1000374 if (def->m_size >= 0) {
Nick Coghland5cacbb2015-05-23 22:24:10 +1000375 PyModuleObject *md = (PyModuleObject*)module;
376 if (md->md_state == NULL) {
377 /* Always set a state pointer; this serves as a marker to skip
378 * multiple initialization (importlib.reload() is no-op) */
379 md->md_state = PyMem_MALLOC(def->m_size);
380 if (!md->md_state) {
381 PyErr_NoMemory();
382 return -1;
383 }
384 memset(md->md_state, 0, def->m_size);
385 }
386 }
387
388 if (def->m_slots == NULL) {
389 return 0;
390 }
391
392 for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) {
393 switch (cur_slot->slot) {
394 case Py_mod_create:
Serhiy Storchaka333ad922016-09-26 23:14:44 +0300395 /* handled in PyModule_FromDefAndSpec2 */
Nick Coghland5cacbb2015-05-23 22:24:10 +1000396 break;
397 case Py_mod_exec:
398 ret = ((int (*)(PyObject *))cur_slot->value)(module);
399 if (ret != 0) {
400 if (!PyErr_Occurred()) {
401 PyErr_Format(
402 PyExc_SystemError,
403 "execution of module %s failed without setting an exception",
404 name);
405 }
406 return -1;
407 }
408 if (PyErr_Occurred()) {
409 PyErr_Format(
410 PyExc_SystemError,
411 "execution of module %s raised unreported exception",
412 name);
413 return -1;
414 }
415 break;
416 default:
417 PyErr_Format(
418 PyExc_SystemError,
419 "module %s initialized with unknown slot %i",
420 name, cur_slot->slot);
421 return -1;
422 }
423 }
424 return 0;
425}
426
427int
428PyModule_AddFunctions(PyObject *m, PyMethodDef *functions)
429{
Nick Coghlan8682f572016-08-21 17:41:56 +1000430 int res;
431 PyObject *name = PyModule_GetNameObject(m);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000432 if (name == NULL) {
433 return -1;
434 }
435
Nick Coghlan8682f572016-08-21 17:41:56 +1000436 res = _add_methods_to_object(m, name, functions);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000437 Py_DECREF(name);
Nick Coghlan8682f572016-08-21 17:41:56 +1000438 return res;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000439}
440
441int
442PyModule_SetDocString(PyObject *m, const char *doc)
443{
444 PyObject *v;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000445
446 v = PyUnicode_FromString(doc);
447 if (v == NULL || _PyObject_SetAttrId(m, &PyId___doc__, v) != 0) {
448 Py_XDECREF(v);
449 return -1;
450 }
451 Py_DECREF(v);
452 return 0;
453}
Martin v. Löwis1a214512008-06-11 05:26:20 +0000454
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000455PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000456PyModule_GetDict(PyObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000457{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 PyObject *d;
459 if (!PyModule_Check(m)) {
460 PyErr_BadInternalCall();
461 return NULL;
462 }
463 d = ((PyModuleObject *)m) -> md_dict;
Berker Peksag7fbce562016-08-19 12:00:13 +0300464 assert(d != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000466}
467
Victor Stinnerbd475112011-02-23 00:21:43 +0000468PyObject*
469PyModule_GetNameObject(PyObject *m)
Guido van Rossum0558a201990-10-26 15:00:11 +0000470{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 PyObject *d;
Victor Stinnerbd475112011-02-23 00:21:43 +0000472 PyObject *name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 if (!PyModule_Check(m)) {
474 PyErr_BadArgument();
475 return NULL;
476 }
477 d = ((PyModuleObject *)m)->md_dict;
478 if (d == NULL ||
Benjamin Peterson027ce162014-04-24 19:39:18 -0400479 (name = _PyDict_GetItemId(d, &PyId___name__)) == NULL ||
Victor Stinnerbd475112011-02-23 00:21:43 +0000480 !PyUnicode_Check(name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 {
482 PyErr_SetString(PyExc_SystemError, "nameless module");
483 return NULL;
484 }
Victor Stinnerbd475112011-02-23 00:21:43 +0000485 Py_INCREF(name);
486 return name;
487}
488
489const char *
490PyModule_GetName(PyObject *m)
491{
492 PyObject *name = PyModule_GetNameObject(m);
493 if (name == NULL)
494 return NULL;
495 Py_DECREF(name); /* module dict has still a reference */
Serhiy Storchaka06515832016-11-20 09:13:07 +0200496 return PyUnicode_AsUTF8(name);
Guido van Rossum0558a201990-10-26 15:00:11 +0000497}
498
Victor Stinner6c00c142010-08-17 23:37:11 +0000499PyObject*
500PyModule_GetFilenameObject(PyObject *m)
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000501{
Benjamin Peterson027ce162014-04-24 19:39:18 -0400502 _Py_IDENTIFIER(__file__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 PyObject *d;
504 PyObject *fileobj;
505 if (!PyModule_Check(m)) {
506 PyErr_BadArgument();
507 return NULL;
508 }
509 d = ((PyModuleObject *)m)->md_dict;
510 if (d == NULL ||
Benjamin Peterson027ce162014-04-24 19:39:18 -0400511 (fileobj = _PyDict_GetItemId(d, &PyId___file__)) == NULL ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 !PyUnicode_Check(fileobj))
513 {
514 PyErr_SetString(PyExc_SystemError, "module filename missing");
515 return NULL;
516 }
Victor Stinner6c00c142010-08-17 23:37:11 +0000517 Py_INCREF(fileobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 return fileobj;
Victor Stinner8124feb2010-05-07 00:50:12 +0000519}
520
521const char *
522PyModule_GetFilename(PyObject *m)
523{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 PyObject *fileobj;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200525 const char *utf8;
Victor Stinner6c00c142010-08-17 23:37:11 +0000526 fileobj = PyModule_GetFilenameObject(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 if (fileobj == NULL)
528 return NULL;
Serhiy Storchaka06515832016-11-20 09:13:07 +0200529 utf8 = PyUnicode_AsUTF8(fileobj);
Victor Stinnerbd475112011-02-23 00:21:43 +0000530 Py_DECREF(fileobj); /* module dict has still a reference */
Victor Stinner6c00c142010-08-17 23:37:11 +0000531 return utf8;
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000532}
533
Martin v. Löwis1a214512008-06-11 05:26:20 +0000534PyModuleDef*
535PyModule_GetDef(PyObject* m)
536{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 if (!PyModule_Check(m)) {
538 PyErr_BadArgument();
539 return NULL;
540 }
541 return ((PyModuleObject *)m)->md_def;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000542}
543
544void*
545PyModule_GetState(PyObject* m)
546{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 if (!PyModule_Check(m)) {
548 PyErr_BadArgument();
549 return NULL;
550 }
551 return ((PyModuleObject *)m)->md_state;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000552}
553
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000554void
Fred Drakeee238b92000-07-09 06:03:25 +0000555_PyModule_Clear(PyObject *m)
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000556{
Serhiy Storchaka87a5c512014-02-10 18:21:34 +0200557 PyObject *d = ((PyModuleObject *)m)->md_dict;
558 if (d != NULL)
559 _PyModule_ClearDict(d);
560}
561
562void
563_PyModule_ClearDict(PyObject *d)
564{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 /* To make the execution order of destructors for global
566 objects a bit more predictable, we first zap all objects
567 whose name starts with a single underscore, before we clear
568 the entire dictionary. We zap them by replacing them with
569 None, rather than deleting them from the dictionary, to
570 avoid rehashing the dictionary (to some extent). */
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 Py_ssize_t pos;
573 PyObject *key, *value;
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000574
Victor Stinner331a6a52019-05-27 16:39:22 +0200575 int verbose = _PyInterpreterState_GET_UNSAFE()->config.verbose;
Victor Stinnerc96be812019-05-14 17:34:56 +0200576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000577 /* First, clear only names starting with a single underscore */
578 pos = 0;
579 while (PyDict_Next(d, &pos, &key, &value)) {
580 if (value != Py_None && PyUnicode_Check(key)) {
Brett Cannon62228db2012-04-29 14:38:11 -0400581 if (PyUnicode_READ_CHAR(key, 0) == '_' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200582 PyUnicode_READ_CHAR(key, 1) != '_') {
Victor Stinnerc96be812019-05-14 17:34:56 +0200583 if (verbose > 1) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200584 const char *s = PyUnicode_AsUTF8(key);
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000585 if (s != NULL)
586 PySys_WriteStderr("# clear[1] %s\n", s);
587 else
588 PyErr_Clear();
589 }
Serhiy Storchakac1a68322018-04-29 22:16:30 +0300590 if (PyDict_SetItem(d, key, Py_None) != 0) {
591 PyErr_WriteUnraisable(NULL);
592 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 }
594 }
595 }
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 /* Next, clear all names except for __builtins__ */
598 pos = 0;
599 while (PyDict_Next(d, &pos, &key, &value)) {
600 if (value != Py_None && PyUnicode_Check(key)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200601 if (PyUnicode_READ_CHAR(key, 0) != '_' ||
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200602 !_PyUnicode_EqualToASCIIString(key, "__builtins__"))
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000603 {
Victor Stinnerc96be812019-05-14 17:34:56 +0200604 if (verbose > 1) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200605 const char *s = PyUnicode_AsUTF8(key);
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000606 if (s != NULL)
607 PySys_WriteStderr("# clear[2] %s\n", s);
608 else
609 PyErr_Clear();
610 }
Serhiy Storchakac1a68322018-04-29 22:16:30 +0300611 if (PyDict_SetItem(d, key, Py_None) != 0) {
612 PyErr_WriteUnraisable(NULL);
613 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000614 }
615 }
616 }
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 /* Note: we leave __builtins__ in place, so that destructors
619 of non-global objects defined in this module can still use
620 builtins, in particularly 'None'. */
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000621
622}
623
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200624/*[clinic input]
625class module "PyModuleObject *" "&PyModule_Type"
626[clinic start generated code]*/
627/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3e35d4f708ecb6af]*/
628
629#include "clinic/moduleobject.c.h"
630
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000631/* Methods */
632
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200633/*[clinic input]
634module.__init__
635 name: unicode
636 doc: object = None
637
638Create a module object.
639
640The name must be a string; the optional doc argument can have any type.
641[clinic start generated code]*/
642
Tim Peters6d6c1a32001-08-02 04:15:00 +0000643static int
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200644module___init___impl(PyModuleObject *self, PyObject *name, PyObject *doc)
645/*[clinic end generated code: output=e7e721c26ce7aad7 input=57f9e177401e5e1e]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +0000646{
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200647 PyObject *dict = self->md_dict;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 if (dict == NULL) {
649 dict = PyDict_New();
650 if (dict == NULL)
651 return -1;
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200652 self->md_dict = dict;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 }
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200654 if (module_init_dict(self, dict, name, doc) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 return -1;
656 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000657}
658
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000659static void
Fred Drakeee238b92000-07-09 06:03:25 +0000660module_dealloc(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000661{
Victor Stinner331a6a52019-05-27 16:39:22 +0200662 int verbose = _PyInterpreterState_GET_UNSAFE()->config.verbose;
Victor Stinnerc96be812019-05-14 17:34:56 +0200663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 PyObject_GC_UnTrack(m);
Victor Stinnerc96be812019-05-14 17:34:56 +0200665 if (verbose && m->md_name) {
Serhiy Storchakad7c38732019-10-08 13:46:17 +0300666 PySys_FormatStderr("# destroy %U\n", m->md_name);
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200667 }
668 if (m->md_weaklist != NULL)
669 PyObject_ClearWeakRefs((PyObject *) m);
Victor Stinner5b1ef202020-03-17 18:09:46 +0100670 /* bpo-39824: Don't call m_free() if m_size > 0 and md_state=NULL */
671 if (m->md_def && m->md_def->m_free
672 && (m->md_def->m_size <= 0 || m->md_state != NULL))
673 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 m->md_def->m_free(m);
Victor Stinner5b1ef202020-03-17 18:09:46 +0100675 }
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200676 Py_XDECREF(m->md_dict);
677 Py_XDECREF(m->md_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 if (m->md_state != NULL)
679 PyMem_FREE(m->md_state);
680 Py_TYPE(m)->tp_free((PyObject *)m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000681}
682
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000683static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000684module_repr(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000685{
Victor Stinnerff4584c2020-03-13 18:03:56 +0100686 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Barry Warsaw2907fe62001-08-16 20:39:24 +0000687
Eric Snowb523f842013-11-22 09:05:39 -0700688 return PyObject_CallMethod(interp->importlib, "_module_repr", "O", m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000689}
690
Serhiy Storchaka3e429dc2018-10-30 13:19:51 +0200691/* Check if the "_initializing" attribute of the module spec is set to true.
692 Clear the exception and return 0 if spec is NULL.
693 */
694int
695_PyModuleSpec_IsInitializing(PyObject *spec)
696{
697 if (spec != NULL) {
698 _Py_IDENTIFIER(_initializing);
699 PyObject *value = _PyObject_GetAttrId(spec, &PyId__initializing);
700 if (value != NULL) {
701 int initializing = PyObject_IsTrue(value);
702 Py_DECREF(value);
703 if (initializing >= 0) {
704 return initializing;
705 }
706 }
707 }
708 PyErr_Clear();
709 return 0;
710}
711
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700712static PyObject*
Benjamin Peterson1184e262014-04-24 19:29:23 -0400713module_getattro(PyModuleObject *m, PyObject *name)
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700714{
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100715 PyObject *attr, *mod_name, *getattr;
Benjamin Peterson1184e262014-04-24 19:29:23 -0400716 attr = PyObject_GenericGetAttr((PyObject *)m, name);
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100717 if (attr || !PyErr_ExceptionMatches(PyExc_AttributeError)) {
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700718 return attr;
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100719 }
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700720 PyErr_Clear();
Benjamin Peterson1184e262014-04-24 19:29:23 -0400721 if (m->md_dict) {
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100722 _Py_IDENTIFIER(__getattr__);
723 getattr = _PyDict_GetItemId(m->md_dict, &PyId___getattr__);
724 if (getattr) {
Petr Viktorinffd97532020-02-11 17:46:57 +0100725 return PyObject_CallOneArg(getattr, name);
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100726 }
Benjamin Peterson027ce162014-04-24 19:39:18 -0400727 mod_name = _PyDict_GetItemId(m->md_dict, &PyId___name__);
Oren Milman6db70332017-09-19 14:23:01 +0300728 if (mod_name && PyUnicode_Check(mod_name)) {
Serhiy Storchaka3e429dc2018-10-30 13:19:51 +0200729 Py_INCREF(mod_name);
730 PyObject *spec = _PyDict_GetItemId(m->md_dict, &PyId___spec__);
731 Py_XINCREF(spec);
732 if (_PyModuleSpec_IsInitializing(spec)) {
733 PyErr_Format(PyExc_AttributeError,
734 "partially initialized "
735 "module '%U' has no attribute '%U' "
736 "(most likely due to a circular import)",
737 mod_name, name);
738 }
739 else {
740 PyErr_Format(PyExc_AttributeError,
741 "module '%U' has no attribute '%U'",
742 mod_name, name);
743 }
744 Py_XDECREF(spec);
745 Py_DECREF(mod_name);
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700746 return NULL;
747 }
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700748 }
749 PyErr_Format(PyExc_AttributeError,
750 "module has no attribute '%U'", name);
751 return NULL;
752}
753
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000754static int
755module_traverse(PyModuleObject *m, visitproc visit, void *arg)
756{
Victor Stinner5b1ef202020-03-17 18:09:46 +0100757 /* bpo-39824: Don't call m_traverse() if m_size > 0 and md_state=NULL */
758 if (m->md_def && m->md_def->m_traverse
759 && (m->md_def->m_size <= 0 || m->md_state != NULL))
760 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 int res = m->md_def->m_traverse((PyObject*)m, visit, arg);
762 if (res)
763 return res;
764 }
765 Py_VISIT(m->md_dict);
766 return 0;
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000767}
768
Martin v. Löwis1a214512008-06-11 05:26:20 +0000769static int
770module_clear(PyModuleObject *m)
771{
Victor Stinner5b1ef202020-03-17 18:09:46 +0100772 /* bpo-39824: Don't call m_clear() if m_size > 0 and md_state=NULL */
773 if (m->md_def && m->md_def->m_clear
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_clear((PyObject*)m);
Serhiy Storchakad7c38732019-10-08 13:46:17 +0300777 if (PyErr_Occurred()) {
778 PySys_FormatStderr("Exception ignored in m_clear of module%s%V\n",
779 m->md_name ? " " : "",
780 m->md_name, "");
781 PyErr_WriteUnraisable(NULL);
782 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 if (res)
784 return res;
785 }
786 Py_CLEAR(m->md_dict);
787 return 0;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000788}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500790static PyObject *
791module_dir(PyObject *self, PyObject *args)
792{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200793 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200794 _Py_IDENTIFIER(__dir__);
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500795 PyObject *result = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200796 PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500797
798 if (dict != NULL) {
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100799 if (PyDict_Check(dict)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200800 PyObject *dirfunc = _PyDict_GetItemIdWithError(dict, &PyId___dir__);
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100801 if (dirfunc) {
802 result = _PyObject_CallNoArg(dirfunc);
803 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200804 else if (!PyErr_Occurred()) {
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100805 result = PyDict_Keys(dict);
806 }
807 }
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500808 else {
809 const char *name = PyModule_GetName(self);
810 if (name)
811 PyErr_Format(PyExc_TypeError,
812 "%.200s.__dict__ is not a dictionary",
813 name);
814 }
815 }
816
817 Py_XDECREF(dict);
818 return result;
819}
820
821static PyMethodDef module_methods[] = {
822 {"__dir__", module_dir, METH_NOARGS,
Benjamin Petersonc7284122011-05-24 12:46:15 -0500823 PyDoc_STR("__dir__() -> list\nspecialized dir() implementation")},
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500824 {0}
825};
826
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000827PyTypeObject PyModule_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 PyVarObject_HEAD_INIT(&PyType_Type, 0)
829 "module", /* tp_name */
Peter Eisentraut0e0bc4e2018-09-10 18:46:08 +0200830 sizeof(PyModuleObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 0, /* tp_itemsize */
832 (destructor)module_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200833 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 0, /* tp_getattr */
835 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200836 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 (reprfunc)module_repr, /* tp_repr */
838 0, /* tp_as_number */
839 0, /* tp_as_sequence */
840 0, /* tp_as_mapping */
841 0, /* tp_hash */
842 0, /* tp_call */
843 0, /* tp_str */
Benjamin Peterson1184e262014-04-24 19:29:23 -0400844 (getattrofunc)module_getattro, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 PyObject_GenericSetAttr, /* tp_setattro */
846 0, /* tp_as_buffer */
847 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
848 Py_TPFLAGS_BASETYPE, /* tp_flags */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200849 module___init____doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 (traverseproc)module_traverse, /* tp_traverse */
851 (inquiry)module_clear, /* tp_clear */
852 0, /* tp_richcompare */
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200853 offsetof(PyModuleObject, md_weaklist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 0, /* tp_iter */
855 0, /* tp_iternext */
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500856 module_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 module_members, /* tp_members */
858 0, /* tp_getset */
859 0, /* tp_base */
860 0, /* tp_dict */
861 0, /* tp_descr_get */
862 0, /* tp_descr_set */
863 offsetof(PyModuleObject, md_dict), /* tp_dictoffset */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200864 module___init__, /* tp_init */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 PyType_GenericAlloc, /* tp_alloc */
866 PyType_GenericNew, /* tp_new */
867 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000868};