Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6610ad9 | 1995-01-04 19:07:38 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 32 | /* Module object implementation */ |
| 33 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 34 | #include "Python.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 35 | |
| 36 | typedef struct { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 37 | PyObject_HEAD |
| 38 | PyObject *md_dict; |
| 39 | } PyModuleObject; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 40 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 41 | PyObject * |
| 42 | PyModule_New(name) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 43 | char *name; |
| 44 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 45 | PyModuleObject *m; |
| 46 | PyObject *nameobj; |
| 47 | m = PyObject_NEW(PyModuleObject, &PyModule_Type); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 48 | if (m == NULL) |
| 49 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 50 | nameobj = PyString_FromString(name); |
| 51 | m->md_dict = PyDict_New(); |
Guido van Rossum | c45611d | 1993-11-17 22:58:56 +0000 | [diff] [blame] | 52 | if (m->md_dict == NULL || nameobj == NULL) |
| 53 | goto fail; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 54 | if (PyDict_SetItemString(m->md_dict, "__name__", nameobj) != 0) |
Guido van Rossum | c45611d | 1993-11-17 22:58:56 +0000 | [diff] [blame] | 55 | goto fail; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 56 | if (PyDict_SetItemString(m->md_dict, "__doc__", Py_None) != 0) |
Guido van Rossum | 8b14b4c | 1995-01-07 11:59:29 +0000 | [diff] [blame] | 57 | goto fail; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 58 | Py_DECREF(nameobj); |
| 59 | return (PyObject *)m; |
Guido van Rossum | c45611d | 1993-11-17 22:58:56 +0000 | [diff] [blame] | 60 | |
| 61 | fail: |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 62 | Py_XDECREF(nameobj); |
| 63 | Py_DECREF(m); |
Guido van Rossum | c45611d | 1993-11-17 22:58:56 +0000 | [diff] [blame] | 64 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 67 | PyObject * |
| 68 | PyModule_GetDict(m) |
| 69 | PyObject *m; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 70 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 71 | if (!PyModule_Check(m)) { |
| 72 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 73 | return NULL; |
| 74 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 75 | return ((PyModuleObject *)m) -> md_dict; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Guido van Rossum | 0558a20 | 1990-10-26 15:00:11 +0000 | [diff] [blame] | 78 | char * |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 79 | PyModule_GetName(m) |
| 80 | PyObject *m; |
Guido van Rossum | 0558a20 | 1990-10-26 15:00:11 +0000 | [diff] [blame] | 81 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 82 | PyObject *nameobj; |
| 83 | if (!PyModule_Check(m)) { |
| 84 | PyErr_BadArgument(); |
Guido van Rossum | 0558a20 | 1990-10-26 15:00:11 +0000 | [diff] [blame] | 85 | return NULL; |
| 86 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 87 | nameobj = PyDict_GetItemString(((PyModuleObject *)m)->md_dict, |
| 88 | "__name__"); |
| 89 | if (nameobj == NULL || !PyString_Check(nameobj)) { |
| 90 | PyErr_SetString(PyExc_SystemError, "nameless module"); |
Guido van Rossum | c45611d | 1993-11-17 22:58:56 +0000 | [diff] [blame] | 91 | return NULL; |
| 92 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 93 | return PyString_AsString(nameobj); |
Guido van Rossum | 0558a20 | 1990-10-26 15:00:11 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Guido van Rossum | f1dc061 | 1998-02-19 20:51:52 +0000 | [diff] [blame] | 96 | void |
| 97 | _PyModule_Clear(m) |
| 98 | PyObject *m; |
| 99 | { |
| 100 | /* To make the execution order of destructors for global |
| 101 | objects a bit more predictable, we first zap all objects |
| 102 | whose name starts with a single underscore, before we clear |
| 103 | the entire dictionary. We zap them by replacing them with |
| 104 | None, rather than deleting them from the dictionary, to |
| 105 | avoid rehashing the dictionary (to some extent). */ |
| 106 | |
| 107 | int pos; |
| 108 | PyObject *key, *value; |
| 109 | PyObject *d; |
| 110 | |
| 111 | d = ((PyModuleObject *)m)->md_dict; |
| 112 | |
| 113 | /* First, clear only names starting with a single underscore */ |
| 114 | pos = 0; |
| 115 | while (PyDict_Next(d, &pos, &key, &value)) { |
| 116 | if (value != Py_None && PyString_Check(key)) { |
| 117 | char *s = PyString_AsString(key); |
| 118 | if (s[0] == '_' && s[1] != '_') { |
| 119 | if (Py_VerboseFlag > 1) |
| 120 | fprintf(stderr, "# clear[1] %s\n", s); |
| 121 | PyDict_SetItem(d, key, Py_None); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /* Next, clear all names except for __builtins__ */ |
| 127 | pos = 0; |
| 128 | while (PyDict_Next(d, &pos, &key, &value)) { |
| 129 | if (value != Py_None && PyString_Check(key)) { |
| 130 | char *s = PyString_AsString(key); |
| 131 | if (s[0] != '_' || strcmp(s, "__builtins__") != 0) { |
| 132 | if (Py_VerboseFlag > 1) |
| 133 | fprintf(stderr, "# clear[2] %s\n", s); |
| 134 | PyDict_SetItem(d, key, Py_None); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /* Note: we leave __builtins__ in place, so that destructors |
| 140 | of non-global objects defined in this module can still use |
| 141 | builtins, in particularly 'None'. */ |
| 142 | |
| 143 | } |
| 144 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 145 | /* Methods */ |
| 146 | |
| 147 | static void |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 148 | module_dealloc(m) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 149 | PyModuleObject *m; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 150 | { |
Guido van Rossum | 27e916f | 1995-01-26 00:39:00 +0000 | [diff] [blame] | 151 | if (m->md_dict != NULL) { |
Guido van Rossum | f1dc061 | 1998-02-19 20:51:52 +0000 | [diff] [blame] | 152 | _PyModule_Clear((PyObject *)m); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 153 | Py_DECREF(m->md_dict); |
Guido van Rossum | 27e916f | 1995-01-26 00:39:00 +0000 | [diff] [blame] | 154 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 155 | free((char *)m); |
| 156 | } |
| 157 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 158 | static PyObject * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 159 | module_repr(m) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 160 | PyModuleObject *m; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 161 | { |
| 162 | char buf[100]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 163 | char *name = PyModule_GetName((PyObject *)m); |
Guido van Rossum | c45611d | 1993-11-17 22:58:56 +0000 | [diff] [blame] | 164 | if (name == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 165 | PyErr_Clear(); |
Guido van Rossum | c45611d | 1993-11-17 22:58:56 +0000 | [diff] [blame] | 166 | name = "?"; |
| 167 | } |
| 168 | sprintf(buf, "<module '%.80s'>", name); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 169 | return PyString_FromString(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 172 | static PyObject * |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 173 | module_getattr(m, name) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 174 | PyModuleObject *m; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 175 | char *name; |
| 176 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 177 | PyObject *res; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 178 | if (strcmp(name, "__dict__") == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 179 | Py_INCREF(m->md_dict); |
Guido van Rossum | e87203a | 1990-10-21 22:12:30 +0000 | [diff] [blame] | 180 | return m->md_dict; |
| 181 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 182 | res = PyDict_GetItemString(m->md_dict, name); |
Guido van Rossum | 2b654f7 | 1990-10-14 20:03:32 +0000 | [diff] [blame] | 183 | if (res == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 184 | PyErr_SetString(PyExc_AttributeError, name); |
Guido van Rossum | 0add15f | 1997-05-09 01:07:15 +0000 | [diff] [blame] | 185 | else |
| 186 | Py_INCREF(res); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 187 | return res; |
| 188 | } |
| 189 | |
| 190 | static int |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 191 | module_setattr(m, name, v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 192 | PyModuleObject *m; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 193 | char *name; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 194 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 195 | { |
Guido van Rossum | c45611d | 1993-11-17 22:58:56 +0000 | [diff] [blame] | 196 | if (name[0] == '_' && strcmp(name, "__dict__") == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 197 | PyErr_SetString(PyExc_TypeError, |
| 198 | "read-only special attribute"); |
Guido van Rossum | c45611d | 1993-11-17 22:58:56 +0000 | [diff] [blame] | 199 | return -1; |
Guido van Rossum | e87203a | 1990-10-21 22:12:30 +0000 | [diff] [blame] | 200 | } |
Guido van Rossum | 94472a0 | 1992-09-04 09:45:18 +0000 | [diff] [blame] | 201 | if (v == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 202 | int rv = PyDict_DelItemString(m->md_dict, name); |
Guido van Rossum | 94472a0 | 1992-09-04 09:45:18 +0000 | [diff] [blame] | 203 | if (rv < 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 204 | PyErr_SetString(PyExc_AttributeError, |
Guido van Rossum | 94472a0 | 1992-09-04 09:45:18 +0000 | [diff] [blame] | 205 | "delete non-existing module attribute"); |
| 206 | return rv; |
| 207 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 208 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 209 | return PyDict_SetItemString(m->md_dict, name, v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 212 | PyTypeObject PyModule_Type = { |
| 213 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 214 | 0, /*ob_size*/ |
| 215 | "module", /*tp_name*/ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 216 | sizeof(PyModuleObject), /*tp_size*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 217 | 0, /*tp_itemsize*/ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 218 | (destructor)module_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 219 | 0, /*tp_print*/ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 220 | (getattrfunc)module_getattr, /*tp_getattr*/ |
| 221 | (setattrfunc)module_setattr, /*tp_setattr*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 222 | 0, /*tp_compare*/ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 223 | (reprfunc)module_repr, /*tp_repr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 224 | }; |