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 * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 18 | PyModule_New(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; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 33 | Py_DECREF(nameobj); |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 34 | PyObject_GC_Track(m); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 35 | return (PyObject *)m; |
Guido van Rossum | c45611d | 1993-11-17 22:58:56 +0000 | [diff] [blame] | 36 | |
| 37 | fail: |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 38 | Py_XDECREF(nameobj); |
| 39 | Py_DECREF(m); |
Guido van Rossum | c45611d | 1993-11-17 22:58:56 +0000 | [diff] [blame] | 40 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 41 | } |
| 42 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 43 | PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 44 | PyModule_GetDict(PyObject *m) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 45 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 46 | if (!PyModule_Check(m)) { |
| 47 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 48 | return NULL; |
| 49 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 50 | return ((PyModuleObject *)m) -> md_dict; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Guido van Rossum | 0558a20 | 1990-10-26 15:00:11 +0000 | [diff] [blame] | 53 | char * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 54 | PyModule_GetName(PyObject *m) |
Guido van Rossum | 0558a20 | 1990-10-26 15:00:11 +0000 | [diff] [blame] | 55 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 56 | PyObject *nameobj; |
| 57 | if (!PyModule_Check(m)) { |
| 58 | PyErr_BadArgument(); |
Guido van Rossum | 0558a20 | 1990-10-26 15:00:11 +0000 | [diff] [blame] | 59 | return NULL; |
| 60 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 61 | nameobj = PyDict_GetItemString(((PyModuleObject *)m)->md_dict, |
| 62 | "__name__"); |
| 63 | if (nameobj == NULL || !PyString_Check(nameobj)) { |
| 64 | PyErr_SetString(PyExc_SystemError, "nameless module"); |
Guido van Rossum | c45611d | 1993-11-17 22:58:56 +0000 | [diff] [blame] | 65 | return NULL; |
| 66 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 67 | return PyString_AsString(nameobj); |
Guido van Rossum | 0558a20 | 1990-10-26 15:00:11 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Guido van Rossum | 98cc19f | 1999-02-15 14:47:16 +0000 | [diff] [blame] | 70 | char * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 71 | PyModule_GetFilename(PyObject *m) |
Guido van Rossum | 98cc19f | 1999-02-15 14:47:16 +0000 | [diff] [blame] | 72 | { |
| 73 | PyObject *fileobj; |
| 74 | if (!PyModule_Check(m)) { |
| 75 | PyErr_BadArgument(); |
| 76 | return NULL; |
| 77 | } |
| 78 | fileobj = PyDict_GetItemString(((PyModuleObject *)m)->md_dict, |
| 79 | "__file__"); |
| 80 | if (fileobj == NULL || !PyString_Check(fileobj)) { |
| 81 | PyErr_SetString(PyExc_SystemError, "module filename missing"); |
| 82 | return NULL; |
| 83 | } |
| 84 | return PyString_AsString(fileobj); |
| 85 | } |
| 86 | |
Guido van Rossum | f1dc061 | 1998-02-19 20:51:52 +0000 | [diff] [blame] | 87 | void |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 88 | _PyModule_Clear(PyObject *m) |
Guido van Rossum | f1dc061 | 1998-02-19 20:51:52 +0000 | [diff] [blame] | 89 | { |
| 90 | /* To make the execution order of destructors for global |
| 91 | objects a bit more predictable, we first zap all objects |
| 92 | whose name starts with a single underscore, before we clear |
| 93 | the entire dictionary. We zap them by replacing them with |
| 94 | None, rather than deleting them from the dictionary, to |
| 95 | avoid rehashing the dictionary (to some extent). */ |
| 96 | |
| 97 | int pos; |
| 98 | PyObject *key, *value; |
| 99 | PyObject *d; |
| 100 | |
| 101 | d = ((PyModuleObject *)m)->md_dict; |
| 102 | |
| 103 | /* First, clear only names starting with a single underscore */ |
| 104 | pos = 0; |
| 105 | while (PyDict_Next(d, &pos, &key, &value)) { |
| 106 | if (value != Py_None && PyString_Check(key)) { |
| 107 | char *s = PyString_AsString(key); |
| 108 | if (s[0] == '_' && s[1] != '_') { |
| 109 | if (Py_VerboseFlag > 1) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 110 | PySys_WriteStderr("# clear[1] %s\n", s); |
Guido van Rossum | f1dc061 | 1998-02-19 20:51:52 +0000 | [diff] [blame] | 111 | PyDict_SetItem(d, key, Py_None); |
| 112 | } |
| 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) |
Guido van Rossum | 2f3667a | 1998-10-12 18:23:55 +0000 | [diff] [blame] | 123 | PySys_WriteStderr("# clear[2] %s\n", s); |
Guido van Rossum | f1dc061 | 1998-02-19 20:51:52 +0000 | [diff] [blame] | 124 | PyDict_SetItem(d, key, Py_None); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /* Note: we leave __builtins__ in place, so that destructors |
| 130 | of non-global objects defined in this module can still use |
| 131 | builtins, in particularly 'None'. */ |
| 132 | |
| 133 | } |
| 134 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 135 | /* Methods */ |
| 136 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 137 | static int |
| 138 | module_init(PyModuleObject *m, PyObject *args, PyObject *kw) |
| 139 | { |
| 140 | m->md_dict = PyDict_New(); |
| 141 | if (m->md_dict == NULL) |
| 142 | return -1; |
| 143 | return 0; |
| 144 | } |
| 145 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 146 | static void |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 147 | module_dealloc(PyModuleObject *m) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 148 | { |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 149 | PyObject_GC_UnTrack(m); |
Guido van Rossum | 27e916f | 1995-01-26 00:39:00 +0000 | [diff] [blame] | 150 | if (m->md_dict != NULL) { |
Guido van Rossum | f1dc061 | 1998-02-19 20:51:52 +0000 | [diff] [blame] | 151 | _PyModule_Clear((PyObject *)m); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 152 | Py_DECREF(m->md_dict); |
Guido van Rossum | 27e916f | 1995-01-26 00:39:00 +0000 | [diff] [blame] | 153 | } |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 154 | m->ob_type->tp_free((PyObject *)m); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 157 | static PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 158 | module_repr(PyModuleObject *m) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 159 | { |
Guido van Rossum | 98cc19f | 1999-02-15 14:47:16 +0000 | [diff] [blame] | 160 | char *name; |
| 161 | char *filename; |
Barry Warsaw | 2907fe6 | 2001-08-16 20:39:24 +0000 | [diff] [blame] | 162 | |
Guido van Rossum | 98cc19f | 1999-02-15 14:47:16 +0000 | [diff] [blame] | 163 | 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 | } |
Guido van Rossum | 98cc19f | 1999-02-15 14:47:16 +0000 | [diff] [blame] | 168 | filename = PyModule_GetFilename((PyObject *)m); |
| 169 | if (filename == NULL) { |
| 170 | PyErr_Clear(); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 171 | return PyString_FromFormat("<module '%s' (built-in)>", name); |
Guido van Rossum | 98cc19f | 1999-02-15 14:47:16 +0000 | [diff] [blame] | 172 | } |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 173 | return PyString_FromFormat("<module '%s' from '%s'>", name, filename); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Neil Schemenauer | 10e31cf | 2001-01-02 15:58:27 +0000 | [diff] [blame] | 176 | /* We only need a traverse function, no clear function: If the module |
| 177 | is in a cycle, md_dict will be cleared as well, which will break |
| 178 | the cycle. */ |
| 179 | static int |
| 180 | module_traverse(PyModuleObject *m, visitproc visit, void *arg) |
| 181 | { |
| 182 | if (m->md_dict != NULL) |
| 183 | return visit(m->md_dict, arg); |
| 184 | return 0; |
| 185 | } |
| 186 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 187 | PyTypeObject PyModule_Type = { |
| 188 | PyObject_HEAD_INIT(&PyType_Type) |
Tim Peters | d85e102 | 2001-05-11 21:51:48 +0000 | [diff] [blame] | 189 | 0, /* ob_size */ |
| 190 | "module", /* tp_name */ |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 191 | sizeof(PyModuleObject), /* tp_size */ |
Tim Peters | d85e102 | 2001-05-11 21:51:48 +0000 | [diff] [blame] | 192 | 0, /* tp_itemsize */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 193 | (destructor)module_dealloc, /* tp_dealloc */ |
Tim Peters | d85e102 | 2001-05-11 21:51:48 +0000 | [diff] [blame] | 194 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 195 | 0, /* tp_getattr */ |
| 196 | 0, /* tp_setattr */ |
Tim Peters | d85e102 | 2001-05-11 21:51:48 +0000 | [diff] [blame] | 197 | 0, /* tp_compare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 198 | (reprfunc)module_repr, /* tp_repr */ |
Tim Peters | d85e102 | 2001-05-11 21:51:48 +0000 | [diff] [blame] | 199 | 0, /* tp_as_number */ |
| 200 | 0, /* tp_as_sequence */ |
| 201 | 0, /* tp_as_mapping */ |
| 202 | 0, /* tp_hash */ |
| 203 | 0, /* tp_call */ |
| 204 | 0, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 205 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 206 | PyObject_GenericSetAttr, /* tp_setattro */ |
Tim Peters | d85e102 | 2001-05-11 21:51:48 +0000 | [diff] [blame] | 207 | 0, /* tp_as_buffer */ |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 208 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 209 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | d85e102 | 2001-05-11 21:51:48 +0000 | [diff] [blame] | 210 | 0, /* tp_doc */ |
| 211 | (traverseproc)module_traverse, /* tp_traverse */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 212 | 0, /* tp_clear */ |
| 213 | 0, /* tp_richcompare */ |
| 214 | 0, /* tp_weaklistoffset */ |
| 215 | 0, /* tp_iter */ |
| 216 | 0, /* tp_iternext */ |
| 217 | 0, /* tp_methods */ |
| 218 | module_members, /* tp_members */ |
| 219 | 0, /* tp_getset */ |
| 220 | 0, /* tp_base */ |
| 221 | 0, /* tp_dict */ |
| 222 | 0, /* tp_descr_get */ |
| 223 | 0, /* tp_descr_set */ |
| 224 | offsetof(PyModuleObject, md_dict), /* tp_dictoffset */ |
| 225 | (initproc)module_init, /* tp_init */ |
| 226 | PyType_GenericAlloc, /* tp_alloc */ |
| 227 | PyType_GenericNew, /* tp_new */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 228 | _PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 229 | }; |