/Objects/moduleobject.c

http://unladen-swallow.googlecode.com/ · C · 260 lines · 220 code · 24 blank · 16 comment · 56 complexity · e2fd824941911811ea0f7c7175aadec4 MD5 · raw file

  1. /* Module object implementation */
  2. #include "Python.h"
  3. #include "structmember.h"
  4. typedef struct {
  5. PyObject_HEAD
  6. PyObject *md_dict;
  7. } PyModuleObject;
  8. static PyMemberDef module_members[] = {
  9. {"__dict__", T_OBJECT, offsetof(PyModuleObject, md_dict), READONLY},
  10. {0}
  11. };
  12. PyObject *
  13. PyModule_New(const char *name)
  14. {
  15. PyModuleObject *m;
  16. PyObject *nameobj;
  17. m = PyObject_GC_New(PyModuleObject, &PyModule_Type);
  18. if (m == NULL)
  19. return NULL;
  20. nameobj = PyString_FromString(name);
  21. m->md_dict = PyDict_New();
  22. if (m->md_dict == NULL || nameobj == NULL)
  23. goto fail;
  24. if (PyDict_SetItemString(m->md_dict, "__name__", nameobj) != 0)
  25. goto fail;
  26. if (PyDict_SetItemString(m->md_dict, "__doc__", Py_None) != 0)
  27. goto fail;
  28. if (PyDict_SetItemString(m->md_dict, "__package__", Py_None) != 0)
  29. goto fail;
  30. Py_DECREF(nameobj);
  31. PyObject_GC_Track(m);
  32. return (PyObject *)m;
  33. fail:
  34. Py_XDECREF(nameobj);
  35. Py_DECREF(m);
  36. return NULL;
  37. }
  38. PyObject *
  39. PyModule_GetDict(PyObject *m)
  40. {
  41. PyObject *d;
  42. if (!PyModule_Check(m)) {
  43. PyErr_BadInternalCall();
  44. return NULL;
  45. }
  46. d = ((PyModuleObject *)m) -> md_dict;
  47. if (d == NULL)
  48. ((PyModuleObject *)m) -> md_dict = d = PyDict_New();
  49. return d;
  50. }
  51. char *
  52. PyModule_GetName(PyObject *m)
  53. {
  54. PyObject *d;
  55. PyObject *nameobj;
  56. if (!PyModule_Check(m)) {
  57. PyErr_BadArgument();
  58. return NULL;
  59. }
  60. d = ((PyModuleObject *)m)->md_dict;
  61. if (d == NULL ||
  62. (nameobj = PyDict_GetItemString(d, "__name__")) == NULL ||
  63. !PyString_Check(nameobj))
  64. {
  65. PyErr_SetString(PyExc_SystemError, "nameless module");
  66. return NULL;
  67. }
  68. return PyString_AsString(nameobj);
  69. }
  70. char *
  71. PyModule_GetFilename(PyObject *m)
  72. {
  73. PyObject *d;
  74. PyObject *fileobj;
  75. if (!PyModule_Check(m)) {
  76. PyErr_BadArgument();
  77. return NULL;
  78. }
  79. d = ((PyModuleObject *)m)->md_dict;
  80. if (d == NULL ||
  81. (fileobj = PyDict_GetItemString(d, "__file__")) == NULL ||
  82. !PyString_Check(fileobj))
  83. {
  84. PyErr_SetString(PyExc_SystemError, "module filename missing");
  85. return NULL;
  86. }
  87. return PyString_AsString(fileobj);
  88. }
  89. void
  90. _PyModule_Clear(PyObject *m)
  91. {
  92. /* To make the execution order of destructors for global
  93. objects a bit more predictable, we first zap all objects
  94. whose name starts with a single underscore, before we clear
  95. the entire dictionary. We zap them by replacing them with
  96. None, rather than deleting them from the dictionary, to
  97. avoid rehashing the dictionary (to some extent). */
  98. Py_ssize_t pos;
  99. PyObject *key, *value;
  100. PyObject *d;
  101. d = ((PyModuleObject *)m)->md_dict;
  102. if (d == NULL)
  103. return;
  104. /* First, clear only names starting with a single underscore */
  105. pos = 0;
  106. while (PyDict_Next(d, &pos, &key, &value)) {
  107. if (value != Py_None && PyString_Check(key)) {
  108. char *s = PyString_AsString(key);
  109. if (s[0] == '_' && s[1] != '_') {
  110. if (Py_VerboseFlag > 1)
  111. PySys_WriteStderr("# clear[1] %s\n", s);
  112. PyDict_SetItem(d, key, Py_None);
  113. }
  114. }
  115. }
  116. /* Next, clear all names except for __builtins__ */
  117. pos = 0;
  118. while (PyDict_Next(d, &pos, &key, &value)) {
  119. if (value != Py_None && PyString_Check(key)) {
  120. char *s = PyString_AsString(key);
  121. if (s[0] != '_' || strcmp(s, "__builtins__") != 0) {
  122. if (Py_VerboseFlag > 1)
  123. PySys_WriteStderr("# clear[2] %s\n", s);
  124. PyDict_SetItem(d, key, Py_None);
  125. }
  126. }
  127. }
  128. /* Note: we leave __builtins__ in place, so that destructors
  129. of non-global objects defined in this module can still use
  130. builtins, in particularly 'None'. */
  131. }
  132. /* Methods */
  133. static int
  134. module_init(PyModuleObject *m, PyObject *args, PyObject *kwds)
  135. {
  136. static char *kwlist[] = {"name", "doc", NULL};
  137. PyObject *dict, *name = Py_None, *doc = Py_None;
  138. if (!PyArg_ParseTupleAndKeywords(args, kwds, "S|O:module.__init__",
  139. kwlist, &name, &doc))
  140. return -1;
  141. dict = m->md_dict;
  142. if (dict == NULL) {
  143. dict = PyDict_New();
  144. if (dict == NULL)
  145. return -1;
  146. m->md_dict = dict;
  147. }
  148. if (PyDict_SetItemString(dict, "__name__", name) < 0)
  149. return -1;
  150. if (PyDict_SetItemString(dict, "__doc__", doc) < 0)
  151. return -1;
  152. return 0;
  153. }
  154. static void
  155. module_dealloc(PyModuleObject *m)
  156. {
  157. PyObject_GC_UnTrack(m);
  158. if (m->md_dict != NULL) {
  159. _PyModule_Clear((PyObject *)m);
  160. Py_DECREF(m->md_dict);
  161. }
  162. Py_TYPE(m)->tp_free((PyObject *)m);
  163. }
  164. static PyObject *
  165. module_repr(PyModuleObject *m)
  166. {
  167. char *name;
  168. char *filename;
  169. name = PyModule_GetName((PyObject *)m);
  170. if (name == NULL) {
  171. PyErr_Clear();
  172. name = "?";
  173. }
  174. filename = PyModule_GetFilename((PyObject *)m);
  175. if (filename == NULL) {
  176. PyErr_Clear();
  177. return PyString_FromFormat("<module '%s' (built-in)>", name);
  178. }
  179. return PyString_FromFormat("<module '%s' from '%s'>", name, filename);
  180. }
  181. /* We only need a traverse function, no clear function: If the module
  182. is in a cycle, md_dict will be cleared as well, which will break
  183. the cycle. */
  184. static int
  185. module_traverse(PyModuleObject *m, visitproc visit, void *arg)
  186. {
  187. Py_VISIT(m->md_dict);
  188. return 0;
  189. }
  190. PyDoc_STRVAR(module_doc,
  191. "module(name[, doc])\n\
  192. \n\
  193. Create a module object.\n\
  194. The name must be a string; the optional doc argument can have any type.");
  195. PyTypeObject PyModule_Type = {
  196. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  197. "module", /* tp_name */
  198. sizeof(PyModuleObject), /* tp_size */
  199. 0, /* tp_itemsize */
  200. (destructor)module_dealloc, /* tp_dealloc */
  201. 0, /* tp_print */
  202. 0, /* tp_getattr */
  203. 0, /* tp_setattr */
  204. 0, /* tp_compare */
  205. (reprfunc)module_repr, /* tp_repr */
  206. 0, /* tp_as_number */
  207. 0, /* tp_as_sequence */
  208. 0, /* tp_as_mapping */
  209. 0, /* tp_hash */
  210. 0, /* tp_call */
  211. 0, /* tp_str */
  212. PyObject_GenericGetAttr, /* tp_getattro */
  213. PyObject_GenericSetAttr, /* tp_setattro */
  214. 0, /* tp_as_buffer */
  215. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
  216. Py_TPFLAGS_BASETYPE, /* tp_flags */
  217. module_doc, /* tp_doc */
  218. (traverseproc)module_traverse, /* tp_traverse */
  219. 0, /* tp_clear */
  220. 0, /* tp_richcompare */
  221. 0, /* tp_weaklistoffset */
  222. 0, /* tp_iter */
  223. 0, /* tp_iternext */
  224. 0, /* tp_methods */
  225. module_members, /* tp_members */
  226. 0, /* tp_getset */
  227. 0, /* tp_base */
  228. 0, /* tp_dict */
  229. 0, /* tp_descr_get */
  230. 0, /* tp_descr_set */
  231. offsetof(PyModuleObject, md_dict), /* tp_dictoffset */
  232. (initproc)module_init, /* tp_init */
  233. PyType_GenericAlloc, /* tp_alloc */
  234. PyType_GenericNew, /* tp_new */
  235. PyObject_GC_Del, /* tp_free */
  236. };