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 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 7 | static Py_ssize_t max_module_number; |
| 8 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 9 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 10 | PyObject_HEAD |
| 11 | PyObject *md_dict; |
| 12 | struct PyModuleDef *md_def; |
| 13 | void *md_state; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 14 | } PyModuleObject; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 15 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 16 | static PyMemberDef module_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 17 | {"__dict__", T_OBJECT, offsetof(PyModuleObject, md_dict), READONLY}, |
| 18 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 19 | }; |
| 20 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 21 | static PyTypeObject moduledef_type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 22 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 23 | "moduledef", /* tp_name */ |
| 24 | sizeof(struct PyModuleDef), /* tp_size */ |
| 25 | 0, /* tp_itemsize */ |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 29 | PyObject * |
Victor Stinner | 0639b56 | 2011-03-04 12:57:07 +0000 | [diff] [blame] | 30 | PyModule_NewObject(PyObject *name) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 31 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 32 | PyModuleObject *m; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 33 | m = PyObject_GC_New(PyModuleObject, &PyModule_Type); |
| 34 | if (m == NULL) |
| 35 | return NULL; |
| 36 | m->md_def = NULL; |
| 37 | m->md_state = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 38 | m->md_dict = PyDict_New(); |
Victor Stinner | 0639b56 | 2011-03-04 12:57:07 +0000 | [diff] [blame] | 39 | if (m->md_dict == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 40 | goto fail; |
Victor Stinner | 0639b56 | 2011-03-04 12:57:07 +0000 | [diff] [blame] | 41 | if (PyDict_SetItemString(m->md_dict, "__name__", name) != 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 42 | goto fail; |
| 43 | if (PyDict_SetItemString(m->md_dict, "__doc__", Py_None) != 0) |
| 44 | goto fail; |
| 45 | if (PyDict_SetItemString(m->md_dict, "__package__", Py_None) != 0) |
| 46 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 47 | PyObject_GC_Track(m); |
| 48 | return (PyObject *)m; |
Guido van Rossum | c45611d | 1993-11-17 22:58:56 +0000 | [diff] [blame] | 49 | |
| 50 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 51 | Py_DECREF(m); |
| 52 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 55 | PyObject * |
Victor Stinner | 0639b56 | 2011-03-04 12:57:07 +0000 | [diff] [blame] | 56 | PyModule_New(const char *name) |
| 57 | { |
| 58 | PyObject *nameobj, *module; |
| 59 | nameobj = PyUnicode_FromString(name); |
| 60 | if (nameobj == NULL) |
| 61 | return NULL; |
| 62 | module = PyModule_NewObject(nameobj); |
| 63 | Py_DECREF(nameobj); |
| 64 | return module; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | PyObject * |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 69 | PyModule_Create2(struct PyModuleDef* module, int module_api_version) |
| 70 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 71 | PyObject *d, *v, *n; |
| 72 | PyMethodDef *ml; |
| 73 | const char* name; |
| 74 | PyModuleObject *m; |
R. David Murray | ce4b170 | 2010-12-14 23:06:25 +0000 | [diff] [blame] | 75 | PyInterpreterState *interp = PyThreadState_Get()->interp; |
| 76 | if (interp->modules == NULL) |
| 77 | Py_FatalError("Python import machinery not initialized"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 78 | if (PyType_Ready(&moduledef_type) < 0) |
| 79 | return NULL; |
| 80 | if (module->m_base.m_index == 0) { |
| 81 | max_module_number++; |
| 82 | Py_REFCNT(module) = 1; |
| 83 | Py_TYPE(module) = &moduledef_type; |
| 84 | module->m_base.m_index = max_module_number; |
| 85 | } |
| 86 | name = module->m_name; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 87 | if (module_api_version != PYTHON_API_VERSION && module_api_version != PYTHON_ABI_VERSION) { |
Victor Stinner | 4a2b7a1 | 2010-08-13 14:03:48 +0000 | [diff] [blame] | 88 | int err; |
| 89 | err = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 90 | "Python C API version mismatch for module %.100s: " |
| 91 | "This Python has API version %d, module %.100s has version %d.", |
| 92 | name, |
| 93 | PYTHON_API_VERSION, name, module_api_version); |
| 94 | if (err) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 95 | return NULL; |
| 96 | } |
| 97 | /* Make sure name is fully qualified. |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 98 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 99 | This is a bit of a hack: when the shared library is loaded, |
| 100 | the module name is "package.module", but the module calls |
| 101 | PyModule_Create*() with just "module" for the name. The shared |
| 102 | library loader squirrels away the true name of the module in |
| 103 | _Py_PackageContext, and PyModule_Create*() will substitute this |
| 104 | (if the name actually matches). |
| 105 | */ |
| 106 | if (_Py_PackageContext != NULL) { |
| 107 | char *p = strrchr(_Py_PackageContext, '.'); |
| 108 | if (p != NULL && strcmp(module->m_name, p+1) == 0) { |
| 109 | name = _Py_PackageContext; |
| 110 | _Py_PackageContext = NULL; |
| 111 | } |
| 112 | } |
| 113 | if ((m = (PyModuleObject*)PyModule_New(name)) == NULL) |
| 114 | return NULL; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 115 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 116 | if (module->m_size > 0) { |
| 117 | m->md_state = PyMem_MALLOC(module->m_size); |
| 118 | if (!m->md_state) { |
| 119 | PyErr_NoMemory(); |
| 120 | Py_DECREF(m); |
| 121 | return NULL; |
| 122 | } |
| 123 | memset(m->md_state, 0, module->m_size); |
| 124 | } |
| 125 | |
| 126 | d = PyModule_GetDict((PyObject*)m); |
| 127 | if (module->m_methods != NULL) { |
| 128 | n = PyUnicode_FromString(name); |
| 129 | if (n == NULL) |
| 130 | return NULL; |
| 131 | for (ml = module->m_methods; ml->ml_name != NULL; ml++) { |
| 132 | if ((ml->ml_flags & METH_CLASS) || |
| 133 | (ml->ml_flags & METH_STATIC)) { |
| 134 | PyErr_SetString(PyExc_ValueError, |
| 135 | "module functions cannot set" |
| 136 | " METH_CLASS or METH_STATIC"); |
| 137 | Py_DECREF(n); |
| 138 | return NULL; |
| 139 | } |
| 140 | v = PyCFunction_NewEx(ml, (PyObject*)m, n); |
| 141 | if (v == NULL) { |
| 142 | Py_DECREF(n); |
| 143 | return NULL; |
| 144 | } |
| 145 | if (PyDict_SetItemString(d, ml->ml_name, v) != 0) { |
| 146 | Py_DECREF(v); |
| 147 | Py_DECREF(n); |
| 148 | return NULL; |
| 149 | } |
| 150 | Py_DECREF(v); |
| 151 | } |
| 152 | Py_DECREF(n); |
| 153 | } |
| 154 | if (module->m_doc != NULL) { |
| 155 | v = PyUnicode_FromString(module->m_doc); |
| 156 | if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0) { |
| 157 | Py_XDECREF(v); |
| 158 | return NULL; |
| 159 | } |
| 160 | Py_DECREF(v); |
| 161 | } |
| 162 | m->md_def = module; |
| 163 | return (PyObject*)m; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 167 | PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 168 | PyModule_GetDict(PyObject *m) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 169 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 170 | PyObject *d; |
| 171 | if (!PyModule_Check(m)) { |
| 172 | PyErr_BadInternalCall(); |
| 173 | return NULL; |
| 174 | } |
| 175 | d = ((PyModuleObject *)m) -> md_dict; |
| 176 | if (d == NULL) |
| 177 | ((PyModuleObject *)m) -> md_dict = d = PyDict_New(); |
| 178 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Victor Stinner | bd47511 | 2011-02-23 00:21:43 +0000 | [diff] [blame] | 181 | PyObject* |
| 182 | PyModule_GetNameObject(PyObject *m) |
Guido van Rossum | 0558a20 | 1990-10-26 15:00:11 +0000 | [diff] [blame] | 183 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 184 | PyObject *d; |
Victor Stinner | bd47511 | 2011-02-23 00:21:43 +0000 | [diff] [blame] | 185 | PyObject *name; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 186 | if (!PyModule_Check(m)) { |
| 187 | PyErr_BadArgument(); |
| 188 | return NULL; |
| 189 | } |
| 190 | d = ((PyModuleObject *)m)->md_dict; |
| 191 | if (d == NULL || |
Victor Stinner | bd47511 | 2011-02-23 00:21:43 +0000 | [diff] [blame] | 192 | (name = PyDict_GetItemString(d, "__name__")) == NULL || |
| 193 | !PyUnicode_Check(name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 194 | { |
| 195 | PyErr_SetString(PyExc_SystemError, "nameless module"); |
| 196 | return NULL; |
| 197 | } |
Victor Stinner | bd47511 | 2011-02-23 00:21:43 +0000 | [diff] [blame] | 198 | Py_INCREF(name); |
| 199 | return name; |
| 200 | } |
| 201 | |
| 202 | const char * |
| 203 | PyModule_GetName(PyObject *m) |
| 204 | { |
| 205 | PyObject *name = PyModule_GetNameObject(m); |
| 206 | if (name == NULL) |
| 207 | return NULL; |
| 208 | Py_DECREF(name); /* module dict has still a reference */ |
| 209 | return _PyUnicode_AsString(name); |
Guido van Rossum | 0558a20 | 1990-10-26 15:00:11 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Victor Stinner | 6c00c14 | 2010-08-17 23:37:11 +0000 | [diff] [blame] | 212 | PyObject* |
| 213 | PyModule_GetFilenameObject(PyObject *m) |
Guido van Rossum | 98cc19f | 1999-02-15 14:47:16 +0000 | [diff] [blame] | 214 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 215 | PyObject *d; |
| 216 | PyObject *fileobj; |
| 217 | if (!PyModule_Check(m)) { |
| 218 | PyErr_BadArgument(); |
| 219 | return NULL; |
| 220 | } |
| 221 | d = ((PyModuleObject *)m)->md_dict; |
| 222 | if (d == NULL || |
| 223 | (fileobj = PyDict_GetItemString(d, "__file__")) == NULL || |
| 224 | !PyUnicode_Check(fileobj)) |
| 225 | { |
| 226 | PyErr_SetString(PyExc_SystemError, "module filename missing"); |
| 227 | return NULL; |
| 228 | } |
Victor Stinner | 6c00c14 | 2010-08-17 23:37:11 +0000 | [diff] [blame] | 229 | Py_INCREF(fileobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 230 | return fileobj; |
Victor Stinner | 8124feb | 2010-05-07 00:50:12 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | const char * |
| 234 | PyModule_GetFilename(PyObject *m) |
| 235 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 236 | PyObject *fileobj; |
Victor Stinner | 6c00c14 | 2010-08-17 23:37:11 +0000 | [diff] [blame] | 237 | char *utf8; |
| 238 | fileobj = PyModule_GetFilenameObject(m); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 239 | if (fileobj == NULL) |
| 240 | return NULL; |
Victor Stinner | 6c00c14 | 2010-08-17 23:37:11 +0000 | [diff] [blame] | 241 | utf8 = _PyUnicode_AsString(fileobj); |
Victor Stinner | bd47511 | 2011-02-23 00:21:43 +0000 | [diff] [blame] | 242 | Py_DECREF(fileobj); /* module dict has still a reference */ |
Victor Stinner | 6c00c14 | 2010-08-17 23:37:11 +0000 | [diff] [blame] | 243 | return utf8; |
Guido van Rossum | 98cc19f | 1999-02-15 14:47:16 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 246 | PyModuleDef* |
| 247 | PyModule_GetDef(PyObject* m) |
| 248 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 249 | if (!PyModule_Check(m)) { |
| 250 | PyErr_BadArgument(); |
| 251 | return NULL; |
| 252 | } |
| 253 | return ((PyModuleObject *)m)->md_def; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | void* |
| 257 | PyModule_GetState(PyObject* m) |
| 258 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 259 | if (!PyModule_Check(m)) { |
| 260 | PyErr_BadArgument(); |
| 261 | return NULL; |
| 262 | } |
| 263 | return ((PyModuleObject *)m)->md_state; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Guido van Rossum | f1dc061 | 1998-02-19 20:51:52 +0000 | [diff] [blame] | 266 | void |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 267 | _PyModule_Clear(PyObject *m) |
Guido van Rossum | f1dc061 | 1998-02-19 20:51:52 +0000 | [diff] [blame] | 268 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 269 | /* To make the execution order of destructors for global |
| 270 | objects a bit more predictable, we first zap all objects |
| 271 | whose name starts with a single underscore, before we clear |
| 272 | the entire dictionary. We zap them by replacing them with |
| 273 | None, rather than deleting them from the dictionary, to |
| 274 | avoid rehashing the dictionary (to some extent). */ |
Guido van Rossum | f1dc061 | 1998-02-19 20:51:52 +0000 | [diff] [blame] | 275 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 276 | Py_ssize_t pos; |
| 277 | PyObject *key, *value; |
| 278 | PyObject *d; |
Guido van Rossum | f1dc061 | 1998-02-19 20:51:52 +0000 | [diff] [blame] | 279 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 280 | d = ((PyModuleObject *)m)->md_dict; |
| 281 | if (d == NULL) |
| 282 | return; |
Guido van Rossum | f1dc061 | 1998-02-19 20:51:52 +0000 | [diff] [blame] | 283 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 284 | /* First, clear only names starting with a single underscore */ |
| 285 | pos = 0; |
| 286 | while (PyDict_Next(d, &pos, &key, &value)) { |
| 287 | if (value != Py_None && PyUnicode_Check(key)) { |
Victor Stinner | f3f22a2 | 2010-05-19 00:03:09 +0000 | [diff] [blame] | 288 | Py_UNICODE *u = PyUnicode_AS_UNICODE(key); |
| 289 | if (u[0] == '_' && u[1] != '_') { |
| 290 | if (Py_VerboseFlag > 1) { |
| 291 | const char *s = _PyUnicode_AsString(key); |
| 292 | if (s != NULL) |
| 293 | PySys_WriteStderr("# clear[1] %s\n", s); |
| 294 | else |
| 295 | PyErr_Clear(); |
| 296 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 297 | PyDict_SetItem(d, key, Py_None); |
| 298 | } |
| 299 | } |
| 300 | } |
Guido van Rossum | f1dc061 | 1998-02-19 20:51:52 +0000 | [diff] [blame] | 301 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 302 | /* Next, clear all names except for __builtins__ */ |
| 303 | pos = 0; |
| 304 | while (PyDict_Next(d, &pos, &key, &value)) { |
| 305 | if (value != Py_None && PyUnicode_Check(key)) { |
Victor Stinner | f3f22a2 | 2010-05-19 00:03:09 +0000 | [diff] [blame] | 306 | Py_UNICODE *u = PyUnicode_AS_UNICODE(key); |
| 307 | if (u[0] != '_' |
| 308 | || PyUnicode_CompareWithASCIIString(key, "__builtins__") != 0) |
| 309 | { |
| 310 | if (Py_VerboseFlag > 1) { |
| 311 | const char *s = _PyUnicode_AsString(key); |
| 312 | if (s != NULL) |
| 313 | PySys_WriteStderr("# clear[2] %s\n", s); |
| 314 | else |
| 315 | PyErr_Clear(); |
| 316 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 317 | PyDict_SetItem(d, key, Py_None); |
| 318 | } |
| 319 | } |
| 320 | } |
Guido van Rossum | f1dc061 | 1998-02-19 20:51:52 +0000 | [diff] [blame] | 321 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 322 | /* Note: we leave __builtins__ in place, so that destructors |
| 323 | of non-global objects defined in this module can still use |
| 324 | builtins, in particularly 'None'. */ |
Guido van Rossum | f1dc061 | 1998-02-19 20:51:52 +0000 | [diff] [blame] | 325 | |
| 326 | } |
| 327 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 328 | /* Methods */ |
| 329 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 330 | static int |
Guido van Rossum | c3a787e | 2002-06-04 05:52:47 +0000 | [diff] [blame] | 331 | module_init(PyModuleObject *m, PyObject *args, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 332 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 333 | static char *kwlist[] = {"name", "doc", NULL}; |
| 334 | PyObject *dict, *name = Py_None, *doc = Py_None; |
| 335 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "U|O:module.__init__", |
| 336 | kwlist, &name, &doc)) |
| 337 | return -1; |
| 338 | dict = m->md_dict; |
| 339 | if (dict == NULL) { |
| 340 | dict = PyDict_New(); |
| 341 | if (dict == NULL) |
| 342 | return -1; |
| 343 | m->md_dict = dict; |
| 344 | } |
| 345 | if (PyDict_SetItemString(dict, "__name__", name) < 0) |
| 346 | return -1; |
| 347 | if (PyDict_SetItemString(dict, "__doc__", doc) < 0) |
| 348 | return -1; |
| 349 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 352 | static void |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 353 | module_dealloc(PyModuleObject *m) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 354 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 355 | PyObject_GC_UnTrack(m); |
| 356 | if (m->md_def && m->md_def->m_free) |
| 357 | m->md_def->m_free(m); |
| 358 | if (m->md_dict != NULL) { |
Benjamin Peterson | 5c4bfc4 | 2010-10-12 22:57:59 +0000 | [diff] [blame] | 359 | _PyModule_Clear((PyObject *)m); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 360 | Py_DECREF(m->md_dict); |
| 361 | } |
| 362 | if (m->md_state != NULL) |
| 363 | PyMem_FREE(m->md_state); |
| 364 | Py_TYPE(m)->tp_free((PyObject *)m); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 367 | static PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 368 | module_repr(PyModuleObject *m) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 369 | { |
Victor Stinner | bd47511 | 2011-02-23 00:21:43 +0000 | [diff] [blame] | 370 | PyObject *name, *filename, *repr; |
Barry Warsaw | 2907fe6 | 2001-08-16 20:39:24 +0000 | [diff] [blame] | 371 | |
Victor Stinner | bd47511 | 2011-02-23 00:21:43 +0000 | [diff] [blame] | 372 | name = PyModule_GetNameObject((PyObject *)m); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 373 | if (name == NULL) { |
| 374 | PyErr_Clear(); |
Victor Stinner | bd47511 | 2011-02-23 00:21:43 +0000 | [diff] [blame] | 375 | name = PyUnicode_FromStringAndSize("?", 1); |
| 376 | if (name == NULL) |
| 377 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 378 | } |
Victor Stinner | 6c00c14 | 2010-08-17 23:37:11 +0000 | [diff] [blame] | 379 | filename = PyModule_GetFilenameObject((PyObject *)m); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 380 | if (filename == NULL) { |
| 381 | PyErr_Clear(); |
Victor Stinner | bd47511 | 2011-02-23 00:21:43 +0000 | [diff] [blame] | 382 | repr = PyUnicode_FromFormat("<module %R (built-in)>", name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 383 | } |
Victor Stinner | bd47511 | 2011-02-23 00:21:43 +0000 | [diff] [blame] | 384 | else { |
| 385 | repr = PyUnicode_FromFormat("<module %R from %R>", name, filename); |
| 386 | Py_DECREF(filename); |
| 387 | } |
| 388 | Py_DECREF(name); |
Victor Stinner | 6c00c14 | 2010-08-17 23:37:11 +0000 | [diff] [blame] | 389 | return repr; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Neil Schemenauer | 10e31cf | 2001-01-02 15:58:27 +0000 | [diff] [blame] | 392 | static int |
| 393 | module_traverse(PyModuleObject *m, visitproc visit, void *arg) |
| 394 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 395 | if (m->md_def && m->md_def->m_traverse) { |
| 396 | int res = m->md_def->m_traverse((PyObject*)m, visit, arg); |
| 397 | if (res) |
| 398 | return res; |
| 399 | } |
| 400 | Py_VISIT(m->md_dict); |
| 401 | return 0; |
Neil Schemenauer | 10e31cf | 2001-01-02 15:58:27 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 404 | static int |
| 405 | module_clear(PyModuleObject *m) |
| 406 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 407 | if (m->md_def && m->md_def->m_clear) { |
| 408 | int res = m->md_def->m_clear((PyObject*)m); |
| 409 | if (res) |
| 410 | return res; |
| 411 | } |
| 412 | Py_CLEAR(m->md_dict); |
| 413 | return 0; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 414 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 415 | |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 416 | static PyObject * |
| 417 | module_dir(PyObject *self, PyObject *args) |
| 418 | { |
| 419 | PyObject *result = NULL; |
| 420 | PyObject *dict = PyObject_GetAttrString(self, "__dict__"); |
| 421 | |
| 422 | if (dict != NULL) { |
| 423 | if (PyDict_Check(dict)) |
| 424 | result = PyDict_Keys(dict); |
| 425 | else { |
| 426 | const char *name = PyModule_GetName(self); |
| 427 | if (name) |
| 428 | PyErr_Format(PyExc_TypeError, |
| 429 | "%.200s.__dict__ is not a dictionary", |
| 430 | name); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | Py_XDECREF(dict); |
| 435 | return result; |
| 436 | } |
| 437 | |
| 438 | static PyMethodDef module_methods[] = { |
| 439 | {"__dir__", module_dir, METH_NOARGS, |
Benjamin Peterson | c728412 | 2011-05-24 12:46:15 -0500 | [diff] [blame] | 440 | PyDoc_STR("__dir__() -> list\nspecialized dir() implementation")}, |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 441 | {0} |
| 442 | }; |
| 443 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 444 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 445 | PyDoc_STRVAR(module_doc, |
Guido van Rossum | ed3e09f | 2002-06-04 06:02:35 +0000 | [diff] [blame] | 446 | "module(name[, doc])\n\ |
| 447 | \n\ |
| 448 | Create a module object.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 449 | 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] | 450 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 451 | PyTypeObject PyModule_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 452 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 453 | "module", /* tp_name */ |
| 454 | sizeof(PyModuleObject), /* tp_size */ |
| 455 | 0, /* tp_itemsize */ |
| 456 | (destructor)module_dealloc, /* tp_dealloc */ |
| 457 | 0, /* tp_print */ |
| 458 | 0, /* tp_getattr */ |
| 459 | 0, /* tp_setattr */ |
| 460 | 0, /* tp_reserved */ |
| 461 | (reprfunc)module_repr, /* tp_repr */ |
| 462 | 0, /* tp_as_number */ |
| 463 | 0, /* tp_as_sequence */ |
| 464 | 0, /* tp_as_mapping */ |
| 465 | 0, /* tp_hash */ |
| 466 | 0, /* tp_call */ |
| 467 | 0, /* tp_str */ |
| 468 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 469 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 470 | 0, /* tp_as_buffer */ |
| 471 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 472 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 473 | module_doc, /* tp_doc */ |
| 474 | (traverseproc)module_traverse, /* tp_traverse */ |
| 475 | (inquiry)module_clear, /* tp_clear */ |
| 476 | 0, /* tp_richcompare */ |
| 477 | 0, /* tp_weaklistoffset */ |
| 478 | 0, /* tp_iter */ |
| 479 | 0, /* tp_iternext */ |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 480 | module_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 481 | module_members, /* tp_members */ |
| 482 | 0, /* tp_getset */ |
| 483 | 0, /* tp_base */ |
| 484 | 0, /* tp_dict */ |
| 485 | 0, /* tp_descr_get */ |
| 486 | 0, /* tp_descr_set */ |
| 487 | offsetof(PyModuleObject, md_dict), /* tp_dictoffset */ |
| 488 | (initproc)module_init, /* tp_init */ |
| 489 | PyType_GenericAlloc, /* tp_alloc */ |
| 490 | PyType_GenericNew, /* tp_new */ |
| 491 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 492 | }; |