Fix an amazing number of typos & malformed sentences reported by Detlef
[python/dscho.git] / Objects / moduleobject.c
blob7dfca731e51981706a686d200a26874e2a92dd90
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
15 permission.
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 /* Module object implementation */
34 #include "Python.h"
36 typedef struct {
37 PyObject_HEAD
38 PyObject *md_dict;
39 } PyModuleObject;
41 PyObject *
42 PyModule_New(name)
43 char *name;
45 PyModuleObject *m;
46 PyObject *nameobj;
47 m = PyObject_NEW(PyModuleObject, &PyModule_Type);
48 if (m == NULL)
49 return NULL;
50 nameobj = PyString_FromString(name);
51 m->md_dict = PyDict_New();
52 if (m->md_dict == NULL || nameobj == NULL)
53 goto fail;
54 if (PyDict_SetItemString(m->md_dict, "__name__", nameobj) != 0)
55 goto fail;
56 if (PyDict_SetItemString(m->md_dict, "__doc__", Py_None) != 0)
57 goto fail;
58 Py_DECREF(nameobj);
59 return (PyObject *)m;
61 fail:
62 Py_XDECREF(nameobj);
63 Py_DECREF(m);
64 return NULL;
67 PyObject *
68 PyModule_GetDict(m)
69 PyObject *m;
71 if (!PyModule_Check(m)) {
72 PyErr_BadInternalCall();
73 return NULL;
75 return ((PyModuleObject *)m) -> md_dict;
78 char *
79 PyModule_GetName(m)
80 PyObject *m;
82 PyObject *nameobj;
83 if (!PyModule_Check(m)) {
84 PyErr_BadArgument();
85 return NULL;
87 nameobj = PyDict_GetItemString(((PyModuleObject *)m)->md_dict,
88 "__name__");
89 if (nameobj == NULL || !PyString_Check(nameobj)) {
90 PyErr_SetString(PyExc_SystemError, "nameless module");
91 return NULL;
93 return PyString_AsString(nameobj);
96 char *
97 PyModule_GetFilename(m)
98 PyObject *m;
100 PyObject *fileobj;
101 if (!PyModule_Check(m)) {
102 PyErr_BadArgument();
103 return NULL;
105 fileobj = PyDict_GetItemString(((PyModuleObject *)m)->md_dict,
106 "__file__");
107 if (fileobj == NULL || !PyString_Check(fileobj)) {
108 PyErr_SetString(PyExc_SystemError, "module filename missing");
109 return NULL;
111 return PyString_AsString(fileobj);
114 void
115 _PyModule_Clear(m)
116 PyObject *m;
118 /* To make the execution order of destructors for global
119 objects a bit more predictable, we first zap all objects
120 whose name starts with a single underscore, before we clear
121 the entire dictionary. We zap them by replacing them with
122 None, rather than deleting them from the dictionary, to
123 avoid rehashing the dictionary (to some extent). */
125 int pos;
126 PyObject *key, *value;
127 PyObject *d;
129 d = ((PyModuleObject *)m)->md_dict;
131 /* First, clear only names starting with a single underscore */
132 pos = 0;
133 while (PyDict_Next(d, &pos, &key, &value)) {
134 if (value != Py_None && PyString_Check(key)) {
135 char *s = PyString_AsString(key);
136 if (s[0] == '_' && s[1] != '_') {
137 if (Py_VerboseFlag > 1)
138 PySys_WriteStderr("# clear[1] %s\n", s);
139 PyDict_SetItem(d, key, Py_None);
144 /* Next, clear all names except for __builtins__ */
145 pos = 0;
146 while (PyDict_Next(d, &pos, &key, &value)) {
147 if (value != Py_None && PyString_Check(key)) {
148 char *s = PyString_AsString(key);
149 if (s[0] != '_' || strcmp(s, "__builtins__") != 0) {
150 if (Py_VerboseFlag > 1)
151 PySys_WriteStderr("# clear[2] %s\n", s);
152 PyDict_SetItem(d, key, Py_None);
157 /* Note: we leave __builtins__ in place, so that destructors
158 of non-global objects defined in this module can still use
159 builtins, in particularly 'None'. */
163 /* Methods */
165 static void
166 module_dealloc(m)
167 PyModuleObject *m;
169 if (m->md_dict != NULL) {
170 _PyModule_Clear((PyObject *)m);
171 Py_DECREF(m->md_dict);
173 free((char *)m);
176 static PyObject *
177 module_repr(m)
178 PyModuleObject *m;
180 char buf[400];
181 char *name;
182 char *filename;
183 name = PyModule_GetName((PyObject *)m);
184 if (name == NULL) {
185 PyErr_Clear();
186 name = "?";
188 filename = PyModule_GetFilename((PyObject *)m);
189 if (filename == NULL) {
190 PyErr_Clear();
191 sprintf(buf, "<module '%.80s' (built-in)>", name);
192 } else {
193 sprintf(buf, "<module '%.80s' from '%.255s'>", name, filename);
196 return PyString_FromString(buf);
199 static PyObject *
200 module_getattr(m, name)
201 PyModuleObject *m;
202 char *name;
204 PyObject *res;
205 if (strcmp(name, "__dict__") == 0) {
206 Py_INCREF(m->md_dict);
207 return m->md_dict;
209 res = PyDict_GetItemString(m->md_dict, name);
210 if (res == NULL)
211 PyErr_SetString(PyExc_AttributeError, name);
212 else
213 Py_INCREF(res);
214 return res;
217 static int
218 module_setattr(m, name, v)
219 PyModuleObject *m;
220 char *name;
221 PyObject *v;
223 if (name[0] == '_' && strcmp(name, "__dict__") == 0) {
224 PyErr_SetString(PyExc_TypeError,
225 "read-only special attribute");
226 return -1;
228 if (v == NULL) {
229 int rv = PyDict_DelItemString(m->md_dict, name);
230 if (rv < 0)
231 PyErr_SetString(PyExc_AttributeError,
232 "delete non-existing module attribute");
233 return rv;
235 else
236 return PyDict_SetItemString(m->md_dict, name, v);
239 PyTypeObject PyModule_Type = {
240 PyObject_HEAD_INIT(&PyType_Type)
241 0, /*ob_size*/
242 "module", /*tp_name*/
243 sizeof(PyModuleObject), /*tp_size*/
244 0, /*tp_itemsize*/
245 (destructor)module_dealloc, /*tp_dealloc*/
246 0, /*tp_print*/
247 (getattrfunc)module_getattr, /*tp_getattr*/
248 (setattrfunc)module_setattr, /*tp_setattr*/
249 0, /*tp_compare*/
250 (reprfunc)module_repr, /*tp_repr*/