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; |
Antoine Pitrou | dcedaf6 | 2013-07-31 23:14:08 +0200 | [diff] [blame] | 14 | PyObject *md_weaklist; |
| 15 | PyObject *md_name; /* for logging purposes after md_dict is cleared */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 16 | } PyModuleObject; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 17 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 18 | static PyMemberDef module_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 19 | {"__dict__", T_OBJECT, offsetof(PyModuleObject, md_dict), READONLY}, |
| 20 | {0} |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 21 | }; |
| 22 | |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 23 | PyTypeObject PyModuleDef_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 24 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 25 | "moduledef", /* tp_name */ |
| 26 | sizeof(struct PyModuleDef), /* tp_size */ |
| 27 | 0, /* tp_itemsize */ |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 31 | PyObject* |
| 32 | PyModuleDef_Init(struct PyModuleDef* def) |
| 33 | { |
| 34 | if (PyType_Ready(&PyModuleDef_Type) < 0) |
| 35 | return NULL; |
| 36 | if (def->m_base.m_index == 0) { |
| 37 | max_module_number++; |
| 38 | Py_REFCNT(def) = 1; |
| 39 | Py_TYPE(def) = &PyModuleDef_Type; |
| 40 | def->m_base.m_index = max_module_number; |
| 41 | } |
| 42 | return (PyObject*)def; |
| 43 | } |
| 44 | |
Brett Cannon | 4c14b5d | 2013-05-04 13:56:58 -0400 | [diff] [blame] | 45 | static int |
Antoine Pitrou | dcedaf6 | 2013-07-31 23:14:08 +0200 | [diff] [blame] | 46 | module_init_dict(PyModuleObject *mod, PyObject *md_dict, |
| 47 | PyObject *name, PyObject *doc) |
Brett Cannon | 4c14b5d | 2013-05-04 13:56:58 -0400 | [diff] [blame] | 48 | { |
Benjamin Peterson | 027ce16 | 2014-04-24 19:39:18 -0400 | [diff] [blame] | 49 | _Py_IDENTIFIER(__name__); |
| 50 | _Py_IDENTIFIER(__doc__); |
| 51 | _Py_IDENTIFIER(__package__); |
| 52 | _Py_IDENTIFIER(__loader__); |
| 53 | _Py_IDENTIFIER(__spec__); |
Serhiy Storchaka | 009b811 | 2015-03-18 21:53:15 +0200 | [diff] [blame] | 54 | |
Brett Cannon | 4c14b5d | 2013-05-04 13:56:58 -0400 | [diff] [blame] | 55 | if (md_dict == NULL) |
| 56 | return -1; |
| 57 | if (doc == NULL) |
| 58 | doc = Py_None; |
| 59 | |
Benjamin Peterson | 027ce16 | 2014-04-24 19:39:18 -0400 | [diff] [blame] | 60 | if (_PyDict_SetItemId(md_dict, &PyId___name__, name) != 0) |
Brett Cannon | 4c14b5d | 2013-05-04 13:56:58 -0400 | [diff] [blame] | 61 | return -1; |
Benjamin Peterson | 027ce16 | 2014-04-24 19:39:18 -0400 | [diff] [blame] | 62 | if (_PyDict_SetItemId(md_dict, &PyId___doc__, doc) != 0) |
Brett Cannon | 4c14b5d | 2013-05-04 13:56:58 -0400 | [diff] [blame] | 63 | return -1; |
Benjamin Peterson | 027ce16 | 2014-04-24 19:39:18 -0400 | [diff] [blame] | 64 | if (_PyDict_SetItemId(md_dict, &PyId___package__, Py_None) != 0) |
Brett Cannon | 4c14b5d | 2013-05-04 13:56:58 -0400 | [diff] [blame] | 65 | return -1; |
Benjamin Peterson | 027ce16 | 2014-04-24 19:39:18 -0400 | [diff] [blame] | 66 | if (_PyDict_SetItemId(md_dict, &PyId___loader__, Py_None) != 0) |
Brett Cannon | 4c14b5d | 2013-05-04 13:56:58 -0400 | [diff] [blame] | 67 | return -1; |
Benjamin Peterson | 027ce16 | 2014-04-24 19:39:18 -0400 | [diff] [blame] | 68 | if (_PyDict_SetItemId(md_dict, &PyId___spec__, Py_None) != 0) |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 69 | return -1; |
Antoine Pitrou | dcedaf6 | 2013-07-31 23:14:08 +0200 | [diff] [blame] | 70 | if (PyUnicode_CheckExact(name)) { |
| 71 | Py_INCREF(name); |
Serhiy Storchaka | 5a57ade | 2015-12-24 10:35:59 +0200 | [diff] [blame] | 72 | Py_SETREF(mod->md_name, name); |
Antoine Pitrou | dcedaf6 | 2013-07-31 23:14:08 +0200 | [diff] [blame] | 73 | } |
Brett Cannon | 4c14b5d | 2013-05-04 13:56:58 -0400 | [diff] [blame] | 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 79 | PyObject * |
Victor Stinner | 0639b56 | 2011-03-04 12:57:07 +0000 | [diff] [blame] | 80 | PyModule_NewObject(PyObject *name) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 81 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 82 | PyModuleObject *m; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 83 | m = PyObject_GC_New(PyModuleObject, &PyModule_Type); |
| 84 | if (m == NULL) |
| 85 | return NULL; |
| 86 | m->md_def = NULL; |
| 87 | m->md_state = NULL; |
Antoine Pitrou | dcedaf6 | 2013-07-31 23:14:08 +0200 | [diff] [blame] | 88 | m->md_weaklist = NULL; |
| 89 | m->md_name = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 90 | m->md_dict = PyDict_New(); |
Antoine Pitrou | dcedaf6 | 2013-07-31 23:14:08 +0200 | [diff] [blame] | 91 | if (module_init_dict(m, m->md_dict, name, NULL) != 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 92 | goto fail; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 93 | PyObject_GC_Track(m); |
| 94 | return (PyObject *)m; |
Guido van Rossum | c45611d | 1993-11-17 22:58:56 +0000 | [diff] [blame] | 95 | |
| 96 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 97 | Py_DECREF(m); |
| 98 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 101 | PyObject * |
Victor Stinner | 0639b56 | 2011-03-04 12:57:07 +0000 | [diff] [blame] | 102 | PyModule_New(const char *name) |
| 103 | { |
| 104 | PyObject *nameobj, *module; |
| 105 | nameobj = PyUnicode_FromString(name); |
| 106 | if (nameobj == NULL) |
| 107 | return NULL; |
| 108 | module = PyModule_NewObject(nameobj); |
| 109 | Py_DECREF(nameobj); |
| 110 | return module; |
| 111 | } |
| 112 | |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 113 | /* Check API/ABI version |
| 114 | * Issues a warning on mismatch, which is usually not fatal. |
| 115 | * Returns 0 if an exception is raised. |
| 116 | */ |
| 117 | static int |
| 118 | check_api_version(const char *name, int module_api_version) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 119 | { |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 120 | 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] | 121 | int err; |
| 122 | err = PyErr_WarnFormat(PyExc_RuntimeWarning, 1, |
| 123 | "Python C API version mismatch for module %.100s: " |
| 124 | "This Python has API version %d, module %.100s has version %d.", |
| 125 | name, |
| 126 | PYTHON_API_VERSION, name, module_api_version); |
| 127 | if (err) |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 128 | return 0; |
| 129 | } |
| 130 | return 1; |
| 131 | } |
| 132 | |
| 133 | PyObject * |
| 134 | PyModule_Create2(struct PyModuleDef* module, int module_api_version) |
| 135 | { |
| 136 | const char* name; |
| 137 | PyModuleObject *m; |
| 138 | PyInterpreterState *interp = PyThreadState_Get()->interp; |
| 139 | if (interp->modules == NULL) |
| 140 | Py_FatalError("Python import machinery not initialized"); |
| 141 | if (!PyModuleDef_Init(module)) |
| 142 | return NULL; |
| 143 | name = module->m_name; |
| 144 | if (!check_api_version(name, module_api_version)) { |
| 145 | return NULL; |
| 146 | } |
| 147 | if (module->m_slots) { |
| 148 | PyErr_Format( |
| 149 | PyExc_SystemError, |
| 150 | "module %s: PyModule_Create is incompatible with m_slots", name); |
| 151 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 152 | } |
| 153 | /* Make sure name is fully qualified. |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 154 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 155 | This is a bit of a hack: when the shared library is loaded, |
| 156 | the module name is "package.module", but the module calls |
| 157 | PyModule_Create*() with just "module" for the name. The shared |
| 158 | library loader squirrels away the true name of the module in |
| 159 | _Py_PackageContext, and PyModule_Create*() will substitute this |
| 160 | (if the name actually matches). |
| 161 | */ |
| 162 | if (_Py_PackageContext != NULL) { |
| 163 | char *p = strrchr(_Py_PackageContext, '.'); |
| 164 | if (p != NULL && strcmp(module->m_name, p+1) == 0) { |
| 165 | name = _Py_PackageContext; |
| 166 | _Py_PackageContext = NULL; |
| 167 | } |
| 168 | } |
| 169 | if ((m = (PyModuleObject*)PyModule_New(name)) == NULL) |
| 170 | return NULL; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 171 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 172 | if (module->m_size > 0) { |
| 173 | m->md_state = PyMem_MALLOC(module->m_size); |
| 174 | if (!m->md_state) { |
| 175 | PyErr_NoMemory(); |
| 176 | Py_DECREF(m); |
| 177 | return NULL; |
| 178 | } |
| 179 | memset(m->md_state, 0, module->m_size); |
| 180 | } |
| 181 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 182 | if (module->m_methods != NULL) { |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 183 | if (PyModule_AddFunctions((PyObject *) m, module->m_methods) != 0) { |
Meador Inge | 29e49d6 | 2012-07-19 13:45:43 -0500 | [diff] [blame] | 184 | Py_DECREF(m); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 185 | return NULL; |
Meador Inge | 29e49d6 | 2012-07-19 13:45:43 -0500 | [diff] [blame] | 186 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 187 | } |
| 188 | if (module->m_doc != NULL) { |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 189 | if (PyModule_SetDocString((PyObject *) m, module->m_doc) != 0) { |
Meador Inge | 29e49d6 | 2012-07-19 13:45:43 -0500 | [diff] [blame] | 190 | Py_DECREF(m); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 191 | return NULL; |
| 192 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 193 | } |
| 194 | m->md_def = module; |
| 195 | return (PyObject*)m; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 198 | PyObject * |
| 199 | PyModule_FromDefAndSpec2(struct PyModuleDef* def, PyObject *spec, int module_api_version) |
| 200 | { |
| 201 | PyModuleDef_Slot* cur_slot; |
| 202 | PyObject *(*create)(PyObject *, PyModuleDef*) = NULL; |
| 203 | PyObject *nameobj; |
| 204 | PyObject *m = NULL; |
| 205 | int has_execution_slots = 0; |
| 206 | char *name; |
| 207 | int ret; |
| 208 | |
| 209 | PyModuleDef_Init(def); |
| 210 | |
| 211 | nameobj = PyObject_GetAttrString(spec, "name"); |
| 212 | if (nameobj == NULL) { |
| 213 | return NULL; |
| 214 | } |
| 215 | name = PyUnicode_AsUTF8(nameobj); |
| 216 | if (name == NULL) { |
| 217 | goto error; |
| 218 | } |
| 219 | |
| 220 | if (!check_api_version(name, module_api_version)) { |
| 221 | goto error; |
| 222 | } |
| 223 | |
| 224 | if (def->m_size < 0) { |
| 225 | PyErr_Format( |
| 226 | PyExc_SystemError, |
| 227 | "module %s: m_size may not be negative for multi-phase initialization", |
| 228 | name); |
| 229 | goto error; |
| 230 | } |
| 231 | |
| 232 | for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) { |
| 233 | if (cur_slot->slot == Py_mod_create) { |
| 234 | if (create) { |
| 235 | PyErr_Format( |
| 236 | PyExc_SystemError, |
| 237 | "module %s has multiple create slots", |
| 238 | name); |
| 239 | goto error; |
| 240 | } |
| 241 | create = cur_slot->value; |
| 242 | } else if (cur_slot->slot < 0 || cur_slot->slot > _Py_mod_LAST_SLOT) { |
| 243 | PyErr_Format( |
| 244 | PyExc_SystemError, |
| 245 | "module %s uses unknown slot ID %i", |
| 246 | name, cur_slot->slot); |
| 247 | goto error; |
| 248 | } else { |
| 249 | has_execution_slots = 1; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | if (create) { |
| 254 | m = create(spec, def); |
| 255 | if (m == NULL) { |
| 256 | if (!PyErr_Occurred()) { |
| 257 | PyErr_Format( |
| 258 | PyExc_SystemError, |
| 259 | "creation of module %s failed without setting an exception", |
| 260 | name); |
| 261 | } |
| 262 | goto error; |
| 263 | } else { |
| 264 | if (PyErr_Occurred()) { |
| 265 | PyErr_Format(PyExc_SystemError, |
| 266 | "creation of module %s raised unreported exception", |
| 267 | name); |
| 268 | goto error; |
| 269 | } |
| 270 | } |
| 271 | } else { |
| 272 | m = PyModule_New(name); |
| 273 | if (m == NULL) { |
| 274 | goto error; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | if (PyModule_Check(m)) { |
| 279 | ((PyModuleObject*)m)->md_state = NULL; |
| 280 | ((PyModuleObject*)m)->md_def = def; |
| 281 | } else { |
| 282 | if (def->m_size > 0 || def->m_traverse || def->m_clear || def->m_free) { |
| 283 | PyErr_Format( |
| 284 | PyExc_SystemError, |
| 285 | "module %s is not a module object, but requests module state", |
| 286 | name); |
| 287 | goto error; |
| 288 | } |
| 289 | if (has_execution_slots) { |
| 290 | PyErr_Format( |
| 291 | PyExc_SystemError, |
| 292 | "module %s specifies execution slots, but did not create " |
| 293 | "a ModuleType instance", |
| 294 | name); |
| 295 | goto error; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | if (def->m_methods != NULL) { |
| 300 | ret = PyModule_AddFunctions(m, def->m_methods); |
| 301 | if (ret != 0) { |
| 302 | goto error; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | if (def->m_doc != NULL) { |
| 307 | ret = PyModule_SetDocString(m, def->m_doc); |
| 308 | if (ret != 0) { |
| 309 | goto error; |
| 310 | } |
| 311 | } |
| 312 | |
Nick Coghlan | a48db2b | 2015-05-24 01:03:46 +1000 | [diff] [blame] | 313 | Py_DECREF(nameobj); |
Nick Coghlan | d5cacbb | 2015-05-23 22:24:10 +1000 | [diff] [blame] | 314 | return m; |
| 315 | |
| 316 | error: |
| 317 | Py_DECREF(nameobj); |
| 318 | Py_XDECREF(m); |
| 319 | return NULL; |
| 320 | } |
| 321 | |
| 322 | int |
| 323 | PyModule_ExecDef(PyObject *module, PyModuleDef *def) |
| 324 | { |
| 325 | PyModuleDef_Slot *cur_slot; |
| 326 | const char *name; |
| 327 | int ret; |
| 328 | |
| 329 | name = PyModule_GetName(module); |
| 330 | if (name == NULL) { |
| 331 | return -1; |
| 332 | } |
| 333 | |
| 334 | if (PyModule_Check(module) && def->m_size >= 0) { |
| 335 | PyModuleObject *md = (PyModuleObject*)module; |
| 336 | if (md->md_state == NULL) { |
| 337 | /* Always set a state pointer; this serves as a marker to skip |
| 338 | * multiple initialization (importlib.reload() is no-op) */ |
| 339 | md->md_state = PyMem_MALLOC(def->m_size); |
| 340 | if (!md->md_state) { |
| 341 | PyErr_NoMemory(); |
| 342 | return -1; |
| 343 | } |
| 344 | memset(md->md_state, 0, def->m_size); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | if (def->m_slots == NULL) { |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) { |
| 353 | switch (cur_slot->slot) { |
| 354 | case Py_mod_create: |
| 355 | /* handled in PyModule_CreateFromSlots */ |
| 356 | break; |
| 357 | case Py_mod_exec: |
| 358 | ret = ((int (*)(PyObject *))cur_slot->value)(module); |
| 359 | if (ret != 0) { |
| 360 | if (!PyErr_Occurred()) { |
| 361 | PyErr_Format( |
| 362 | PyExc_SystemError, |
| 363 | "execution of module %s failed without setting an exception", |
| 364 | name); |
| 365 | } |
| 366 | return -1; |
| 367 | } |
| 368 | if (PyErr_Occurred()) { |
| 369 | PyErr_Format( |
| 370 | PyExc_SystemError, |
| 371 | "execution of module %s raised unreported exception", |
| 372 | name); |
| 373 | return -1; |
| 374 | } |
| 375 | break; |
| 376 | default: |
| 377 | PyErr_Format( |
| 378 | PyExc_SystemError, |
| 379 | "module %s initialized with unknown slot %i", |
| 380 | name, cur_slot->slot); |
| 381 | return -1; |
| 382 | } |
| 383 | } |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | int |
| 388 | PyModule_AddFunctions(PyObject *m, PyMethodDef *functions) |
| 389 | { |
| 390 | PyObject *name, *func; |
| 391 | PyMethodDef *fdef; |
| 392 | |
| 393 | name = PyModule_GetNameObject(m); |
| 394 | if (name == NULL) { |
| 395 | return -1; |
| 396 | } |
| 397 | |
| 398 | for (fdef = functions; fdef->ml_name != NULL; fdef++) { |
| 399 | if ((fdef->ml_flags & METH_CLASS) || |
| 400 | (fdef->ml_flags & METH_STATIC)) { |
| 401 | PyErr_SetString(PyExc_ValueError, |
| 402 | "module functions cannot set" |
| 403 | " METH_CLASS or METH_STATIC"); |
| 404 | Py_DECREF(name); |
| 405 | return -1; |
| 406 | } |
| 407 | func = PyCFunction_NewEx(fdef, (PyObject*)m, name); |
| 408 | if (func == NULL) { |
| 409 | Py_DECREF(name); |
| 410 | return -1; |
| 411 | } |
| 412 | if (PyObject_SetAttrString(m, fdef->ml_name, func) != 0) { |
| 413 | Py_DECREF(func); |
| 414 | Py_DECREF(name); |
| 415 | return -1; |
| 416 | } |
| 417 | Py_DECREF(func); |
| 418 | } |
| 419 | Py_DECREF(name); |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | int |
| 424 | PyModule_SetDocString(PyObject *m, const char *doc) |
| 425 | { |
| 426 | PyObject *v; |
| 427 | _Py_IDENTIFIER(__doc__); |
| 428 | |
| 429 | v = PyUnicode_FromString(doc); |
| 430 | if (v == NULL || _PyObject_SetAttrId(m, &PyId___doc__, v) != 0) { |
| 431 | Py_XDECREF(v); |
| 432 | return -1; |
| 433 | } |
| 434 | Py_DECREF(v); |
| 435 | return 0; |
| 436 | } |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 437 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 438 | PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 439 | PyModule_GetDict(PyObject *m) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 440 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 441 | PyObject *d; |
| 442 | if (!PyModule_Check(m)) { |
| 443 | PyErr_BadInternalCall(); |
| 444 | return NULL; |
| 445 | } |
| 446 | d = ((PyModuleObject *)m) -> md_dict; |
| 447 | if (d == NULL) |
| 448 | ((PyModuleObject *)m) -> md_dict = d = PyDict_New(); |
| 449 | return d; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Victor Stinner | bd47511 | 2011-02-23 00:21:43 +0000 | [diff] [blame] | 452 | PyObject* |
| 453 | PyModule_GetNameObject(PyObject *m) |
Guido van Rossum | 0558a20 | 1990-10-26 15:00:11 +0000 | [diff] [blame] | 454 | { |
Benjamin Peterson | 027ce16 | 2014-04-24 19:39:18 -0400 | [diff] [blame] | 455 | _Py_IDENTIFIER(__name__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 456 | PyObject *d; |
Victor Stinner | bd47511 | 2011-02-23 00:21:43 +0000 | [diff] [blame] | 457 | PyObject *name; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 458 | if (!PyModule_Check(m)) { |
| 459 | PyErr_BadArgument(); |
| 460 | return NULL; |
| 461 | } |
| 462 | d = ((PyModuleObject *)m)->md_dict; |
| 463 | if (d == NULL || |
Benjamin Peterson | 027ce16 | 2014-04-24 19:39:18 -0400 | [diff] [blame] | 464 | (name = _PyDict_GetItemId(d, &PyId___name__)) == NULL || |
Victor Stinner | bd47511 | 2011-02-23 00:21:43 +0000 | [diff] [blame] | 465 | !PyUnicode_Check(name)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 466 | { |
| 467 | PyErr_SetString(PyExc_SystemError, "nameless module"); |
| 468 | return NULL; |
| 469 | } |
Victor Stinner | bd47511 | 2011-02-23 00:21:43 +0000 | [diff] [blame] | 470 | Py_INCREF(name); |
| 471 | return name; |
| 472 | } |
| 473 | |
| 474 | const char * |
| 475 | PyModule_GetName(PyObject *m) |
| 476 | { |
| 477 | PyObject *name = PyModule_GetNameObject(m); |
| 478 | if (name == NULL) |
| 479 | return NULL; |
| 480 | Py_DECREF(name); /* module dict has still a reference */ |
| 481 | return _PyUnicode_AsString(name); |
Guido van Rossum | 0558a20 | 1990-10-26 15:00:11 +0000 | [diff] [blame] | 482 | } |
| 483 | |
Victor Stinner | 6c00c14 | 2010-08-17 23:37:11 +0000 | [diff] [blame] | 484 | PyObject* |
| 485 | PyModule_GetFilenameObject(PyObject *m) |
Guido van Rossum | 98cc19f | 1999-02-15 14:47:16 +0000 | [diff] [blame] | 486 | { |
Benjamin Peterson | 027ce16 | 2014-04-24 19:39:18 -0400 | [diff] [blame] | 487 | _Py_IDENTIFIER(__file__); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 488 | PyObject *d; |
| 489 | PyObject *fileobj; |
| 490 | if (!PyModule_Check(m)) { |
| 491 | PyErr_BadArgument(); |
| 492 | return NULL; |
| 493 | } |
| 494 | d = ((PyModuleObject *)m)->md_dict; |
| 495 | if (d == NULL || |
Benjamin Peterson | 027ce16 | 2014-04-24 19:39:18 -0400 | [diff] [blame] | 496 | (fileobj = _PyDict_GetItemId(d, &PyId___file__)) == NULL || |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 497 | !PyUnicode_Check(fileobj)) |
| 498 | { |
| 499 | PyErr_SetString(PyExc_SystemError, "module filename missing"); |
| 500 | return NULL; |
| 501 | } |
Victor Stinner | 6c00c14 | 2010-08-17 23:37:11 +0000 | [diff] [blame] | 502 | Py_INCREF(fileobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 503 | return fileobj; |
Victor Stinner | 8124feb | 2010-05-07 00:50:12 +0000 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | const char * |
| 507 | PyModule_GetFilename(PyObject *m) |
| 508 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 509 | PyObject *fileobj; |
Victor Stinner | 6c00c14 | 2010-08-17 23:37:11 +0000 | [diff] [blame] | 510 | char *utf8; |
| 511 | fileobj = PyModule_GetFilenameObject(m); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 512 | if (fileobj == NULL) |
| 513 | return NULL; |
Victor Stinner | 6c00c14 | 2010-08-17 23:37:11 +0000 | [diff] [blame] | 514 | utf8 = _PyUnicode_AsString(fileobj); |
Victor Stinner | bd47511 | 2011-02-23 00:21:43 +0000 | [diff] [blame] | 515 | Py_DECREF(fileobj); /* module dict has still a reference */ |
Victor Stinner | 6c00c14 | 2010-08-17 23:37:11 +0000 | [diff] [blame] | 516 | return utf8; |
Guido van Rossum | 98cc19f | 1999-02-15 14:47:16 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 519 | PyModuleDef* |
| 520 | PyModule_GetDef(PyObject* m) |
| 521 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 522 | if (!PyModule_Check(m)) { |
| 523 | PyErr_BadArgument(); |
| 524 | return NULL; |
| 525 | } |
| 526 | return ((PyModuleObject *)m)->md_def; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | void* |
| 530 | PyModule_GetState(PyObject* m) |
| 531 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 532 | if (!PyModule_Check(m)) { |
| 533 | PyErr_BadArgument(); |
| 534 | return NULL; |
| 535 | } |
| 536 | return ((PyModuleObject *)m)->md_state; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Guido van Rossum | f1dc061 | 1998-02-19 20:51:52 +0000 | [diff] [blame] | 539 | void |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 540 | _PyModule_Clear(PyObject *m) |
Guido van Rossum | f1dc061 | 1998-02-19 20:51:52 +0000 | [diff] [blame] | 541 | { |
Serhiy Storchaka | 87a5c51 | 2014-02-10 18:21:34 +0200 | [diff] [blame] | 542 | PyObject *d = ((PyModuleObject *)m)->md_dict; |
| 543 | if (d != NULL) |
| 544 | _PyModule_ClearDict(d); |
| 545 | } |
| 546 | |
| 547 | void |
| 548 | _PyModule_ClearDict(PyObject *d) |
| 549 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 550 | /* To make the execution order of destructors for global |
| 551 | objects a bit more predictable, we first zap all objects |
| 552 | whose name starts with a single underscore, before we clear |
| 553 | the entire dictionary. We zap them by replacing them with |
| 554 | None, rather than deleting them from the dictionary, to |
| 555 | avoid rehashing the dictionary (to some extent). */ |
Guido van Rossum | f1dc061 | 1998-02-19 20:51:52 +0000 | [diff] [blame] | 556 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 557 | Py_ssize_t pos; |
| 558 | PyObject *key, *value; |
Guido van Rossum | f1dc061 | 1998-02-19 20:51:52 +0000 | [diff] [blame] | 559 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 560 | /* First, clear only names starting with a single underscore */ |
| 561 | pos = 0; |
| 562 | while (PyDict_Next(d, &pos, &key, &value)) { |
| 563 | if (value != Py_None && PyUnicode_Check(key)) { |
Brett Cannon | 62228db | 2012-04-29 14:38:11 -0400 | [diff] [blame] | 564 | if (PyUnicode_READ_CHAR(key, 0) == '_' && |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 565 | PyUnicode_READ_CHAR(key, 1) != '_') { |
Victor Stinner | f3f22a2 | 2010-05-19 00:03:09 +0000 | [diff] [blame] | 566 | if (Py_VerboseFlag > 1) { |
| 567 | const char *s = _PyUnicode_AsString(key); |
| 568 | if (s != NULL) |
| 569 | PySys_WriteStderr("# clear[1] %s\n", s); |
| 570 | else |
| 571 | PyErr_Clear(); |
| 572 | } |
Serhiy Storchaka | 1f9d11b | 2014-02-12 09:55:01 +0200 | [diff] [blame] | 573 | if (PyDict_SetItem(d, key, Py_None) != 0) |
| 574 | PyErr_Clear(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 575 | } |
| 576 | } |
| 577 | } |
Guido van Rossum | f1dc061 | 1998-02-19 20:51:52 +0000 | [diff] [blame] | 578 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 579 | /* Next, clear all names except for __builtins__ */ |
| 580 | pos = 0; |
| 581 | while (PyDict_Next(d, &pos, &key, &value)) { |
| 582 | if (value != Py_None && PyUnicode_Check(key)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 583 | if (PyUnicode_READ_CHAR(key, 0) != '_' || |
| 584 | PyUnicode_CompareWithASCIIString(key, "__builtins__") != 0) |
Victor Stinner | f3f22a2 | 2010-05-19 00:03:09 +0000 | [diff] [blame] | 585 | { |
| 586 | if (Py_VerboseFlag > 1) { |
| 587 | const char *s = _PyUnicode_AsString(key); |
| 588 | if (s != NULL) |
| 589 | PySys_WriteStderr("# clear[2] %s\n", s); |
| 590 | else |
| 591 | PyErr_Clear(); |
| 592 | } |
Serhiy Storchaka | 1f9d11b | 2014-02-12 09:55:01 +0200 | [diff] [blame] | 593 | if (PyDict_SetItem(d, key, Py_None) != 0) |
| 594 | PyErr_Clear(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | } |
Guido van Rossum | f1dc061 | 1998-02-19 20:51:52 +0000 | [diff] [blame] | 598 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 599 | /* Note: we leave __builtins__ in place, so that destructors |
| 600 | of non-global objects defined in this module can still use |
| 601 | builtins, in particularly 'None'. */ |
Guido van Rossum | f1dc061 | 1998-02-19 20:51:52 +0000 | [diff] [blame] | 602 | |
| 603 | } |
| 604 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 605 | /* Methods */ |
| 606 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 607 | static int |
Guido van Rossum | c3a787e | 2002-06-04 05:52:47 +0000 | [diff] [blame] | 608 | module_init(PyModuleObject *m, PyObject *args, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 609 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 610 | static char *kwlist[] = {"name", "doc", NULL}; |
| 611 | PyObject *dict, *name = Py_None, *doc = Py_None; |
| 612 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "U|O:module.__init__", |
| 613 | kwlist, &name, &doc)) |
| 614 | return -1; |
| 615 | dict = m->md_dict; |
| 616 | if (dict == NULL) { |
| 617 | dict = PyDict_New(); |
| 618 | if (dict == NULL) |
| 619 | return -1; |
| 620 | m->md_dict = dict; |
| 621 | } |
Antoine Pitrou | dcedaf6 | 2013-07-31 23:14:08 +0200 | [diff] [blame] | 622 | if (module_init_dict(m, dict, name, doc) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 623 | return -1; |
| 624 | return 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 627 | static void |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 628 | module_dealloc(PyModuleObject *m) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 629 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 630 | PyObject_GC_UnTrack(m); |
Antoine Pitrou | dcedaf6 | 2013-07-31 23:14:08 +0200 | [diff] [blame] | 631 | if (Py_VerboseFlag && m->md_name) { |
| 632 | PySys_FormatStderr("# destroy %S\n", m->md_name); |
| 633 | } |
| 634 | if (m->md_weaklist != NULL) |
| 635 | PyObject_ClearWeakRefs((PyObject *) m); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 636 | if (m->md_def && m->md_def->m_free) |
| 637 | m->md_def->m_free(m); |
Antoine Pitrou | dcedaf6 | 2013-07-31 23:14:08 +0200 | [diff] [blame] | 638 | Py_XDECREF(m->md_dict); |
| 639 | Py_XDECREF(m->md_name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 640 | if (m->md_state != NULL) |
| 641 | PyMem_FREE(m->md_state); |
| 642 | Py_TYPE(m)->tp_free((PyObject *)m); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 645 | static PyObject * |
Fred Drake | ee238b9 | 2000-07-09 06:03:25 +0000 | [diff] [blame] | 646 | module_repr(PyModuleObject *m) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 647 | { |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 648 | PyThreadState *tstate = PyThreadState_GET(); |
| 649 | PyInterpreterState *interp = tstate->interp; |
Barry Warsaw | 2907fe6 | 2001-08-16 20:39:24 +0000 | [diff] [blame] | 650 | |
Eric Snow | b523f84 | 2013-11-22 09:05:39 -0700 | [diff] [blame] | 651 | return PyObject_CallMethod(interp->importlib, "_module_repr", "O", m); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 652 | } |
| 653 | |
Ethan Furman | 7b9ff0e | 2014-04-24 14:47:47 -0700 | [diff] [blame] | 654 | static PyObject* |
Benjamin Peterson | 1184e26 | 2014-04-24 19:29:23 -0400 | [diff] [blame] | 655 | module_getattro(PyModuleObject *m, PyObject *name) |
Ethan Furman | 7b9ff0e | 2014-04-24 14:47:47 -0700 | [diff] [blame] | 656 | { |
Ethan Furman | 7b9ff0e | 2014-04-24 14:47:47 -0700 | [diff] [blame] | 657 | PyObject *attr, *mod_name; |
Benjamin Peterson | 1184e26 | 2014-04-24 19:29:23 -0400 | [diff] [blame] | 658 | attr = PyObject_GenericGetAttr((PyObject *)m, name); |
| 659 | if (attr || !PyErr_ExceptionMatches(PyExc_AttributeError)) |
Ethan Furman | 7b9ff0e | 2014-04-24 14:47:47 -0700 | [diff] [blame] | 660 | return attr; |
| 661 | PyErr_Clear(); |
Benjamin Peterson | 1184e26 | 2014-04-24 19:29:23 -0400 | [diff] [blame] | 662 | if (m->md_dict) { |
Benjamin Peterson | 027ce16 | 2014-04-24 19:39:18 -0400 | [diff] [blame] | 663 | _Py_IDENTIFIER(__name__); |
| 664 | mod_name = _PyDict_GetItemId(m->md_dict, &PyId___name__); |
Benjamin Peterson | 1184e26 | 2014-04-24 19:29:23 -0400 | [diff] [blame] | 665 | if (mod_name) { |
Ethan Furman | 7b9ff0e | 2014-04-24 14:47:47 -0700 | [diff] [blame] | 666 | PyErr_Format(PyExc_AttributeError, |
| 667 | "module '%U' has no attribute '%U'", mod_name, name); |
| 668 | return NULL; |
| 669 | } |
Benjamin Peterson | 1184e26 | 2014-04-24 19:29:23 -0400 | [diff] [blame] | 670 | else if (PyErr_Occurred()) { |
Ethan Furman | 7b9ff0e | 2014-04-24 14:47:47 -0700 | [diff] [blame] | 671 | PyErr_Clear(); |
Benjamin Peterson | 1184e26 | 2014-04-24 19:29:23 -0400 | [diff] [blame] | 672 | } |
Ethan Furman | 7b9ff0e | 2014-04-24 14:47:47 -0700 | [diff] [blame] | 673 | } |
| 674 | PyErr_Format(PyExc_AttributeError, |
| 675 | "module has no attribute '%U'", name); |
| 676 | return NULL; |
| 677 | } |
| 678 | |
Neil Schemenauer | 10e31cf | 2001-01-02 15:58:27 +0000 | [diff] [blame] | 679 | static int |
| 680 | module_traverse(PyModuleObject *m, visitproc visit, void *arg) |
| 681 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 682 | if (m->md_def && m->md_def->m_traverse) { |
| 683 | int res = m->md_def->m_traverse((PyObject*)m, visit, arg); |
| 684 | if (res) |
| 685 | return res; |
| 686 | } |
| 687 | Py_VISIT(m->md_dict); |
| 688 | return 0; |
Neil Schemenauer | 10e31cf | 2001-01-02 15:58:27 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 691 | static int |
| 692 | module_clear(PyModuleObject *m) |
| 693 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 694 | if (m->md_def && m->md_def->m_clear) { |
| 695 | int res = m->md_def->m_clear((PyObject*)m); |
| 696 | if (res) |
| 697 | return res; |
| 698 | } |
| 699 | Py_CLEAR(m->md_dict); |
| 700 | return 0; |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 701 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 702 | |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 703 | static PyObject * |
| 704 | module_dir(PyObject *self, PyObject *args) |
| 705 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 706 | _Py_IDENTIFIER(__dict__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 707 | PyObject *result = NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 708 | PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__); |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 709 | |
| 710 | if (dict != NULL) { |
| 711 | if (PyDict_Check(dict)) |
| 712 | result = PyDict_Keys(dict); |
| 713 | else { |
| 714 | const char *name = PyModule_GetName(self); |
| 715 | if (name) |
| 716 | PyErr_Format(PyExc_TypeError, |
| 717 | "%.200s.__dict__ is not a dictionary", |
| 718 | name); |
| 719 | } |
| 720 | } |
| 721 | |
| 722 | Py_XDECREF(dict); |
| 723 | return result; |
| 724 | } |
| 725 | |
| 726 | static PyMethodDef module_methods[] = { |
| 727 | {"__dir__", module_dir, METH_NOARGS, |
Benjamin Peterson | c728412 | 2011-05-24 12:46:15 -0500 | [diff] [blame] | 728 | PyDoc_STR("__dir__() -> list\nspecialized dir() implementation")}, |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 729 | {0} |
| 730 | }; |
| 731 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 732 | PyDoc_STRVAR(module_doc, |
Guido van Rossum | ed3e09f | 2002-06-04 06:02:35 +0000 | [diff] [blame] | 733 | "module(name[, doc])\n\ |
| 734 | \n\ |
| 735 | Create a module object.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 736 | 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] | 737 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 738 | PyTypeObject PyModule_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 739 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 740 | "module", /* tp_name */ |
| 741 | sizeof(PyModuleObject), /* tp_size */ |
| 742 | 0, /* tp_itemsize */ |
| 743 | (destructor)module_dealloc, /* tp_dealloc */ |
| 744 | 0, /* tp_print */ |
| 745 | 0, /* tp_getattr */ |
| 746 | 0, /* tp_setattr */ |
| 747 | 0, /* tp_reserved */ |
| 748 | (reprfunc)module_repr, /* tp_repr */ |
| 749 | 0, /* tp_as_number */ |
| 750 | 0, /* tp_as_sequence */ |
| 751 | 0, /* tp_as_mapping */ |
| 752 | 0, /* tp_hash */ |
| 753 | 0, /* tp_call */ |
| 754 | 0, /* tp_str */ |
Benjamin Peterson | 1184e26 | 2014-04-24 19:29:23 -0400 | [diff] [blame] | 755 | (getattrofunc)module_getattro, /* tp_getattro */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 756 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 757 | 0, /* tp_as_buffer */ |
| 758 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 759 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 760 | module_doc, /* tp_doc */ |
| 761 | (traverseproc)module_traverse, /* tp_traverse */ |
| 762 | (inquiry)module_clear, /* tp_clear */ |
| 763 | 0, /* tp_richcompare */ |
Antoine Pitrou | dcedaf6 | 2013-07-31 23:14:08 +0200 | [diff] [blame] | 764 | offsetof(PyModuleObject, md_weaklist), /* tp_weaklistoffset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 765 | 0, /* tp_iter */ |
| 766 | 0, /* tp_iternext */ |
Benjamin Peterson | 82b00c1 | 2011-05-24 11:09:06 -0500 | [diff] [blame] | 767 | module_methods, /* tp_methods */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 768 | module_members, /* tp_members */ |
| 769 | 0, /* tp_getset */ |
| 770 | 0, /* tp_base */ |
| 771 | 0, /* tp_dict */ |
| 772 | 0, /* tp_descr_get */ |
| 773 | 0, /* tp_descr_set */ |
| 774 | offsetof(PyModuleObject, md_dict), /* tp_dictoffset */ |
| 775 | (initproc)module_init, /* tp_init */ |
| 776 | PyType_GenericAlloc, /* tp_alloc */ |
| 777 | PyType_GenericNew, /* tp_new */ |
| 778 | PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 779 | }; |