You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

795 lines
22 KiB

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