ยปCore Development>Code coverage>Objects/moduleobject.c

Python code coverage for Objects/moduleobject.c

#countcontent
1n/a
2n/a/* Module object implementation */
3n/a
4n/a#include "Python.h"
5n/a#include "structmember.h"
6n/a
7n/astatic Py_ssize_t max_module_number;
8n/a
9n/atypedef struct {
10n/a PyObject_HEAD
11n/a PyObject *md_dict;
12n/a struct PyModuleDef *md_def;
13n/a void *md_state;
14n/a PyObject *md_weaklist;
15n/a PyObject *md_name; /* for logging purposes after md_dict is cleared */
16n/a} PyModuleObject;
17n/a
18n/astatic PyMemberDef module_members[] = {
19n/a {"__dict__", T_OBJECT, offsetof(PyModuleObject, md_dict), READONLY},
20n/a {0}
21n/a};
22n/a
23n/aPyTypeObject PyModuleDef_Type = {
24n/a PyVarObject_HEAD_INIT(&PyType_Type, 0)
25n/a "moduledef", /* tp_name */
26n/a sizeof(struct PyModuleDef), /* tp_size */
27n/a 0, /* tp_itemsize */
28n/a};
29n/a
30n/a
31n/aPyObject*
32n/aPyModuleDef_Init(struct PyModuleDef* def)
33n/a{
34n/a if (PyType_Ready(&PyModuleDef_Type) < 0)
35n/a return NULL;
36n/a if (def->m_base.m_index == 0) {
37n/a max_module_number++;
38n/a Py_REFCNT(def) = 1;
39n/a Py_TYPE(def) = &PyModuleDef_Type;
40n/a def->m_base.m_index = max_module_number;
41n/a }
42n/a return (PyObject*)def;
43n/a}
44n/a
45n/astatic int
46n/amodule_init_dict(PyModuleObject *mod, PyObject *md_dict,
47n/a PyObject *name, PyObject *doc)
48n/a{
49n/a _Py_IDENTIFIER(__name__);
50n/a _Py_IDENTIFIER(__doc__);
51n/a _Py_IDENTIFIER(__package__);
52n/a _Py_IDENTIFIER(__loader__);
53n/a _Py_IDENTIFIER(__spec__);
54n/a
55n/a if (md_dict == NULL)
56n/a return -1;
57n/a if (doc == NULL)
58n/a doc = Py_None;
59n/a
60n/a if (_PyDict_SetItemId(md_dict, &PyId___name__, name) != 0)
61n/a return -1;
62n/a if (_PyDict_SetItemId(md_dict, &PyId___doc__, doc) != 0)
63n/a return -1;
64n/a if (_PyDict_SetItemId(md_dict, &PyId___package__, Py_None) != 0)
65n/a return -1;
66n/a if (_PyDict_SetItemId(md_dict, &PyId___loader__, Py_None) != 0)
67n/a return -1;
68n/a if (_PyDict_SetItemId(md_dict, &PyId___spec__, Py_None) != 0)
69n/a return -1;
70n/a if (PyUnicode_CheckExact(name)) {
71n/a Py_INCREF(name);
72n/a Py_XSETREF(mod->md_name, name);
73n/a }
74n/a
75n/a return 0;
76n/a}
77n/a
78n/a
79n/aPyObject *
80n/aPyModule_NewObject(PyObject *name)
81n/a{
82n/a PyModuleObject *m;
83n/a m = PyObject_GC_New(PyModuleObject, &PyModule_Type);
84n/a if (m == NULL)
85n/a return NULL;
86n/a m->md_def = NULL;
87n/a m->md_state = NULL;
88n/a m->md_weaklist = NULL;
89n/a m->md_name = NULL;
90n/a m->md_dict = PyDict_New();
91n/a if (module_init_dict(m, m->md_dict, name, NULL) != 0)
92n/a goto fail;
93n/a PyObject_GC_Track(m);
94n/a return (PyObject *)m;
95n/a
96n/a fail:
97n/a Py_DECREF(m);
98n/a return NULL;
99n/a}
100n/a
101n/aPyObject *
102n/aPyModule_New(const char *name)
103n/a{
104n/a PyObject *nameobj, *module;
105n/a nameobj = PyUnicode_FromString(name);
106n/a if (nameobj == NULL)
107n/a return NULL;
108n/a module = PyModule_NewObject(nameobj);
109n/a Py_DECREF(nameobj);
110n/a return module;
111n/a}
112n/a
113n/a/* Check API/ABI version
114n/a * Issues a warning on mismatch, which is usually not fatal.
115n/a * Returns 0 if an exception is raised.
116n/a */
117n/astatic int
118n/acheck_api_version(const char *name, int module_api_version)
119n/a{
120n/a if (module_api_version != PYTHON_API_VERSION && module_api_version != PYTHON_ABI_VERSION) {
121n/a int err;
122n/a err = PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
123n/a "Python C API version mismatch for module %.100s: "
124n/a "This Python has API version %d, module %.100s has version %d.",
125n/a name,
126n/a PYTHON_API_VERSION, name, module_api_version);
127n/a if (err)
128n/a return 0;
129n/a }
130n/a return 1;
131n/a}
132n/a
133n/astatic int
134n/a_add_methods_to_object(PyObject *module, PyObject *name, PyMethodDef *functions)
135n/a{
136n/a PyObject *func;
137n/a PyMethodDef *fdef;
138n/a
139n/a for (fdef = functions; fdef->ml_name != NULL; fdef++) {
140n/a if ((fdef->ml_flags & METH_CLASS) ||
141n/a (fdef->ml_flags & METH_STATIC)) {
142n/a PyErr_SetString(PyExc_ValueError,
143n/a "module functions cannot set"
144n/a " METH_CLASS or METH_STATIC");
145n/a return -1;
146n/a }
147n/a func = PyCFunction_NewEx(fdef, (PyObject*)module, name);
148n/a if (func == NULL) {
149n/a return -1;
150n/a }
151n/a if (PyObject_SetAttrString(module, fdef->ml_name, func) != 0) {
152n/a Py_DECREF(func);
153n/a return -1;
154n/a }
155n/a Py_DECREF(func);
156n/a }
157n/a
158n/a return 0;
159n/a}
160n/a
161n/aPyObject *
162n/aPyModule_Create2(struct PyModuleDef* module, int module_api_version)
163n/a{
164n/a const char* name;
165n/a PyModuleObject *m;
166n/a PyInterpreterState *interp = PyThreadState_Get()->interp;
167n/a if (interp->modules == NULL)
168n/a Py_FatalError("Python import machinery not initialized");
169n/a if (!PyModuleDef_Init(module))
170n/a return NULL;
171n/a name = module->m_name;
172n/a if (!check_api_version(name, module_api_version)) {
173n/a return NULL;
174n/a }
175n/a if (module->m_slots) {
176n/a PyErr_Format(
177n/a PyExc_SystemError,
178n/a "module %s: PyModule_Create is incompatible with m_slots", name);
179n/a return NULL;
180n/a }
181n/a /* Make sure name is fully qualified.
182n/a
183n/a This is a bit of a hack: when the shared library is loaded,
184n/a the module name is "package.module", but the module calls
185n/a PyModule_Create*() with just "module" for the name. The shared
186n/a library loader squirrels away the true name of the module in
187n/a _Py_PackageContext, and PyModule_Create*() will substitute this
188n/a (if the name actually matches).
189n/a */
190n/a if (_Py_PackageContext != NULL) {
191n/a const char *p = strrchr(_Py_PackageContext, '.');
192n/a if (p != NULL && strcmp(module->m_name, p+1) == 0) {
193n/a name = _Py_PackageContext;
194n/a _Py_PackageContext = NULL;
195n/a }
196n/a }
197n/a if ((m = (PyModuleObject*)PyModule_New(name)) == NULL)
198n/a return NULL;
199n/a
200n/a if (module->m_size > 0) {
201n/a m->md_state = PyMem_MALLOC(module->m_size);
202n/a if (!m->md_state) {
203n/a PyErr_NoMemory();
204n/a Py_DECREF(m);
205n/a return NULL;
206n/a }
207n/a memset(m->md_state, 0, module->m_size);
208n/a }
209n/a
210n/a if (module->m_methods != NULL) {
211n/a if (PyModule_AddFunctions((PyObject *) m, module->m_methods) != 0) {
212n/a Py_DECREF(m);
213n/a return NULL;
214n/a }
215n/a }
216n/a if (module->m_doc != NULL) {
217n/a if (PyModule_SetDocString((PyObject *) m, module->m_doc) != 0) {
218n/a Py_DECREF(m);
219n/a return NULL;
220n/a }
221n/a }
222n/a m->md_def = module;
223n/a return (PyObject*)m;
224n/a}
225n/a
226n/aPyObject *
227n/aPyModule_FromDefAndSpec2(struct PyModuleDef* def, PyObject *spec, int module_api_version)
228n/a{
229n/a PyModuleDef_Slot* cur_slot;
230n/a PyObject *(*create)(PyObject *, PyModuleDef*) = NULL;
231n/a PyObject *nameobj;
232n/a PyObject *m = NULL;
233n/a int has_execution_slots = 0;
234n/a const char *name;
235n/a int ret;
236n/a
237n/a PyModuleDef_Init(def);
238n/a
239n/a nameobj = PyObject_GetAttrString(spec, "name");
240n/a if (nameobj == NULL) {
241n/a return NULL;
242n/a }
243n/a name = PyUnicode_AsUTF8(nameobj);
244n/a if (name == NULL) {
245n/a goto error;
246n/a }
247n/a
248n/a if (!check_api_version(name, module_api_version)) {
249n/a goto error;
250n/a }
251n/a
252n/a if (def->m_size < 0) {
253n/a PyErr_Format(
254n/a PyExc_SystemError,
255n/a "module %s: m_size may not be negative for multi-phase initialization",
256n/a name);
257n/a goto error;
258n/a }
259n/a
260n/a for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) {
261n/a if (cur_slot->slot == Py_mod_create) {
262n/a if (create) {
263n/a PyErr_Format(
264n/a PyExc_SystemError,
265n/a "module %s has multiple create slots",
266n/a name);
267n/a goto error;
268n/a }
269n/a create = cur_slot->value;
270n/a } else if (cur_slot->slot < 0 || cur_slot->slot > _Py_mod_LAST_SLOT) {
271n/a PyErr_Format(
272n/a PyExc_SystemError,
273n/a "module %s uses unknown slot ID %i",
274n/a name, cur_slot->slot);
275n/a goto error;
276n/a } else {
277n/a has_execution_slots = 1;
278n/a }
279n/a }
280n/a
281n/a if (create) {
282n/a m = create(spec, def);
283n/a if (m == NULL) {
284n/a if (!PyErr_Occurred()) {
285n/a PyErr_Format(
286n/a PyExc_SystemError,
287n/a "creation of module %s failed without setting an exception",
288n/a name);
289n/a }
290n/a goto error;
291n/a } else {
292n/a if (PyErr_Occurred()) {
293n/a PyErr_Format(PyExc_SystemError,
294n/a "creation of module %s raised unreported exception",
295n/a name);
296n/a goto error;
297n/a }
298n/a }
299n/a } else {
300n/a m = PyModule_NewObject(nameobj);
301n/a if (m == NULL) {
302n/a goto error;
303n/a }
304n/a }
305n/a
306n/a if (PyModule_Check(m)) {
307n/a ((PyModuleObject*)m)->md_state = NULL;
308n/a ((PyModuleObject*)m)->md_def = def;
309n/a } else {
310n/a if (def->m_size > 0 || def->m_traverse || def->m_clear || def->m_free) {
311n/a PyErr_Format(
312n/a PyExc_SystemError,
313n/a "module %s is not a module object, but requests module state",
314n/a name);
315n/a goto error;
316n/a }
317n/a if (has_execution_slots) {
318n/a PyErr_Format(
319n/a PyExc_SystemError,
320n/a "module %s specifies execution slots, but did not create "
321n/a "a ModuleType instance",
322n/a name);
323n/a goto error;
324n/a }
325n/a }
326n/a
327n/a if (def->m_methods != NULL) {
328n/a ret = _add_methods_to_object(m, nameobj, def->m_methods);
329n/a if (ret != 0) {
330n/a goto error;
331n/a }
332n/a }
333n/a
334n/a if (def->m_doc != NULL) {
335n/a ret = PyModule_SetDocString(m, def->m_doc);
336n/a if (ret != 0) {
337n/a goto error;
338n/a }
339n/a }
340n/a
341n/a Py_DECREF(nameobj);
342n/a return m;
343n/a
344n/aerror:
345n/a Py_DECREF(nameobj);
346n/a Py_XDECREF(m);
347n/a return NULL;
348n/a}
349n/a
350n/aint
351n/aPyModule_ExecDef(PyObject *module, PyModuleDef *def)
352n/a{
353n/a PyModuleDef_Slot *cur_slot;
354n/a const char *name;
355n/a int ret;
356n/a
357n/a name = PyModule_GetName(module);
358n/a if (name == NULL) {
359n/a return -1;
360n/a }
361n/a
362n/a if (def->m_size >= 0) {
363n/a PyModuleObject *md = (PyModuleObject*)module;
364n/a if (md->md_state == NULL) {
365n/a /* Always set a state pointer; this serves as a marker to skip
366n/a * multiple initialization (importlib.reload() is no-op) */
367n/a md->md_state = PyMem_MALLOC(def->m_size);
368n/a if (!md->md_state) {
369n/a PyErr_NoMemory();
370n/a return -1;
371n/a }
372n/a memset(md->md_state, 0, def->m_size);
373n/a }
374n/a }
375n/a
376n/a if (def->m_slots == NULL) {
377n/a return 0;
378n/a }
379n/a
380n/a for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) {
381n/a switch (cur_slot->slot) {
382n/a case Py_mod_create:
383n/a /* handled in PyModule_FromDefAndSpec2 */
384n/a break;
385n/a case Py_mod_exec:
386n/a ret = ((int (*)(PyObject *))cur_slot->value)(module);
387n/a if (ret != 0) {
388n/a if (!PyErr_Occurred()) {
389n/a PyErr_Format(
390n/a PyExc_SystemError,
391n/a "execution of module %s failed without setting an exception",
392n/a name);
393n/a }
394n/a return -1;
395n/a }
396n/a if (PyErr_Occurred()) {
397n/a PyErr_Format(
398n/a PyExc_SystemError,
399n/a "execution of module %s raised unreported exception",
400n/a name);
401n/a return -1;
402n/a }
403n/a break;
404n/a default:
405n/a PyErr_Format(
406n/a PyExc_SystemError,
407n/a "module %s initialized with unknown slot %i",
408n/a name, cur_slot->slot);
409n/a return -1;
410n/a }
411n/a }
412n/a return 0;
413n/a}
414n/a
415n/aint
416n/aPyModule_AddFunctions(PyObject *m, PyMethodDef *functions)
417n/a{
418n/a int res;
419n/a PyObject *name = PyModule_GetNameObject(m);
420n/a if (name == NULL) {
421n/a return -1;
422n/a }
423n/a
424n/a res = _add_methods_to_object(m, name, functions);
425n/a Py_DECREF(name);
426n/a return res;
427n/a}
428n/a
429n/aint
430n/aPyModule_SetDocString(PyObject *m, const char *doc)
431n/a{
432n/a PyObject *v;
433n/a _Py_IDENTIFIER(__doc__);
434n/a
435n/a v = PyUnicode_FromString(doc);
436n/a if (v == NULL || _PyObject_SetAttrId(m, &PyId___doc__, v) != 0) {
437n/a Py_XDECREF(v);
438n/a return -1;
439n/a }
440n/a Py_DECREF(v);
441n/a return 0;
442n/a}
443n/a
444n/aPyObject *
445n/aPyModule_GetDict(PyObject *m)
446n/a{
447n/a PyObject *d;
448n/a if (!PyModule_Check(m)) {
449n/a PyErr_BadInternalCall();
450n/a return NULL;
451n/a }
452n/a d = ((PyModuleObject *)m) -> md_dict;
453n/a assert(d != NULL);
454n/a return d;
455n/a}
456n/a
457n/aPyObject*
458n/aPyModule_GetNameObject(PyObject *m)
459n/a{
460n/a _Py_IDENTIFIER(__name__);
461n/a PyObject *d;
462n/a PyObject *name;
463n/a if (!PyModule_Check(m)) {
464n/a PyErr_BadArgument();
465n/a return NULL;
466n/a }
467n/a d = ((PyModuleObject *)m)->md_dict;
468n/a if (d == NULL ||
469n/a (name = _PyDict_GetItemId(d, &PyId___name__)) == NULL ||
470n/a !PyUnicode_Check(name))
471n/a {
472n/a PyErr_SetString(PyExc_SystemError, "nameless module");
473n/a return NULL;
474n/a }
475n/a Py_INCREF(name);
476n/a return name;
477n/a}
478n/a
479n/aconst char *
480n/aPyModule_GetName(PyObject *m)
481n/a{
482n/a PyObject *name = PyModule_GetNameObject(m);
483n/a if (name == NULL)
484n/a return NULL;
485n/a Py_DECREF(name); /* module dict has still a reference */
486n/a return PyUnicode_AsUTF8(name);
487n/a}
488n/a
489n/aPyObject*
490n/aPyModule_GetFilenameObject(PyObject *m)
491n/a{
492n/a _Py_IDENTIFIER(__file__);
493n/a PyObject *d;
494n/a PyObject *fileobj;
495n/a if (!PyModule_Check(m)) {
496n/a PyErr_BadArgument();
497n/a return NULL;
498n/a }
499n/a d = ((PyModuleObject *)m)->md_dict;
500n/a if (d == NULL ||
501n/a (fileobj = _PyDict_GetItemId(d, &PyId___file__)) == NULL ||
502n/a !PyUnicode_Check(fileobj))
503n/a {
504n/a PyErr_SetString(PyExc_SystemError, "module filename missing");
505n/a return NULL;
506n/a }
507n/a Py_INCREF(fileobj);
508n/a return fileobj;
509n/a}
510n/a
511n/aconst char *
512n/aPyModule_GetFilename(PyObject *m)
513n/a{
514n/a PyObject *fileobj;
515n/a const char *utf8;
516n/a fileobj = PyModule_GetFilenameObject(m);
517n/a if (fileobj == NULL)
518n/a return NULL;
519n/a utf8 = PyUnicode_AsUTF8(fileobj);
520n/a Py_DECREF(fileobj); /* module dict has still a reference */
521n/a return utf8;
522n/a}
523n/a
524n/aPyModuleDef*
525n/aPyModule_GetDef(PyObject* m)
526n/a{
527n/a if (!PyModule_Check(m)) {
528n/a PyErr_BadArgument();
529n/a return NULL;
530n/a }
531n/a return ((PyModuleObject *)m)->md_def;
532n/a}
533n/a
534n/avoid*
535n/aPyModule_GetState(PyObject* m)
536n/a{
537n/a if (!PyModule_Check(m)) {
538n/a PyErr_BadArgument();
539n/a return NULL;
540n/a }
541n/a return ((PyModuleObject *)m)->md_state;
542n/a}
543n/a
544n/avoid
545n/a_PyModule_Clear(PyObject *m)
546n/a{
547n/a PyObject *d = ((PyModuleObject *)m)->md_dict;
548n/a if (d != NULL)
549n/a _PyModule_ClearDict(d);
550n/a}
551n/a
552n/avoid
553n/a_PyModule_ClearDict(PyObject *d)
554n/a{
555n/a /* To make the execution order of destructors for global
556n/a objects a bit more predictable, we first zap all objects
557n/a whose name starts with a single underscore, before we clear
558n/a the entire dictionary. We zap them by replacing them with
559n/a None, rather than deleting them from the dictionary, to
560n/a avoid rehashing the dictionary (to some extent). */
561n/a
562n/a Py_ssize_t pos;
563n/a PyObject *key, *value;
564n/a
565n/a /* First, clear only names starting with a single underscore */
566n/a pos = 0;
567n/a while (PyDict_Next(d, &pos, &key, &value)) {
568n/a if (value != Py_None && PyUnicode_Check(key)) {
569n/a if (PyUnicode_READ_CHAR(key, 0) == '_' &&
570n/a PyUnicode_READ_CHAR(key, 1) != '_') {
571n/a if (Py_VerboseFlag > 1) {
572n/a const char *s = PyUnicode_AsUTF8(key);
573n/a if (s != NULL)
574n/a PySys_WriteStderr("# clear[1] %s\n", s);
575n/a else
576n/a PyErr_Clear();
577n/a }
578n/a if (PyDict_SetItem(d, key, Py_None) != 0)
579n/a PyErr_Clear();
580n/a }
581n/a }
582n/a }
583n/a
584n/a /* Next, clear all names except for __builtins__ */
585n/a pos = 0;
586n/a while (PyDict_Next(d, &pos, &key, &value)) {
587n/a if (value != Py_None && PyUnicode_Check(key)) {
588n/a if (PyUnicode_READ_CHAR(key, 0) != '_' ||
589n/a !_PyUnicode_EqualToASCIIString(key, "__builtins__"))
590n/a {
591n/a if (Py_VerboseFlag > 1) {
592n/a const char *s = PyUnicode_AsUTF8(key);
593n/a if (s != NULL)
594n/a PySys_WriteStderr("# clear[2] %s\n", s);
595n/a else
596n/a PyErr_Clear();
597n/a }
598n/a if (PyDict_SetItem(d, key, Py_None) != 0)
599n/a PyErr_Clear();
600n/a }
601n/a }
602n/a }
603n/a
604n/a /* Note: we leave __builtins__ in place, so that destructors
605n/a of non-global objects defined in this module can still use
606n/a builtins, in particularly 'None'. */
607n/a
608n/a}
609n/a
610n/a/* Methods */
611n/a
612n/astatic int
613n/amodule_init(PyModuleObject *m, PyObject *args, PyObject *kwds)
614n/a{
615n/a static char *kwlist[] = {"name", "doc", NULL};
616n/a PyObject *dict, *name = Py_None, *doc = Py_None;
617n/a if (!PyArg_ParseTupleAndKeywords(args, kwds, "U|O:module.__init__",
618n/a kwlist, &name, &doc))
619n/a return -1;
620n/a dict = m->md_dict;
621n/a if (dict == NULL) {
622n/a dict = PyDict_New();
623n/a if (dict == NULL)
624n/a return -1;
625n/a m->md_dict = dict;
626n/a }
627n/a if (module_init_dict(m, dict, name, doc) < 0)
628n/a return -1;
629n/a return 0;
630n/a}
631n/a
632n/astatic void
633n/amodule_dealloc(PyModuleObject *m)
634n/a{
635n/a PyObject_GC_UnTrack(m);
636n/a if (Py_VerboseFlag && m->md_name) {
637n/a PySys_FormatStderr("# destroy %S\n", m->md_name);
638n/a }
639n/a if (m->md_weaklist != NULL)
640n/a PyObject_ClearWeakRefs((PyObject *) m);
641n/a if (m->md_def && m->md_def->m_free)
642n/a m->md_def->m_free(m);
643n/a Py_XDECREF(m->md_dict);
644n/a Py_XDECREF(m->md_name);
645n/a if (m->md_state != NULL)
646n/a PyMem_FREE(m->md_state);
647n/a Py_TYPE(m)->tp_free((PyObject *)m);
648n/a}
649n/a
650n/astatic PyObject *
651n/amodule_repr(PyModuleObject *m)
652n/a{
653n/a PyThreadState *tstate = PyThreadState_GET();
654n/a PyInterpreterState *interp = tstate->interp;
655n/a
656n/a return PyObject_CallMethod(interp->importlib, "_module_repr", "O", m);
657n/a}
658n/a
659n/astatic PyObject*
660n/amodule_getattro(PyModuleObject *m, PyObject *name)
661n/a{
662n/a PyObject *attr, *mod_name;
663n/a attr = PyObject_GenericGetAttr((PyObject *)m, name);
664n/a if (attr || !PyErr_ExceptionMatches(PyExc_AttributeError))
665n/a return attr;
666n/a PyErr_Clear();
667n/a if (m->md_dict) {
668n/a _Py_IDENTIFIER(__name__);
669n/a mod_name = _PyDict_GetItemId(m->md_dict, &PyId___name__);
670n/a if (mod_name) {
671n/a PyErr_Format(PyExc_AttributeError,
672n/a "module '%U' has no attribute '%U'", mod_name, name);
673n/a return NULL;
674n/a }
675n/a else if (PyErr_Occurred()) {
676n/a PyErr_Clear();
677n/a }
678n/a }
679n/a PyErr_Format(PyExc_AttributeError,
680n/a "module has no attribute '%U'", name);
681n/a return NULL;
682n/a}
683n/a
684n/astatic int
685n/amodule_traverse(PyModuleObject *m, visitproc visit, void *arg)
686n/a{
687n/a if (m->md_def && m->md_def->m_traverse) {
688n/a int res = m->md_def->m_traverse((PyObject*)m, visit, arg);
689n/a if (res)
690n/a return res;
691n/a }
692n/a Py_VISIT(m->md_dict);
693n/a return 0;
694n/a}
695n/a
696n/astatic int
697n/amodule_clear(PyModuleObject *m)
698n/a{
699n/a if (m->md_def && m->md_def->m_clear) {
700n/a int res = m->md_def->m_clear((PyObject*)m);
701n/a if (res)
702n/a return res;
703n/a }
704n/a Py_CLEAR(m->md_dict);
705n/a return 0;
706n/a}
707n/a
708n/astatic PyObject *
709n/amodule_dir(PyObject *self, PyObject *args)
710n/a{
711n/a _Py_IDENTIFIER(__dict__);
712n/a PyObject *result = NULL;
713n/a PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
714n/a
715n/a if (dict != NULL) {
716n/a if (PyDict_Check(dict))
717n/a result = PyDict_Keys(dict);
718n/a else {
719n/a const char *name = PyModule_GetName(self);
720n/a if (name)
721n/a PyErr_Format(PyExc_TypeError,
722n/a "%.200s.__dict__ is not a dictionary",
723n/a name);
724n/a }
725n/a }
726n/a
727n/a Py_XDECREF(dict);
728n/a return result;
729n/a}
730n/a
731n/astatic PyMethodDef module_methods[] = {
732n/a {"__dir__", module_dir, METH_NOARGS,
733n/a PyDoc_STR("__dir__() -> list\nspecialized dir() implementation")},
734n/a {0}
735n/a};
736n/a
737n/aPyDoc_STRVAR(module_doc,
738n/a"module(name[, doc])\n\
739n/a\n\
740n/aCreate a module object.\n\
741n/aThe name must be a string; the optional doc argument can have any type.");
742n/a
743n/aPyTypeObject PyModule_Type = {
744n/a PyVarObject_HEAD_INIT(&PyType_Type, 0)
745n/a "module", /* tp_name */
746n/a sizeof(PyModuleObject), /* tp_size */
747n/a 0, /* tp_itemsize */
748n/a (destructor)module_dealloc, /* tp_dealloc */
749n/a 0, /* tp_print */
750n/a 0, /* tp_getattr */
751n/a 0, /* tp_setattr */
752n/a 0, /* tp_reserved */
753n/a (reprfunc)module_repr, /* tp_repr */
754n/a 0, /* tp_as_number */
755n/a 0, /* tp_as_sequence */
756n/a 0, /* tp_as_mapping */
757n/a 0, /* tp_hash */
758n/a 0, /* tp_call */
759n/a 0, /* tp_str */
760n/a (getattrofunc)module_getattro, /* tp_getattro */
761n/a PyObject_GenericSetAttr, /* tp_setattro */
762n/a 0, /* tp_as_buffer */
763n/a Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
764n/a Py_TPFLAGS_BASETYPE, /* tp_flags */
765n/a module_doc, /* tp_doc */
766n/a (traverseproc)module_traverse, /* tp_traverse */
767n/a (inquiry)module_clear, /* tp_clear */
768n/a 0, /* tp_richcompare */
769n/a offsetof(PyModuleObject, md_weaklist), /* tp_weaklistoffset */
770n/a 0, /* tp_iter */
771n/a 0, /* tp_iternext */
772n/a module_methods, /* tp_methods */
773n/a module_members, /* tp_members */
774n/a 0, /* tp_getset */
775n/a 0, /* tp_base */
776n/a 0, /* tp_dict */
777n/a 0, /* tp_descr_get */
778n/a 0, /* tp_descr_set */
779n/a offsetof(PyModuleObject, md_dict), /* tp_dictoffset */
780n/a (initproc)module_init, /* tp_init */
781n/a PyType_GenericAlloc, /* tp_alloc */
782n/a PyType_GenericNew, /* tp_new */
783n/a PyObject_GC_Del, /* tp_free */
784n/a};