Bump version to 0.9.1.
[python/dscho.git] / Objects / moduleobject.c
blob4e4395e1164aa3e45476db6d6083465d2b45a7ee
1 /***********************************************************
2 Copyright (c) 2000, BeOpen.com.
3 Copyright (c) 1995-2000, Corporation for National Research Initiatives.
4 Copyright (c) 1990-1995, Stichting Mathematisch Centrum.
5 All rights reserved.
7 See the file "Misc/COPYRIGHT" for information on usage and
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
9 ******************************************************************/
11 /* Module object implementation */
13 #include "Python.h"
15 typedef struct {
16 PyObject_HEAD
17 PyObject *md_dict;
18 } PyModuleObject;
20 PyObject *
21 PyModule_New(char *name)
23 PyModuleObject *m;
24 PyObject *nameobj;
25 m = PyObject_NEW(PyModuleObject, &PyModule_Type);
26 if (m == NULL)
27 return NULL;
28 nameobj = PyString_FromString(name);
29 m->md_dict = PyDict_New();
30 if (m->md_dict == NULL || nameobj == NULL)
31 goto fail;
32 if (PyDict_SetItemString(m->md_dict, "__name__", nameobj) != 0)
33 goto fail;
34 if (PyDict_SetItemString(m->md_dict, "__doc__", Py_None) != 0)
35 goto fail;
36 Py_DECREF(nameobj);
37 return (PyObject *)m;
39 fail:
40 Py_XDECREF(nameobj);
41 Py_DECREF(m);
42 return NULL;
45 PyObject *
46 PyModule_GetDict(PyObject *m)
48 if (!PyModule_Check(m)) {
49 PyErr_BadInternalCall();
50 return NULL;
52 return ((PyModuleObject *)m) -> md_dict;
55 char *
56 PyModule_GetName(PyObject *m)
58 PyObject *nameobj;
59 if (!PyModule_Check(m)) {
60 PyErr_BadArgument();
61 return NULL;
63 nameobj = PyDict_GetItemString(((PyModuleObject *)m)->md_dict,
64 "__name__");
65 if (nameobj == NULL || !PyString_Check(nameobj)) {
66 PyErr_SetString(PyExc_SystemError, "nameless module");
67 return NULL;
69 return PyString_AsString(nameobj);
72 char *
73 PyModule_GetFilename(PyObject *m)
75 PyObject *fileobj;
76 if (!PyModule_Check(m)) {
77 PyErr_BadArgument();
78 return NULL;
80 fileobj = PyDict_GetItemString(((PyModuleObject *)m)->md_dict,
81 "__file__");
82 if (fileobj == NULL || !PyString_Check(fileobj)) {
83 PyErr_SetString(PyExc_SystemError, "module filename missing");
84 return NULL;
86 return PyString_AsString(fileobj);
89 void
90 _PyModule_Clear(PyObject *m)
92 /* To make the execution order of destructors for global
93 objects a bit more predictable, we first zap all objects
94 whose name starts with a single underscore, before we clear
95 the entire dictionary. We zap them by replacing them with
96 None, rather than deleting them from the dictionary, to
97 avoid rehashing the dictionary (to some extent). */
99 int pos;
100 PyObject *key, *value;
101 PyObject *d;
103 d = ((PyModuleObject *)m)->md_dict;
105 /* First, clear only names starting with a single underscore */
106 pos = 0;
107 while (PyDict_Next(d, &pos, &key, &value)) {
108 if (value != Py_None && PyString_Check(key)) {
109 char *s = PyString_AsString(key);
110 if (s[0] == '_' && s[1] != '_') {
111 if (Py_VerboseFlag > 1)
112 PySys_WriteStderr("# clear[1] %s\n", s);
113 PyDict_SetItem(d, key, Py_None);
118 /* Next, clear all names except for __builtins__ */
119 pos = 0;
120 while (PyDict_Next(d, &pos, &key, &value)) {
121 if (value != Py_None && PyString_Check(key)) {
122 char *s = PyString_AsString(key);
123 if (s[0] != '_' || strcmp(s, "__builtins__") != 0) {
124 if (Py_VerboseFlag > 1)
125 PySys_WriteStderr("# clear[2] %s\n", s);
126 PyDict_SetItem(d, key, Py_None);
131 /* Note: we leave __builtins__ in place, so that destructors
132 of non-global objects defined in this module can still use
133 builtins, in particularly 'None'. */
137 /* Methods */
139 static void
140 module_dealloc(PyModuleObject *m)
142 if (m->md_dict != NULL) {
143 _PyModule_Clear((PyObject *)m);
144 Py_DECREF(m->md_dict);
146 PyObject_DEL(m);
149 static PyObject *
150 module_repr(PyModuleObject *m)
152 char buf[400];
153 char *name;
154 char *filename;
155 name = PyModule_GetName((PyObject *)m);
156 if (name == NULL) {
157 PyErr_Clear();
158 name = "?";
160 filename = PyModule_GetFilename((PyObject *)m);
161 if (filename == NULL) {
162 PyErr_Clear();
163 sprintf(buf, "<module '%.80s' (built-in)>", name);
164 } else {
165 sprintf(buf, "<module '%.80s' from '%.255s'>", name, filename);
168 return PyString_FromString(buf);
171 static PyObject *
172 module_getattr(PyModuleObject *m, char *name)
174 PyObject *res;
175 if (strcmp(name, "__dict__") == 0) {
176 Py_INCREF(m->md_dict);
177 return m->md_dict;
179 res = PyDict_GetItemString(m->md_dict, name);
180 if (res == NULL)
181 PyErr_SetString(PyExc_AttributeError, name);
182 else
183 Py_INCREF(res);
184 return res;
187 static int
188 module_setattr(PyModuleObject *m, char *name, PyObject *v)
190 if (name[0] == '_' && strcmp(name, "__dict__") == 0) {
191 PyErr_SetString(PyExc_TypeError,
192 "read-only special attribute");
193 return -1;
195 if (v == NULL) {
196 int rv = PyDict_DelItemString(m->md_dict, name);
197 if (rv < 0)
198 PyErr_SetString(PyExc_AttributeError,
199 "delete non-existing module attribute");
200 return rv;
202 else
203 return PyDict_SetItemString(m->md_dict, name, v);
206 PyTypeObject PyModule_Type = {
207 PyObject_HEAD_INIT(&PyType_Type)
208 0, /*ob_size*/
209 "module", /*tp_name*/
210 sizeof(PyModuleObject), /*tp_size*/
211 0, /*tp_itemsize*/
212 (destructor)module_dealloc, /*tp_dealloc*/
213 0, /*tp_print*/
214 (getattrfunc)module_getattr, /*tp_getattr*/
215 (setattrfunc)module_setattr, /*tp_setattr*/
216 0, /*tp_compare*/
217 (reprfunc)module_repr, /*tp_repr*/