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