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_getattro(PyModuleObject
*m
, PyObject
*name
)
168 char *sname
= PyString_AsString(name
);
170 if (sname
[0] == '_' && strcmp(sname
, "__dict__") == 0) {
171 Py_INCREF(m
->md_dict
);
174 res
= PyDict_GetItem(m
->md_dict
, name
);
176 char *modname
= PyModule_GetName((PyObject
*)m
);
177 if (modname
== NULL
) {
181 PyErr_Format(PyExc_AttributeError
,
182 "'%.50s' module has no attribute '%.400s'",
191 module_setattro(PyModuleObject
*m
, PyObject
*name
, PyObject
*v
)
193 char *sname
= PyString_AsString(name
);
194 if (sname
[0] == '_' && strcmp(sname
, "__dict__") == 0) {
195 PyErr_SetString(PyExc_TypeError
,
196 "read-only special attribute");
200 int rv
= PyDict_DelItem(m
->md_dict
, name
);
202 char *modname
= PyModule_GetName((PyObject
*)m
);
203 if (modname
== NULL
) {
207 PyErr_Format(PyExc_AttributeError
,
208 "'%.50s' module has no attribute '%.400s'",
214 return PyDict_SetItem(m
->md_dict
, name
, v
);
217 /* We only need a traverse function, no clear function: If the module
218 is in a cycle, md_dict will be cleared as well, which will break
221 module_traverse(PyModuleObject
*m
, visitproc visit
, void *arg
)
223 if (m
->md_dict
!= NULL
)
224 return visit(m
->md_dict
, arg
);
228 PyTypeObject PyModule_Type
= {
229 PyObject_HEAD_INIT(&PyType_Type
)
231 "module", /* tp_name */
232 sizeof(PyModuleObject
) + PyGC_HEAD_SIZE
,/* tp_size */
234 (destructor
)module_dealloc
, /* tp_dealloc */
239 (reprfunc
)module_repr
, /* tp_repr */
240 0, /* tp_as_number */
241 0, /* tp_as_sequence */
242 0, /* tp_as_mapping */
246 (getattrofunc
)module_getattro
, /* tp_getattro */
247 (setattrofunc
)module_setattro
, /* tp_setattro */
248 0, /* tp_as_buffer */
249 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_GC
, /* tp_flags */
251 (traverseproc
)module_traverse
, /* tp_traverse */