blob: ced16ebd9e047fe7f6875bedcc4ee56181698321 [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"
Tim Peters6d6c1a32001-08-02 04:15:00 +00005#include "structmember.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006
7typedef struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00008 PyObject_HEAD
9 PyObject *md_dict;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000010} PyModuleObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000011
Neil Schemenauerf23473f2001-10-21 22:28:58 +000012static PyMemberDef module_members[] = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +000013 {"__dict__", T_OBJECT, offsetof(PyModuleObject, md_dict), READONLY},
14 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +000015};
16
Guido van Rossumc0b618a1997-05-02 03:12:38 +000017PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000018PyModule_New(const char *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000019{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000020 PyModuleObject *m;
21 PyObject *nameobj;
22 m = PyObject_GC_New(PyModuleObject, &PyModule_Type);
23 if (m == NULL)
24 return NULL;
25 nameobj = PyString_FromString(name);
26 m->md_dict = PyDict_New();
27 if (m->md_dict == NULL || nameobj == NULL)
28 goto fail;
29 if (PyDict_SetItemString(m->md_dict, "__name__", nameobj) != 0)
30 goto fail;
31 if (PyDict_SetItemString(m->md_dict, "__doc__", Py_None) != 0)
32 goto fail;
33 if (PyDict_SetItemString(m->md_dict, "__package__", Py_None) != 0)
34 goto fail;
35 Py_DECREF(nameobj);
36 PyObject_GC_Track(m);
37 return (PyObject *)m;
Guido van Rossumc45611d1993-11-17 22:58:56 +000038
39 fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +000040 Py_XDECREF(nameobj);
41 Py_DECREF(m);
42 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000043}
44
Guido van Rossumc0b618a1997-05-02 03:12:38 +000045PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +000046PyModule_GetDict(PyObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000047{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000048 PyObject *d;
49 if (!PyModule_Check(m)) {
50 PyErr_BadInternalCall();
51 return NULL;
52 }
53 d = ((PyModuleObject *)m) -> md_dict;
54 if (d == NULL)
55 ((PyModuleObject *)m) -> md_dict = d = PyDict_New();
56 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057}
58
Guido van Rossum0558a201990-10-26 15:00:11 +000059char *
Fred Drakeee238b92000-07-09 06:03:25 +000060PyModule_GetName(PyObject *m)
Guido van Rossum0558a201990-10-26 15:00:11 +000061{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000062 PyObject *d;
63 PyObject *nameobj;
64 if (!PyModule_Check(m)) {
65 PyErr_BadArgument();
66 return NULL;
67 }
68 d = ((PyModuleObject *)m)->md_dict;
69 if (d == NULL ||
70 (nameobj = PyDict_GetItemString(d, "__name__")) == NULL ||
71 !PyString_Check(nameobj))
72 {
73 PyErr_SetString(PyExc_SystemError, "nameless module");
74 return NULL;
75 }
76 return PyString_AsString(nameobj);
Guido van Rossum0558a201990-10-26 15:00:11 +000077}
78
Guido van Rossum98cc19f1999-02-15 14:47:16 +000079char *
Fred Drakeee238b92000-07-09 06:03:25 +000080PyModule_GetFilename(PyObject *m)
Guido van Rossum98cc19f1999-02-15 14:47:16 +000081{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000082 PyObject *d;
83 PyObject *fileobj;
84 if (!PyModule_Check(m)) {
85 PyErr_BadArgument();
86 return NULL;
87 }
88 d = ((PyModuleObject *)m)->md_dict;
89 if (d == NULL ||
90 (fileobj = PyDict_GetItemString(d, "__file__")) == NULL ||
91 !PyString_Check(fileobj))
92 {
93 PyErr_SetString(PyExc_SystemError, "module filename missing");
94 return NULL;
95 }
96 return PyString_AsString(fileobj);
Guido van Rossum98cc19f1999-02-15 14:47:16 +000097}
98
Guido van Rossumf1dc0611998-02-19 20:51:52 +000099void
Fred Drakeee238b92000-07-09 06:03:25 +0000100_PyModule_Clear(PyObject *m)
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000101{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000102 /* To make the execution order of destructors for global
103 objects a bit more predictable, we first zap all objects
104 whose name starts with a single underscore, before we clear
105 the entire dictionary. We zap them by replacing them with
106 None, rather than deleting them from the dictionary, to
107 avoid rehashing the dictionary (to some extent). */
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000108
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000109 Py_ssize_t pos;
110 PyObject *key, *value;
111 PyObject *d;
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000112
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000113 d = ((PyModuleObject *)m)->md_dict;
114 if (d == NULL)
115 return;
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000116
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000117 /* First, clear only names starting with a single underscore */
118 pos = 0;
119 while (PyDict_Next(d, &pos, &key, &value)) {
120 if (value != Py_None && PyString_Check(key)) {
121 char *s = PyString_AsString(key);
122 if (s[0] == '_' && s[1] != '_') {
123 if (Py_VerboseFlag > 1)
124 PySys_WriteStderr("# clear[1] %s\n", s);
125 PyDict_SetItem(d, key, Py_None);
126 }
127 }
128 }
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000129
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000130 /* Next, clear all names except for __builtins__ */
131 pos = 0;
132 while (PyDict_Next(d, &pos, &key, &value)) {
133 if (value != Py_None && PyString_Check(key)) {
134 char *s = PyString_AsString(key);
135 if (s[0] != '_' || strcmp(s, "__builtins__") != 0) {
136 if (Py_VerboseFlag > 1)
137 PySys_WriteStderr("# clear[2] %s\n", s);
138 PyDict_SetItem(d, key, Py_None);
139 }
140 }
141 }
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000142
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000143 /* Note: we leave __builtins__ in place, so that destructors
144 of non-global objects defined in this module can still use
145 builtins, in particularly 'None'. */
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000146
147}
148
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000149/* Methods */
150
Tim Peters6d6c1a32001-08-02 04:15:00 +0000151static int
Guido van Rossumc3a787e2002-06-04 05:52:47 +0000152module_init(PyModuleObject *m, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000153{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000154 static char *kwlist[] = {"name", "doc", NULL};
155 PyObject *dict, *name = Py_None, *doc = Py_None;
156 if (!PyArg_ParseTupleAndKeywords(args, kwds, "S|O:module.__init__",
157 kwlist, &name, &doc))
158 return -1;
159 dict = m->md_dict;
160 if (dict == NULL) {
161 dict = PyDict_New();
162 if (dict == NULL)
163 return -1;
164 m->md_dict = dict;
165 }
166 if (PyDict_SetItemString(dict, "__name__", name) < 0)
167 return -1;
168 if (PyDict_SetItemString(dict, "__doc__", doc) < 0)
169 return -1;
170 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000171}
172
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000173static void
Fred Drakeee238b92000-07-09 06:03:25 +0000174module_dealloc(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000175{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000176 PyObject_GC_UnTrack(m);
177 if (m->md_dict != NULL) {
178 /* If we are the only ones holding a reference, we can clear
179 the dictionary. */
180 if (Py_REFCNT(m->md_dict) == 1)
181 _PyModule_Clear((PyObject *)m);
182 Py_DECREF(m->md_dict);
183 }
184 Py_TYPE(m)->tp_free((PyObject *)m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000185}
186
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000187static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000188module_repr(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000189{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000190 char *name;
191 char *filename;
Barry Warsaw2907fe62001-08-16 20:39:24 +0000192
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000193 name = PyModule_GetName((PyObject *)m);
194 if (name == NULL) {
195 PyErr_Clear();
196 name = "?";
197 }
198 filename = PyModule_GetFilename((PyObject *)m);
199 if (filename == NULL) {
200 PyErr_Clear();
201 return PyString_FromFormat("<module '%s' (built-in)>", name);
202 }
203 return PyString_FromFormat("<module '%s' from '%s'>", name, filename);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000204}
205
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000206/* We only need a traverse function, no clear function: If the module
207 is in a cycle, md_dict will be cleared as well, which will break
208 the cycle. */
209static int
210module_traverse(PyModuleObject *m, visitproc visit, void *arg)
211{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000212 Py_VISIT(m->md_dict);
213 return 0;
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000214}
215
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000216PyDoc_STRVAR(module_doc,
Guido van Rossumed3e09f2002-06-04 06:02:35 +0000217"module(name[, doc])\n\
218\n\
219Create a module object.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000220The name must be a string; the optional doc argument can have any type.");
Guido van Rossumed3e09f2002-06-04 06:02:35 +0000221
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000222PyTypeObject PyModule_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000223 PyVarObject_HEAD_INIT(&PyType_Type, 0)
224 "module", /* tp_name */
225 sizeof(PyModuleObject), /* tp_size */
226 0, /* tp_itemsize */
227 (destructor)module_dealloc, /* tp_dealloc */
228 0, /* tp_print */
229 0, /* tp_getattr */
230 0, /* tp_setattr */
231 0, /* tp_compare */
232 (reprfunc)module_repr, /* tp_repr */
233 0, /* tp_as_number */
234 0, /* tp_as_sequence */
235 0, /* tp_as_mapping */
236 0, /* tp_hash */
237 0, /* tp_call */
238 0, /* tp_str */
239 PyObject_GenericGetAttr, /* tp_getattro */
240 PyObject_GenericSetAttr, /* tp_setattro */
241 0, /* tp_as_buffer */
242 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
243 Py_TPFLAGS_BASETYPE, /* tp_flags */
244 module_doc, /* tp_doc */
245 (traverseproc)module_traverse, /* tp_traverse */
246 0, /* tp_clear */
247 0, /* tp_richcompare */
248 0, /* tp_weaklistoffset */
249 0, /* tp_iter */
250 0, /* tp_iternext */
251 0, /* tp_methods */
252 module_members, /* tp_members */
253 0, /* tp_getset */
254 0, /* tp_base */
255 0, /* tp_dict */
256 0, /* tp_descr_get */
257 0, /* tp_descr_set */
258 offsetof(PyModuleObject, md_dict), /* tp_dictoffset */
259 (initproc)module_init, /* tp_init */
260 PyType_GenericAlloc, /* tp_alloc */
261 PyType_GenericNew, /* tp_new */
262 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000263};