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.
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 */
21 PyModule_New(char *name
)
25 m
= PyObject_NEW(PyModuleObject
, &PyModule_Type
);
28 nameobj
= PyString_FromString(name
);
29 m
->md_dict
= PyDict_New();
30 if (m
->md_dict
== NULL
|| nameobj
== NULL
)
32 if (PyDict_SetItemString(m
->md_dict
, "__name__", nameobj
) != 0)
34 if (PyDict_SetItemString(m
->md_dict
, "__doc__", Py_None
) != 0)
46 PyModule_GetDict(PyObject
*m
)
48 if (!PyModule_Check(m
)) {
49 PyErr_BadInternalCall();
52 return ((PyModuleObject
*)m
) -> md_dict
;
56 PyModule_GetName(PyObject
*m
)
59 if (!PyModule_Check(m
)) {
63 nameobj
= PyDict_GetItemString(((PyModuleObject
*)m
)->md_dict
,
65 if (nameobj
== NULL
|| !PyString_Check(nameobj
)) {
66 PyErr_SetString(PyExc_SystemError
, "nameless module");
69 return PyString_AsString(nameobj
);
73 PyModule_GetFilename(PyObject
*m
)
76 if (!PyModule_Check(m
)) {
80 fileobj
= PyDict_GetItemString(((PyModuleObject
*)m
)->md_dict
,
82 if (fileobj
== NULL
|| !PyString_Check(fileobj
)) {
83 PyErr_SetString(PyExc_SystemError
, "module filename missing");
86 return PyString_AsString(fileobj
);
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). */
100 PyObject
*key
, *value
;
103 d
= ((PyModuleObject
*)m
)->md_dict
;
105 /* First, clear only names starting with a single underscore */
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__ */
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'. */
140 module_dealloc(PyModuleObject
*m
)
142 if (m
->md_dict
!= NULL
) {
143 _PyModule_Clear((PyObject
*)m
);
144 Py_DECREF(m
->md_dict
);
150 module_repr(PyModuleObject
*m
)
155 name
= PyModule_GetName((PyObject
*)m
);
160 filename
= PyModule_GetFilename((PyObject
*)m
);
161 if (filename
== NULL
) {
163 sprintf(buf
, "<module '%.80s' (built-in)>", name
);
165 sprintf(buf
, "<module '%.80s' from '%.255s'>", name
, filename
);
168 return PyString_FromString(buf
);
172 module_getattr(PyModuleObject
*m
, char *name
)
175 if (strcmp(name
, "__dict__") == 0) {
176 Py_INCREF(m
->md_dict
);
179 res
= PyDict_GetItemString(m
->md_dict
, name
);
181 PyErr_SetString(PyExc_AttributeError
, name
);
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");
196 int rv
= PyDict_DelItemString(m
->md_dict
, name
);
198 PyErr_SetString(PyExc_AttributeError
,
199 "delete non-existing module attribute");
203 return PyDict_SetItemString(m
->md_dict
, name
, v
);
206 PyTypeObject PyModule_Type
= {
207 PyObject_HEAD_INIT(&PyType_Type
)
209 "module", /*tp_name*/
210 sizeof(PyModuleObject
), /*tp_size*/
212 (destructor
)module_dealloc
, /*tp_dealloc*/
214 (getattrfunc
)module_getattr
, /*tp_getattr*/
215 (setattrfunc
)module_setattr
, /*tp_setattr*/
217 (reprfunc
)module_repr
, /*tp_repr*/