2 /* Module object implementation */
12 PyModule_New(char *name
)
16 m
= PyObject_NEW(PyModuleObject
, &PyModule_Type
);
19 nameobj
= PyString_FromString(name
);
20 m
->md_dict
= PyDict_New();
22 if (m
->md_dict
== NULL
|| nameobj
== NULL
)
24 if (PyDict_SetItemString(m
->md_dict
, "__name__", nameobj
) != 0)
26 if (PyDict_SetItemString(m
->md_dict
, "__doc__", Py_None
) != 0)
38 PyModule_GetDict(PyObject
*m
)
40 if (!PyModule_Check(m
)) {
41 PyErr_BadInternalCall();
44 return ((PyModuleObject
*)m
) -> md_dict
;
48 PyModule_GetName(PyObject
*m
)
51 if (!PyModule_Check(m
)) {
55 nameobj
= PyDict_GetItemString(((PyModuleObject
*)m
)->md_dict
,
57 if (nameobj
== NULL
|| !PyString_Check(nameobj
)) {
58 PyErr_SetString(PyExc_SystemError
, "nameless module");
61 return PyString_AsString(nameobj
);
65 PyModule_GetFilename(PyObject
*m
)
68 if (!PyModule_Check(m
)) {
72 fileobj
= PyDict_GetItemString(((PyModuleObject
*)m
)->md_dict
,
74 if (fileobj
== NULL
|| !PyString_Check(fileobj
)) {
75 PyErr_SetString(PyExc_SystemError
, "module filename missing");
78 return PyString_AsString(fileobj
);
82 _PyModule_Clear(PyObject
*m
)
84 /* To make the execution order of destructors for global
85 objects a bit more predictable, we first zap all objects
86 whose name starts with a single underscore, before we clear
87 the entire dictionary. We zap them by replacing them with
88 None, rather than deleting them from the dictionary, to
89 avoid rehashing the dictionary (to some extent). */
92 PyObject
*key
, *value
;
95 d
= ((PyModuleObject
*)m
)->md_dict
;
97 /* First, clear only names starting with a single underscore */
99 while (PyDict_Next(d
, &pos
, &key
, &value
)) {
100 if (value
!= Py_None
&& PyString_Check(key
)) {
101 char *s
= PyString_AsString(key
);
102 if (s
[0] == '_' && s
[1] != '_') {
103 if (Py_VerboseFlag
> 1)
104 PySys_WriteStderr("# clear[1] %s\n", s
);
105 PyDict_SetItem(d
, key
, Py_None
);
110 /* Next, clear all names except for __builtins__ */
112 while (PyDict_Next(d
, &pos
, &key
, &value
)) {
113 if (value
!= Py_None
&& PyString_Check(key
)) {
114 char *s
= PyString_AsString(key
);
115 if (s
[0] != '_' || strcmp(s
, "__builtins__") != 0) {
116 if (Py_VerboseFlag
> 1)
117 PySys_WriteStderr("# clear[2] %s\n", s
);
118 PyDict_SetItem(d
, key
, Py_None
);
123 /* Note: we leave __builtins__ in place, so that destructors
124 of non-global objects defined in this module can still use
125 builtins, in particularly 'None'. */
132 module_dealloc(PyModuleObject
*m
)
135 if (m
->md_dict
!= NULL
) {
136 _PyModule_Clear((PyObject
*)m
);
137 Py_DECREF(m
->md_dict
);
139 PyObject_DEL(PyObject_AS_GC(m
));
143 module_repr(PyModuleObject
*m
)
148 name
= PyModule_GetName((PyObject
*)m
);
153 filename
= PyModule_GetFilename((PyObject
*)m
);
154 if (filename
== NULL
) {
156 sprintf(buf
, "<module '%.80s' (built-in)>", name
);
158 sprintf(buf
, "<module '%.80s' from '%.255s'>", name
, filename
);
161 return PyString_FromString(buf
);
165 module_getattr(PyModuleObject
*m
, char *name
)
169 if (strcmp(name
, "__dict__") == 0) {
170 Py_INCREF(m
->md_dict
);
173 res
= PyDict_GetItemString(m
->md_dict
, name
);
175 modname
= PyModule_GetName((PyObject
*)m
);
176 if (modname
== NULL
) {
180 PyErr_Format(PyExc_AttributeError
,
181 "'%.50s' module has no attribute '%.400s'",
190 module_setattr(PyModuleObject
*m
, char *name
, PyObject
*v
)
193 if (name
[0] == '_' && strcmp(name
, "__dict__") == 0) {
194 PyErr_SetString(PyExc_TypeError
,
195 "read-only special attribute");
199 int rv
= PyDict_DelItemString(m
->md_dict
, name
);
201 modname
= PyModule_GetName((PyObject
*)m
);
202 if (modname
== NULL
) {
206 PyErr_Format(PyExc_AttributeError
,
207 "'%.50s' module has no attribute '%.400s'",
213 return PyDict_SetItemString(m
->md_dict
, name
, v
);
216 /* We only need a traverse function, no clear function: If the module
217 is in a cycle, md_dict will be cleared as well, which will break
220 module_traverse(PyModuleObject
*m
, visitproc visit
, void *arg
)
222 if (m
->md_dict
!= NULL
)
223 return visit(m
->md_dict
, arg
);
227 PyTypeObject PyModule_Type
= {
228 PyObject_HEAD_INIT(&PyType_Type
)
230 "module", /*tp_name*/
231 sizeof(PyModuleObject
) + PyGC_HEAD_SIZE
, /*tp_size*/
233 (destructor
)module_dealloc
, /*tp_dealloc*/
235 (getattrfunc
)module_getattr
, /*tp_getattr*/
236 (setattrfunc
)module_setattr
, /*tp_setattr*/
238 (reprfunc
)module_repr
, /*tp_repr*/
240 0, /*tp_as_sequence*/
247 0, /* tp_as_buffer */
248 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_GC
, /*tp_flags*/
250 (traverseproc
)module_traverse
, /* tp_traverse */