blob: 29732633da1184a720080eb65d3a912669cec1fa [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{
Eric Snowd393c1b2017-09-14 12:18:12 -0600165 if (!_PyImport_IsInitialized(PyThreadState_GET()->interp))
166 Py_FatalError("Python import machinery not initialized");
167 return _PyModule_CreateInitialized(module, module_api_version);
168}
169
170PyObject *
171_PyModule_CreateInitialized(struct PyModuleDef* module, int module_api_version)
172{
Nick Coghland5cacbb2015-05-23 22:24:10 +1000173 const char* name;
174 PyModuleObject *m;
Eric Snowd393c1b2017-09-14 12:18:12 -0600175
Nick Coghland5cacbb2015-05-23 22:24:10 +1000176 if (!PyModuleDef_Init(module))
177 return NULL;
178 name = module->m_name;
179 if (!check_api_version(name, module_api_version)) {
180 return NULL;
181 }
182 if (module->m_slots) {
183 PyErr_Format(
184 PyExc_SystemError,
185 "module %s: PyModule_Create is incompatible with m_slots", name);
186 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000187 }
188 /* Make sure name is fully qualified.
Martin v. Löwis1a214512008-06-11 05:26:20 +0000189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190 This is a bit of a hack: when the shared library is loaded,
191 the module name is "package.module", but the module calls
192 PyModule_Create*() with just "module" for the name. The shared
193 library loader squirrels away the true name of the module in
194 _Py_PackageContext, and PyModule_Create*() will substitute this
195 (if the name actually matches).
196 */
197 if (_Py_PackageContext != NULL) {
Serhiy Storchakab57d9ea2016-11-21 10:25:54 +0200198 const char *p = strrchr(_Py_PackageContext, '.');
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 if (p != NULL && strcmp(module->m_name, p+1) == 0) {
200 name = _Py_PackageContext;
201 _Py_PackageContext = NULL;
202 }
203 }
204 if ((m = (PyModuleObject*)PyModule_New(name)) == NULL)
205 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 if (module->m_size > 0) {
208 m->md_state = PyMem_MALLOC(module->m_size);
209 if (!m->md_state) {
210 PyErr_NoMemory();
211 Py_DECREF(m);
212 return NULL;
213 }
214 memset(m->md_state, 0, module->m_size);
215 }
216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 if (module->m_methods != NULL) {
Nick Coghland5cacbb2015-05-23 22:24:10 +1000218 if (PyModule_AddFunctions((PyObject *) m, module->m_methods) != 0) {
Meador Inge29e49d62012-07-19 13:45:43 -0500219 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 return NULL;
Meador Inge29e49d62012-07-19 13:45:43 -0500221 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 }
223 if (module->m_doc != NULL) {
Nick Coghland5cacbb2015-05-23 22:24:10 +1000224 if (PyModule_SetDocString((PyObject *) m, module->m_doc) != 0) {
Meador Inge29e49d62012-07-19 13:45:43 -0500225 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 return NULL;
227 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 }
229 m->md_def = module;
230 return (PyObject*)m;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000231}
232
Nick Coghland5cacbb2015-05-23 22:24:10 +1000233PyObject *
234PyModule_FromDefAndSpec2(struct PyModuleDef* def, PyObject *spec, int module_api_version)
235{
236 PyModuleDef_Slot* cur_slot;
237 PyObject *(*create)(PyObject *, PyModuleDef*) = NULL;
238 PyObject *nameobj;
239 PyObject *m = NULL;
240 int has_execution_slots = 0;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200241 const char *name;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000242 int ret;
243
244 PyModuleDef_Init(def);
245
246 nameobj = PyObject_GetAttrString(spec, "name");
247 if (nameobj == NULL) {
248 return NULL;
249 }
250 name = PyUnicode_AsUTF8(nameobj);
251 if (name == NULL) {
252 goto error;
253 }
254
255 if (!check_api_version(name, module_api_version)) {
256 goto error;
257 }
258
259 if (def->m_size < 0) {
260 PyErr_Format(
261 PyExc_SystemError,
262 "module %s: m_size may not be negative for multi-phase initialization",
263 name);
264 goto error;
265 }
266
267 for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) {
268 if (cur_slot->slot == Py_mod_create) {
269 if (create) {
270 PyErr_Format(
271 PyExc_SystemError,
272 "module %s has multiple create slots",
273 name);
274 goto error;
275 }
276 create = cur_slot->value;
277 } else if (cur_slot->slot < 0 || cur_slot->slot > _Py_mod_LAST_SLOT) {
278 PyErr_Format(
279 PyExc_SystemError,
280 "module %s uses unknown slot ID %i",
281 name, cur_slot->slot);
282 goto error;
283 } else {
284 has_execution_slots = 1;
285 }
286 }
287
288 if (create) {
289 m = create(spec, def);
290 if (m == NULL) {
291 if (!PyErr_Occurred()) {
292 PyErr_Format(
293 PyExc_SystemError,
294 "creation of module %s failed without setting an exception",
295 name);
296 }
297 goto error;
298 } else {
299 if (PyErr_Occurred()) {
300 PyErr_Format(PyExc_SystemError,
301 "creation of module %s raised unreported exception",
302 name);
303 goto error;
304 }
305 }
306 } else {
Nick Coghlan8682f572016-08-21 17:41:56 +1000307 m = PyModule_NewObject(nameobj);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000308 if (m == NULL) {
309 goto error;
310 }
311 }
312
313 if (PyModule_Check(m)) {
314 ((PyModuleObject*)m)->md_state = NULL;
315 ((PyModuleObject*)m)->md_def = def;
316 } else {
317 if (def->m_size > 0 || def->m_traverse || def->m_clear || def->m_free) {
318 PyErr_Format(
319 PyExc_SystemError,
320 "module %s is not a module object, but requests module state",
321 name);
322 goto error;
323 }
324 if (has_execution_slots) {
325 PyErr_Format(
326 PyExc_SystemError,
327 "module %s specifies execution slots, but did not create "
328 "a ModuleType instance",
329 name);
330 goto error;
331 }
332 }
333
334 if (def->m_methods != NULL) {
Nick Coghlan8682f572016-08-21 17:41:56 +1000335 ret = _add_methods_to_object(m, nameobj, def->m_methods);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000336 if (ret != 0) {
337 goto error;
338 }
339 }
340
341 if (def->m_doc != NULL) {
342 ret = PyModule_SetDocString(m, def->m_doc);
343 if (ret != 0) {
344 goto error;
345 }
346 }
347
Nick Coghlana48db2b2015-05-24 01:03:46 +1000348 Py_DECREF(nameobj);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000349 return m;
350
351error:
352 Py_DECREF(nameobj);
353 Py_XDECREF(m);
354 return NULL;
355}
356
357int
358PyModule_ExecDef(PyObject *module, PyModuleDef *def)
359{
360 PyModuleDef_Slot *cur_slot;
361 const char *name;
362 int ret;
363
364 name = PyModule_GetName(module);
365 if (name == NULL) {
366 return -1;
367 }
368
Nick Coghlan8682f572016-08-21 17:41:56 +1000369 if (def->m_size >= 0) {
Nick Coghland5cacbb2015-05-23 22:24:10 +1000370 PyModuleObject *md = (PyModuleObject*)module;
371 if (md->md_state == NULL) {
372 /* Always set a state pointer; this serves as a marker to skip
373 * multiple initialization (importlib.reload() is no-op) */
374 md->md_state = PyMem_MALLOC(def->m_size);
375 if (!md->md_state) {
376 PyErr_NoMemory();
377 return -1;
378 }
379 memset(md->md_state, 0, def->m_size);
380 }
381 }
382
383 if (def->m_slots == NULL) {
384 return 0;
385 }
386
387 for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) {
388 switch (cur_slot->slot) {
389 case Py_mod_create:
Serhiy Storchaka333ad922016-09-26 23:14:44 +0300390 /* handled in PyModule_FromDefAndSpec2 */
Nick Coghland5cacbb2015-05-23 22:24:10 +1000391 break;
392 case Py_mod_exec:
393 ret = ((int (*)(PyObject *))cur_slot->value)(module);
394 if (ret != 0) {
395 if (!PyErr_Occurred()) {
396 PyErr_Format(
397 PyExc_SystemError,
398 "execution of module %s failed without setting an exception",
399 name);
400 }
401 return -1;
402 }
403 if (PyErr_Occurred()) {
404 PyErr_Format(
405 PyExc_SystemError,
406 "execution of module %s raised unreported exception",
407 name);
408 return -1;
409 }
410 break;
411 default:
412 PyErr_Format(
413 PyExc_SystemError,
414 "module %s initialized with unknown slot %i",
415 name, cur_slot->slot);
416 return -1;
417 }
418 }
419 return 0;
420}
421
422int
423PyModule_AddFunctions(PyObject *m, PyMethodDef *functions)
424{
Nick Coghlan8682f572016-08-21 17:41:56 +1000425 int res;
426 PyObject *name = PyModule_GetNameObject(m);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000427 if (name == NULL) {
428 return -1;
429 }
430
Nick Coghlan8682f572016-08-21 17:41:56 +1000431 res = _add_methods_to_object(m, name, functions);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000432 Py_DECREF(name);
Nick Coghlan8682f572016-08-21 17:41:56 +1000433 return res;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000434}
435
436int
437PyModule_SetDocString(PyObject *m, const char *doc)
438{
439 PyObject *v;
440 _Py_IDENTIFIER(__doc__);
441
442 v = PyUnicode_FromString(doc);
443 if (v == NULL || _PyObject_SetAttrId(m, &PyId___doc__, v) != 0) {
444 Py_XDECREF(v);
445 return -1;
446 }
447 Py_DECREF(v);
448 return 0;
449}
Martin v. Löwis1a214512008-06-11 05:26:20 +0000450
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000451PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000452PyModule_GetDict(PyObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000453{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 PyObject *d;
455 if (!PyModule_Check(m)) {
456 PyErr_BadInternalCall();
457 return NULL;
458 }
459 d = ((PyModuleObject *)m) -> md_dict;
Berker Peksag7fbce562016-08-19 12:00:13 +0300460 assert(d != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000462}
463
Victor Stinnerbd475112011-02-23 00:21:43 +0000464PyObject*
465PyModule_GetNameObject(PyObject *m)
Guido van Rossum0558a201990-10-26 15:00:11 +0000466{
Benjamin Peterson027ce162014-04-24 19:39:18 -0400467 _Py_IDENTIFIER(__name__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000468 PyObject *d;
Victor Stinnerbd475112011-02-23 00:21:43 +0000469 PyObject *name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 if (!PyModule_Check(m)) {
471 PyErr_BadArgument();
472 return NULL;
473 }
474 d = ((PyModuleObject *)m)->md_dict;
475 if (d == NULL ||
Benjamin Peterson027ce162014-04-24 19:39:18 -0400476 (name = _PyDict_GetItemId(d, &PyId___name__)) == NULL ||
Victor Stinnerbd475112011-02-23 00:21:43 +0000477 !PyUnicode_Check(name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 {
479 PyErr_SetString(PyExc_SystemError, "nameless module");
480 return NULL;
481 }
Victor Stinnerbd475112011-02-23 00:21:43 +0000482 Py_INCREF(name);
483 return name;
484}
485
486const char *
487PyModule_GetName(PyObject *m)
488{
489 PyObject *name = PyModule_GetNameObject(m);
490 if (name == NULL)
491 return NULL;
492 Py_DECREF(name); /* module dict has still a reference */
Serhiy Storchaka06515832016-11-20 09:13:07 +0200493 return PyUnicode_AsUTF8(name);
Guido van Rossum0558a201990-10-26 15:00:11 +0000494}
495
Victor Stinner6c00c142010-08-17 23:37:11 +0000496PyObject*
497PyModule_GetFilenameObject(PyObject *m)
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000498{
Benjamin Peterson027ce162014-04-24 19:39:18 -0400499 _Py_IDENTIFIER(__file__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 PyObject *d;
501 PyObject *fileobj;
502 if (!PyModule_Check(m)) {
503 PyErr_BadArgument();
504 return NULL;
505 }
506 d = ((PyModuleObject *)m)->md_dict;
507 if (d == NULL ||
Benjamin Peterson027ce162014-04-24 19:39:18 -0400508 (fileobj = _PyDict_GetItemId(d, &PyId___file__)) == NULL ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 !PyUnicode_Check(fileobj))
510 {
511 PyErr_SetString(PyExc_SystemError, "module filename missing");
512 return NULL;
513 }
Victor Stinner6c00c142010-08-17 23:37:11 +0000514 Py_INCREF(fileobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 return fileobj;
Victor Stinner8124feb2010-05-07 00:50:12 +0000516}
517
518const char *
519PyModule_GetFilename(PyObject *m)
520{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 PyObject *fileobj;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200522 const char *utf8;
Victor Stinner6c00c142010-08-17 23:37:11 +0000523 fileobj = PyModule_GetFilenameObject(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 if (fileobj == NULL)
525 return NULL;
Serhiy Storchaka06515832016-11-20 09:13:07 +0200526 utf8 = PyUnicode_AsUTF8(fileobj);
Victor Stinnerbd475112011-02-23 00:21:43 +0000527 Py_DECREF(fileobj); /* module dict has still a reference */
Victor Stinner6c00c142010-08-17 23:37:11 +0000528 return utf8;
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000529}
530
Martin v. Löwis1a214512008-06-11 05:26:20 +0000531PyModuleDef*
532PyModule_GetDef(PyObject* m)
533{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 if (!PyModule_Check(m)) {
535 PyErr_BadArgument();
536 return NULL;
537 }
538 return ((PyModuleObject *)m)->md_def;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000539}
540
541void*
542PyModule_GetState(PyObject* m)
543{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 if (!PyModule_Check(m)) {
545 PyErr_BadArgument();
546 return NULL;
547 }
548 return ((PyModuleObject *)m)->md_state;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000549}
550
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000551void
Fred Drakeee238b92000-07-09 06:03:25 +0000552_PyModule_Clear(PyObject *m)
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000553{
Serhiy Storchaka87a5c512014-02-10 18:21:34 +0200554 PyObject *d = ((PyModuleObject *)m)->md_dict;
555 if (d != NULL)
556 _PyModule_ClearDict(d);
557}
558
559void
560_PyModule_ClearDict(PyObject *d)
561{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 /* To make the execution order of destructors for global
563 objects a bit more predictable, we first zap all objects
564 whose name starts with a single underscore, before we clear
565 the entire dictionary. We zap them by replacing them with
566 None, rather than deleting them from the dictionary, to
567 avoid rehashing the dictionary (to some extent). */
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 Py_ssize_t pos;
570 PyObject *key, *value;
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 /* First, clear only names starting with a single underscore */
573 pos = 0;
574 while (PyDict_Next(d, &pos, &key, &value)) {
575 if (value != Py_None && PyUnicode_Check(key)) {
Brett Cannon62228db2012-04-29 14:38:11 -0400576 if (PyUnicode_READ_CHAR(key, 0) == '_' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200577 PyUnicode_READ_CHAR(key, 1) != '_') {
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000578 if (Py_VerboseFlag > 1) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200579 const char *s = PyUnicode_AsUTF8(key);
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000580 if (s != NULL)
581 PySys_WriteStderr("# clear[1] %s\n", s);
582 else
583 PyErr_Clear();
584 }
Serhiy Storchaka1f9d11b2014-02-12 09:55:01 +0200585 if (PyDict_SetItem(d, key, Py_None) != 0)
586 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 }
588 }
589 }
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 /* Next, clear all names except for __builtins__ */
592 pos = 0;
593 while (PyDict_Next(d, &pos, &key, &value)) {
594 if (value != Py_None && PyUnicode_Check(key)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200595 if (PyUnicode_READ_CHAR(key, 0) != '_' ||
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200596 !_PyUnicode_EqualToASCIIString(key, "__builtins__"))
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000597 {
598 if (Py_VerboseFlag > 1) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200599 const char *s = PyUnicode_AsUTF8(key);
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000600 if (s != NULL)
601 PySys_WriteStderr("# clear[2] %s\n", s);
602 else
603 PyErr_Clear();
604 }
Serhiy Storchaka1f9d11b2014-02-12 09:55:01 +0200605 if (PyDict_SetItem(d, key, Py_None) != 0)
606 PyErr_Clear();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 }
608 }
609 }
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 /* Note: we leave __builtins__ in place, so that destructors
612 of non-global objects defined in this module can still use
613 builtins, in particularly 'None'. */
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000614
615}
616
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200617/*[clinic input]
618class module "PyModuleObject *" "&PyModule_Type"
619[clinic start generated code]*/
620/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3e35d4f708ecb6af]*/
621
622#include "clinic/moduleobject.c.h"
623
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000624/* Methods */
625
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200626/*[clinic input]
627module.__init__
628 name: unicode
629 doc: object = None
630
631Create a module object.
632
633The name must be a string; the optional doc argument can have any type.
634[clinic start generated code]*/
635
Tim Peters6d6c1a32001-08-02 04:15:00 +0000636static int
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200637module___init___impl(PyModuleObject *self, PyObject *name, PyObject *doc)
638/*[clinic end generated code: output=e7e721c26ce7aad7 input=57f9e177401e5e1e]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +0000639{
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200640 PyObject *dict = self->md_dict;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 if (dict == NULL) {
642 dict = PyDict_New();
643 if (dict == NULL)
644 return -1;
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200645 self->md_dict = dict;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 }
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200647 if (module_init_dict(self, dict, name, doc) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 return -1;
649 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000650}
651
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000652static void
Fred Drakeee238b92000-07-09 06:03:25 +0000653module_dealloc(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000654{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 PyObject_GC_UnTrack(m);
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200656 if (Py_VerboseFlag && m->md_name) {
657 PySys_FormatStderr("# destroy %S\n", m->md_name);
658 }
659 if (m->md_weaklist != NULL)
660 PyObject_ClearWeakRefs((PyObject *) m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 if (m->md_def && m->md_def->m_free)
662 m->md_def->m_free(m);
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200663 Py_XDECREF(m->md_dict);
664 Py_XDECREF(m->md_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 if (m->md_state != NULL)
666 PyMem_FREE(m->md_state);
667 Py_TYPE(m)->tp_free((PyObject *)m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000668}
669
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000670static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000671module_repr(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000672{
Eric Snowb523f842013-11-22 09:05:39 -0700673 PyThreadState *tstate = PyThreadState_GET();
674 PyInterpreterState *interp = tstate->interp;
Barry Warsaw2907fe62001-08-16 20:39:24 +0000675
Eric Snowb523f842013-11-22 09:05:39 -0700676 return PyObject_CallMethod(interp->importlib, "_module_repr", "O", m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000677}
678
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700679static PyObject*
Benjamin Peterson1184e262014-04-24 19:29:23 -0400680module_getattro(PyModuleObject *m, PyObject *name)
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700681{
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700682 PyObject *attr, *mod_name;
Benjamin Peterson1184e262014-04-24 19:29:23 -0400683 attr = PyObject_GenericGetAttr((PyObject *)m, name);
684 if (attr || !PyErr_ExceptionMatches(PyExc_AttributeError))
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700685 return attr;
686 PyErr_Clear();
Benjamin Peterson1184e262014-04-24 19:29:23 -0400687 if (m->md_dict) {
Benjamin Peterson027ce162014-04-24 19:39:18 -0400688 _Py_IDENTIFIER(__name__);
689 mod_name = _PyDict_GetItemId(m->md_dict, &PyId___name__);
Oren Milman6db70332017-09-19 14:23:01 +0300690 if (mod_name && PyUnicode_Check(mod_name)) {
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700691 PyErr_Format(PyExc_AttributeError,
692 "module '%U' has no attribute '%U'", mod_name, name);
693 return NULL;
694 }
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700695 }
696 PyErr_Format(PyExc_AttributeError,
697 "module has no attribute '%U'", name);
698 return NULL;
699}
700
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000701static int
702module_traverse(PyModuleObject *m, visitproc visit, void *arg)
703{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 if (m->md_def && m->md_def->m_traverse) {
705 int res = m->md_def->m_traverse((PyObject*)m, visit, arg);
706 if (res)
707 return res;
708 }
709 Py_VISIT(m->md_dict);
710 return 0;
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000711}
712
Martin v. Löwis1a214512008-06-11 05:26:20 +0000713static int
714module_clear(PyModuleObject *m)
715{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 if (m->md_def && m->md_def->m_clear) {
717 int res = m->md_def->m_clear((PyObject*)m);
718 if (res)
719 return res;
720 }
721 Py_CLEAR(m->md_dict);
722 return 0;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000723}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500725static PyObject *
726module_dir(PyObject *self, PyObject *args)
727{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200728 _Py_IDENTIFIER(__dict__);
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500729 PyObject *result = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200730 PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500731
732 if (dict != NULL) {
733 if (PyDict_Check(dict))
734 result = PyDict_Keys(dict);
735 else {
736 const char *name = PyModule_GetName(self);
737 if (name)
738 PyErr_Format(PyExc_TypeError,
739 "%.200s.__dict__ is not a dictionary",
740 name);
741 }
742 }
743
744 Py_XDECREF(dict);
745 return result;
746}
747
748static PyMethodDef module_methods[] = {
749 {"__dir__", module_dir, METH_NOARGS,
Benjamin Petersonc7284122011-05-24 12:46:15 -0500750 PyDoc_STR("__dir__() -> list\nspecialized dir() implementation")},
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500751 {0}
752};
753
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000754PyTypeObject PyModule_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 PyVarObject_HEAD_INIT(&PyType_Type, 0)
756 "module", /* tp_name */
757 sizeof(PyModuleObject), /* tp_size */
758 0, /* tp_itemsize */
759 (destructor)module_dealloc, /* tp_dealloc */
760 0, /* tp_print */
761 0, /* tp_getattr */
762 0, /* tp_setattr */
763 0, /* tp_reserved */
764 (reprfunc)module_repr, /* tp_repr */
765 0, /* tp_as_number */
766 0, /* tp_as_sequence */
767 0, /* tp_as_mapping */
768 0, /* tp_hash */
769 0, /* tp_call */
770 0, /* tp_str */
Benjamin Peterson1184e262014-04-24 19:29:23 -0400771 (getattrofunc)module_getattro, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 PyObject_GenericSetAttr, /* tp_setattro */
773 0, /* tp_as_buffer */
774 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
775 Py_TPFLAGS_BASETYPE, /* tp_flags */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200776 module___init____doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 (traverseproc)module_traverse, /* tp_traverse */
778 (inquiry)module_clear, /* tp_clear */
779 0, /* tp_richcompare */
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200780 offsetof(PyModuleObject, md_weaklist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 0, /* tp_iter */
782 0, /* tp_iternext */
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500783 module_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 module_members, /* tp_members */
785 0, /* tp_getset */
786 0, /* tp_base */
787 0, /* tp_dict */
788 0, /* tp_descr_get */
789 0, /* tp_descr_set */
790 offsetof(PyModuleObject, md_dict), /* tp_dictoffset */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200791 module___init__, /* tp_init */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 PyType_GenericAlloc, /* tp_alloc */
793 PyType_GenericNew, /* tp_new */
794 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000795};