blob: 92f97e6dd48502da941c818c9a939384d4df7103 [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
Victor Stinner331a6a52019-05-27 16:39:22 +0200593 int verbose = _PyInterpreterState_GET_UNSAFE()->config.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 Stinner331a6a52019-05-27 16:39:22 +0200680 int verbose = _PyInterpreterState_GET_UNSAFE()->config.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) {
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200684 PySys_FormatStderr("# destroy %S\n", m->md_name);
685 }
686 if (m->md_weaklist != NULL)
687 PyObject_ClearWeakRefs((PyObject *) m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 if (m->md_def && m->md_def->m_free)
689 m->md_def->m_free(m);
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200690 Py_XDECREF(m->md_dict);
691 Py_XDECREF(m->md_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 if (m->md_state != NULL)
693 PyMem_FREE(m->md_state);
694 Py_TYPE(m)->tp_free((PyObject *)m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000695}
696
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000697static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000698module_repr(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000699{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200700 PyInterpreterState *interp = _PyInterpreterState_Get();
Barry Warsaw2907fe62001-08-16 20:39:24 +0000701
Eric Snowb523f842013-11-22 09:05:39 -0700702 return PyObject_CallMethod(interp->importlib, "_module_repr", "O", m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000703}
704
Serhiy Storchaka3e429dc2018-10-30 13:19:51 +0200705/* Check if the "_initializing" attribute of the module spec is set to true.
706 Clear the exception and return 0 if spec is NULL.
707 */
708int
709_PyModuleSpec_IsInitializing(PyObject *spec)
710{
711 if (spec != NULL) {
712 _Py_IDENTIFIER(_initializing);
713 PyObject *value = _PyObject_GetAttrId(spec, &PyId__initializing);
714 if (value != NULL) {
715 int initializing = PyObject_IsTrue(value);
716 Py_DECREF(value);
717 if (initializing >= 0) {
718 return initializing;
719 }
720 }
721 }
722 PyErr_Clear();
723 return 0;
724}
725
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700726static PyObject*
Benjamin Peterson1184e262014-04-24 19:29:23 -0400727module_getattro(PyModuleObject *m, PyObject *name)
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700728{
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100729 PyObject *attr, *mod_name, *getattr;
Benjamin Peterson1184e262014-04-24 19:29:23 -0400730 attr = PyObject_GenericGetAttr((PyObject *)m, name);
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100731 if (attr || !PyErr_ExceptionMatches(PyExc_AttributeError)) {
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700732 return attr;
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100733 }
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700734 PyErr_Clear();
Benjamin Peterson1184e262014-04-24 19:29:23 -0400735 if (m->md_dict) {
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100736 _Py_IDENTIFIER(__getattr__);
737 getattr = _PyDict_GetItemId(m->md_dict, &PyId___getattr__);
738 if (getattr) {
Jeroen Demeyer196a5302019-07-04 12:31:34 +0200739 return _PyObject_CallOneArg(getattr, name);
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100740 }
Benjamin Peterson027ce162014-04-24 19:39:18 -0400741 _Py_IDENTIFIER(__name__);
742 mod_name = _PyDict_GetItemId(m->md_dict, &PyId___name__);
Oren Milman6db70332017-09-19 14:23:01 +0300743 if (mod_name && PyUnicode_Check(mod_name)) {
Serhiy Storchaka3e429dc2018-10-30 13:19:51 +0200744 _Py_IDENTIFIER(__spec__);
745 Py_INCREF(mod_name);
746 PyObject *spec = _PyDict_GetItemId(m->md_dict, &PyId___spec__);
747 Py_XINCREF(spec);
748 if (_PyModuleSpec_IsInitializing(spec)) {
749 PyErr_Format(PyExc_AttributeError,
750 "partially initialized "
751 "module '%U' has no attribute '%U' "
752 "(most likely due to a circular import)",
753 mod_name, name);
754 }
755 else {
756 PyErr_Format(PyExc_AttributeError,
757 "module '%U' has no attribute '%U'",
758 mod_name, name);
759 }
760 Py_XDECREF(spec);
761 Py_DECREF(mod_name);
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700762 return NULL;
763 }
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700764 }
765 PyErr_Format(PyExc_AttributeError,
766 "module has no attribute '%U'", name);
767 return NULL;
768}
769
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000770static int
771module_traverse(PyModuleObject *m, visitproc visit, void *arg)
772{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 if (m->md_def && m->md_def->m_traverse) {
774 int res = m->md_def->m_traverse((PyObject*)m, visit, arg);
775 if (res)
776 return res;
777 }
778 Py_VISIT(m->md_dict);
779 return 0;
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000780}
781
Martin v. Löwis1a214512008-06-11 05:26:20 +0000782static int
783module_clear(PyModuleObject *m)
784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 if (m->md_def && m->md_def->m_clear) {
786 int res = m->md_def->m_clear((PyObject*)m);
787 if (res)
788 return res;
789 }
790 Py_CLEAR(m->md_dict);
791 return 0;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000792}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500794static PyObject *
795module_dir(PyObject *self, PyObject *args)
796{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200797 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200798 _Py_IDENTIFIER(__dir__);
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500799 PyObject *result = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200800 PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500801
802 if (dict != NULL) {
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100803 if (PyDict_Check(dict)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200804 PyObject *dirfunc = _PyDict_GetItemIdWithError(dict, &PyId___dir__);
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100805 if (dirfunc) {
806 result = _PyObject_CallNoArg(dirfunc);
807 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200808 else if (!PyErr_Occurred()) {
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100809 result = PyDict_Keys(dict);
810 }
811 }
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500812 else {
813 const char *name = PyModule_GetName(self);
814 if (name)
815 PyErr_Format(PyExc_TypeError,
816 "%.200s.__dict__ is not a dictionary",
817 name);
818 }
819 }
820
821 Py_XDECREF(dict);
822 return result;
823}
824
825static PyMethodDef module_methods[] = {
826 {"__dir__", module_dir, METH_NOARGS,
Benjamin Petersonc7284122011-05-24 12:46:15 -0500827 PyDoc_STR("__dir__() -> list\nspecialized dir() implementation")},
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500828 {0}
829};
830
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000831PyTypeObject PyModule_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 PyVarObject_HEAD_INIT(&PyType_Type, 0)
833 "module", /* tp_name */
Peter Eisentraut0e0bc4e2018-09-10 18:46:08 +0200834 sizeof(PyModuleObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 0, /* tp_itemsize */
836 (destructor)module_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200837 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 0, /* tp_getattr */
839 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200840 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 (reprfunc)module_repr, /* tp_repr */
842 0, /* tp_as_number */
843 0, /* tp_as_sequence */
844 0, /* tp_as_mapping */
845 0, /* tp_hash */
846 0, /* tp_call */
847 0, /* tp_str */
Benjamin Peterson1184e262014-04-24 19:29:23 -0400848 (getattrofunc)module_getattro, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 PyObject_GenericSetAttr, /* tp_setattro */
850 0, /* tp_as_buffer */
851 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
852 Py_TPFLAGS_BASETYPE, /* tp_flags */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200853 module___init____doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 (traverseproc)module_traverse, /* tp_traverse */
855 (inquiry)module_clear, /* tp_clear */
856 0, /* tp_richcompare */
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200857 offsetof(PyModuleObject, md_weaklist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 0, /* tp_iter */
859 0, /* tp_iternext */
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500860 module_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 module_members, /* tp_members */
862 0, /* tp_getset */
863 0, /* tp_base */
864 0, /* tp_dict */
865 0, /* tp_descr_get */
866 0, /* tp_descr_set */
867 offsetof(PyModuleObject, md_dict), /* tp_dictoffset */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200868 module___init__, /* tp_init */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 PyType_GenericAlloc, /* tp_alloc */
870 PyType_GenericNew, /* tp_new */
871 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000872};