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