blob: e57ea86e7694ceabb49c3dd888d1226dbaa9955c [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
Victor Stinner250035d2021-01-18 20:47:13 +010038int
39_PyModule_IsExtension(PyObject *obj)
40{
41 if (!PyModule_Check(obj)) {
42 return 0;
43 }
44 PyModuleObject *module = (PyModuleObject*)obj;
45
46 struct PyModuleDef *def = module->md_def;
47 return (def != NULL && def->m_methods != NULL);
48}
49
50
Nick Coghland5cacbb2015-05-23 22:24:10 +100051PyObject*
52PyModuleDef_Init(struct PyModuleDef* def)
53{
54 if (PyType_Ready(&PyModuleDef_Type) < 0)
55 return NULL;
56 if (def->m_base.m_index == 0) {
57 max_module_number++;
Victor Stinnerc86a1122020-02-07 01:24:29 +010058 Py_SET_REFCNT(def, 1);
Victor Stinnerd2ec81a2020-02-07 09:17:07 +010059 Py_SET_TYPE(def, &PyModuleDef_Type);
Nick Coghland5cacbb2015-05-23 22:24:10 +100060 def->m_base.m_index = max_module_number;
61 }
62 return (PyObject*)def;
63}
64
Brett Cannon4c14b5d2013-05-04 13:56:58 -040065static int
Antoine Pitroudcedaf62013-07-31 23:14:08 +020066module_init_dict(PyModuleObject *mod, PyObject *md_dict,
67 PyObject *name, PyObject *doc)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040068{
Benjamin Peterson027ce162014-04-24 19:39:18 -040069 _Py_IDENTIFIER(__package__);
70 _Py_IDENTIFIER(__loader__);
Serhiy Storchaka009b8112015-03-18 21:53:15 +020071
Brett Cannon4c14b5d2013-05-04 13:56:58 -040072 if (md_dict == NULL)
73 return -1;
74 if (doc == NULL)
75 doc = Py_None;
76
Benjamin Peterson027ce162014-04-24 19:39:18 -040077 if (_PyDict_SetItemId(md_dict, &PyId___name__, name) != 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___doc__, doc) != 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___package__, Py_None) != 0)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040082 return -1;
Benjamin Peterson027ce162014-04-24 19:39:18 -040083 if (_PyDict_SetItemId(md_dict, &PyId___loader__, Py_None) != 0)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040084 return -1;
Benjamin Peterson027ce162014-04-24 19:39:18 -040085 if (_PyDict_SetItemId(md_dict, &PyId___spec__, Py_None) != 0)
Eric Snowb523f842013-11-22 09:05:39 -070086 return -1;
Antoine Pitroudcedaf62013-07-31 23:14:08 +020087 if (PyUnicode_CheckExact(name)) {
88 Py_INCREF(name);
Serhiy Storchaka48842712016-04-06 09:45:48 +030089 Py_XSETREF(mod->md_name, name);
Antoine Pitroudcedaf62013-07-31 23:14:08 +020090 }
Brett Cannon4c14b5d2013-05-04 13:56:58 -040091
92 return 0;
93}
94
95
Guido van Rossumc0b618a1997-05-02 03:12:38 +000096PyObject *
Victor Stinner0639b562011-03-04 12:57:07 +000097PyModule_NewObject(PyObject *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000098{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099 PyModuleObject *m;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 m = PyObject_GC_New(PyModuleObject, &PyModule_Type);
101 if (m == NULL)
102 return NULL;
103 m->md_def = NULL;
104 m->md_state = NULL;
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200105 m->md_weaklist = NULL;
106 m->md_name = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 m->md_dict = PyDict_New();
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200108 if (module_init_dict(m, m->md_dict, name, NULL) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 PyObject_GC_Track(m);
111 return (PyObject *)m;
Guido van Rossumc45611d1993-11-17 22:58:56 +0000112
113 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 Py_DECREF(m);
115 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000116}
117
Martin v. Löwis1a214512008-06-11 05:26:20 +0000118PyObject *
Victor Stinner0639b562011-03-04 12:57:07 +0000119PyModule_New(const char *name)
120{
121 PyObject *nameobj, *module;
122 nameobj = PyUnicode_FromString(name);
123 if (nameobj == NULL)
124 return NULL;
125 module = PyModule_NewObject(nameobj);
126 Py_DECREF(nameobj);
127 return module;
128}
129
Nick Coghland5cacbb2015-05-23 22:24:10 +1000130/* Check API/ABI version
131 * Issues a warning on mismatch, which is usually not fatal.
132 * Returns 0 if an exception is raised.
133 */
134static int
135check_api_version(const char *name, int module_api_version)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000136{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000137 if (module_api_version != PYTHON_API_VERSION && module_api_version != PYTHON_ABI_VERSION) {
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000138 int err;
139 err = PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
140 "Python C API version mismatch for module %.100s: "
141 "This Python has API version %d, module %.100s has version %d.",
142 name,
143 PYTHON_API_VERSION, name, module_api_version);
144 if (err)
Nick Coghland5cacbb2015-05-23 22:24:10 +1000145 return 0;
146 }
147 return 1;
148}
149
Nick Coghlan8682f572016-08-21 17:41:56 +1000150static int
151_add_methods_to_object(PyObject *module, PyObject *name, PyMethodDef *functions)
152{
153 PyObject *func;
154 PyMethodDef *fdef;
155
156 for (fdef = functions; fdef->ml_name != NULL; fdef++) {
157 if ((fdef->ml_flags & METH_CLASS) ||
158 (fdef->ml_flags & METH_STATIC)) {
159 PyErr_SetString(PyExc_ValueError,
160 "module functions cannot set"
161 " METH_CLASS or METH_STATIC");
162 return -1;
163 }
164 func = PyCFunction_NewEx(fdef, (PyObject*)module, name);
165 if (func == NULL) {
166 return -1;
167 }
168 if (PyObject_SetAttrString(module, fdef->ml_name, func) != 0) {
169 Py_DECREF(func);
170 return -1;
171 }
172 Py_DECREF(func);
173 }
174
175 return 0;
176}
177
Nick Coghland5cacbb2015-05-23 22:24:10 +1000178PyObject *
179PyModule_Create2(struct PyModuleDef* module, int module_api_version)
180{
Victor Stinner81a7be32020-04-14 15:14:01 +0200181 if (!_PyImport_IsInitialized(_PyInterpreterState_GET())) {
Victor Stinnera94c6b62020-01-27 22:37:05 +0100182 PyErr_SetString(PyExc_SystemError,
183 "Python import machinery not initialized");
184 return NULL;
185 }
Eric Snowd393c1b2017-09-14 12:18:12 -0600186 return _PyModule_CreateInitialized(module, module_api_version);
187}
188
189PyObject *
190_PyModule_CreateInitialized(struct PyModuleDef* module, int module_api_version)
191{
Nick Coghland5cacbb2015-05-23 22:24:10 +1000192 const char* name;
193 PyModuleObject *m;
Eric Snowd393c1b2017-09-14 12:18:12 -0600194
Nick Coghland5cacbb2015-05-23 22:24:10 +1000195 if (!PyModuleDef_Init(module))
196 return NULL;
197 name = module->m_name;
198 if (!check_api_version(name, module_api_version)) {
199 return NULL;
200 }
201 if (module->m_slots) {
202 PyErr_Format(
203 PyExc_SystemError,
204 "module %s: PyModule_Create is incompatible with m_slots", name);
205 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 }
207 /* Make sure name is fully qualified.
Martin v. Löwis1a214512008-06-11 05:26:20 +0000208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 This is a bit of a hack: when the shared library is loaded,
210 the module name is "package.module", but the module calls
211 PyModule_Create*() with just "module" for the name. The shared
212 library loader squirrels away the true name of the module in
213 _Py_PackageContext, and PyModule_Create*() will substitute this
214 (if the name actually matches).
215 */
216 if (_Py_PackageContext != NULL) {
Serhiy Storchakab57d9ea2016-11-21 10:25:54 +0200217 const char *p = strrchr(_Py_PackageContext, '.');
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 if (p != NULL && strcmp(module->m_name, p+1) == 0) {
219 name = _Py_PackageContext;
220 _Py_PackageContext = NULL;
221 }
222 }
223 if ((m = (PyModuleObject*)PyModule_New(name)) == NULL)
224 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 if (module->m_size > 0) {
Victor Stinner00d7abd2020-12-01 09:56:42 +0100227 m->md_state = PyMem_Malloc(module->m_size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 if (!m->md_state) {
229 PyErr_NoMemory();
230 Py_DECREF(m);
231 return NULL;
232 }
233 memset(m->md_state, 0, module->m_size);
234 }
235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 if (module->m_methods != NULL) {
Nick Coghland5cacbb2015-05-23 22:24:10 +1000237 if (PyModule_AddFunctions((PyObject *) m, module->m_methods) != 0) {
Meador Inge29e49d62012-07-19 13:45:43 -0500238 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 return NULL;
Meador Inge29e49d62012-07-19 13:45:43 -0500240 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 }
242 if (module->m_doc != NULL) {
Nick Coghland5cacbb2015-05-23 22:24:10 +1000243 if (PyModule_SetDocString((PyObject *) m, module->m_doc) != 0) {
Meador Inge29e49d62012-07-19 13:45:43 -0500244 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 return NULL;
246 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 }
248 m->md_def = module;
249 return (PyObject*)m;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000250}
251
Nick Coghland5cacbb2015-05-23 22:24:10 +1000252PyObject *
253PyModule_FromDefAndSpec2(struct PyModuleDef* def, PyObject *spec, int module_api_version)
254{
255 PyModuleDef_Slot* cur_slot;
256 PyObject *(*create)(PyObject *, PyModuleDef*) = NULL;
257 PyObject *nameobj;
258 PyObject *m = NULL;
259 int has_execution_slots = 0;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200260 const char *name;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000261 int ret;
262
263 PyModuleDef_Init(def);
264
265 nameobj = PyObject_GetAttrString(spec, "name");
266 if (nameobj == NULL) {
267 return NULL;
268 }
269 name = PyUnicode_AsUTF8(nameobj);
270 if (name == NULL) {
271 goto error;
272 }
273
274 if (!check_api_version(name, module_api_version)) {
275 goto error;
276 }
277
278 if (def->m_size < 0) {
279 PyErr_Format(
280 PyExc_SystemError,
281 "module %s: m_size may not be negative for multi-phase initialization",
282 name);
283 goto error;
284 }
285
286 for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) {
287 if (cur_slot->slot == Py_mod_create) {
288 if (create) {
289 PyErr_Format(
290 PyExc_SystemError,
291 "module %s has multiple create slots",
292 name);
293 goto error;
294 }
295 create = cur_slot->value;
296 } else if (cur_slot->slot < 0 || cur_slot->slot > _Py_mod_LAST_SLOT) {
297 PyErr_Format(
298 PyExc_SystemError,
299 "module %s uses unknown slot ID %i",
300 name, cur_slot->slot);
301 goto error;
302 } else {
303 has_execution_slots = 1;
304 }
305 }
306
307 if (create) {
308 m = create(spec, def);
309 if (m == NULL) {
310 if (!PyErr_Occurred()) {
311 PyErr_Format(
312 PyExc_SystemError,
313 "creation of module %s failed without setting an exception",
314 name);
315 }
316 goto error;
317 } else {
318 if (PyErr_Occurred()) {
319 PyErr_Format(PyExc_SystemError,
320 "creation of module %s raised unreported exception",
321 name);
322 goto error;
323 }
324 }
325 } else {
Nick Coghlan8682f572016-08-21 17:41:56 +1000326 m = PyModule_NewObject(nameobj);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000327 if (m == NULL) {
328 goto error;
329 }
330 }
331
332 if (PyModule_Check(m)) {
333 ((PyModuleObject*)m)->md_state = NULL;
334 ((PyModuleObject*)m)->md_def = def;
335 } else {
336 if (def->m_size > 0 || def->m_traverse || def->m_clear || def->m_free) {
337 PyErr_Format(
338 PyExc_SystemError,
339 "module %s is not a module object, but requests module state",
340 name);
341 goto error;
342 }
343 if (has_execution_slots) {
344 PyErr_Format(
345 PyExc_SystemError,
346 "module %s specifies execution slots, but did not create "
347 "a ModuleType instance",
348 name);
349 goto error;
350 }
351 }
352
353 if (def->m_methods != NULL) {
Nick Coghlan8682f572016-08-21 17:41:56 +1000354 ret = _add_methods_to_object(m, nameobj, def->m_methods);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000355 if (ret != 0) {
356 goto error;
357 }
358 }
359
360 if (def->m_doc != NULL) {
361 ret = PyModule_SetDocString(m, def->m_doc);
362 if (ret != 0) {
363 goto error;
364 }
365 }
366
Nick Coghlana48db2b2015-05-24 01:03:46 +1000367 Py_DECREF(nameobj);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000368 return m;
369
370error:
371 Py_DECREF(nameobj);
372 Py_XDECREF(m);
373 return NULL;
374}
375
376int
377PyModule_ExecDef(PyObject *module, PyModuleDef *def)
378{
379 PyModuleDef_Slot *cur_slot;
380 const char *name;
381 int ret;
382
383 name = PyModule_GetName(module);
384 if (name == NULL) {
385 return -1;
386 }
387
Nick Coghlan8682f572016-08-21 17:41:56 +1000388 if (def->m_size >= 0) {
Nick Coghland5cacbb2015-05-23 22:24:10 +1000389 PyModuleObject *md = (PyModuleObject*)module;
390 if (md->md_state == NULL) {
391 /* Always set a state pointer; this serves as a marker to skip
392 * multiple initialization (importlib.reload() is no-op) */
Victor Stinner00d7abd2020-12-01 09:56:42 +0100393 md->md_state = PyMem_Malloc(def->m_size);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000394 if (!md->md_state) {
395 PyErr_NoMemory();
396 return -1;
397 }
398 memset(md->md_state, 0, def->m_size);
399 }
400 }
401
402 if (def->m_slots == NULL) {
403 return 0;
404 }
405
406 for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) {
407 switch (cur_slot->slot) {
408 case Py_mod_create:
Serhiy Storchaka333ad922016-09-26 23:14:44 +0300409 /* handled in PyModule_FromDefAndSpec2 */
Nick Coghland5cacbb2015-05-23 22:24:10 +1000410 break;
411 case Py_mod_exec:
412 ret = ((int (*)(PyObject *))cur_slot->value)(module);
413 if (ret != 0) {
414 if (!PyErr_Occurred()) {
415 PyErr_Format(
416 PyExc_SystemError,
417 "execution of module %s failed without setting an exception",
418 name);
419 }
420 return -1;
421 }
422 if (PyErr_Occurred()) {
423 PyErr_Format(
424 PyExc_SystemError,
425 "execution of module %s raised unreported exception",
426 name);
427 return -1;
428 }
429 break;
430 default:
431 PyErr_Format(
432 PyExc_SystemError,
433 "module %s initialized with unknown slot %i",
434 name, cur_slot->slot);
435 return -1;
436 }
437 }
438 return 0;
439}
440
441int
442PyModule_AddFunctions(PyObject *m, PyMethodDef *functions)
443{
Nick Coghlan8682f572016-08-21 17:41:56 +1000444 int res;
445 PyObject *name = PyModule_GetNameObject(m);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000446 if (name == NULL) {
447 return -1;
448 }
449
Nick Coghlan8682f572016-08-21 17:41:56 +1000450 res = _add_methods_to_object(m, name, functions);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000451 Py_DECREF(name);
Nick Coghlan8682f572016-08-21 17:41:56 +1000452 return res;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000453}
454
455int
456PyModule_SetDocString(PyObject *m, const char *doc)
457{
458 PyObject *v;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000459
460 v = PyUnicode_FromString(doc);
461 if (v == NULL || _PyObject_SetAttrId(m, &PyId___doc__, v) != 0) {
462 Py_XDECREF(v);
463 return -1;
464 }
465 Py_DECREF(v);
466 return 0;
467}
Martin v. Löwis1a214512008-06-11 05:26:20 +0000468
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000469PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000470PyModule_GetDict(PyObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000471{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 PyObject *d;
473 if (!PyModule_Check(m)) {
474 PyErr_BadInternalCall();
475 return NULL;
476 }
477 d = ((PyModuleObject *)m) -> md_dict;
Berker Peksag7fbce562016-08-19 12:00:13 +0300478 assert(d != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000480}
481
Victor Stinnerbd475112011-02-23 00:21:43 +0000482PyObject*
483PyModule_GetNameObject(PyObject *m)
Guido van Rossum0558a201990-10-26 15:00:11 +0000484{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 PyObject *d;
Victor Stinnerbd475112011-02-23 00:21:43 +0000486 PyObject *name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 if (!PyModule_Check(m)) {
488 PyErr_BadArgument();
489 return NULL;
490 }
491 d = ((PyModuleObject *)m)->md_dict;
492 if (d == NULL ||
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200493 (name = _PyDict_GetItemIdWithError(d, &PyId___name__)) == NULL ||
Victor Stinnerbd475112011-02-23 00:21:43 +0000494 !PyUnicode_Check(name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 {
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200496 if (!PyErr_Occurred()) {
497 PyErr_SetString(PyExc_SystemError, "nameless module");
498 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 return NULL;
500 }
Victor Stinnerbd475112011-02-23 00:21:43 +0000501 Py_INCREF(name);
502 return name;
503}
504
505const char *
506PyModule_GetName(PyObject *m)
507{
508 PyObject *name = PyModule_GetNameObject(m);
509 if (name == NULL)
510 return NULL;
511 Py_DECREF(name); /* module dict has still a reference */
Serhiy Storchaka06515832016-11-20 09:13:07 +0200512 return PyUnicode_AsUTF8(name);
Guido van Rossum0558a201990-10-26 15:00:11 +0000513}
514
Victor Stinner6c00c142010-08-17 23:37:11 +0000515PyObject*
516PyModule_GetFilenameObject(PyObject *m)
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000517{
Benjamin Peterson027ce162014-04-24 19:39:18 -0400518 _Py_IDENTIFIER(__file__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 PyObject *d;
520 PyObject *fileobj;
521 if (!PyModule_Check(m)) {
522 PyErr_BadArgument();
523 return NULL;
524 }
525 d = ((PyModuleObject *)m)->md_dict;
526 if (d == NULL ||
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200527 (fileobj = _PyDict_GetItemIdWithError(d, &PyId___file__)) == NULL ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 !PyUnicode_Check(fileobj))
529 {
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200530 if (!PyErr_Occurred()) {
531 PyErr_SetString(PyExc_SystemError, "module filename missing");
532 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 return NULL;
534 }
Victor Stinner6c00c142010-08-17 23:37:11 +0000535 Py_INCREF(fileobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 return fileobj;
Victor Stinner8124feb2010-05-07 00:50:12 +0000537}
538
539const char *
540PyModule_GetFilename(PyObject *m)
541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 PyObject *fileobj;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200543 const char *utf8;
Victor Stinner6c00c142010-08-17 23:37:11 +0000544 fileobj = PyModule_GetFilenameObject(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 if (fileobj == NULL)
546 return NULL;
Serhiy Storchaka06515832016-11-20 09:13:07 +0200547 utf8 = PyUnicode_AsUTF8(fileobj);
Victor Stinnerbd475112011-02-23 00:21:43 +0000548 Py_DECREF(fileobj); /* module dict has still a reference */
Victor Stinner6c00c142010-08-17 23:37:11 +0000549 return utf8;
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000550}
551
Martin v. Löwis1a214512008-06-11 05:26:20 +0000552PyModuleDef*
553PyModule_GetDef(PyObject* m)
554{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 if (!PyModule_Check(m)) {
556 PyErr_BadArgument();
557 return NULL;
558 }
559 return ((PyModuleObject *)m)->md_def;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000560}
561
562void*
563PyModule_GetState(PyObject* m)
564{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 if (!PyModule_Check(m)) {
566 PyErr_BadArgument();
567 return NULL;
568 }
569 return ((PyModuleObject *)m)->md_state;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000570}
571
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000572void
Fred Drakeee238b92000-07-09 06:03:25 +0000573_PyModule_Clear(PyObject *m)
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000574{
Serhiy Storchaka87a5c512014-02-10 18:21:34 +0200575 PyObject *d = ((PyModuleObject *)m)->md_dict;
576 if (d != NULL)
577 _PyModule_ClearDict(d);
578}
579
580void
581_PyModule_ClearDict(PyObject *d)
582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 /* To make the execution order of destructors for global
584 objects a bit more predictable, we first zap all objects
585 whose name starts with a single underscore, before we clear
586 the entire dictionary. We zap them by replacing them with
587 None, rather than deleting them from the dictionary, to
588 avoid rehashing the dictionary (to some extent). */
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 Py_ssize_t pos;
591 PyObject *key, *value;
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000592
Victor Stinnerda7933e2020-04-13 03:04:28 +0200593 int verbose = _Py_GetConfig()->verbose;
Victor Stinnerc96be812019-05-14 17:34:56 +0200594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 /* First, clear only names starting with a single underscore */
596 pos = 0;
597 while (PyDict_Next(d, &pos, &key, &value)) {
598 if (value != Py_None && PyUnicode_Check(key)) {
Brett Cannon62228db2012-04-29 14:38:11 -0400599 if (PyUnicode_READ_CHAR(key, 0) == '_' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200600 PyUnicode_READ_CHAR(key, 1) != '_') {
Victor Stinnerc96be812019-05-14 17:34:56 +0200601 if (verbose > 1) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200602 const char *s = PyUnicode_AsUTF8(key);
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000603 if (s != NULL)
604 PySys_WriteStderr("# clear[1] %s\n", s);
605 else
606 PyErr_Clear();
607 }
Serhiy Storchakac1a68322018-04-29 22:16:30 +0300608 if (PyDict_SetItem(d, key, Py_None) != 0) {
609 PyErr_WriteUnraisable(NULL);
610 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 }
612 }
613 }
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 /* Next, clear all names except for __builtins__ */
616 pos = 0;
617 while (PyDict_Next(d, &pos, &key, &value)) {
618 if (value != Py_None && PyUnicode_Check(key)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200619 if (PyUnicode_READ_CHAR(key, 0) != '_' ||
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200620 !_PyUnicode_EqualToASCIIString(key, "__builtins__"))
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000621 {
Victor Stinnerc96be812019-05-14 17:34:56 +0200622 if (verbose > 1) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200623 const char *s = PyUnicode_AsUTF8(key);
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000624 if (s != NULL)
625 PySys_WriteStderr("# clear[2] %s\n", s);
626 else
627 PyErr_Clear();
628 }
Serhiy Storchakac1a68322018-04-29 22:16:30 +0300629 if (PyDict_SetItem(d, key, Py_None) != 0) {
630 PyErr_WriteUnraisable(NULL);
631 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 }
633 }
634 }
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 /* Note: we leave __builtins__ in place, so that destructors
637 of non-global objects defined in this module can still use
638 builtins, in particularly 'None'. */
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000639
640}
641
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200642/*[clinic input]
643class module "PyModuleObject *" "&PyModule_Type"
644[clinic start generated code]*/
645/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3e35d4f708ecb6af]*/
646
647#include "clinic/moduleobject.c.h"
648
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000649/* Methods */
650
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200651/*[clinic input]
652module.__init__
653 name: unicode
654 doc: object = None
655
656Create a module object.
657
658The name must be a string; the optional doc argument can have any type.
659[clinic start generated code]*/
660
Tim Peters6d6c1a32001-08-02 04:15:00 +0000661static int
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200662module___init___impl(PyModuleObject *self, PyObject *name, PyObject *doc)
663/*[clinic end generated code: output=e7e721c26ce7aad7 input=57f9e177401e5e1e]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +0000664{
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200665 PyObject *dict = self->md_dict;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666 if (dict == NULL) {
667 dict = PyDict_New();
668 if (dict == NULL)
669 return -1;
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200670 self->md_dict = dict;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 }
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200672 if (module_init_dict(self, dict, name, doc) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 return -1;
674 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000675}
676
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000677static void
Fred Drakeee238b92000-07-09 06:03:25 +0000678module_dealloc(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000679{
Victor Stinnerda7933e2020-04-13 03:04:28 +0200680 int verbose = _Py_GetConfig()->verbose;
Victor Stinnerc96be812019-05-14 17:34:56 +0200681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 PyObject_GC_UnTrack(m);
Victor Stinnerc96be812019-05-14 17:34:56 +0200683 if (verbose && m->md_name) {
Serhiy Storchakad7c38732019-10-08 13:46:17 +0300684 PySys_FormatStderr("# destroy %U\n", m->md_name);
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200685 }
686 if (m->md_weaklist != NULL)
687 PyObject_ClearWeakRefs((PyObject *) m);
Victor Stinner5b1ef202020-03-17 18:09:46 +0100688 /* bpo-39824: Don't call m_free() if m_size > 0 and md_state=NULL */
689 if (m->md_def && m->md_def->m_free
690 && (m->md_def->m_size <= 0 || m->md_state != NULL))
691 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 m->md_def->m_free(m);
Victor Stinner5b1ef202020-03-17 18:09:46 +0100693 }
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200694 Py_XDECREF(m->md_dict);
695 Py_XDECREF(m->md_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 if (m->md_state != NULL)
Victor Stinner00d7abd2020-12-01 09:56:42 +0100697 PyMem_Free(m->md_state);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 Py_TYPE(m)->tp_free((PyObject *)m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000699}
700
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000701static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000702module_repr(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000703{
Victor Stinner81a7be32020-04-14 15:14:01 +0200704 PyInterpreterState *interp = _PyInterpreterState_GET();
Barry Warsaw2907fe62001-08-16 20:39:24 +0000705
Eric Snowb523f842013-11-22 09:05:39 -0700706 return PyObject_CallMethod(interp->importlib, "_module_repr", "O", m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000707}
708
Serhiy Storchaka3e429dc2018-10-30 13:19:51 +0200709/* Check if the "_initializing" attribute of the module spec is set to true.
710 Clear the exception and return 0 if spec is NULL.
711 */
712int
713_PyModuleSpec_IsInitializing(PyObject *spec)
714{
715 if (spec != NULL) {
716 _Py_IDENTIFIER(_initializing);
717 PyObject *value = _PyObject_GetAttrId(spec, &PyId__initializing);
718 if (value != NULL) {
719 int initializing = PyObject_IsTrue(value);
720 Py_DECREF(value);
721 if (initializing >= 0) {
722 return initializing;
723 }
724 }
725 }
726 PyErr_Clear();
727 return 0;
728}
729
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700730static PyObject*
Benjamin Peterson1184e262014-04-24 19:29:23 -0400731module_getattro(PyModuleObject *m, PyObject *name)
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700732{
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100733 PyObject *attr, *mod_name, *getattr;
Benjamin Peterson1184e262014-04-24 19:29:23 -0400734 attr = PyObject_GenericGetAttr((PyObject *)m, name);
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100735 if (attr || !PyErr_ExceptionMatches(PyExc_AttributeError)) {
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700736 return attr;
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100737 }
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700738 PyErr_Clear();
Benjamin Peterson1184e262014-04-24 19:29:23 -0400739 if (m->md_dict) {
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100740 _Py_IDENTIFIER(__getattr__);
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200741 getattr = _PyDict_GetItemIdWithError(m->md_dict, &PyId___getattr__);
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100742 if (getattr) {
Petr Viktorinffd97532020-02-11 17:46:57 +0100743 return PyObject_CallOneArg(getattr, name);
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100744 }
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200745 if (PyErr_Occurred()) {
746 return NULL;
747 }
748 mod_name = _PyDict_GetItemIdWithError(m->md_dict, &PyId___name__);
Oren Milman6db70332017-09-19 14:23:01 +0300749 if (mod_name && PyUnicode_Check(mod_name)) {
Serhiy Storchaka3e429dc2018-10-30 13:19:51 +0200750 Py_INCREF(mod_name);
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200751 PyObject *spec = _PyDict_GetItemIdWithError(m->md_dict, &PyId___spec__);
752 if (spec == NULL && PyErr_Occurred()) {
753 Py_DECREF(mod_name);
754 return NULL;
755 }
Serhiy Storchaka3e429dc2018-10-30 13:19:51 +0200756 Py_XINCREF(spec);
757 if (_PyModuleSpec_IsInitializing(spec)) {
758 PyErr_Format(PyExc_AttributeError,
759 "partially initialized "
760 "module '%U' has no attribute '%U' "
761 "(most likely due to a circular import)",
762 mod_name, name);
763 }
764 else {
765 PyErr_Format(PyExc_AttributeError,
766 "module '%U' has no attribute '%U'",
767 mod_name, name);
768 }
769 Py_XDECREF(spec);
770 Py_DECREF(mod_name);
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700771 return NULL;
772 }
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200773 else if (PyErr_Occurred()) {
774 return NULL;
775 }
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700776 }
777 PyErr_Format(PyExc_AttributeError,
778 "module has no attribute '%U'", name);
779 return NULL;
780}
781
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000782static int
783module_traverse(PyModuleObject *m, visitproc visit, void *arg)
784{
Victor Stinner5b1ef202020-03-17 18:09:46 +0100785 /* bpo-39824: Don't call m_traverse() if m_size > 0 and md_state=NULL */
786 if (m->md_def && m->md_def->m_traverse
787 && (m->md_def->m_size <= 0 || m->md_state != NULL))
788 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 int res = m->md_def->m_traverse((PyObject*)m, visit, arg);
790 if (res)
791 return res;
792 }
793 Py_VISIT(m->md_dict);
794 return 0;
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000795}
796
Martin v. Löwis1a214512008-06-11 05:26:20 +0000797static int
798module_clear(PyModuleObject *m)
799{
Victor Stinner5b1ef202020-03-17 18:09:46 +0100800 /* bpo-39824: Don't call m_clear() if m_size > 0 and md_state=NULL */
801 if (m->md_def && m->md_def->m_clear
802 && (m->md_def->m_size <= 0 || m->md_state != NULL))
803 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 int res = m->md_def->m_clear((PyObject*)m);
Serhiy Storchakad7c38732019-10-08 13:46:17 +0300805 if (PyErr_Occurred()) {
806 PySys_FormatStderr("Exception ignored in m_clear of module%s%V\n",
807 m->md_name ? " " : "",
808 m->md_name, "");
809 PyErr_WriteUnraisable(NULL);
810 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 if (res)
812 return res;
813 }
814 Py_CLEAR(m->md_dict);
815 return 0;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000816}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500818static PyObject *
819module_dir(PyObject *self, PyObject *args)
820{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200821 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200822 _Py_IDENTIFIER(__dir__);
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500823 PyObject *result = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200824 PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500825
826 if (dict != NULL) {
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100827 if (PyDict_Check(dict)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200828 PyObject *dirfunc = _PyDict_GetItemIdWithError(dict, &PyId___dir__);
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100829 if (dirfunc) {
830 result = _PyObject_CallNoArg(dirfunc);
831 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200832 else if (!PyErr_Occurred()) {
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100833 result = PyDict_Keys(dict);
834 }
835 }
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500836 else {
837 const char *name = PyModule_GetName(self);
838 if (name)
839 PyErr_Format(PyExc_TypeError,
840 "%.200s.__dict__ is not a dictionary",
841 name);
842 }
843 }
844
845 Py_XDECREF(dict);
846 return result;
847}
848
849static PyMethodDef module_methods[] = {
850 {"__dir__", module_dir, METH_NOARGS,
Benjamin Petersonc7284122011-05-24 12:46:15 -0500851 PyDoc_STR("__dir__() -> list\nspecialized dir() implementation")},
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500852 {0}
853};
854
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000855PyTypeObject PyModule_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 PyVarObject_HEAD_INIT(&PyType_Type, 0)
857 "module", /* tp_name */
Peter Eisentraut0e0bc4e2018-09-10 18:46:08 +0200858 sizeof(PyModuleObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 0, /* tp_itemsize */
860 (destructor)module_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200861 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 0, /* tp_getattr */
863 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200864 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 (reprfunc)module_repr, /* tp_repr */
866 0, /* tp_as_number */
867 0, /* tp_as_sequence */
868 0, /* tp_as_mapping */
869 0, /* tp_hash */
870 0, /* tp_call */
871 0, /* tp_str */
Benjamin Peterson1184e262014-04-24 19:29:23 -0400872 (getattrofunc)module_getattro, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 PyObject_GenericSetAttr, /* tp_setattro */
874 0, /* tp_as_buffer */
875 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
876 Py_TPFLAGS_BASETYPE, /* tp_flags */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200877 module___init____doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 (traverseproc)module_traverse, /* tp_traverse */
879 (inquiry)module_clear, /* tp_clear */
880 0, /* tp_richcompare */
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200881 offsetof(PyModuleObject, md_weaklist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 0, /* tp_iter */
883 0, /* tp_iternext */
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500884 module_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 module_members, /* tp_members */
886 0, /* tp_getset */
887 0, /* tp_base */
888 0, /* tp_dict */
889 0, /* tp_descr_get */
890 0, /* tp_descr_set */
891 offsetof(PyModuleObject, md_dict), /* tp_dictoffset */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200892 module___init__, /* tp_init */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 PyType_GenericAlloc, /* tp_alloc */
894 PyType_GenericNew, /* tp_new */
895 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000896};