blob: 9d6533257bb1214289213b4bde8f5c0e86dd775e [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
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000011 PyObject_HEAD
12 PyObject *md_dict;
13 struct PyModuleDef *md_def;
14 void *md_state;
Antoine Pitroudcedaf62013-07-31 23:14:08 +020015 PyObject *md_weaklist;
16 PyObject *md_name; /* for logging purposes after md_dict is cleared */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000017} PyModuleObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000018
Neil Schemenauerf23473f2001-10-21 22:28:58 +000019static PyMemberDef module_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000020 {"__dict__", T_OBJECT, offsetof(PyModuleObject, md_dict), READONLY},
21 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +000022};
23
Marcel Plchc2b0b122018-03-17 06:41:20 +010024
25/* Helper for sanity check for traverse not handling m_state == NULL
26 * Issue #32374 */
27#ifdef Py_DEBUG
28static int
29bad_traverse_test(PyObject *self, void *arg) {
30 assert(self != NULL);
31 return 0;
32}
33#endif
34
Nick Coghland5cacbb2015-05-23 22:24:10 +100035PyTypeObject PyModuleDef_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036 PyVarObject_HEAD_INIT(&PyType_Type, 0)
37 "moduledef", /* tp_name */
Peter Eisentraut0e0bc4e2018-09-10 18:46:08 +020038 sizeof(struct PyModuleDef), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000039 0, /* tp_itemsize */
Martin v. Löwis1a214512008-06-11 05:26:20 +000040};
41
42
Nick Coghland5cacbb2015-05-23 22:24:10 +100043PyObject*
44PyModuleDef_Init(struct PyModuleDef* def)
45{
46 if (PyType_Ready(&PyModuleDef_Type) < 0)
47 return NULL;
48 if (def->m_base.m_index == 0) {
49 max_module_number++;
50 Py_REFCNT(def) = 1;
51 Py_TYPE(def) = &PyModuleDef_Type;
52 def->m_base.m_index = max_module_number;
53 }
54 return (PyObject*)def;
55}
56
Brett Cannon4c14b5d2013-05-04 13:56:58 -040057static int
Antoine Pitroudcedaf62013-07-31 23:14:08 +020058module_init_dict(PyModuleObject *mod, PyObject *md_dict,
59 PyObject *name, PyObject *doc)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040060{
Benjamin Peterson027ce162014-04-24 19:39:18 -040061 _Py_IDENTIFIER(__name__);
62 _Py_IDENTIFIER(__doc__);
63 _Py_IDENTIFIER(__package__);
64 _Py_IDENTIFIER(__loader__);
65 _Py_IDENTIFIER(__spec__);
Serhiy Storchaka009b8112015-03-18 21:53:15 +020066
Brett Cannon4c14b5d2013-05-04 13:56:58 -040067 if (md_dict == NULL)
68 return -1;
69 if (doc == NULL)
70 doc = Py_None;
71
Benjamin Peterson027ce162014-04-24 19:39:18 -040072 if (_PyDict_SetItemId(md_dict, &PyId___name__, name) != 0)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040073 return -1;
Benjamin Peterson027ce162014-04-24 19:39:18 -040074 if (_PyDict_SetItemId(md_dict, &PyId___doc__, doc) != 0)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040075 return -1;
Benjamin Peterson027ce162014-04-24 19:39:18 -040076 if (_PyDict_SetItemId(md_dict, &PyId___package__, Py_None) != 0)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040077 return -1;
Benjamin Peterson027ce162014-04-24 19:39:18 -040078 if (_PyDict_SetItemId(md_dict, &PyId___loader__, Py_None) != 0)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040079 return -1;
Benjamin Peterson027ce162014-04-24 19:39:18 -040080 if (_PyDict_SetItemId(md_dict, &PyId___spec__, Py_None) != 0)
Eric Snowb523f842013-11-22 09:05:39 -070081 return -1;
Antoine Pitroudcedaf62013-07-31 23:14:08 +020082 if (PyUnicode_CheckExact(name)) {
83 Py_INCREF(name);
Serhiy Storchaka48842712016-04-06 09:45:48 +030084 Py_XSETREF(mod->md_name, name);
Antoine Pitroudcedaf62013-07-31 23:14:08 +020085 }
Brett Cannon4c14b5d2013-05-04 13:56:58 -040086
87 return 0;
88}
89
90
Guido van Rossumc0b618a1997-05-02 03:12:38 +000091PyObject *
Victor Stinner0639b562011-03-04 12:57:07 +000092PyModule_NewObject(PyObject *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 PyModuleObject *m;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 m = PyObject_GC_New(PyModuleObject, &PyModule_Type);
96 if (m == NULL)
97 return NULL;
98 m->md_def = NULL;
99 m->md_state = NULL;
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200100 m->md_weaklist = NULL;
101 m->md_name = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 m->md_dict = PyDict_New();
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200103 if (module_init_dict(m, m->md_dict, name, NULL) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 PyObject_GC_Track(m);
106 return (PyObject *)m;
Guido van Rossumc45611d1993-11-17 22:58:56 +0000107
108 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 Py_DECREF(m);
110 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000111}
112
Martin v. Löwis1a214512008-06-11 05:26:20 +0000113PyObject *
Victor Stinner0639b562011-03-04 12:57:07 +0000114PyModule_New(const char *name)
115{
116 PyObject *nameobj, *module;
117 nameobj = PyUnicode_FromString(name);
118 if (nameobj == NULL)
119 return NULL;
120 module = PyModule_NewObject(nameobj);
121 Py_DECREF(nameobj);
122 return module;
123}
124
Nick Coghland5cacbb2015-05-23 22:24:10 +1000125/* Check API/ABI version
126 * Issues a warning on mismatch, which is usually not fatal.
127 * Returns 0 if an exception is raised.
128 */
129static int
130check_api_version(const char *name, int module_api_version)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000131{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000132 if (module_api_version != PYTHON_API_VERSION && module_api_version != PYTHON_ABI_VERSION) {
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000133 int err;
134 err = PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
135 "Python C API version mismatch for module %.100s: "
136 "This Python has API version %d, module %.100s has version %d.",
137 name,
138 PYTHON_API_VERSION, name, module_api_version);
139 if (err)
Nick Coghland5cacbb2015-05-23 22:24:10 +1000140 return 0;
141 }
142 return 1;
143}
144
Nick Coghlan8682f572016-08-21 17:41:56 +1000145static int
146_add_methods_to_object(PyObject *module, PyObject *name, PyMethodDef *functions)
147{
148 PyObject *func;
149 PyMethodDef *fdef;
150
151 for (fdef = functions; fdef->ml_name != NULL; fdef++) {
152 if ((fdef->ml_flags & METH_CLASS) ||
153 (fdef->ml_flags & METH_STATIC)) {
154 PyErr_SetString(PyExc_ValueError,
155 "module functions cannot set"
156 " METH_CLASS or METH_STATIC");
157 return -1;
158 }
159 func = PyCFunction_NewEx(fdef, (PyObject*)module, name);
160 if (func == NULL) {
161 return -1;
162 }
163 if (PyObject_SetAttrString(module, fdef->ml_name, func) != 0) {
164 Py_DECREF(func);
165 return -1;
166 }
167 Py_DECREF(func);
168 }
169
170 return 0;
171}
172
Nick Coghland5cacbb2015-05-23 22:24:10 +1000173PyObject *
174PyModule_Create2(struct PyModuleDef* module, int module_api_version)
175{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200176 if (!_PyImport_IsInitialized(_PyInterpreterState_Get()))
Eric Snowd393c1b2017-09-14 12:18:12 -0600177 Py_FatalError("Python import machinery not initialized");
178 return _PyModule_CreateInitialized(module, module_api_version);
179}
180
181PyObject *
182_PyModule_CreateInitialized(struct PyModuleDef* module, int module_api_version)
183{
Nick Coghland5cacbb2015-05-23 22:24:10 +1000184 const char* name;
185 PyModuleObject *m;
Eric Snowd393c1b2017-09-14 12:18:12 -0600186
Nick Coghland5cacbb2015-05-23 22:24:10 +1000187 if (!PyModuleDef_Init(module))
188 return NULL;
189 name = module->m_name;
190 if (!check_api_version(name, module_api_version)) {
191 return NULL;
192 }
193 if (module->m_slots) {
194 PyErr_Format(
195 PyExc_SystemError,
196 "module %s: PyModule_Create is incompatible with m_slots", name);
197 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 }
199 /* Make sure name is fully qualified.
Martin v. Löwis1a214512008-06-11 05:26:20 +0000200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 This is a bit of a hack: when the shared library is loaded,
202 the module name is "package.module", but the module calls
203 PyModule_Create*() with just "module" for the name. The shared
204 library loader squirrels away the true name of the module in
205 _Py_PackageContext, and PyModule_Create*() will substitute this
206 (if the name actually matches).
207 */
208 if (_Py_PackageContext != NULL) {
Serhiy Storchakab57d9ea2016-11-21 10:25:54 +0200209 const char *p = strrchr(_Py_PackageContext, '.');
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 if (p != NULL && strcmp(module->m_name, p+1) == 0) {
211 name = _Py_PackageContext;
212 _Py_PackageContext = NULL;
213 }
214 }
215 if ((m = (PyModuleObject*)PyModule_New(name)) == NULL)
216 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 if (module->m_size > 0) {
219 m->md_state = PyMem_MALLOC(module->m_size);
220 if (!m->md_state) {
221 PyErr_NoMemory();
222 Py_DECREF(m);
223 return NULL;
224 }
225 memset(m->md_state, 0, module->m_size);
226 }
227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 if (module->m_methods != NULL) {
Nick Coghland5cacbb2015-05-23 22:24:10 +1000229 if (PyModule_AddFunctions((PyObject *) m, module->m_methods) != 0) {
Meador Inge29e49d62012-07-19 13:45:43 -0500230 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 return NULL;
Meador Inge29e49d62012-07-19 13:45:43 -0500232 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 }
234 if (module->m_doc != NULL) {
Nick Coghland5cacbb2015-05-23 22:24:10 +1000235 if (PyModule_SetDocString((PyObject *) m, module->m_doc) != 0) {
Meador Inge29e49d62012-07-19 13:45:43 -0500236 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 return NULL;
238 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 }
240 m->md_def = module;
241 return (PyObject*)m;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000242}
243
Nick Coghland5cacbb2015-05-23 22:24:10 +1000244PyObject *
245PyModule_FromDefAndSpec2(struct PyModuleDef* def, PyObject *spec, int module_api_version)
246{
247 PyModuleDef_Slot* cur_slot;
248 PyObject *(*create)(PyObject *, PyModuleDef*) = NULL;
249 PyObject *nameobj;
250 PyObject *m = NULL;
251 int has_execution_slots = 0;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200252 const char *name;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000253 int ret;
254
255 PyModuleDef_Init(def);
256
257 nameobj = PyObject_GetAttrString(spec, "name");
258 if (nameobj == NULL) {
259 return NULL;
260 }
261 name = PyUnicode_AsUTF8(nameobj);
262 if (name == NULL) {
263 goto error;
264 }
265
266 if (!check_api_version(name, module_api_version)) {
267 goto error;
268 }
269
270 if (def->m_size < 0) {
271 PyErr_Format(
272 PyExc_SystemError,
273 "module %s: m_size may not be negative for multi-phase initialization",
274 name);
275 goto error;
276 }
277
278 for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) {
279 if (cur_slot->slot == Py_mod_create) {
280 if (create) {
281 PyErr_Format(
282 PyExc_SystemError,
283 "module %s has multiple create slots",
284 name);
285 goto error;
286 }
287 create = cur_slot->value;
288 } else if (cur_slot->slot < 0 || cur_slot->slot > _Py_mod_LAST_SLOT) {
289 PyErr_Format(
290 PyExc_SystemError,
291 "module %s uses unknown slot ID %i",
292 name, cur_slot->slot);
293 goto error;
294 } else {
295 has_execution_slots = 1;
296 }
297 }
298
299 if (create) {
300 m = create(spec, def);
301 if (m == NULL) {
302 if (!PyErr_Occurred()) {
303 PyErr_Format(
304 PyExc_SystemError,
305 "creation of module %s failed without setting an exception",
306 name);
307 }
308 goto error;
309 } else {
310 if (PyErr_Occurred()) {
311 PyErr_Format(PyExc_SystemError,
312 "creation of module %s raised unreported exception",
313 name);
314 goto error;
315 }
316 }
317 } else {
Nick Coghlan8682f572016-08-21 17:41:56 +1000318 m = PyModule_NewObject(nameobj);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000319 if (m == NULL) {
320 goto error;
321 }
322 }
323
324 if (PyModule_Check(m)) {
325 ((PyModuleObject*)m)->md_state = NULL;
326 ((PyModuleObject*)m)->md_def = def;
327 } else {
328 if (def->m_size > 0 || def->m_traverse || def->m_clear || def->m_free) {
329 PyErr_Format(
330 PyExc_SystemError,
331 "module %s is not a module object, but requests module state",
332 name);
333 goto error;
334 }
335 if (has_execution_slots) {
336 PyErr_Format(
337 PyExc_SystemError,
338 "module %s specifies execution slots, but did not create "
339 "a ModuleType instance",
340 name);
341 goto error;
342 }
343 }
344
345 if (def->m_methods != NULL) {
Nick Coghlan8682f572016-08-21 17:41:56 +1000346 ret = _add_methods_to_object(m, nameobj, def->m_methods);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000347 if (ret != 0) {
348 goto error;
349 }
350 }
351
352 if (def->m_doc != NULL) {
353 ret = PyModule_SetDocString(m, def->m_doc);
354 if (ret != 0) {
355 goto error;
356 }
357 }
358
Marcel Plchc2b0b122018-03-17 06:41:20 +0100359 /* Sanity check for traverse not handling m_state == NULL
360 * This doesn't catch all possible cases, but in many cases it should
361 * make many cases of invalid code crash or raise Valgrind issues
362 * sooner than they would otherwise.
363 * Issue #32374 */
364#ifdef Py_DEBUG
365 if (def->m_traverse != NULL) {
366 def->m_traverse(m, bad_traverse_test, NULL);
367 }
368#endif
Nick Coghlana48db2b2015-05-24 01:03:46 +1000369 Py_DECREF(nameobj);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000370 return m;
371
372error:
373 Py_DECREF(nameobj);
374 Py_XDECREF(m);
375 return NULL;
376}
377
378int
379PyModule_ExecDef(PyObject *module, PyModuleDef *def)
380{
381 PyModuleDef_Slot *cur_slot;
382 const char *name;
383 int ret;
384
385 name = PyModule_GetName(module);
386 if (name == NULL) {
387 return -1;
388 }
389
Nick Coghlan8682f572016-08-21 17:41:56 +1000390 if (def->m_size >= 0) {
Nick Coghland5cacbb2015-05-23 22:24:10 +1000391 PyModuleObject *md = (PyModuleObject*)module;
392 if (md->md_state == NULL) {
393 /* Always set a state pointer; this serves as a marker to skip
394 * multiple initialization (importlib.reload() is no-op) */
395 md->md_state = PyMem_MALLOC(def->m_size);
396 if (!md->md_state) {
397 PyErr_NoMemory();
398 return -1;
399 }
400 memset(md->md_state, 0, def->m_size);
401 }
402 }
403
404 if (def->m_slots == NULL) {
405 return 0;
406 }
407
408 for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) {
409 switch (cur_slot->slot) {
410 case Py_mod_create:
Serhiy Storchaka333ad922016-09-26 23:14:44 +0300411 /* handled in PyModule_FromDefAndSpec2 */
Nick Coghland5cacbb2015-05-23 22:24:10 +1000412 break;
413 case Py_mod_exec:
414 ret = ((int (*)(PyObject *))cur_slot->value)(module);
415 if (ret != 0) {
416 if (!PyErr_Occurred()) {
417 PyErr_Format(
418 PyExc_SystemError,
419 "execution of module %s failed without setting an exception",
420 name);
421 }
422 return -1;
423 }
424 if (PyErr_Occurred()) {
425 PyErr_Format(
426 PyExc_SystemError,
427 "execution of module %s raised unreported exception",
428 name);
429 return -1;
430 }
431 break;
432 default:
433 PyErr_Format(
434 PyExc_SystemError,
435 "module %s initialized with unknown slot %i",
436 name, cur_slot->slot);
437 return -1;
438 }
439 }
440 return 0;
441}
442
443int
444PyModule_AddFunctions(PyObject *m, PyMethodDef *functions)
445{
Nick Coghlan8682f572016-08-21 17:41:56 +1000446 int res;
447 PyObject *name = PyModule_GetNameObject(m);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000448 if (name == NULL) {
449 return -1;
450 }
451
Nick Coghlan8682f572016-08-21 17:41:56 +1000452 res = _add_methods_to_object(m, name, functions);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000453 Py_DECREF(name);
Nick Coghlan8682f572016-08-21 17:41:56 +1000454 return res;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000455}
456
457int
458PyModule_SetDocString(PyObject *m, const char *doc)
459{
460 PyObject *v;
461 _Py_IDENTIFIER(__doc__);
462
463 v = PyUnicode_FromString(doc);
464 if (v == NULL || _PyObject_SetAttrId(m, &PyId___doc__, v) != 0) {
465 Py_XDECREF(v);
466 return -1;
467 }
468 Py_DECREF(v);
469 return 0;
470}
Martin v. Löwis1a214512008-06-11 05:26:20 +0000471
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000472PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000473PyModule_GetDict(PyObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000474{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 PyObject *d;
476 if (!PyModule_Check(m)) {
477 PyErr_BadInternalCall();
478 return NULL;
479 }
480 d = ((PyModuleObject *)m) -> md_dict;
Berker Peksag7fbce562016-08-19 12:00:13 +0300481 assert(d != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000483}
484
Victor Stinnerbd475112011-02-23 00:21:43 +0000485PyObject*
486PyModule_GetNameObject(PyObject *m)
Guido van Rossum0558a201990-10-26 15:00:11 +0000487{
Benjamin Peterson027ce162014-04-24 19:39:18 -0400488 _Py_IDENTIFIER(__name__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 PyObject *d;
Victor Stinnerbd475112011-02-23 00:21:43 +0000490 PyObject *name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 if (!PyModule_Check(m)) {
492 PyErr_BadArgument();
493 return NULL;
494 }
495 d = ((PyModuleObject *)m)->md_dict;
496 if (d == NULL ||
Benjamin Peterson027ce162014-04-24 19:39:18 -0400497 (name = _PyDict_GetItemId(d, &PyId___name__)) == NULL ||
Victor Stinnerbd475112011-02-23 00:21:43 +0000498 !PyUnicode_Check(name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 {
500 PyErr_SetString(PyExc_SystemError, "nameless module");
501 return NULL;
502 }
Victor Stinnerbd475112011-02-23 00:21:43 +0000503 Py_INCREF(name);
504 return name;
505}
506
507const char *
508PyModule_GetName(PyObject *m)
509{
510 PyObject *name = PyModule_GetNameObject(m);
511 if (name == NULL)
512 return NULL;
513 Py_DECREF(name); /* module dict has still a reference */
Serhiy Storchaka06515832016-11-20 09:13:07 +0200514 return PyUnicode_AsUTF8(name);
Guido van Rossum0558a201990-10-26 15:00:11 +0000515}
516
Victor Stinner6c00c142010-08-17 23:37:11 +0000517PyObject*
518PyModule_GetFilenameObject(PyObject *m)
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000519{
Benjamin Peterson027ce162014-04-24 19:39:18 -0400520 _Py_IDENTIFIER(__file__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 PyObject *d;
522 PyObject *fileobj;
523 if (!PyModule_Check(m)) {
524 PyErr_BadArgument();
525 return NULL;
526 }
527 d = ((PyModuleObject *)m)->md_dict;
528 if (d == NULL ||
Benjamin Peterson027ce162014-04-24 19:39:18 -0400529 (fileobj = _PyDict_GetItemId(d, &PyId___file__)) == NULL ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000530 !PyUnicode_Check(fileobj))
531 {
532 PyErr_SetString(PyExc_SystemError, "module filename missing");
533 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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 /* First, clear only names starting with a single underscore */
594 pos = 0;
595 while (PyDict_Next(d, &pos, &key, &value)) {
596 if (value != Py_None && PyUnicode_Check(key)) {
Brett Cannon62228db2012-04-29 14:38:11 -0400597 if (PyUnicode_READ_CHAR(key, 0) == '_' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200598 PyUnicode_READ_CHAR(key, 1) != '_') {
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000599 if (Py_VerboseFlag > 1) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200600 const char *s = PyUnicode_AsUTF8(key);
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000601 if (s != NULL)
602 PySys_WriteStderr("# clear[1] %s\n", s);
603 else
604 PyErr_Clear();
605 }
Serhiy Storchakac1a68322018-04-29 22:16:30 +0300606 if (PyDict_SetItem(d, key, Py_None) != 0) {
607 PyErr_WriteUnraisable(NULL);
608 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 }
610 }
611 }
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 /* Next, clear all names except for __builtins__ */
614 pos = 0;
615 while (PyDict_Next(d, &pos, &key, &value)) {
616 if (value != Py_None && PyUnicode_Check(key)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200617 if (PyUnicode_READ_CHAR(key, 0) != '_' ||
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200618 !_PyUnicode_EqualToASCIIString(key, "__builtins__"))
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000619 {
620 if (Py_VerboseFlag > 1) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200621 const char *s = PyUnicode_AsUTF8(key);
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000622 if (s != NULL)
623 PySys_WriteStderr("# clear[2] %s\n", s);
624 else
625 PyErr_Clear();
626 }
Serhiy Storchakac1a68322018-04-29 22:16:30 +0300627 if (PyDict_SetItem(d, key, Py_None) != 0) {
628 PyErr_WriteUnraisable(NULL);
629 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 }
631 }
632 }
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 /* Note: we leave __builtins__ in place, so that destructors
635 of non-global objects defined in this module can still use
636 builtins, in particularly 'None'. */
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000637
638}
639
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200640/*[clinic input]
641class module "PyModuleObject *" "&PyModule_Type"
642[clinic start generated code]*/
643/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3e35d4f708ecb6af]*/
644
645#include "clinic/moduleobject.c.h"
646
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000647/* Methods */
648
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200649/*[clinic input]
650module.__init__
651 name: unicode
652 doc: object = None
653
654Create a module object.
655
656The name must be a string; the optional doc argument can have any type.
657[clinic start generated code]*/
658
Tim Peters6d6c1a32001-08-02 04:15:00 +0000659static int
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200660module___init___impl(PyModuleObject *self, PyObject *name, PyObject *doc)
661/*[clinic end generated code: output=e7e721c26ce7aad7 input=57f9e177401e5e1e]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +0000662{
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200663 PyObject *dict = self->md_dict;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 if (dict == NULL) {
665 dict = PyDict_New();
666 if (dict == NULL)
667 return -1;
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200668 self->md_dict = dict;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 }
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200670 if (module_init_dict(self, dict, name, doc) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 return -1;
672 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000673}
674
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000675static void
Fred Drakeee238b92000-07-09 06:03:25 +0000676module_dealloc(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000677{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000678 PyObject_GC_UnTrack(m);
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200679 if (Py_VerboseFlag && m->md_name) {
680 PySys_FormatStderr("# destroy %S\n", m->md_name);
681 }
682 if (m->md_weaklist != NULL)
683 PyObject_ClearWeakRefs((PyObject *) m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 if (m->md_def && m->md_def->m_free)
685 m->md_def->m_free(m);
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200686 Py_XDECREF(m->md_dict);
687 Py_XDECREF(m->md_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 if (m->md_state != NULL)
689 PyMem_FREE(m->md_state);
690 Py_TYPE(m)->tp_free((PyObject *)m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000691}
692
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000693static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000694module_repr(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000695{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200696 PyInterpreterState *interp = _PyInterpreterState_Get();
Barry Warsaw2907fe62001-08-16 20:39:24 +0000697
Eric Snowb523f842013-11-22 09:05:39 -0700698 return PyObject_CallMethod(interp->importlib, "_module_repr", "O", m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000699}
700
Serhiy Storchaka3e429dc2018-10-30 13:19:51 +0200701/* Check if the "_initializing" attribute of the module spec is set to true.
702 Clear the exception and return 0 if spec is NULL.
703 */
704int
705_PyModuleSpec_IsInitializing(PyObject *spec)
706{
707 if (spec != NULL) {
708 _Py_IDENTIFIER(_initializing);
709 PyObject *value = _PyObject_GetAttrId(spec, &PyId__initializing);
710 if (value != NULL) {
711 int initializing = PyObject_IsTrue(value);
712 Py_DECREF(value);
713 if (initializing >= 0) {
714 return initializing;
715 }
716 }
717 }
718 PyErr_Clear();
719 return 0;
720}
721
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700722static PyObject*
Benjamin Peterson1184e262014-04-24 19:29:23 -0400723module_getattro(PyModuleObject *m, PyObject *name)
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700724{
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100725 PyObject *attr, *mod_name, *getattr;
Benjamin Peterson1184e262014-04-24 19:29:23 -0400726 attr = PyObject_GenericGetAttr((PyObject *)m, name);
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100727 if (attr || !PyErr_ExceptionMatches(PyExc_AttributeError)) {
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700728 return attr;
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100729 }
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700730 PyErr_Clear();
Benjamin Peterson1184e262014-04-24 19:29:23 -0400731 if (m->md_dict) {
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100732 _Py_IDENTIFIER(__getattr__);
733 getattr = _PyDict_GetItemId(m->md_dict, &PyId___getattr__);
734 if (getattr) {
735 PyObject* stack[1] = {name};
736 return _PyObject_FastCall(getattr, stack, 1);
737 }
Benjamin Peterson027ce162014-04-24 19:39:18 -0400738 _Py_IDENTIFIER(__name__);
739 mod_name = _PyDict_GetItemId(m->md_dict, &PyId___name__);
Oren Milman6db70332017-09-19 14:23:01 +0300740 if (mod_name && PyUnicode_Check(mod_name)) {
Serhiy Storchaka3e429dc2018-10-30 13:19:51 +0200741 _Py_IDENTIFIER(__spec__);
742 Py_INCREF(mod_name);
743 PyObject *spec = _PyDict_GetItemId(m->md_dict, &PyId___spec__);
744 Py_XINCREF(spec);
745 if (_PyModuleSpec_IsInitializing(spec)) {
746 PyErr_Format(PyExc_AttributeError,
747 "partially initialized "
748 "module '%U' has no attribute '%U' "
749 "(most likely due to a circular import)",
750 mod_name, name);
751 }
752 else {
753 PyErr_Format(PyExc_AttributeError,
754 "module '%U' has no attribute '%U'",
755 mod_name, name);
756 }
757 Py_XDECREF(spec);
758 Py_DECREF(mod_name);
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700759 return NULL;
760 }
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700761 }
762 PyErr_Format(PyExc_AttributeError,
763 "module has no attribute '%U'", name);
764 return NULL;
765}
766
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000767static int
768module_traverse(PyModuleObject *m, visitproc visit, void *arg)
769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 if (m->md_def && m->md_def->m_traverse) {
771 int res = m->md_def->m_traverse((PyObject*)m, visit, arg);
772 if (res)
773 return res;
774 }
775 Py_VISIT(m->md_dict);
776 return 0;
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000777}
778
Martin v. Löwis1a214512008-06-11 05:26:20 +0000779static int
780module_clear(PyModuleObject *m)
781{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 if (m->md_def && m->md_def->m_clear) {
783 int res = m->md_def->m_clear((PyObject*)m);
784 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 */
834 0, /* tp_print */
835 0, /* tp_getattr */
836 0, /* tp_setattr */
837 0, /* tp_reserved */
838 (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};