blob: cdb365d29a914db2e32787be24ff43bc72c02371 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Module object implementation */
3
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Victor Stinner4a21e572020-04-15 02:35:41 +02005#include "pycore_interp.h" // PyInterpreterState.importlib
6#include "pycore_pystate.h" // _PyInterpreterState_GET()
Victor Stinnercdad2722021-04-22 00:52:52 +02007#include "pycore_moduleobject.h" // _PyModule_GetDef()
Victor Stinner4a21e572020-04-15 02:35:41 +02008#include "structmember.h" // PyMemberDef
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009
Martin v. Löwis1a214512008-06-11 05:26:20 +000010static Py_ssize_t max_module_number;
11
Hai Shi46874c22020-01-30 17:20:25 -060012_Py_IDENTIFIER(__doc__);
13_Py_IDENTIFIER(__name__);
14_Py_IDENTIFIER(__spec__);
larryhastings2f2b6982021-04-29 20:09:08 -070015_Py_IDENTIFIER(__dict__);
16_Py_IDENTIFIER(__dir__);
17_Py_IDENTIFIER(__annotations__);
Hai Shi46874c22020-01-30 17:20:25 -060018
Neil Schemenauerf23473f2001-10-21 22:28:58 +000019static PyMemberDef module_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000020 {"__dict__", T_OBJECT, offsetof(PyModuleObject, md_dict), READONLY},
21 {0}
Tim Peters6d6c1a32001-08-02 04:15:00 +000022};
23
Marcel Plchc2b0b122018-03-17 06:41:20 +010024
Nick Coghland5cacbb2015-05-23 22:24:10 +100025PyTypeObject PyModuleDef_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000026 PyVarObject_HEAD_INIT(&PyType_Type, 0)
27 "moduledef", /* tp_name */
Peter Eisentraut0e0bc4e2018-09-10 18:46:08 +020028 sizeof(struct PyModuleDef), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029 0, /* tp_itemsize */
Martin v. Löwis1a214512008-06-11 05:26:20 +000030};
31
32
Victor Stinner250035d2021-01-18 20:47:13 +010033int
34_PyModule_IsExtension(PyObject *obj)
35{
36 if (!PyModule_Check(obj)) {
37 return 0;
38 }
39 PyModuleObject *module = (PyModuleObject*)obj;
40
41 struct PyModuleDef *def = module->md_def;
42 return (def != NULL && def->m_methods != NULL);
43}
44
45
Nick Coghland5cacbb2015-05-23 22:24:10 +100046PyObject*
47PyModuleDef_Init(struct PyModuleDef* def)
48{
49 if (PyType_Ready(&PyModuleDef_Type) < 0)
50 return NULL;
51 if (def->m_base.m_index == 0) {
52 max_module_number++;
Victor Stinnerc86a1122020-02-07 01:24:29 +010053 Py_SET_REFCNT(def, 1);
Victor Stinnerd2ec81a2020-02-07 09:17:07 +010054 Py_SET_TYPE(def, &PyModuleDef_Type);
Nick Coghland5cacbb2015-05-23 22:24:10 +100055 def->m_base.m_index = max_module_number;
56 }
57 return (PyObject*)def;
58}
59
Brett Cannon4c14b5d2013-05-04 13:56:58 -040060static int
Antoine Pitroudcedaf62013-07-31 23:14:08 +020061module_init_dict(PyModuleObject *mod, PyObject *md_dict,
62 PyObject *name, PyObject *doc)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040063{
Benjamin Peterson027ce162014-04-24 19:39:18 -040064 _Py_IDENTIFIER(__package__);
65 _Py_IDENTIFIER(__loader__);
Serhiy Storchaka009b8112015-03-18 21:53:15 +020066
Brett Cannon4c14b5d2013-05-04 13:56:58 -040067 if (md_dict == NULL)
68 return -1;
69 if (doc == NULL)
70 doc = Py_None;
71
Benjamin Peterson027ce162014-04-24 19:39:18 -040072 if (_PyDict_SetItemId(md_dict, &PyId___name__, name) != 0)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040073 return -1;
Benjamin Peterson027ce162014-04-24 19:39:18 -040074 if (_PyDict_SetItemId(md_dict, &PyId___doc__, doc) != 0)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040075 return -1;
Benjamin Peterson027ce162014-04-24 19:39:18 -040076 if (_PyDict_SetItemId(md_dict, &PyId___package__, Py_None) != 0)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040077 return -1;
Benjamin Peterson027ce162014-04-24 19:39:18 -040078 if (_PyDict_SetItemId(md_dict, &PyId___loader__, Py_None) != 0)
Brett Cannon4c14b5d2013-05-04 13:56:58 -040079 return -1;
Benjamin Peterson027ce162014-04-24 19:39:18 -040080 if (_PyDict_SetItemId(md_dict, &PyId___spec__, Py_None) != 0)
Eric Snowb523f842013-11-22 09:05:39 -070081 return -1;
Antoine Pitroudcedaf62013-07-31 23:14:08 +020082 if (PyUnicode_CheckExact(name)) {
83 Py_INCREF(name);
Serhiy Storchaka48842712016-04-06 09:45:48 +030084 Py_XSETREF(mod->md_name, name);
Antoine Pitroudcedaf62013-07-31 23:14:08 +020085 }
Brett Cannon4c14b5d2013-05-04 13:56:58 -040086
87 return 0;
88}
89
90
Guido van Rossumc0b618a1997-05-02 03:12:38 +000091PyObject *
Victor Stinner0639b562011-03-04 12:57:07 +000092PyModule_NewObject(PyObject *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000093{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 PyModuleObject *m;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 m = PyObject_GC_New(PyModuleObject, &PyModule_Type);
96 if (m == NULL)
97 return NULL;
98 m->md_def = NULL;
99 m->md_state = NULL;
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200100 m->md_weaklist = NULL;
101 m->md_name = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 m->md_dict = PyDict_New();
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200103 if (module_init_dict(m, m->md_dict, name, NULL) != 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 PyObject_GC_Track(m);
106 return (PyObject *)m;
Guido van Rossumc45611d1993-11-17 22:58:56 +0000107
108 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 Py_DECREF(m);
110 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000111}
112
Martin v. Löwis1a214512008-06-11 05:26:20 +0000113PyObject *
Victor Stinner0639b562011-03-04 12:57:07 +0000114PyModule_New(const char *name)
115{
116 PyObject *nameobj, *module;
117 nameobj = PyUnicode_FromString(name);
118 if (nameobj == NULL)
119 return NULL;
120 module = PyModule_NewObject(nameobj);
121 Py_DECREF(nameobj);
122 return module;
123}
124
Nick Coghland5cacbb2015-05-23 22:24:10 +1000125/* Check API/ABI version
126 * Issues a warning on mismatch, which is usually not fatal.
127 * Returns 0 if an exception is raised.
128 */
129static int
130check_api_version(const char *name, int module_api_version)
Martin v. Löwis1a214512008-06-11 05:26:20 +0000131{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000132 if (module_api_version != PYTHON_API_VERSION && module_api_version != PYTHON_ABI_VERSION) {
Victor Stinner4a2b7a12010-08-13 14:03:48 +0000133 int err;
134 err = PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
135 "Python C API version mismatch for module %.100s: "
136 "This Python has API version %d, module %.100s has version %d.",
137 name,
138 PYTHON_API_VERSION, name, module_api_version);
139 if (err)
Nick Coghland5cacbb2015-05-23 22:24:10 +1000140 return 0;
141 }
142 return 1;
143}
144
Nick Coghlan8682f572016-08-21 17:41:56 +1000145static int
146_add_methods_to_object(PyObject *module, PyObject *name, PyMethodDef *functions)
147{
148 PyObject *func;
149 PyMethodDef *fdef;
150
151 for (fdef = functions; fdef->ml_name != NULL; fdef++) {
152 if ((fdef->ml_flags & METH_CLASS) ||
153 (fdef->ml_flags & METH_STATIC)) {
154 PyErr_SetString(PyExc_ValueError,
155 "module functions cannot set"
156 " METH_CLASS or METH_STATIC");
157 return -1;
158 }
159 func = PyCFunction_NewEx(fdef, (PyObject*)module, name);
160 if (func == NULL) {
161 return -1;
162 }
163 if (PyObject_SetAttrString(module, fdef->ml_name, func) != 0) {
164 Py_DECREF(func);
165 return -1;
166 }
167 Py_DECREF(func);
168 }
169
170 return 0;
171}
172
Nick Coghland5cacbb2015-05-23 22:24:10 +1000173PyObject *
174PyModule_Create2(struct PyModuleDef* module, int module_api_version)
175{
Victor Stinner81a7be32020-04-14 15:14:01 +0200176 if (!_PyImport_IsInitialized(_PyInterpreterState_GET())) {
Victor Stinnera94c6b62020-01-27 22:37:05 +0100177 PyErr_SetString(PyExc_SystemError,
178 "Python import machinery not initialized");
179 return NULL;
180 }
Eric Snowd393c1b2017-09-14 12:18:12 -0600181 return _PyModule_CreateInitialized(module, module_api_version);
182}
183
184PyObject *
185_PyModule_CreateInitialized(struct PyModuleDef* module, int module_api_version)
186{
Nick Coghland5cacbb2015-05-23 22:24:10 +1000187 const char* name;
188 PyModuleObject *m;
Eric Snowd393c1b2017-09-14 12:18:12 -0600189
Nick Coghland5cacbb2015-05-23 22:24:10 +1000190 if (!PyModuleDef_Init(module))
191 return NULL;
192 name = module->m_name;
193 if (!check_api_version(name, module_api_version)) {
194 return NULL;
195 }
196 if (module->m_slots) {
197 PyErr_Format(
198 PyExc_SystemError,
199 "module %s: PyModule_Create is incompatible with m_slots", name);
200 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 }
202 /* Make sure name is fully qualified.
Martin v. Löwis1a214512008-06-11 05:26:20 +0000203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 This is a bit of a hack: when the shared library is loaded,
205 the module name is "package.module", but the module calls
206 PyModule_Create*() with just "module" for the name. The shared
207 library loader squirrels away the true name of the module in
208 _Py_PackageContext, and PyModule_Create*() will substitute this
209 (if the name actually matches).
210 */
211 if (_Py_PackageContext != NULL) {
Serhiy Storchakab57d9ea2016-11-21 10:25:54 +0200212 const char *p = strrchr(_Py_PackageContext, '.');
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 if (p != NULL && strcmp(module->m_name, p+1) == 0) {
214 name = _Py_PackageContext;
215 _Py_PackageContext = NULL;
216 }
217 }
218 if ((m = (PyModuleObject*)PyModule_New(name)) == NULL)
219 return NULL;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 if (module->m_size > 0) {
Victor Stinner00d7abd2020-12-01 09:56:42 +0100222 m->md_state = PyMem_Malloc(module->m_size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 if (!m->md_state) {
224 PyErr_NoMemory();
225 Py_DECREF(m);
226 return NULL;
227 }
228 memset(m->md_state, 0, module->m_size);
229 }
230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 if (module->m_methods != NULL) {
Nick Coghland5cacbb2015-05-23 22:24:10 +1000232 if (PyModule_AddFunctions((PyObject *) m, module->m_methods) != 0) {
Meador Inge29e49d62012-07-19 13:45:43 -0500233 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 return NULL;
Meador Inge29e49d62012-07-19 13:45:43 -0500235 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 }
237 if (module->m_doc != NULL) {
Nick Coghland5cacbb2015-05-23 22:24:10 +1000238 if (PyModule_SetDocString((PyObject *) m, module->m_doc) != 0) {
Meador Inge29e49d62012-07-19 13:45:43 -0500239 Py_DECREF(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 return NULL;
241 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 }
243 m->md_def = module;
244 return (PyObject*)m;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000245}
246
Nick Coghland5cacbb2015-05-23 22:24:10 +1000247PyObject *
248PyModule_FromDefAndSpec2(struct PyModuleDef* def, PyObject *spec, int module_api_version)
249{
250 PyModuleDef_Slot* cur_slot;
251 PyObject *(*create)(PyObject *, PyModuleDef*) = NULL;
252 PyObject *nameobj;
253 PyObject *m = NULL;
254 int has_execution_slots = 0;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200255 const char *name;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000256 int ret;
257
258 PyModuleDef_Init(def);
259
260 nameobj = PyObject_GetAttrString(spec, "name");
261 if (nameobj == NULL) {
262 return NULL;
263 }
264 name = PyUnicode_AsUTF8(nameobj);
265 if (name == NULL) {
266 goto error;
267 }
268
269 if (!check_api_version(name, module_api_version)) {
270 goto error;
271 }
272
273 if (def->m_size < 0) {
274 PyErr_Format(
275 PyExc_SystemError,
276 "module %s: m_size may not be negative for multi-phase initialization",
277 name);
278 goto error;
279 }
280
281 for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) {
282 if (cur_slot->slot == Py_mod_create) {
283 if (create) {
284 PyErr_Format(
285 PyExc_SystemError,
286 "module %s has multiple create slots",
287 name);
288 goto error;
289 }
290 create = cur_slot->value;
291 } else if (cur_slot->slot < 0 || cur_slot->slot > _Py_mod_LAST_SLOT) {
292 PyErr_Format(
293 PyExc_SystemError,
294 "module %s uses unknown slot ID %i",
295 name, cur_slot->slot);
296 goto error;
297 } else {
298 has_execution_slots = 1;
299 }
300 }
301
302 if (create) {
303 m = create(spec, def);
304 if (m == NULL) {
305 if (!PyErr_Occurred()) {
306 PyErr_Format(
307 PyExc_SystemError,
308 "creation of module %s failed without setting an exception",
309 name);
310 }
311 goto error;
312 } else {
313 if (PyErr_Occurred()) {
314 PyErr_Format(PyExc_SystemError,
315 "creation of module %s raised unreported exception",
316 name);
317 goto error;
318 }
319 }
320 } else {
Nick Coghlan8682f572016-08-21 17:41:56 +1000321 m = PyModule_NewObject(nameobj);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000322 if (m == NULL) {
323 goto error;
324 }
325 }
326
327 if (PyModule_Check(m)) {
328 ((PyModuleObject*)m)->md_state = NULL;
329 ((PyModuleObject*)m)->md_def = def;
330 } else {
331 if (def->m_size > 0 || def->m_traverse || def->m_clear || def->m_free) {
332 PyErr_Format(
333 PyExc_SystemError,
334 "module %s is not a module object, but requests module state",
335 name);
336 goto error;
337 }
338 if (has_execution_slots) {
339 PyErr_Format(
340 PyExc_SystemError,
341 "module %s specifies execution slots, but did not create "
342 "a ModuleType instance",
343 name);
344 goto error;
345 }
346 }
347
348 if (def->m_methods != NULL) {
Nick Coghlan8682f572016-08-21 17:41:56 +1000349 ret = _add_methods_to_object(m, nameobj, def->m_methods);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000350 if (ret != 0) {
351 goto error;
352 }
353 }
354
355 if (def->m_doc != NULL) {
356 ret = PyModule_SetDocString(m, def->m_doc);
357 if (ret != 0) {
358 goto error;
359 }
360 }
361
Nick Coghlana48db2b2015-05-24 01:03:46 +1000362 Py_DECREF(nameobj);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000363 return m;
364
365error:
366 Py_DECREF(nameobj);
367 Py_XDECREF(m);
368 return NULL;
369}
370
371int
372PyModule_ExecDef(PyObject *module, PyModuleDef *def)
373{
374 PyModuleDef_Slot *cur_slot;
375 const char *name;
376 int ret;
377
378 name = PyModule_GetName(module);
379 if (name == NULL) {
380 return -1;
381 }
382
Nick Coghlan8682f572016-08-21 17:41:56 +1000383 if (def->m_size >= 0) {
Nick Coghland5cacbb2015-05-23 22:24:10 +1000384 PyModuleObject *md = (PyModuleObject*)module;
385 if (md->md_state == NULL) {
386 /* Always set a state pointer; this serves as a marker to skip
387 * multiple initialization (importlib.reload() is no-op) */
Victor Stinner00d7abd2020-12-01 09:56:42 +0100388 md->md_state = PyMem_Malloc(def->m_size);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000389 if (!md->md_state) {
390 PyErr_NoMemory();
391 return -1;
392 }
393 memset(md->md_state, 0, def->m_size);
394 }
395 }
396
397 if (def->m_slots == NULL) {
398 return 0;
399 }
400
401 for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) {
402 switch (cur_slot->slot) {
403 case Py_mod_create:
Serhiy Storchaka333ad922016-09-26 23:14:44 +0300404 /* handled in PyModule_FromDefAndSpec2 */
Nick Coghland5cacbb2015-05-23 22:24:10 +1000405 break;
406 case Py_mod_exec:
407 ret = ((int (*)(PyObject *))cur_slot->value)(module);
408 if (ret != 0) {
409 if (!PyErr_Occurred()) {
410 PyErr_Format(
411 PyExc_SystemError,
412 "execution of module %s failed without setting an exception",
413 name);
414 }
415 return -1;
416 }
417 if (PyErr_Occurred()) {
418 PyErr_Format(
419 PyExc_SystemError,
420 "execution of module %s raised unreported exception",
421 name);
422 return -1;
423 }
424 break;
425 default:
426 PyErr_Format(
427 PyExc_SystemError,
428 "module %s initialized with unknown slot %i",
429 name, cur_slot->slot);
430 return -1;
431 }
432 }
433 return 0;
434}
435
436int
437PyModule_AddFunctions(PyObject *m, PyMethodDef *functions)
438{
Nick Coghlan8682f572016-08-21 17:41:56 +1000439 int res;
440 PyObject *name = PyModule_GetNameObject(m);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000441 if (name == NULL) {
442 return -1;
443 }
444
Nick Coghlan8682f572016-08-21 17:41:56 +1000445 res = _add_methods_to_object(m, name, functions);
Nick Coghland5cacbb2015-05-23 22:24:10 +1000446 Py_DECREF(name);
Nick Coghlan8682f572016-08-21 17:41:56 +1000447 return res;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000448}
449
450int
451PyModule_SetDocString(PyObject *m, const char *doc)
452{
453 PyObject *v;
Nick Coghland5cacbb2015-05-23 22:24:10 +1000454
455 v = PyUnicode_FromString(doc);
456 if (v == NULL || _PyObject_SetAttrId(m, &PyId___doc__, v) != 0) {
457 Py_XDECREF(v);
458 return -1;
459 }
460 Py_DECREF(v);
461 return 0;
462}
Martin v. Löwis1a214512008-06-11 05:26:20 +0000463
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000464PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000465PyModule_GetDict(PyObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000466{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 if (!PyModule_Check(m)) {
468 PyErr_BadInternalCall();
469 return NULL;
470 }
Victor Stinnercdad2722021-04-22 00:52:52 +0200471 return _PyModule_GetDict(m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000472}
473
Victor Stinnerbd475112011-02-23 00:21:43 +0000474PyObject*
475PyModule_GetNameObject(PyObject *m)
Guido van Rossum0558a201990-10-26 15:00:11 +0000476{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 PyObject *d;
Victor Stinnerbd475112011-02-23 00:21:43 +0000478 PyObject *name;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 if (!PyModule_Check(m)) {
480 PyErr_BadArgument();
481 return NULL;
482 }
483 d = ((PyModuleObject *)m)->md_dict;
484 if (d == NULL ||
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200485 (name = _PyDict_GetItemIdWithError(d, &PyId___name__)) == NULL ||
Victor Stinnerbd475112011-02-23 00:21:43 +0000486 !PyUnicode_Check(name))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 {
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200488 if (!PyErr_Occurred()) {
489 PyErr_SetString(PyExc_SystemError, "nameless module");
490 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 return NULL;
492 }
Victor Stinnerbd475112011-02-23 00:21:43 +0000493 Py_INCREF(name);
494 return name;
495}
496
497const char *
498PyModule_GetName(PyObject *m)
499{
500 PyObject *name = PyModule_GetNameObject(m);
501 if (name == NULL)
502 return NULL;
503 Py_DECREF(name); /* module dict has still a reference */
Serhiy Storchaka06515832016-11-20 09:13:07 +0200504 return PyUnicode_AsUTF8(name);
Guido van Rossum0558a201990-10-26 15:00:11 +0000505}
506
Victor Stinner6c00c142010-08-17 23:37:11 +0000507PyObject*
508PyModule_GetFilenameObject(PyObject *m)
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000509{
Benjamin Peterson027ce162014-04-24 19:39:18 -0400510 _Py_IDENTIFIER(__file__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 PyObject *d;
512 PyObject *fileobj;
513 if (!PyModule_Check(m)) {
514 PyErr_BadArgument();
515 return NULL;
516 }
517 d = ((PyModuleObject *)m)->md_dict;
518 if (d == NULL ||
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200519 (fileobj = _PyDict_GetItemIdWithError(d, &PyId___file__)) == NULL ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 !PyUnicode_Check(fileobj))
521 {
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200522 if (!PyErr_Occurred()) {
523 PyErr_SetString(PyExc_SystemError, "module filename missing");
524 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 return NULL;
526 }
Victor Stinner6c00c142010-08-17 23:37:11 +0000527 Py_INCREF(fileobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 return fileobj;
Victor Stinner8124feb2010-05-07 00:50:12 +0000529}
530
531const char *
532PyModule_GetFilename(PyObject *m)
533{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 PyObject *fileobj;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +0200535 const char *utf8;
Victor Stinner6c00c142010-08-17 23:37:11 +0000536 fileobj = PyModule_GetFilenameObject(m);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 if (fileobj == NULL)
538 return NULL;
Serhiy Storchaka06515832016-11-20 09:13:07 +0200539 utf8 = PyUnicode_AsUTF8(fileobj);
Victor Stinnerbd475112011-02-23 00:21:43 +0000540 Py_DECREF(fileobj); /* module dict has still a reference */
Victor Stinner6c00c142010-08-17 23:37:11 +0000541 return utf8;
Guido van Rossum98cc19f1999-02-15 14:47:16 +0000542}
543
Martin v. Löwis1a214512008-06-11 05:26:20 +0000544PyModuleDef*
545PyModule_GetDef(PyObject* m)
546{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 if (!PyModule_Check(m)) {
548 PyErr_BadArgument();
549 return NULL;
550 }
Victor Stinnercdad2722021-04-22 00:52:52 +0200551 return _PyModule_GetDef(m);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000552}
553
554void*
555PyModule_GetState(PyObject* m)
556{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 if (!PyModule_Check(m)) {
558 PyErr_BadArgument();
559 return NULL;
560 }
Victor Stinnercdad2722021-04-22 00:52:52 +0200561 return _PyModule_GetState(m);
Martin v. Löwis1a214512008-06-11 05:26:20 +0000562}
563
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000564void
Fred Drakeee238b92000-07-09 06:03:25 +0000565_PyModule_Clear(PyObject *m)
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000566{
Serhiy Storchaka87a5c512014-02-10 18:21:34 +0200567 PyObject *d = ((PyModuleObject *)m)->md_dict;
568 if (d != NULL)
569 _PyModule_ClearDict(d);
570}
571
572void
573_PyModule_ClearDict(PyObject *d)
574{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 /* To make the execution order of destructors for global
576 objects a bit more predictable, we first zap all objects
577 whose name starts with a single underscore, before we clear
578 the entire dictionary. We zap them by replacing them with
579 None, rather than deleting them from the dictionary, to
580 avoid rehashing the dictionary (to some extent). */
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 Py_ssize_t pos;
583 PyObject *key, *value;
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000584
Victor Stinnerda7933e2020-04-13 03:04:28 +0200585 int verbose = _Py_GetConfig()->verbose;
Victor Stinnerc96be812019-05-14 17:34:56 +0200586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 /* First, clear only names starting with a single underscore */
588 pos = 0;
589 while (PyDict_Next(d, &pos, &key, &value)) {
590 if (value != Py_None && PyUnicode_Check(key)) {
Brett Cannon62228db2012-04-29 14:38:11 -0400591 if (PyUnicode_READ_CHAR(key, 0) == '_' &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200592 PyUnicode_READ_CHAR(key, 1) != '_') {
Victor Stinnerc96be812019-05-14 17:34:56 +0200593 if (verbose > 1) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200594 const char *s = PyUnicode_AsUTF8(key);
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000595 if (s != NULL)
596 PySys_WriteStderr("# clear[1] %s\n", s);
597 else
598 PyErr_Clear();
599 }
Serhiy Storchakac1a68322018-04-29 22:16:30 +0300600 if (PyDict_SetItem(d, key, Py_None) != 0) {
601 PyErr_WriteUnraisable(NULL);
602 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 }
604 }
605 }
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 /* Next, clear all names except for __builtins__ */
608 pos = 0;
609 while (PyDict_Next(d, &pos, &key, &value)) {
610 if (value != Py_None && PyUnicode_Check(key)) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200611 if (PyUnicode_READ_CHAR(key, 0) != '_' ||
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +0200612 !_PyUnicode_EqualToASCIIString(key, "__builtins__"))
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000613 {
Victor Stinnerc96be812019-05-14 17:34:56 +0200614 if (verbose > 1) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200615 const char *s = PyUnicode_AsUTF8(key);
Victor Stinnerf3f22a22010-05-19 00:03:09 +0000616 if (s != NULL)
617 PySys_WriteStderr("# clear[2] %s\n", s);
618 else
619 PyErr_Clear();
620 }
Serhiy Storchakac1a68322018-04-29 22:16:30 +0300621 if (PyDict_SetItem(d, key, Py_None) != 0) {
622 PyErr_WriteUnraisable(NULL);
623 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 }
625 }
626 }
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 /* Note: we leave __builtins__ in place, so that destructors
629 of non-global objects defined in this module can still use
630 builtins, in particularly 'None'. */
Guido van Rossumf1dc0611998-02-19 20:51:52 +0000631
632}
633
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200634/*[clinic input]
635class module "PyModuleObject *" "&PyModule_Type"
636[clinic start generated code]*/
637/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3e35d4f708ecb6af]*/
638
639#include "clinic/moduleobject.c.h"
640
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000641/* Methods */
642
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200643/*[clinic input]
644module.__init__
645 name: unicode
646 doc: object = None
647
648Create a module object.
649
650The name must be a string; the optional doc argument can have any type.
651[clinic start generated code]*/
652
Tim Peters6d6c1a32001-08-02 04:15:00 +0000653static int
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200654module___init___impl(PyModuleObject *self, PyObject *name, PyObject *doc)
655/*[clinic end generated code: output=e7e721c26ce7aad7 input=57f9e177401e5e1e]*/
Tim Peters6d6c1a32001-08-02 04:15:00 +0000656{
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200657 PyObject *dict = self->md_dict;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 if (dict == NULL) {
659 dict = PyDict_New();
660 if (dict == NULL)
661 return -1;
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200662 self->md_dict = dict;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663 }
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200664 if (module_init_dict(self, dict, name, doc) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000665 return -1;
666 return 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000667}
668
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000669static void
Fred Drakeee238b92000-07-09 06:03:25 +0000670module_dealloc(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000671{
Victor Stinnerda7933e2020-04-13 03:04:28 +0200672 int verbose = _Py_GetConfig()->verbose;
Victor Stinnerc96be812019-05-14 17:34:56 +0200673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 PyObject_GC_UnTrack(m);
Victor Stinnerc96be812019-05-14 17:34:56 +0200675 if (verbose && m->md_name) {
Serhiy Storchakad7c38732019-10-08 13:46:17 +0300676 PySys_FormatStderr("# destroy %U\n", m->md_name);
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200677 }
678 if (m->md_weaklist != NULL)
679 PyObject_ClearWeakRefs((PyObject *) m);
Victor Stinner5b1ef202020-03-17 18:09:46 +0100680 /* bpo-39824: Don't call m_free() if m_size > 0 and md_state=NULL */
681 if (m->md_def && m->md_def->m_free
682 && (m->md_def->m_size <= 0 || m->md_state != NULL))
683 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 m->md_def->m_free(m);
Victor Stinner5b1ef202020-03-17 18:09:46 +0100685 }
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200686 Py_XDECREF(m->md_dict);
687 Py_XDECREF(m->md_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 if (m->md_state != NULL)
Victor Stinner00d7abd2020-12-01 09:56:42 +0100689 PyMem_Free(m->md_state);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 Py_TYPE(m)->tp_free((PyObject *)m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000691}
692
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000693static PyObject *
Fred Drakeee238b92000-07-09 06:03:25 +0000694module_repr(PyModuleObject *m)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000695{
Victor Stinner81a7be32020-04-14 15:14:01 +0200696 PyInterpreterState *interp = _PyInterpreterState_GET();
Barry Warsaw2907fe62001-08-16 20:39:24 +0000697
Eric Snowb523f842013-11-22 09:05:39 -0700698 return PyObject_CallMethod(interp->importlib, "_module_repr", "O", m);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000699}
700
Serhiy Storchaka3e429dc2018-10-30 13:19:51 +0200701/* Check if the "_initializing" attribute of the module spec is set to true.
702 Clear the exception and return 0 if spec is NULL.
703 */
704int
705_PyModuleSpec_IsInitializing(PyObject *spec)
706{
707 if (spec != NULL) {
708 _Py_IDENTIFIER(_initializing);
709 PyObject *value = _PyObject_GetAttrId(spec, &PyId__initializing);
710 if (value != NULL) {
711 int initializing = PyObject_IsTrue(value);
712 Py_DECREF(value);
713 if (initializing >= 0) {
714 return initializing;
715 }
716 }
717 }
718 PyErr_Clear();
719 return 0;
720}
721
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700722static PyObject*
Benjamin Peterson1184e262014-04-24 19:29:23 -0400723module_getattro(PyModuleObject *m, PyObject *name)
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700724{
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100725 PyObject *attr, *mod_name, *getattr;
Benjamin Peterson1184e262014-04-24 19:29:23 -0400726 attr = PyObject_GenericGetAttr((PyObject *)m, name);
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100727 if (attr || !PyErr_ExceptionMatches(PyExc_AttributeError)) {
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700728 return attr;
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100729 }
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700730 PyErr_Clear();
Benjamin Peterson1184e262014-04-24 19:29:23 -0400731 if (m->md_dict) {
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100732 _Py_IDENTIFIER(__getattr__);
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200733 getattr = _PyDict_GetItemIdWithError(m->md_dict, &PyId___getattr__);
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100734 if (getattr) {
Petr Viktorinffd97532020-02-11 17:46:57 +0100735 return PyObject_CallOneArg(getattr, name);
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100736 }
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200737 if (PyErr_Occurred()) {
738 return NULL;
739 }
740 mod_name = _PyDict_GetItemIdWithError(m->md_dict, &PyId___name__);
Oren Milman6db70332017-09-19 14:23:01 +0300741 if (mod_name && PyUnicode_Check(mod_name)) {
Serhiy Storchaka3e429dc2018-10-30 13:19:51 +0200742 Py_INCREF(mod_name);
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200743 PyObject *spec = _PyDict_GetItemIdWithError(m->md_dict, &PyId___spec__);
744 if (spec == NULL && PyErr_Occurred()) {
745 Py_DECREF(mod_name);
746 return NULL;
747 }
Serhiy Storchaka3e429dc2018-10-30 13:19:51 +0200748 Py_XINCREF(spec);
749 if (_PyModuleSpec_IsInitializing(spec)) {
750 PyErr_Format(PyExc_AttributeError,
751 "partially initialized "
752 "module '%U' has no attribute '%U' "
753 "(most likely due to a circular import)",
754 mod_name, name);
755 }
756 else {
757 PyErr_Format(PyExc_AttributeError,
758 "module '%U' has no attribute '%U'",
759 mod_name, name);
760 }
761 Py_XDECREF(spec);
762 Py_DECREF(mod_name);
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700763 return NULL;
764 }
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200765 else if (PyErr_Occurred()) {
766 return NULL;
767 }
Ethan Furman7b9ff0e2014-04-24 14:47:47 -0700768 }
769 PyErr_Format(PyExc_AttributeError,
770 "module has no attribute '%U'", name);
771 return NULL;
772}
773
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000774static int
775module_traverse(PyModuleObject *m, visitproc visit, void *arg)
776{
Victor Stinner5b1ef202020-03-17 18:09:46 +0100777 /* bpo-39824: Don't call m_traverse() if m_size > 0 and md_state=NULL */
778 if (m->md_def && m->md_def->m_traverse
779 && (m->md_def->m_size <= 0 || m->md_state != NULL))
780 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000781 int res = m->md_def->m_traverse((PyObject*)m, visit, arg);
782 if (res)
783 return res;
784 }
785 Py_VISIT(m->md_dict);
786 return 0;
Neil Schemenauer10e31cf2001-01-02 15:58:27 +0000787}
788
Martin v. Löwis1a214512008-06-11 05:26:20 +0000789static int
790module_clear(PyModuleObject *m)
791{
Victor Stinner5b1ef202020-03-17 18:09:46 +0100792 /* bpo-39824: Don't call m_clear() if m_size > 0 and md_state=NULL */
793 if (m->md_def && m->md_def->m_clear
794 && (m->md_def->m_size <= 0 || m->md_state != NULL))
795 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 int res = m->md_def->m_clear((PyObject*)m);
Serhiy Storchakad7c38732019-10-08 13:46:17 +0300797 if (PyErr_Occurred()) {
798 PySys_FormatStderr("Exception ignored in m_clear of module%s%V\n",
799 m->md_name ? " " : "",
800 m->md_name, "");
801 PyErr_WriteUnraisable(NULL);
802 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 if (res)
804 return res;
805 }
806 Py_CLEAR(m->md_dict);
807 return 0;
Martin v. Löwis1a214512008-06-11 05:26:20 +0000808}
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500810static PyObject *
811module_dir(PyObject *self, PyObject *args)
812{
813 PyObject *result = NULL;
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200814 PyObject *dict = _PyObject_GetAttrId(self, &PyId___dict__);
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500815
816 if (dict != NULL) {
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100817 if (PyDict_Check(dict)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200818 PyObject *dirfunc = _PyDict_GetItemIdWithError(dict, &PyId___dir__);
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100819 if (dirfunc) {
820 result = _PyObject_CallNoArg(dirfunc);
821 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200822 else if (!PyErr_Occurred()) {
Ivan Levkivskyi5364b5c2017-12-14 11:59:44 +0100823 result = PyDict_Keys(dict);
824 }
825 }
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500826 else {
827 const char *name = PyModule_GetName(self);
828 if (name)
829 PyErr_Format(PyExc_TypeError,
830 "%.200s.__dict__ is not a dictionary",
831 name);
832 }
833 }
834
835 Py_XDECREF(dict);
836 return result;
837}
838
839static PyMethodDef module_methods[] = {
840 {"__dir__", module_dir, METH_NOARGS,
Benjamin Petersonc7284122011-05-24 12:46:15 -0500841 PyDoc_STR("__dir__() -> list\nspecialized dir() implementation")},
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500842 {0}
843};
844
larryhastings2f2b6982021-04-29 20:09:08 -0700845static PyObject *
846module_get_annotations(PyModuleObject *m, void *Py_UNUSED(ignored))
847{
848 PyObject *dict = _PyObject_GetAttrId((PyObject *)m, &PyId___dict__);
849
850 if ((dict == NULL) || !PyDict_Check(dict)) {
851 PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary");
852 return NULL;
853 }
854
855 PyObject *annotations;
856 /* there's no _PyDict_GetItemId without WithError, so let's LBYL. */
857 if (_PyDict_ContainsId(dict, &PyId___annotations__)) {
858 annotations = _PyDict_GetItemIdWithError(dict, &PyId___annotations__);
859 /*
860 ** _PyDict_GetItemIdWithError could still fail,
861 ** for instance with a well-timed Ctrl-C or a MemoryError.
862 ** so let's be totally safe.
863 */
864 if (annotations) {
865 Py_INCREF(annotations);
866 }
867 } else {
868 annotations = PyDict_New();
869 if (annotations) {
870 int result = _PyDict_SetItemId(dict, &PyId___annotations__, annotations);
871 if (result) {
872 Py_CLEAR(annotations);
873 }
874 }
875 }
876 Py_DECREF(dict);
877 return annotations;
878}
879
880static int
881module_set_annotations(PyModuleObject *m, PyObject *value, void *Py_UNUSED(ignored))
882{
883 PyObject *dict = _PyObject_GetAttrId((PyObject *)m, &PyId___dict__);
884
885 if ((dict == NULL) || !PyDict_Check(dict)) {
886 PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary");
887 return -1;
888 }
889
890 if (value != NULL) {
891 /* set */
892 return _PyDict_SetItemId(dict, &PyId___annotations__, value);
893 }
894
895 /* delete */
896 if (!_PyDict_ContainsId(dict, &PyId___annotations__)) {
897 PyErr_Format(PyExc_AttributeError, "__annotations__");
898 return -1;
899 }
900
901 return _PyDict_DelItemId(dict, &PyId___annotations__);
902}
903
904
905static PyGetSetDef module_getsets[] = {
906 {"__annotations__", (getter)module_get_annotations, (setter)module_set_annotations},
907 {NULL}
908};
909
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000910PyTypeObject PyModule_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 PyVarObject_HEAD_INIT(&PyType_Type, 0)
912 "module", /* tp_name */
Peter Eisentraut0e0bc4e2018-09-10 18:46:08 +0200913 sizeof(PyModuleObject), /* tp_basicsize */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 0, /* tp_itemsize */
915 (destructor)module_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200916 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 0, /* tp_getattr */
918 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200919 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 (reprfunc)module_repr, /* tp_repr */
921 0, /* tp_as_number */
922 0, /* tp_as_sequence */
923 0, /* tp_as_mapping */
924 0, /* tp_hash */
925 0, /* tp_call */
926 0, /* tp_str */
Benjamin Peterson1184e262014-04-24 19:29:23 -0400927 (getattrofunc)module_getattro, /* tp_getattro */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 PyObject_GenericSetAttr, /* tp_setattro */
929 0, /* tp_as_buffer */
930 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
931 Py_TPFLAGS_BASETYPE, /* tp_flags */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200932 module___init____doc__, /* tp_doc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 (traverseproc)module_traverse, /* tp_traverse */
934 (inquiry)module_clear, /* tp_clear */
935 0, /* tp_richcompare */
Antoine Pitroudcedaf62013-07-31 23:14:08 +0200936 offsetof(PyModuleObject, md_weaklist), /* tp_weaklistoffset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 0, /* tp_iter */
938 0, /* tp_iternext */
Benjamin Peterson82b00c12011-05-24 11:09:06 -0500939 module_methods, /* tp_methods */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 module_members, /* tp_members */
larryhastings2f2b6982021-04-29 20:09:08 -0700941 module_getsets, /* tp_getset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942 0, /* tp_base */
943 0, /* tp_dict */
944 0, /* tp_descr_get */
945 0, /* tp_descr_set */
946 offsetof(PyModuleObject, md_dict), /* tp_dictoffset */
Serhiy Storchaka18b250f2017-03-19 08:51:07 +0200947 module___init__, /* tp_init */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000948 PyType_GenericAlloc, /* tp_alloc */
949 PyType_GenericNew, /* tp_new */
950 PyObject_GC_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000951};