This commit was manufactured by cvs2svn to create tag
[python/dscho.git] / Objects / moduleobject.c
blob180f7bc3c72e9b36004d05b89a64495e77cc0e4b
2 /* Module object implementation */
4 #include "Python.h"
5 #include "structmember.h"
7 typedef struct {
8 PyObject_HEAD
9 PyObject *md_dict;
10 } PyModuleObject;
12 static PyMemberDef module_members[] = {
13 {"__dict__", T_OBJECT, offsetof(PyModuleObject, md_dict), READONLY},
14 {0}
17 PyObject *
18 PyModule_New(char *name)
20 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 Py_DECREF(nameobj);
34 PyObject_GC_Track(m);
35 return (PyObject *)m;
37 fail:
38 Py_XDECREF(nameobj);
39 Py_DECREF(m);
40 return NULL;
43 PyObject *
44 PyModule_GetDict(PyObject *m)
46 if (!PyModule_Check(m)) {
47 PyErr_BadInternalCall();
48 return NULL;
50 return ((PyModuleObject *)m) -> md_dict;
53 char *
54 PyModule_GetName(PyObject *m)
56 PyObject *nameobj;
57 if (!PyModule_Check(m)) {
58 PyErr_BadArgument();
59 return NULL;
61 nameobj = PyDict_GetItemString(((PyModuleObject *)m)->md_dict,
62 "__name__");
63 if (nameobj == NULL || !PyString_Check(nameobj)) {
64 PyErr_SetString(PyExc_SystemError, "nameless module");
65 return NULL;
67 return PyString_AsString(nameobj);
70 char *
71 PyModule_GetFilename(PyObject *m)
73 PyObject *fileobj;
74 if (!PyModule_Check(m)) {
75 PyErr_BadArgument();
76 return NULL;
78 fileobj = PyDict_GetItemString(((PyModuleObject *)m)->md_dict,
79 "__file__");
80 if (fileobj == NULL || !PyString_Check(fileobj)) {
81 PyErr_SetString(PyExc_SystemError, "module filename missing");
82 return NULL;
84 return PyString_AsString(fileobj);
87 void
88 _PyModule_Clear(PyObject *m)
90 /* To make the execution order of destructors for global
91 objects a bit more predictable, we first zap all objects
92 whose name starts with a single underscore, before we clear
93 the entire dictionary. We zap them by replacing them with
94 None, rather than deleting them from the dictionary, to
95 avoid rehashing the dictionary (to some extent). */
97 int pos;
98 PyObject *key, *value;
99 PyObject *d;
101 d = ((PyModuleObject *)m)->md_dict;
103 /* First, clear only names starting with a single underscore */
104 pos = 0;
105 while (PyDict_Next(d, &pos, &key, &value)) {
106 if (value != Py_None && PyString_Check(key)) {
107 char *s = PyString_AsString(key);
108 if (s[0] == '_' && s[1] != '_') {
109 if (Py_VerboseFlag > 1)
110 PySys_WriteStderr("# clear[1] %s\n", s);
111 PyDict_SetItem(d, key, Py_None);
116 /* Next, clear all names except for __builtins__ */
117 pos = 0;
118 while (PyDict_Next(d, &pos, &key, &value)) {
119 if (value != Py_None && PyString_Check(key)) {
120 char *s = PyString_AsString(key);
121 if (s[0] != '_' || strcmp(s, "__builtins__") != 0) {
122 if (Py_VerboseFlag > 1)
123 PySys_WriteStderr("# clear[2] %s\n", s);
124 PyDict_SetItem(d, key, Py_None);
129 /* Note: we leave __builtins__ in place, so that destructors
130 of non-global objects defined in this module can still use
131 builtins, in particularly 'None'. */
135 /* Methods */
137 static int
138 module_init(PyModuleObject *m, PyObject *args, PyObject *kw)
140 m->md_dict = PyDict_New();
141 if (m->md_dict == NULL)
142 return -1;
143 return 0;
146 static void
147 module_dealloc(PyModuleObject *m)
149 PyObject_GC_UnTrack(m);
150 if (m->md_dict != NULL) {
151 _PyModule_Clear((PyObject *)m);
152 Py_DECREF(m->md_dict);
154 m->ob_type->tp_free((PyObject *)m);
157 static PyObject *
158 module_repr(PyModuleObject *m)
160 char *name;
161 char *filename;
163 name = PyModule_GetName((PyObject *)m);
164 if (name == NULL) {
165 PyErr_Clear();
166 name = "?";
168 filename = PyModule_GetFilename((PyObject *)m);
169 if (filename == NULL) {
170 PyErr_Clear();
171 return PyString_FromFormat("<module '%s' (built-in)>", name);
173 return PyString_FromFormat("<module '%s' from '%s'>", name, filename);
176 /* We only need a traverse function, no clear function: If the module
177 is in a cycle, md_dict will be cleared as well, which will break
178 the cycle. */
179 static int
180 module_traverse(PyModuleObject *m, visitproc visit, void *arg)
182 if (m->md_dict != NULL)
183 return visit(m->md_dict, arg);
184 return 0;
187 PyTypeObject PyModule_Type = {
188 PyObject_HEAD_INIT(&PyType_Type)
189 0, /* ob_size */
190 "module", /* tp_name */
191 sizeof(PyModuleObject), /* tp_size */
192 0, /* tp_itemsize */
193 (destructor)module_dealloc, /* tp_dealloc */
194 0, /* tp_print */
195 0, /* tp_getattr */
196 0, /* tp_setattr */
197 0, /* tp_compare */
198 (reprfunc)module_repr, /* tp_repr */
199 0, /* tp_as_number */
200 0, /* tp_as_sequence */
201 0, /* tp_as_mapping */
202 0, /* tp_hash */
203 0, /* tp_call */
204 0, /* tp_str */
205 PyObject_GenericGetAttr, /* tp_getattro */
206 PyObject_GenericSetAttr, /* tp_setattro */
207 0, /* tp_as_buffer */
208 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
209 Py_TPFLAGS_BASETYPE, /* tp_flags */
210 0, /* tp_doc */
211 (traverseproc)module_traverse, /* tp_traverse */
212 0, /* tp_clear */
213 0, /* tp_richcompare */
214 0, /* tp_weaklistoffset */
215 0, /* tp_iter */
216 0, /* tp_iternext */
217 0, /* tp_methods */
218 module_members, /* tp_members */
219 0, /* tp_getset */
220 0, /* tp_base */
221 0, /* tp_dict */
222 0, /* tp_descr_get */
223 0, /* tp_descr_set */
224 offsetof(PyModuleObject, md_dict), /* tp_dictoffset */
225 (initproc)module_init, /* tp_init */
226 PyType_GenericAlloc, /* tp_alloc */
227 PyType_GenericNew, /* tp_new */
228 _PyObject_GC_Del, /* tp_free */