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(const 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)
33 if (PyDict_SetItemString(m
->md_dict
, "__package__", Py_None
) != 0)
46 PyModule_GetDict(PyObject
*m
)
49 if (!PyModule_Check(m
)) {
50 PyErr_BadInternalCall();
53 d
= ((PyModuleObject
*)m
) -> md_dict
;
55 ((PyModuleObject
*)m
) -> md_dict
= d
= PyDict_New();
60 PyModule_GetName(PyObject
*m
)
64 if (!PyModule_Check(m
)) {
68 d
= ((PyModuleObject
*)m
)->md_dict
;
70 (nameobj
= PyDict_GetItemString(d
, "__name__")) == NULL
||
71 !PyString_Check(nameobj
))
73 PyErr_SetString(PyExc_SystemError
, "nameless module");
76 return PyString_AsString(nameobj
);
80 PyModule_GetFilename(PyObject
*m
)
84 if (!PyModule_Check(m
)) {
88 d
= ((PyModuleObject
*)m
)->md_dict
;
90 (fileobj
= PyDict_GetItemString(d
, "__file__")) == NULL
||
91 !PyString_Check(fileobj
))
93 PyErr_SetString(PyExc_SystemError
, "module filename missing");
96 return PyString_AsString(fileobj
);
100 _PyModule_Clear(PyObject
*m
)
102 /* To make the execution order of destructors for global
103 objects a bit more predictable, we first zap all objects
104 whose name starts with a single underscore, before we clear
105 the entire dictionary. We zap them by replacing them with
106 None, rather than deleting them from the dictionary, to
107 avoid rehashing the dictionary (to some extent). */
110 PyObject
*key
, *value
;
113 d
= ((PyModuleObject
*)m
)->md_dict
;
117 /* First, clear only names starting with a single underscore */
119 while (PyDict_Next(d
, &pos
, &key
, &value
)) {
120 if (value
!= Py_None
&& PyString_Check(key
)) {
121 char *s
= PyString_AsString(key
);
122 if (s
[0] == '_' && s
[1] != '_') {
123 if (Py_VerboseFlag
> 1)
124 PySys_WriteStderr("# clear[1] %s\n", s
);
125 PyDict_SetItem(d
, key
, Py_None
);
130 /* Next, clear all names except for __builtins__ */
132 while (PyDict_Next(d
, &pos
, &key
, &value
)) {
133 if (value
!= Py_None
&& PyString_Check(key
)) {
134 char *s
= PyString_AsString(key
);
135 if (s
[0] != '_' || strcmp(s
, "__builtins__") != 0) {
136 if (Py_VerboseFlag
> 1)
137 PySys_WriteStderr("# clear[2] %s\n", s
);
138 PyDict_SetItem(d
, key
, Py_None
);
143 /* Note: we leave __builtins__ in place, so that destructors
144 of non-global objects defined in this module can still use
145 builtins, in particularly 'None'. */
152 module_init(PyModuleObject
*m
, PyObject
*args
, PyObject
*kwds
)
154 static char *kwlist
[] = {"name", "doc", NULL
};
155 PyObject
*dict
, *name
= Py_None
, *doc
= Py_None
;
156 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "S|O:module.__init__",
157 kwlist
, &name
, &doc
))
166 if (PyDict_SetItemString(dict
, "__name__", name
) < 0)
168 if (PyDict_SetItemString(dict
, "__doc__", doc
) < 0)
174 module_dealloc(PyModuleObject
*m
)
176 PyObject_GC_UnTrack(m
);
177 if (m
->md_dict
!= NULL
) {
178 /* If we are the only ones holding a reference, we can clear
180 if (Py_REFCNT(m
->md_dict
) == 1)
181 _PyModule_Clear((PyObject
*)m
);
182 Py_DECREF(m
->md_dict
);
184 Py_TYPE(m
)->tp_free((PyObject
*)m
);
188 module_repr(PyModuleObject
*m
)
193 name
= PyModule_GetName((PyObject
*)m
);
198 filename
= PyModule_GetFilename((PyObject
*)m
);
199 if (filename
== NULL
) {
201 return PyString_FromFormat("<module '%s' (built-in)>", name
);
203 return PyString_FromFormat("<module '%s' from '%s'>", name
, filename
);
206 /* We only need a traverse function, no clear function: If the module
207 is in a cycle, md_dict will be cleared as well, which will break
210 module_traverse(PyModuleObject
*m
, visitproc visit
, void *arg
)
212 Py_VISIT(m
->md_dict
);
216 PyDoc_STRVAR(module_doc
,
217 "module(name[, doc])\n\
219 Create a module object.\n\
220 The name must be a string; the optional doc argument can have any type.");
222 PyTypeObject PyModule_Type
= {
223 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
224 "module", /* tp_name */
225 sizeof(PyModuleObject
), /* tp_size */
227 (destructor
)module_dealloc
, /* tp_dealloc */
232 (reprfunc
)module_repr
, /* tp_repr */
233 0, /* tp_as_number */
234 0, /* tp_as_sequence */
235 0, /* tp_as_mapping */
239 PyObject_GenericGetAttr
, /* tp_getattro */
240 PyObject_GenericSetAttr
, /* tp_setattro */
241 0, /* tp_as_buffer */
242 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
243 Py_TPFLAGS_BASETYPE
, /* tp_flags */
244 module_doc
, /* tp_doc */
245 (traverseproc
)module_traverse
, /* tp_traverse */
247 0, /* tp_richcompare */
248 0, /* tp_weaklistoffset */
252 module_members
, /* tp_members */
256 0, /* tp_descr_get */
257 0, /* tp_descr_set */
258 offsetof(PyModuleObject
, md_dict
), /* tp_dictoffset */
259 (initproc
)module_init
, /* tp_init */
260 PyType_GenericAlloc
, /* tp_alloc */
261 PyType_GenericNew
, /* tp_new */
262 PyObject_GC_Del
, /* tp_free */