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