1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
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
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 */
47 m
= PyObject_NEW(PyModuleObject
, &PyModule_Type
);
50 nameobj
= PyString_FromString(name
);
51 m
->md_dict
= PyDict_New();
52 if (m
->md_dict
== NULL
|| nameobj
== NULL
)
54 if (PyDict_SetItemString(m
->md_dict
, "__name__", nameobj
) != 0)
56 if (PyDict_SetItemString(m
->md_dict
, "__doc__", Py_None
) != 0)
71 if (!PyModule_Check(m
)) {
72 PyErr_BadInternalCall();
75 return ((PyModuleObject
*)m
) -> md_dict
;
83 if (!PyModule_Check(m
)) {
87 nameobj
= PyDict_GetItemString(((PyModuleObject
*)m
)->md_dict
,
89 if (nameobj
== NULL
|| !PyString_Check(nameobj
)) {
90 PyErr_SetString(PyExc_SystemError
, "nameless module");
93 return PyString_AsString(nameobj
);
97 PyModule_GetFilename(m
)
101 if (!PyModule_Check(m
)) {
105 fileobj
= PyDict_GetItemString(((PyModuleObject
*)m
)->md_dict
,
107 if (fileobj
== NULL
|| !PyString_Check(fileobj
)) {
108 PyErr_SetString(PyExc_SystemError
, "module filename missing");
111 return PyString_AsString(fileobj
);
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). */
126 PyObject
*key
, *value
;
129 d
= ((PyModuleObject
*)m
)->md_dict
;
131 /* First, clear only names starting with a single underscore */
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__ */
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'. */
169 if (m
->md_dict
!= NULL
) {
170 _PyModule_Clear((PyObject
*)m
);
171 Py_DECREF(m
->md_dict
);
183 name
= PyModule_GetName((PyObject
*)m
);
188 filename
= PyModule_GetFilename((PyObject
*)m
);
189 if (filename
== NULL
) {
191 sprintf(buf
, "<module '%.80s' (built-in)>", name
);
193 sprintf(buf
, "<module '%.80s' from '%.255s'>", name
, filename
);
196 return PyString_FromString(buf
);
200 module_getattr(m
, name
)
205 if (strcmp(name
, "__dict__") == 0) {
206 Py_INCREF(m
->md_dict
);
209 res
= PyDict_GetItemString(m
->md_dict
, name
);
211 PyErr_SetString(PyExc_AttributeError
, name
);
218 module_setattr(m
, name
, v
)
223 if (name
[0] == '_' && strcmp(name
, "__dict__") == 0) {
224 PyErr_SetString(PyExc_TypeError
,
225 "read-only special attribute");
229 int rv
= PyDict_DelItemString(m
->md_dict
, name
);
231 PyErr_SetString(PyExc_AttributeError
,
232 "delete non-existing module attribute");
236 return PyDict_SetItemString(m
->md_dict
, name
, v
);
239 PyTypeObject PyModule_Type
= {
240 PyObject_HEAD_INIT(&PyType_Type
)
242 "module", /*tp_name*/
243 sizeof(PyModuleObject
), /*tp_size*/
245 (destructor
)module_dealloc
, /*tp_dealloc*/
247 (getattrfunc
)module_getattr
, /*tp_getattr*/
248 (setattrfunc
)module_setattr
, /*tp_setattr*/
250 (reprfunc
)module_repr
, /*tp_repr*/