2 /* Module object implementation */
5 #include "structmember.h"
7 static Py_ssize_t max_module_number
;
12 struct PyModuleDef
*md_def
;
16 static PyMemberDef module_members
[] = {
17 {"__dict__", T_OBJECT
, offsetof(PyModuleObject
, md_dict
), READONLY
},
21 static PyTypeObject moduledef_type
= {
22 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
23 "moduledef", /* tp_name */
24 sizeof(struct PyModuleDef
), /* tp_size */
30 PyModule_New(const char *name
)
34 m
= PyObject_GC_New(PyModuleObject
, &PyModule_Type
);
39 nameobj
= PyUnicode_FromString(name
);
40 m
->md_dict
= PyDict_New();
41 if (m
->md_dict
== NULL
|| nameobj
== NULL
)
43 if (PyDict_SetItemString(m
->md_dict
, "__name__", nameobj
) != 0)
45 if (PyDict_SetItemString(m
->md_dict
, "__doc__", Py_None
) != 0)
47 if (PyDict_SetItemString(m
->md_dict
, "__package__", Py_None
) != 0)
59 static char api_version_warning
[] =
60 "Python C API version mismatch for module %.100s:\
61 This Python has API version %d, module %.100s has version %d.";
64 PyModule_Create2(struct PyModuleDef
* module
, int module_api_version
)
70 if (!Py_IsInitialized())
71 Py_FatalError("Interpreter not initialized (version mismatch?)");
72 if (PyType_Ready(&moduledef_type
) < 0)
74 if (module
->m_base
.m_index
== 0) {
76 Py_REFCNT(module
) = 1;
77 Py_TYPE(module
) = &moduledef_type
;
78 module
->m_base
.m_index
= max_module_number
;
80 name
= module
->m_name
;
81 if (module_api_version
!= PYTHON_API_VERSION
) {
83 PyOS_snprintf(message
, sizeof(message
),
84 api_version_warning
, name
,
85 PYTHON_API_VERSION
, name
,
87 if (PyErr_WarnEx(PyExc_RuntimeWarning
, message
, 1))
90 /* Make sure name is fully qualified.
92 This is a bit of a hack: when the shared library is loaded,
93 the module name is "package.module", but the module calls
94 PyModule_Create*() with just "module" for the name. The shared
95 library loader squirrels away the true name of the module in
96 _Py_PackageContext, and PyModule_Create*() will substitute this
97 (if the name actually matches).
99 if (_Py_PackageContext
!= NULL
) {
100 char *p
= strrchr(_Py_PackageContext
, '.');
101 if (p
!= NULL
&& strcmp(module
->m_name
, p
+1) == 0) {
102 name
= _Py_PackageContext
;
103 _Py_PackageContext
= NULL
;
106 if ((m
= (PyModuleObject
*)PyModule_New(name
)) == NULL
)
109 if (module
->m_size
> 0) {
110 m
->md_state
= PyMem_MALLOC(module
->m_size
);
116 memset(m
->md_state
, 0, module
->m_size
);
119 d
= PyModule_GetDict((PyObject
*)m
);
120 if (module
->m_methods
!= NULL
) {
121 n
= PyUnicode_FromString(name
);
124 for (ml
= module
->m_methods
; ml
->ml_name
!= NULL
; ml
++) {
125 if ((ml
->ml_flags
& METH_CLASS
) ||
126 (ml
->ml_flags
& METH_STATIC
)) {
127 PyErr_SetString(PyExc_ValueError
,
128 "module functions cannot set"
129 " METH_CLASS or METH_STATIC");
133 v
= PyCFunction_NewEx(ml
, (PyObject
*)m
, n
);
138 if (PyDict_SetItemString(d
, ml
->ml_name
, v
) != 0) {
147 if (module
->m_doc
!= NULL
) {
148 v
= PyUnicode_FromString(module
->m_doc
);
149 if (v
== NULL
|| PyDict_SetItemString(d
, "__doc__", v
) != 0) {
161 PyModule_GetDict(PyObject
*m
)
164 if (!PyModule_Check(m
)) {
165 PyErr_BadInternalCall();
168 d
= ((PyModuleObject
*)m
) -> md_dict
;
170 ((PyModuleObject
*)m
) -> md_dict
= d
= PyDict_New();
175 PyModule_GetName(PyObject
*m
)
179 if (!PyModule_Check(m
)) {
183 d
= ((PyModuleObject
*)m
)->md_dict
;
185 (nameobj
= PyDict_GetItemString(d
, "__name__")) == NULL
||
186 !PyUnicode_Check(nameobj
))
188 PyErr_SetString(PyExc_SystemError
, "nameless module");
191 return _PyUnicode_AsString(nameobj
);
195 PyModule_GetFilename(PyObject
*m
)
199 if (!PyModule_Check(m
)) {
203 d
= ((PyModuleObject
*)m
)->md_dict
;
205 (fileobj
= PyDict_GetItemString(d
, "__file__")) == NULL
||
206 !PyUnicode_Check(fileobj
))
208 PyErr_SetString(PyExc_SystemError
, "module filename missing");
211 return _PyUnicode_AsString(fileobj
);
215 PyModule_GetDef(PyObject
* m
)
217 if (!PyModule_Check(m
)) {
221 return ((PyModuleObject
*)m
)->md_def
;
225 PyModule_GetState(PyObject
* m
)
227 if (!PyModule_Check(m
)) {
231 return ((PyModuleObject
*)m
)->md_state
;
235 _PyModule_Clear(PyObject
*m
)
237 /* To make the execution order of destructors for global
238 objects a bit more predictable, we first zap all objects
239 whose name starts with a single underscore, before we clear
240 the entire dictionary. We zap them by replacing them with
241 None, rather than deleting them from the dictionary, to
242 avoid rehashing the dictionary (to some extent). */
245 PyObject
*key
, *value
;
248 d
= ((PyModuleObject
*)m
)->md_dict
;
252 /* First, clear only names starting with a single underscore */
254 while (PyDict_Next(d
, &pos
, &key
, &value
)) {
255 if (value
!= Py_None
&& PyUnicode_Check(key
)) {
256 const char *s
= _PyUnicode_AsString(key
);
257 if (s
[0] == '_' && s
[1] != '_') {
258 if (Py_VerboseFlag
> 1)
259 PySys_WriteStderr("# clear[1] %s\n", s
);
260 PyDict_SetItem(d
, key
, Py_None
);
265 /* Next, clear all names except for __builtins__ */
267 while (PyDict_Next(d
, &pos
, &key
, &value
)) {
268 if (value
!= Py_None
&& PyUnicode_Check(key
)) {
269 const char *s
= _PyUnicode_AsString(key
);
270 if (s
[0] != '_' || strcmp(s
, "__builtins__") != 0) {
271 if (Py_VerboseFlag
> 1)
272 PySys_WriteStderr("# clear[2] %s\n", s
);
273 PyDict_SetItem(d
, key
, Py_None
);
278 /* Note: we leave __builtins__ in place, so that destructors
279 of non-global objects defined in this module can still use
280 builtins, in particularly 'None'. */
287 module_init(PyModuleObject
*m
, PyObject
*args
, PyObject
*kwds
)
289 static char *kwlist
[] = {"name", "doc", NULL
};
290 PyObject
*dict
, *name
= Py_None
, *doc
= Py_None
;
291 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "U|O:module.__init__",
292 kwlist
, &name
, &doc
))
301 if (PyDict_SetItemString(dict
, "__name__", name
) < 0)
303 if (PyDict_SetItemString(dict
, "__doc__", doc
) < 0)
309 module_dealloc(PyModuleObject
*m
)
311 PyObject_GC_UnTrack(m
);
312 if (m
->md_def
&& m
->md_def
->m_free
)
313 m
->md_def
->m_free(m
);
314 if (m
->md_dict
!= NULL
) {
315 _PyModule_Clear((PyObject
*)m
);
316 Py_DECREF(m
->md_dict
);
318 if (m
->md_state
!= NULL
)
319 PyMem_FREE(m
->md_state
);
320 Py_TYPE(m
)->tp_free((PyObject
*)m
);
324 module_repr(PyModuleObject
*m
)
327 const char *filename
;
329 name
= PyModule_GetName((PyObject
*)m
);
334 filename
= PyModule_GetFilename((PyObject
*)m
);
335 if (filename
== NULL
) {
337 return PyUnicode_FromFormat("<module '%s' (built-in)>", name
);
339 return PyUnicode_FromFormat("<module '%s' from '%s'>", name
, filename
);
343 module_traverse(PyModuleObject
*m
, visitproc visit
, void *arg
)
345 if (m
->md_def
&& m
->md_def
->m_traverse
) {
346 int res
= m
->md_def
->m_traverse((PyObject
*)m
, visit
, arg
);
350 Py_VISIT(m
->md_dict
);
355 module_clear(PyModuleObject
*m
)
357 if (m
->md_def
&& m
->md_def
->m_clear
) {
358 int res
= m
->md_def
->m_clear((PyObject
*)m
);
362 Py_CLEAR(m
->md_dict
);
367 PyDoc_STRVAR(module_doc
,
368 "module(name[, doc])\n\
370 Create a module object.\n\
371 The name must be a string; the optional doc argument can have any type.");
373 PyTypeObject PyModule_Type
= {
374 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
375 "module", /* tp_name */
376 sizeof(PyModuleObject
), /* tp_size */
378 (destructor
)module_dealloc
, /* tp_dealloc */
383 (reprfunc
)module_repr
, /* tp_repr */
384 0, /* tp_as_number */
385 0, /* tp_as_sequence */
386 0, /* tp_as_mapping */
390 PyObject_GenericGetAttr
, /* tp_getattro */
391 PyObject_GenericSetAttr
, /* tp_setattro */
392 0, /* tp_as_buffer */
393 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_HAVE_GC
|
394 Py_TPFLAGS_BASETYPE
, /* tp_flags */
395 module_doc
, /* tp_doc */
396 (traverseproc
)module_traverse
, /* tp_traverse */
397 (inquiry
)module_clear
, /* tp_clear */
398 0, /* tp_richcompare */
399 0, /* tp_weaklistoffset */
403 module_members
, /* tp_members */
407 0, /* tp_descr_get */
408 0, /* tp_descr_set */
409 offsetof(PyModuleObject
, md_dict
), /* tp_dictoffset */
410 (initproc
)module_init
, /* tp_init */
411 PyType_GenericAlloc
, /* tp_alloc */
412 PyType_GenericNew
, /* tp_new */
413 PyObject_GC_Del
, /* tp_free */