2 /* Module object implementation */
5 #include "structmember.h"
12 struct memberlist module_members
[] = {
13 {"__dict__", T_OBJECT
, offsetof(PyModuleObject
, md_dict
), READONLY
},
18 PyModule_New(char *name
)
22 m
= PyObject_NEW(PyModuleObject
, &PyModule_Type
);
25 nameobj
= PyString_FromString(name
);
26 m
->md_dict
= PyDict_New();
28 if (m
->md_dict
== NULL
|| nameobj
== NULL
)
30 if (PyDict_SetItemString(m
->md_dict
, "__name__", nameobj
) != 0)
32 if (PyDict_SetItemString(m
->md_dict
, "__doc__", Py_None
) != 0)
44 PyModule_GetDict(PyObject
*m
)
46 if (!PyModule_Check(m
)) {
47 PyErr_BadInternalCall();
50 return ((PyModuleObject
*)m
) -> md_dict
;
54 PyModule_GetName(PyObject
*m
)
57 if (!PyModule_Check(m
)) {
61 nameobj
= PyDict_GetItemString(((PyModuleObject
*)m
)->md_dict
,
63 if (nameobj
== NULL
|| !PyString_Check(nameobj
)) {
64 PyErr_SetString(PyExc_SystemError
, "nameless module");
67 return PyString_AsString(nameobj
);
71 PyModule_GetFilename(PyObject
*m
)
74 if (!PyModule_Check(m
)) {
78 fileobj
= PyDict_GetItemString(((PyModuleObject
*)m
)->md_dict
,
80 if (fileobj
== NULL
|| !PyString_Check(fileobj
)) {
81 PyErr_SetString(PyExc_SystemError
, "module filename missing");
84 return PyString_AsString(fileobj
);
88 _PyModule_Clear(PyObject
*m
)
90 /* To make the execution order of destructors for global
91 objects a bit more predictable, we first zap all objects
92 whose name starts with a single underscore, before we clear
93 the entire dictionary. We zap them by replacing them with
94 None, rather than deleting them from the dictionary, to
95 avoid rehashing the dictionary (to some extent). */
98 PyObject
*key
, *value
;
101 d
= ((PyModuleObject
*)m
)->md_dict
;
103 /* First, clear only names starting with a single underscore */
105 while (PyDict_Next(d
, &pos
, &key
, &value
)) {
106 if (value
!= Py_None
&& PyString_Check(key
)) {
107 char *s
= PyString_AsString(key
);
108 if (s
[0] == '_' && s
[1] != '_') {
109 if (Py_VerboseFlag
> 1)
110 PySys_WriteStderr("# clear[1] %s\n", s
);
111 PyDict_SetItem(d
, key
, Py_None
);
116 /* Next, clear all names except for __builtins__ */
118 while (PyDict_Next(d
, &pos
, &key
, &value
)) {
119 if (value
!= Py_None
&& PyString_Check(key
)) {
120 char *s
= PyString_AsString(key
);
121 if (s
[0] != '_' || strcmp(s
, "__builtins__") != 0) {
122 if (Py_VerboseFlag
> 1)
123 PySys_WriteStderr("# clear[2] %s\n", s
);
124 PyDict_SetItem(d
, key
, Py_None
);
129 /* Note: we leave __builtins__ in place, so that destructors
130 of non-global objects defined in this module can still use
131 builtins, in particularly 'None'. */
138 module_init(PyModuleObject
*m
, PyObject
*args
, PyObject
*kw
)
140 m
->md_dict
= PyDict_New();
141 if (m
->md_dict
== NULL
)
147 module_dealloc(PyModuleObject
*m
)
150 if (m
->md_dict
!= NULL
) {
151 _PyModule_Clear((PyObject
*)m
);
152 Py_DECREF(m
->md_dict
);
154 PyObject_DEL(PyObject_AS_GC(m
));
158 module_repr(PyModuleObject
*m
)
163 name
= PyModule_GetName((PyObject
*)m
);
168 filename
= PyModule_GetFilename((PyObject
*)m
);
169 if (filename
== NULL
) {
171 sprintf(buf
, "<module '%.80s' (built-in)>", name
);
173 sprintf(buf
, "<module '%.80s' from '%.255s'>", name
, filename
);
176 return PyString_FromString(buf
);
179 /* We only need a traverse function, no clear function: If the module
180 is in a cycle, md_dict will be cleared as well, which will break
183 module_traverse(PyModuleObject
*m
, visitproc visit
, void *arg
)
185 if (m
->md_dict
!= NULL
)
186 return visit(m
->md_dict
, arg
);
190 PyTypeObject PyModule_Type
= {
191 PyObject_HEAD_INIT(&PyType_Type
)
193 "module", /* tp_name */
194 sizeof(PyModuleObject
) + PyGC_HEAD_SIZE
, /* tp_size */
196 (destructor
)module_dealloc
, /* tp_dealloc */
201 (reprfunc
)module_repr
, /* tp_repr */
202 0, /* tp_as_number */
203 0, /* tp_as_sequence */
204 0, /* tp_as_mapping */
208 PyObject_GenericGetAttr
, /* tp_getattro */
209 PyObject_GenericSetAttr
, /* tp_setattro */
210 0, /* tp_as_buffer */
211 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_GC
|
212 Py_TPFLAGS_BASETYPE
, /* tp_flags */
214 (traverseproc
)module_traverse
, /* tp_traverse */
216 0, /* tp_richcompare */
217 0, /* tp_weaklistoffset */
221 module_members
, /* tp_members */
225 0, /* tp_descr_get */
226 0, /* tp_descr_set */
227 offsetof(PyModuleObject
, md_dict
), /* tp_dictoffset */
228 (initproc
)module_init
, /* tp_init */
229 PyType_GenericAlloc
, /* tp_alloc */
230 PyType_GenericNew
, /* tp_new */