blob: 5fab06d31df0365ecf7b4d717df310eb5f9df432 [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"
Eric Snow2ebc5ce2017-09-07 23:51:28 -06005#include "internal/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
Nick Coghland5cacbb2015-05-23 22:24:10 +100024PyTypeObject PyModuleDef_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000025 PyVarObject_HEAD_INIT(&PyType_Type, 0)
26 "moduledef", /* tp_name */
27 sizeof(struct PyModuleDef), /* tp_size */
28 0, /* tp_itemsize */
Martin v. Löwis1a214512008-06-11 05:26:20 +000029};
30
31
Nick Coghland5cacbb2015-05-23 22:24:10 +100032PyObject*
33PyModuleDef_Init(struct PyModuleDef* def)
34{
35 if (PyType_Ready(&PyModuleDef_Type) < 0)
36 return NULL;
37 if (def->m_base.m_index == 0) {
38 max_module_number++;
39 Py_REFCNT(def) = 1;
40 Py_TYPE(def) = &PyModuleDef_Type;
41 def->m_base.m_index = max_module_number;
42 }
43 return (PyObject*)def;
44}
45
Brett Cannon4c14b5d2013-05-04 13:56:58 -040046static int
Antoine Pitroudcedaf62013-07-31 23:14:08 +020047module_init_dict(PyModuleObject *mod, PyObject *md_dict,
48 PyObject *name, PyObject *doc)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040049{
Benjamin Peterson027ce162014-04-24 19:39:18 -040050 _Py_IDENTIFIER(__name__);
51 _Py_IDENTIFIER(__doc__);
52 _Py_IDENTIFIER(__package__);
53 _Py_IDENTIFIER(__loader__);
54 _Py_IDENTIFIER(__spec__);
Serhiy Storchaka009b8112015-03-18 21:53:15 +020055
Brett Cannon4c14b5d2013-05-04 13:56:58 -040056 if (md_dict == NULL)
57 return -1;
58 if (doc == NULL)
59 doc = Py_None;
60
Benjamin Peterson027ce162014-04-24 19:39:18 -040061 if (_PyDict_SetItemId(md_dict, &PyId___name__, name) != 0)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040062 return -1;
Benjamin Peterson027ce162014-04-24 19:39:18 -040063 if (_PyDict_SetItemId(md_dict, &PyId___doc__, doc) != 0)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040064 return -1;
Benjamin Peterson027ce162014-04-24 19:39:18 -040065 if (_PyDict_SetItemId(md_dict, &PyId___package__, Py_None) != 0)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040066 return -1;
Benjamin Peterson027ce162014-04-24 19:39:18 -040067 if (_PyDict_SetItemId(md_dict, &PyId___loader__, Py_None) != 0)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040068 return -1;
Benjamin Peterson027ce162014-04-24 19:39:18 -040069 if (_PyDict_SetItemId(md_dict, &PyId___spec__, Py_None) != 0)
Eric Snowb523f842013-11-22 09:05:39 -070070 return -1;
Antoine Pitroudcedaf62013-07-31 23:14:08 +020071 if (PyUnicode_CheckExact(name)) {
72 Py_INCREF(name);
Serhiy Storchaka48842712016-04-06 09:45:48 +030073 Py_XSETREF(mod->md_name, name);
Antoine Pitroudcedaf62013-07-31 23:14:08 +020074 }
Brett Cannon4c14b5d2013-05-04 13:56:58 -040075
76 return 0;
77}
78
79
Guido van Rossumc0b618a1997-05-02 03:12:38 +000080PyObject *
Victor Stinner0639b562011-03-04 12:57:07 +000081PyModule_NewObject(PyObject *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000082{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 PyModuleObject *m;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 m = PyObject_GC_New(PyModuleObject, &PyModule_Type);
85 if (m == NULL)
86 return NULL;
87 m->md_def = NULL;
88 m->md_state = NULL;
Antoine Pitroudcedaf62013-07-31 23:14:08 +020089 m->md_weaklist = NULL;
90 m->md_name = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 m->md_dict = PyDict_New();
Antoine Pitroudcedaf62013-07-31 23:14:08 +020092 if (module_init_dict(m, m->md_dict, name, NULL) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 PyObject_GC_Track(m);
95 return (PyObject *)m;
Guido van Rossumc45611d1993-11-17 22:58:56 +000096
97 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 Py_DECREF(m);
99 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100}
101
Martin v. Löwis1a214512008-06-11 05:26:20 +0000102PyObject *
Victor Stinner0639b562011-03-04 12:57:07 +0000103PyModule_New(const char *name)
104{
105 PyObject *nameobj, *module;
106 nameobj = PyUnicode_FromString(name);
107 if (nameobj == NULL)
108 return NULL;
109 module = PyModule_NewObject(nameobj);
110 Py_DECREF(nameobj);
111 return module;
112}
113
Nick Coghland5cacbb2015-05-23 22:24:10 +1000114/* Check API/ABI version
115 * Issues a warning on mismatch, which is usually not fatal.
116 * Returns 0 if an exception is raised.
117 */
118static int
119check_api_version(const char *name, int module_api_version)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000120{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000121 if (module_api_version != PYTHON_API_VERSION && module_api_version != PYTHON_ABI_VERSION) {
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000122 int err;
123 err = PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
124 "Python C API version mismatch for module %.100s: "
125 "This Python has API version %d, module %.100s has version %d.",
126 name,
127 PYTHON_API_VERSION, name, module_api_version);
128 if (err)
Nick Coghland5cacbb2015-05-23 22:24:10 +1000129 return 0;
130 }
131 return 1;
132}
133
Nick Coghlan8682f572016-08-21 17:41:56 +1000134static int
135_add_methods_to_object(PyObject *module, PyObject *name, PyMethodDef *functions)
136{
137 PyObject *func;
138 PyMethodDef *fdef;
139
140 for (fdef = functions; fdef->ml_name != NULL; fdef++) {
141 if ((fdef->ml_flags & METH_CLASS) ||
142 (fdef->ml_flags & METH_STATIC)) {
143 PyErr_SetString(PyExc_ValueError,
144 "module functions cannot set"
145 " METH_CLASS or METH_STATIC");
146 return -1;
147 }
148 func = PyCFunction_NewEx(fdef, (PyObject*)module, name);
149 if (func == NULL) {
150 return -1;
151 }
152 if (PyObject_SetAttrString(module, fdef->ml_name, func) != 0) {
153 Py_DECREF(func);
154 return -1;
155 }
156 Py_DECREF(func);
157 }
158
159 return 0;
160}
161
Nick Coghland5cacbb2015-05-23 22:24:10 +1000162PyObject *
163PyModule_Create2(struct PyModuleDef* module, int module_api_version)
164{
165 const char* name;
166 PyModuleObject *m;
Eric Snow93c92f72017-09-13 23:46:04 -0700167 PyInterpreterState *interp = PyThreadState_Get()->interp;
168 if (interp->modules == NULL)
169 Py_FatalError("Python import machinery not initialized");
Nick Coghland5cacbb2015-05-23 22:24:10 +1000170 if (!PyModuleDef_Init(module))
171 return NULL;
172 name = module->m_name;
173 if (!check_api_version(name, module_api_version)) {
174 return NULL;
175 }
176 if (module->m_slots) {
177 PyErr_Format(
178 PyExc_SystemError,
179 "module %s: PyModule_Create is incompatible with m_slots", name);
180 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 }
182 /* Make sure name is fully qualified.
Martin v. Löwis1a214512008-06-11 05:26:20 +0000183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 This is a bit of a hack: when the shared library is loaded,
185 the module name is "package.module", but the module calls
186 PyModule_Create*() with just "module" for the name. The shared
187 library loader squirrels away the true name of the module in
188 _Py_PackageContext, and PyModule_Create*() will substitute this
189 (if the name actually matches).
190 */
191 if (_Py_PackageContext != NULL) {
Serhiy Storchakab57d9ea2016-11-21 10:25:54 +0200192 const char *p = strrchr(_Py_PackageContext, '.');
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193 if (p != NULL && strcmp(module->m_name, p+1) == 0) {
194 name = _Py_PackageContext;
195 _Py_PackageContext = NULL;
196 }
197 }
198 if ((m = (PyModuleObject*)PyModule_New(name)) == NULL)
199 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 if (module->m_size > 0) {
202 m->md_state = PyMem_MALLOC(module->m_size);
203 if (!m->md_state) {
204 PyErr_NoMemory();
205 Py_DECREF(m);
206 return NULL;
207 }
208 memset(m->md_state, 0, module->m_size);
209 }
210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 if (module->m_methods != NULL) {
Nick Coghland5cacbb2015-05-23 22:24:10 +1000212 if (PyModule_AddFunctions((PyObject *) m, module->m_methods) != 0) {
Meador Inge29e49d62012-07-19 13:45:43 -0500213 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 return NULL;
Meador Inge29e49d62012-07-19 13:45:43 -0500215 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 }
217 if (module->m_doc != NULL) {
Nick Coghland5cacbb2015-05-23 22:24:10 +1000218 if (PyModule_SetDocString((PyObject *) m, module->m_doc) != 0) {
Meador Inge29e49d62012-07-19 13:45:43 -0500219 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 return NULL;
221 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 }
223 m->md_def = module;
224 return (PyObject*)m;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000225}
226
Nick Coghland5cacbb2015-05-23 22:24:10 +1000227PyObject *
228PyModule_FromDefAndSpec2(struct PyModuleDef* def, PyObject *spec, int module_api_version)
229{
230 PyModuleDef_Slot* cur_slot;
231 PyObject *(*create)(PyObject *, PyModuleDef*) = NULL;
232 PyObject *nameobj;
233 PyObject *m = NULL;
234 int has_execution_slots = 0;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200235 const char *name;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000236 int ret;
237
238 PyModuleDef_Init(def);
239
240 nameobj = PyObject_GetAttrString(spec, "name");
241 if (nameobj == NULL) {
242 return NULL;
243 }
244 name = PyUnicode_AsUTF8(nameobj);
245 if (name == NULL) {
246 goto error;
247 }
248
249 if (!check_api_version(name, module_api_version)) {
250 goto error;
251 }
252
253 if (def->m_size < 0) {
254 PyErr_Format(
255 PyExc_SystemError,
256 "module %s: m_size may not be negative for multi-phase initialization",
257 name);
258 goto error;
259 }
260
261 for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) {
262 if (cur_slot->slot == Py_mod_create) {
263 if (create) {
264 PyErr_Format(
265 PyExc_SystemError,
266 "module %s has multiple create slots",
267 name);
268 goto error;
269 }
270 create = cur_slot->value;
271 } else if (cur_slot->slot < 0 || cur_slot->slot > _Py_mod_LAST_SLOT) {
272 PyErr_Format(
273 PyExc_SystemError,
274 "module %s uses unknown slot ID %i",
275 name, cur_slot->slot);
276 goto error;
277 } else {
278 has_execution_slots = 1;
279 }
280 }
281
282 if (create) {
283 m = create(spec, def);
284 if (m == NULL) {
285 if (!PyErr_Occurred()) {
286 PyErr_Format(
287 PyExc_SystemError,
288 "creation of module %s failed without setting an exception",
289 name);
290 }
291 goto error;
292 } else {
293 if (PyErr_Occurred()) {
294 PyErr_Format(PyExc_SystemError,
295 "creation of module %s raised unreported exception",
296 name);
297 goto error;
298 }
299 }
300 } else {
Nick Coghlan8682f572016-08-21 17:41:56 +1000301 m = PyModule_NewObject(nameobj);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000302 if (m == NULL) {
303 goto error;
304 }
305 }
306
307 if (PyModule_Check(m)) {
308 ((PyModuleObject*)m)->md_state = NULL;
309 ((PyModuleObject*)m)->md_def = def;
310 } else {
311 if (def->m_size > 0 || def->m_traverse || def->m_clear || def->m_free) {
312 PyErr_Format(
313 PyExc_SystemError,
314 "module %s is not a module object, but requests module state",
315 name);
316 goto error;
317 }
318 if (has_execution_slots) {
319 PyErr_Format(
320 PyExc_SystemError,
321 "module %s specifies execution slots, but did not create "
322 "a ModuleType instance",
323 name);
324 goto error;
325 }
326 }
327
328 if (def->m_methods != NULL) {
Nick Coghlan8682f572016-08-21 17:41:56 +1000329 ret = _add_methods_to_object(m, nameobj, def->m_methods);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000330 if (ret != 0) {
331 goto error;
332 }
333 }
334
335 if (def->m_doc != NULL) {
336 ret = PyModule_SetDocString(m, def->m_doc);
337 if (ret != 0) {
338 goto error;
339 }
340 }
341
Nick Coghlana48db2b2015-05-24 01:03:46 +1000342 Py_DECREF(nameobj);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000343 return m;
344
345error:
346 Py_DECREF(nameobj);
347 Py_XDECREF(m);
348 return NULL;
349}
350
351int
352PyModule_ExecDef(PyObject *module, PyModuleDef *def)
353{
354 PyModuleDef_Slot *cur_slot;
355 const char *name;
356 int ret;
357
358 name = PyModule_GetName(module);
359 if (name == NULL) {
360 return -1;
361 }
362
Nick Coghlan8682f572016-08-21 17:41:56 +1000363 if (def->m_size >= 0) {
Nick Coghland5cacbb2015-05-23 22:24:10 +1000364 PyModuleObject *md = (PyModuleObject*)module;
365 if (md->md_state == NULL) {
366 /* Always set a state pointer; this serves as a marker to skip
367 * multiple initialization (importlib.reload() is no-op) */
368 md->md_state = PyMem_MALLOC(def->m_size);
369 if (!md->md_state) {
370 PyErr_NoMemory();
371 return -1;
372 }
373 memset(md->md_state, 0, def->m_size);
374 }
375 }
376
377 if (def->m_slots == NULL) {
378 return 0;
379 }
380
381 for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) {
382 switch (cur_slot->slot) {
383 case Py_mod_create:
Serhiy Storchaka333ad922016-09-26 23:14:44 +0300384 /* handled in PyModule_FromDefAndSpec2 */
Nick Coghland5cacbb2015-05-23 22:24:10 +1000385 break;
386 case Py_mod_exec:
387 ret = ((int (*)(PyObject *))cur_slot->value)(module);
388 if (ret != 0) {
389 if (!PyErr_Occurred()) {
390 PyErr_Format(
391 PyExc_SystemError,
392 "execution of module %s failed without setting an exception",
393 name);
394 }
395 return -1;
396 }
397 if (PyErr_Occurred()) {
398 PyErr_Format(
399 PyExc_SystemError,
400 "execution of module %s raised unreported exception",
401 name);
402 return -1;
403 }
404 break;
405 default:
406 PyErr_Format(
407 PyExc_SystemError,
408 "module %s initialized with unknown slot %i",
409 name, cur_slot->slot);
410 return -1;
411 }
412 }
413 return 0;
414}
415
416int
417PyModule_AddFunctions(PyObject *m, PyMethodDef *functions)
418{
Nick Coghlan8682f572016-08-21 17:41:56 +1000419 int res;
420 PyObject *name = PyModule_GetNameObject(m);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000421 if (name == NULL) {
422 return -1;
423 }
424
Nick Coghlan8682f572016-08-21 17:41:56 +1000425 res = _add_methods_to_object(m, name, functions);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000426 Py_DECREF(name);
Nick Coghlan8682f572016-08-21 17:41:56 +1000427 return res;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000428}
429
430int
431PyModule_SetDocString(PyObject *m, const char *doc)
432{
433 PyObject *v;
434 _Py_IDENTIFIER(__doc__);
435
436 v = PyUnicode_FromString(doc);
437 if (v == NULL || _PyObject_SetAttrId(m, &PyId___doc__, v) != 0) {
438 Py_XDECREF(v);
439 return -1;
440 }
441 Py_DECREF(v);
442 return 0;
443}
Martin v. Löwis1a214512008-06-11 05:26:20 +0000444
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000445PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000446PyModule_GetDict(PyObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000447{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 PyObject *d;
449 if (!PyModule_Check(m)) {
450 PyErr_BadInternalCall();
451 return NULL;
452 }
453 d = ((PyModuleObject *)m) -> md_dict;
Berker Peksag7fbce562016-08-19 12:00:13 +0300454 assert(d != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000456}
457
Victor Stinnerbd475112011-02-23 00:21:43 +0000458PyObject*
459PyModule_GetNameObject(PyObject *m)
Guido van Rossum0558a201990-10-26 15:00:11 +0000460{
Benjamin Peterson027ce162014-04-24 19:39:18 -0400461 _Py_IDENTIFIER(__name__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 PyObject *d;
Victor Stinnerbd475112011-02-23 00:21:43 +0000463 PyObject *name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 if (!PyModule_Check(m)) {
465 PyErr_BadArgument();
466 return NULL;
467 }
468 d = ((PyModuleObject *)m)->md_dict;
469 if (d == NULL ||
Benjamin Peterson027ce162014-04-24 19:39:18 -0400470 (name = _PyDict_GetItemId(d, &PyId___name__)) == NULL ||
Victor Stinnerbd475112011-02-23 00:21:43 +0000471 !PyUnicode_Check(name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 {
473 PyErr_SetString(PyExc_SystemError, "nameless module");
474 return NULL;
475 }
Victor Stinnerbd475112011-02-23 00:21:43 +0000476 Py_INCREF(name);
477 return name;
478}
479
480const char *
481PyModule_GetName(PyObject *m)
482{
483 PyObject *name = PyModule_GetNameObject(m);
484 if (name == NULL)
485 return NULL;
486 Py_DECREF(name); /* module dict has still a reference */
Serhiy Storchaka06515832016-11-20 09:13:07 +0200487 return PyUnicode_AsUTF8(name);
Guido van Rossum0558a201990-10-26 15:00:11 +0000488}
489
Victor Stinner6c00c142010-08-17 23:37:11 +0000490PyObject*
491PyModule_GetFilenameObject(PyObject *m)
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000492{
Benjamin Peterson027ce162014-04-24 19:39:18 -0400493 _Py_IDENTIFIER(__file__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 PyObject *d;
495 PyObject *fileobj;
496 if (!PyModule_Check(m)) {
497 PyErr_BadArgument();
498 return NULL;
499 }
500 d = ((PyModuleObject *)m)->md_dict;
501 if (d == NULL ||
Benjamin Peterson027ce162014-04-24 19:39:18 -0400502 (fileobj = _PyDict_GetItemId(d, &PyId___file__)) == NULL ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 !PyUnicode_Check(fileobj))
504 {
505 PyErr_SetString(PyExc_SystemError, "module filename missing");
506 return NULL;
507 }
Victor Stinner6c00c142010-08-17 23:37:11 +0000508 Py_INCREF(fileobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 return fileobj;
Victor Stinner8124feb2010-05-07 00:50:12 +0000510}
511
512const char *
513PyModule_GetFilename(PyObject *m)
514{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 PyObject *fileobj;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200516 const char *utf8;
Victor Stinner6c00c142010-08-17 23:37:11 +0000517 fileobj = PyModule_GetFilenameObject(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 if (fileobj == NULL)
519 return NULL;
Serhiy Storchaka06515832016-11-20 09:13:07 +0200520 utf8 = PyUnicode_AsUTF8(fileobj);
Victor Stinnerbd475112011-02-23 00:21:43 +0000521 Py_DECREF(fileobj); /* module dict has still a reference */
Victor Stinner6c00c142010-08-17 23:37:11 +0000522 return utf8;
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000523}
524
Martin v. Löwis1a214512008-06-11 05:26:20 +0000525PyModuleDef*
526PyModule_GetDef(PyObject* m)
527{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 if (!PyModule_Check(m)) {
529 PyErr_BadArgument();
530 return NULL;
531 }
532 return ((PyModuleObject *)m)->md_def;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000533}
534
535void*
536PyModule_GetState(PyObject* m)
537{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 if (!PyModule_Check(m)) {
539 PyErr_BadArgument();
540 return NULL;
541 }
542 return ((PyModuleObject *)m)->md_state;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000543}
544
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000545void
Fred Drakeee238b92000-07-09 06:03:25 +0000546_PyModule_Clear(PyObject *m)
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000547{
Serhiy Storchaka87a5c512014-02-10 18:21:34 +0200548 PyObject *d = ((PyModuleObject *)m)->md_dict;
549 if (d != NULL)
550 _PyModule_ClearDict(d);
551}
552
553void
554_PyModule_ClearDict(PyObject *d)
555{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 /* To make the execution order of destructors for global
557 objects a bit more predictable, we first zap all objects
558 whose name starts with a single underscore, before we clear
559 the entire dictionary. We zap them by replacing them with
560 None, rather than deleting them from the dictionary, to
561 avoid rehashing the dictionary (to some extent). */
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 Py_ssize_t pos;
564 PyObject *key, *value;
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 /* First, clear only names starting with a single underscore */
567 pos = 0;
568 while (PyDict_Next(d, &pos, &key, &value)) {
569 if (value != Py_None && PyUnicode_Check(key)) {
Brett Cannon62228db2012-04-29 14:38:11 -0400570 if (PyUnicode_READ_CHAR(key, 0) == '_' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200571 PyUnicode_READ_CHAR(key, 1) != '_') {
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000572 if (Py_VerboseFlag > 1) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200573 const char *s = PyUnicode_AsUTF8(key);
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000574 if (s != NULL)
575 PySys_WriteStderr("# clear[1] %s\n", s);
576 else
577 PyErr_Clear();
578 }
Serhiy Storchaka1f9d11b2014-02-12 09:55:01 +0200579 if (PyDict_SetItem(d, key, Py_None) != 0)
580 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 }
582 }
583 }
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 /* Next, clear all names except for __builtins__ */
586 pos = 0;
587 while (PyDict_Next(d, &pos, &key, &value)) {
588 if (value != Py_None && PyUnicode_Check(key)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200589 if (PyUnicode_READ_CHAR(key, 0) != '_' ||
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200590 !_PyUnicode_EqualToASCIIString(key, "__builtins__"))
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000591 {
592 if (Py_VerboseFlag > 1) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200593 const char *s = PyUnicode_AsUTF8(key);
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000594 if (s != NULL)
595 PySys_WriteStderr("# clear[2] %s\n", s);
596 else
597 PyErr_Clear();
598 }
Serhiy Storchaka1f9d11b2014-02-12 09:55:01 +0200599 if (PyDict_SetItem(d, key, Py_None) != 0)
600 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 }
602 }
603 }
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 /* Note: we leave __builtins__ in place, so that destructors
606 of non-global objects defined in this module can still use
607 builtins, in particularly 'None'. */
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000608
609}
610
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200611/*[clinic input]
612class module "PyModuleObject *" "&PyModule_Type"
613[clinic start generated code]*/
614/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3e35d4f708ecb6af]*/
615
616#include "clinic/moduleobject.c.h"
617
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000618/* Methods */
619
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200620/*[clinic input]
621module.__init__
622 name: unicode
623 doc: object = None
624
625Create a module object.
626
627The name must be a string; the optional doc argument can have any type.
628[clinic start generated code]*/
629
Tim Peters6d6c1a32001-08-02 04:15:00 +0000630static int
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200631module___init___impl(PyModuleObject *self, PyObject *name, PyObject *doc)
632/*[clinic end generated code: output=e7e721c26ce7aad7 input=57f9e177401e5e1e]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +0000633{
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200634 PyObject *dict = self->md_dict;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 if (dict == NULL) {
636 dict = PyDict_New();
637 if (dict == NULL)
638 return -1;
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200639 self->md_dict = dict;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 }
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200641 if (module_init_dict(self, dict, name, doc) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 return -1;
643 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000644}
645
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000646static void
Fred Drakeee238b92000-07-09 06:03:25 +0000647module_dealloc(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000648{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 PyObject_GC_UnTrack(m);
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200650 if (Py_VerboseFlag && m->md_name) {
651 PySys_FormatStderr("# destroy %S\n", m->md_name);
652 }
653 if (m->md_weaklist != NULL)
654 PyObject_ClearWeakRefs((PyObject *) m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 if (m->md_def && m->md_def->m_free)
656 m->md_def->m_free(m);
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200657 Py_XDECREF(m->md_dict);
658 Py_XDECREF(m->md_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 if (m->md_state != NULL)
660 PyMem_FREE(m->md_state);
661 Py_TYPE(m)->tp_free((PyObject *)m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000662}
663
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000664static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000665module_repr(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000666{
Eric Snowb523f842013-11-22 09:05:39 -0700667 PyThreadState *tstate = PyThreadState_GET();
668 PyInterpreterState *interp = tstate->interp;
Barry Warsaw2907fe62001-08-16 20:39:24 +0000669
Eric Snowb523f842013-11-22 09:05:39 -0700670 return PyObject_CallMethod(interp->importlib, "_module_repr", "O", m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000671}
672
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700673static PyObject*
Benjamin Peterson1184e262014-04-24 19:29:23 -0400674module_getattro(PyModuleObject *m, PyObject *name)
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700675{
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700676 PyObject *attr, *mod_name;
Benjamin Peterson1184e262014-04-24 19:29:23 -0400677 attr = PyObject_GenericGetAttr((PyObject *)m, name);
678 if (attr || !PyErr_ExceptionMatches(PyExc_AttributeError))
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700679 return attr;
680 PyErr_Clear();
Benjamin Peterson1184e262014-04-24 19:29:23 -0400681 if (m->md_dict) {
Benjamin Peterson027ce162014-04-24 19:39:18 -0400682 _Py_IDENTIFIER(__name__);
683 mod_name = _PyDict_GetItemId(m->md_dict, &PyId___name__);
Benjamin Peterson1184e262014-04-24 19:29:23 -0400684 if (mod_name) {
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700685 PyErr_Format(PyExc_AttributeError,
686 "module '%U' has no attribute '%U'", mod_name, name);
687 return NULL;
688 }
Benjamin Peterson1184e262014-04-24 19:29:23 -0400689 else if (PyErr_Occurred()) {
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700690 PyErr_Clear();
Benjamin Peterson1184e262014-04-24 19:29:23 -0400691 }
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700692 }
693 PyErr_Format(PyExc_AttributeError,
694 "module has no attribute '%U'", name);
695 return NULL;
696}
697
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000698static int
699module_traverse(PyModuleObject *m, visitproc visit, void *arg)
700{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 if (m->md_def && m->md_def->m_traverse) {
702 int res = m->md_def->m_traverse((PyObject*)m, visit, arg);
703 if (res)
704 return res;
705 }
706 Py_VISIT(m->md_dict);
707 return 0;
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000708}
709
Martin v. Löwis1a214512008-06-11 05:26:20 +0000710static int
711module_clear(PyModuleObject *m)
712{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 if (m->md_def && m->md_def->m_clear) {
714 int res = m->md_def->m_clear((PyObject*)m);
715 if (res)
716 return res;
717 }
718 Py_CLEAR(m->md_dict);
719 return 0;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000720}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500722static PyObject *
723module_dir(PyObject *self, PyObject *args)
724{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200725 _Py_IDENTIFIER(__dict__);
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500726 PyObject *result = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200727 PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500728
729 if (dict != NULL) {
730 if (PyDict_Check(dict))
731 result = PyDict_Keys(dict);
732 else {
733 const char *name = PyModule_GetName(self);
734 if (name)
735 PyErr_Format(PyExc_TypeError,
736 "%.200s.__dict__ is not a dictionary",
737 name);
738 }
739 }
740
741 Py_XDECREF(dict);
742 return result;
743}
744
745static PyMethodDef module_methods[] = {
746 {"__dir__", module_dir, METH_NOARGS,
Benjamin Petersonc7284122011-05-24 12:46:15 -0500747 PyDoc_STR("__dir__() -> list\nspecialized dir() implementation")},
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500748 {0}
749};
750
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000751PyTypeObject PyModule_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 PyVarObject_HEAD_INIT(&PyType_Type, 0)
753 "module", /* tp_name */
754 sizeof(PyModuleObject), /* tp_size */
755 0, /* tp_itemsize */
756 (destructor)module_dealloc, /* tp_dealloc */
757 0, /* tp_print */
758 0, /* tp_getattr */
759 0, /* tp_setattr */
760 0, /* tp_reserved */
761 (reprfunc)module_repr, /* tp_repr */
762 0, /* tp_as_number */
763 0, /* tp_as_sequence */
764 0, /* tp_as_mapping */
765 0, /* tp_hash */
766 0, /* tp_call */
767 0, /* tp_str */
Benjamin Peterson1184e262014-04-24 19:29:23 -0400768 (getattrofunc)module_getattro, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 PyObject_GenericSetAttr, /* tp_setattro */
770 0, /* tp_as_buffer */
771 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
772 Py_TPFLAGS_BASETYPE, /* tp_flags */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200773 module___init____doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 (traverseproc)module_traverse, /* tp_traverse */
775 (inquiry)module_clear, /* tp_clear */
776 0, /* tp_richcompare */
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200777 offsetof(PyModuleObject, md_weaklist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 0, /* tp_iter */
779 0, /* tp_iternext */
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500780 module_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 module_members, /* tp_members */
782 0, /* tp_getset */
783 0, /* tp_base */
784 0, /* tp_dict */
785 0, /* tp_descr_get */
786 0, /* tp_descr_set */
787 offsetof(PyModuleObject, md_dict), /* tp_dictoffset */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200788 module___init__, /* tp_init */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 PyType_GenericAlloc, /* tp_alloc */
790 PyType_GenericNew, /* tp_new */
791 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000792};