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" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 5 | |
| 6 | typedef struct { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 7 | PyObject_HEAD |
| 8 | PyObject *md_dict; |
| 9 | } PyModuleObject; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 11 | PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 12 | PyModule_New(char *name) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 13 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 14 | PyModuleObject *m; |
| 15 | PyObject *nameobj; |
| 16 | m = PyObject_NEW(PyModuleObject, &PyModule_Type); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 17 | if (m == NULL) |
| 18 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 19 | nameobj = PyString_FromString(name); |
| 20 | m->md_dict = PyDict_New(); |
Guido van Rossum | c45611d | 1993-11-17 22:58:56 +0000 | [diff] [blame] | 21 | if (m->md_dict == NULL || nameobj == NULL) |
| 22 | goto fail; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 23 | if (PyDict_SetItemString(m->md_dict, "__name__", nameobj) != 0) |
Guido van Rossum | c45611d | 1993-11-17 22:58:56 +0000 | [diff] [blame] | 24 | goto fail; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 25 | if (PyDict_SetItemString(m->md_dict, "__doc__", Py_None) != 0) |
Guido van Rossum | 8b14b4c | 1995-01-07 11:59:29 +0000 | [diff] [blame] | 26 | goto fail; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 27 | Py_DECREF(nameobj); |
| 28 | return (PyObject *)m; |
Guido van Rossum | c45611d | 1993-11-17 22:58:56 +0000 | [diff] [blame] | 29 | |
| 30 | fail: |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 31 | Py_XDECREF(nameobj); |
| 32 | Py_DECREF(m); |
Guido van Rossum | c45611d | 1993-11-17 22:58:56 +0000 | [diff] [blame] | 33 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 36 | PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 37 | PyModule_GetDict(PyObject *m) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 38 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 39 | if (!PyModule_Check(m)) { |
| 40 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 41 | return NULL; |
| 42 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 43 | return ((PyModuleObject *)m) -> md_dict; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Guido van Rossum | 0558a20 | 1990-10-26 15:00:11 +0000 | [diff] [blame] | 46 | char * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 47 | PyModule_GetName(PyObject *m) |
Guido van Rossum | 0558a20 | 1990-10-26 15:00:11 +0000 | [diff] [blame] | 48 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 49 | PyObject *nameobj; |
| 50 | if (!PyModule_Check(m)) { |
| 51 | PyErr_BadArgument(); |
Guido van Rossum | 0558a20 | 1990-10-26 15:00:11 +0000 | [diff] [blame] | 52 | return NULL; |
| 53 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 54 | nameobj = PyDict_GetItemString(((PyModuleObject *)m)->md_dict, |
| 55 | "__name__"); |
| 56 | if (nameobj == NULL || !PyString_Check(nameobj)) { |
| 57 | PyErr_SetString(PyExc_SystemError, "nameless module"); |
Guido van Rossum | c45611d | 1993-11-17 22:58:56 +0000 | [diff] [blame] | 58 | return NULL; |
| 59 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 60 | return PyString_AsString(nameobj); |
Guido van Rossum | 0558a20 | 1990-10-26 15:00:11 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Guido van Rossum | 98cc19f | 1999-02-15 14:47:16 +0000 | [diff] [blame] | 63 | char * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 64 | PyModule_GetFilename(PyObject *m) |
Guido van Rossum | 98cc19f | 1999-02-15 14:47:16 +0000 | [diff] [blame] | 65 | { |
| 66 | PyObject *fileobj; |
| 67 | if (!PyModule_Check(m)) { |
| 68 | PyErr_BadArgument(); |
| 69 | return NULL; |
| 70 | } |
| 71 | fileobj = PyDict_GetItemString(((PyModuleObject *)m)->md_dict, |
| 72 | "__file__"); |
| 73 | if (fileobj == NULL || !PyString_Check(fileobj)) { |
| 74 | PyErr_SetString(PyExc_SystemError, "module filename missing"); |
| 75 | return NULL; |
| 76 | } |
| 77 | return PyString_AsString(fileobj); |
| 78 | } |
| 79 | |
Guido van Rossum | f1dc061 | 1998-02-19 20:51:52 +0000 | [diff] [blame] | 80 | void |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 81 | _PyModule_Clear(PyObject *m) |
Guido van Rossum | f1dc061 | 1998-02-19 20:51:52 +0000 | [diff] [blame] | 82 | { |
| 83 | /* To make the execution order of destructors for global |
| 84 | objects a bit more predictable, we first zap all objects |
| 85 | whose name starts with a single underscore, before we clear |
| 86 | the entire dictionary. We zap them by replacing them with |
| 87 | None, rather than deleting them from the dictionary, to |
| 88 | avoid rehashing the dictionary (to some extent). */ |
| 89 | |
| 90 | int pos; |
| 91 | PyObject *key, *value; |
| 92 | PyObject *d; |
| 93 | |
| 94 | d = ((PyModuleObject *)m)->md_dict; |
| 95 | |
| 96 | /* First, clear only names starting with a single underscore */ |
| 97 | pos = 0; |
| 98 | while (PyDict_Next(d, &pos, &key, &value)) { |
| 99 | if (value != Py_None && PyString_Check(key)) { |
| 100 | char *s = PyString_AsString(key); |
| 101 | if (s[0] == '_' && s[1] != '_') { |
| 102 | if (Py_VerboseFlag > 1) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 103 | PySys_WriteStderr("# clear[1] %s\n", s); |
Guido van Rossum | f1dc061 | 1998-02-19 20:51:52 +0000 | [diff] [blame] | 104 | PyDict_SetItem(d, key, Py_None); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /* Next, clear all names except for __builtins__ */ |
| 110 | pos = 0; |
| 111 | while (PyDict_Next(d, &pos, &key, &value)) { |
| 112 | if (value != Py_None && PyString_Check(key)) { |
| 113 | char *s = PyString_AsString(key); |
| 114 | if (s[0] != '_' || strcmp(s, "__builtins__") != 0) { |
| 115 | if (Py_VerboseFlag > 1) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 116 | PySys_WriteStderr("# clear[2] %s\n", s); |
Guido van Rossum | f1dc061 | 1998-02-19 20:51:52 +0000 | [diff] [blame] | 117 | PyDict_SetItem(d, key, Py_None); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /* Note: we leave __builtins__ in place, so that destructors |
| 123 | of non-global objects defined in this module can still use |
| 124 | builtins, in particularly 'None'. */ |
| 125 | |
| 126 | } |
| 127 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 128 | /* Methods */ |
| 129 | |
| 130 | static void |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 131 | module_dealloc(PyModuleObject *m) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 132 | { |
Guido van Rossum | 27e916f | 1995-01-26 00:39:00 +0000 | [diff] [blame] | 133 | if (m->md_dict != NULL) { |
Guido van Rossum | f1dc061 | 1998-02-19 20:51:52 +0000 | [diff] [blame] | 134 | _PyModule_Clear((PyObject *)m); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 135 | Py_DECREF(m->md_dict); |
Guido van Rossum | 27e916f | 1995-01-26 00:39:00 +0000 | [diff] [blame] | 136 | } |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 137 | PyObject_DEL(m); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 140 | static PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 141 | module_repr(PyModuleObject *m) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 142 | { |
Guido van Rossum | 98cc19f | 1999-02-15 14:47:16 +0000 | [diff] [blame] | 143 | char buf[400]; |
| 144 | char *name; |
| 145 | char *filename; |
| 146 | name = PyModule_GetName((PyObject *)m); |
Guido van Rossum | c45611d | 1993-11-17 22:58:56 +0000 | [diff] [blame] | 147 | if (name == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 148 | PyErr_Clear(); |
Guido van Rossum | c45611d | 1993-11-17 22:58:56 +0000 | [diff] [blame] | 149 | name = "?"; |
| 150 | } |
Guido van Rossum | 98cc19f | 1999-02-15 14:47:16 +0000 | [diff] [blame] | 151 | filename = PyModule_GetFilename((PyObject *)m); |
| 152 | if (filename == NULL) { |
| 153 | PyErr_Clear(); |
| 154 | sprintf(buf, "<module '%.80s' (built-in)>", name); |
| 155 | } else { |
| 156 | sprintf(buf, "<module '%.80s' from '%.255s'>", name, filename); |
| 157 | } |
| 158 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 159 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 162 | static PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 163 | module_getattr(PyModuleObject *m, char *name) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 164 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 165 | PyObject *res; |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 166 | char* modname; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 167 | if (strcmp(name, "__dict__") == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 168 | Py_INCREF(m->md_dict); |
Guido van Rossum | e87203a | 1990-10-21 22:12:30 +0000 | [diff] [blame] | 169 | return m->md_dict; |
| 170 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 171 | res = PyDict_GetItemString(m->md_dict, name); |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 172 | if (res == NULL) { |
| 173 | modname = PyModule_GetName((PyObject *)m); |
| 174 | if (modname == NULL) { |
| 175 | PyErr_Clear(); |
| 176 | modname = "?"; |
| 177 | } |
| 178 | PyErr_Format(PyExc_AttributeError, |
| 179 | "'%.50s' module has no attribute '%.400s'", |
| 180 | modname, name); |
| 181 | } |
Guido van Rossum | 0add15f | 1997-05-09 01:07:15 +0000 | [diff] [blame] | 182 | else |
| 183 | Py_INCREF(res); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 184 | return res; |
| 185 | } |
| 186 | |
| 187 | static int |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 188 | module_setattr(PyModuleObject *m, char *name, PyObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 189 | { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 190 | char* modname; |
Guido van Rossum | c45611d | 1993-11-17 22:58:56 +0000 | [diff] [blame] | 191 | if (name[0] == '_' && strcmp(name, "__dict__") == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 192 | PyErr_SetString(PyExc_TypeError, |
| 193 | "read-only special attribute"); |
Guido van Rossum | c45611d | 1993-11-17 22:58:56 +0000 | [diff] [blame] | 194 | return -1; |
Guido van Rossum | e87203a | 1990-10-21 22:12:30 +0000 | [diff] [blame] | 195 | } |
Guido van Rossum | 94472a0 | 1992-09-04 09:45:18 +0000 | [diff] [blame] | 196 | if (v == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 197 | int rv = PyDict_DelItemString(m->md_dict, name); |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 198 | if (rv < 0) { |
| 199 | modname = PyModule_GetName((PyObject *)m); |
| 200 | if (modname == NULL) { |
| 201 | PyErr_Clear(); |
| 202 | modname = "?"; |
| 203 | } |
| 204 | PyErr_Format(PyExc_AttributeError, |
| 205 | "'%.50s' module has no attribute '%.400s'", |
| 206 | modname, name); |
| 207 | } |
Guido van Rossum | 94472a0 | 1992-09-04 09:45:18 +0000 | [diff] [blame] | 208 | return rv; |
| 209 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 210 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 211 | return PyDict_SetItemString(m->md_dict, name, v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 214 | PyTypeObject PyModule_Type = { |
| 215 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 216 | 0, /*ob_size*/ |
| 217 | "module", /*tp_name*/ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 218 | sizeof(PyModuleObject), /*tp_size*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 219 | 0, /*tp_itemsize*/ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 220 | (destructor)module_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 221 | 0, /*tp_print*/ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 222 | (getattrfunc)module_getattr, /*tp_getattr*/ |
| 223 | (setattrfunc)module_setattr, /*tp_setattr*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 224 | 0, /*tp_compare*/ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 225 | (reprfunc)module_repr, /*tp_repr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 226 | }; |