2 /* Module object implementation */
5 #include "structmember.h"
12 static PyMemberDef module_members
[] = {
13 {"__dict__", T_OBJECT
, offsetof(PyModuleObject
, md_dict
), READONLY
},
18 PyModule_New(char *name
)
22 m
= PyObject_GC_New(PyModuleObject
, &PyModule_Type
);
25 nameobj
= PyString_FromString(name
);
26 m
->md_dict
= PyDict_New();
27 if (m
->md_dict
== NULL
|| nameobj
== NULL
)
29 if (PyDict_SetItemString(m
->md_dict
, "__name__", nameobj
) != 0)
31 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
)
149 PyObject_GC_UnTrack(m
);
150 if (m
->md_dict
!= NULL
) {
151 _PyModule_Clear((PyObject
*)m
);
152 Py_DECREF(m
->md_dict
);
154 m
->ob_type
->tp_free((PyObject
*)m
);
158 module_repr(PyModuleObject
*m
)
163 name
= PyModule_GetName((PyObject
*)m
);
168 filename
= PyModule_GetFilename((PyObject
*)m
);
169 if (filename
== NULL
) {
171 return PyString_FromFormat("<module '%s' (built-in)>", name
);
173 return PyString_FromFormat("<module '%s' from '%s'>", name
, filename
);
176 /* We only need a traverse function, no clear function: If the module
177 is in a cycle, md_dict will be cleared as well, which will break
180 module_traverse(PyModuleObject
*m
, visitproc visit
, void *arg
)
182 if (m
->md_dict
!= NULL
)
183 return visit(m
->md_dict
, arg
);
187 PyTypeObject PyModule_Type
= {
188 PyObject_HEAD_INIT(&PyType_Type
)
190 "module", /* tp_name */
191 sizeof(PyModuleObject
), /* tp_size */
193 (destructor
)module_dealloc
, /* tp_dealloc */
198 (reprfunc
)module_repr
, /* tp_repr */
199 0, /* tp_as_number */
200 0, /* tp_as_sequence */
201 0, /* tp_as_mapping */
205 PyObject_GenericGetAttr
, /* tp_getattro */
206 PyObject_GenericSetAttr
, /* tp_setattro */
207 0, /* tp_as_buffer */
208 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
209 Py_TPFLAGS_BASETYPE
, /* tp_flags */
211 (traverseproc
)module_traverse
, /* tp_traverse */
213 0, /* tp_richcompare */
214 0, /* tp_weaklistoffset */
218 module_members
, /* tp_members */
222 0, /* tp_descr_get */
223 0, /* tp_descr_set */
224 offsetof(PyModuleObject
, md_dict
), /* tp_dictoffset */
225 (initproc
)module_init
, /* tp_init */
226 PyType_GenericAlloc
, /* tp_alloc */
227 PyType_GenericNew
, /* tp_new */
228 _PyObject_GC_Del
, /* tp_free */