Coverage Report

Created: 2025-11-09 06:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Objects/moduleobject.c
Line
Count
Source
1
/* Module object implementation */
2
3
#include "Python.h"
4
#include "pycore_call.h"          // _PyObject_CallNoArgs()
5
#include "pycore_dict.h"          // _PyDict_EnablePerThreadRefcounting()
6
#include "pycore_fileutils.h"     // _Py_wgetcwd
7
#include "pycore_import.h"        // _PyImport_GetNextModuleIndex()
8
#include "pycore_interp.h"        // PyInterpreterState.importlib
9
#include "pycore_long.h"          // _PyLong_GetOne()
10
#include "pycore_modsupport.h"    // _PyModule_CreateInitialized()
11
#include "pycore_moduleobject.h"  // _PyModule_GetDefOrNull()
12
#include "pycore_object.h"        // _PyType_AllocNoTrack
13
#include "pycore_pyerrors.h"      // _PyErr_FormatFromCause()
14
#include "pycore_pystate.h"       // _PyInterpreterState_GET()
15
#include "pycore_unicodeobject.h" // _PyUnicode_EqualToASCIIString()
16
#include "pycore_weakref.h"       // FT_CLEAR_WEAKREFS()
17
18
#include "osdefs.h"               // MAXPATHLEN
19
20
21
#define _PyModule_CAST(op) \
22
3.03M
    (assert(PyModule_Check(op)), _Py_CAST(PyModuleObject*, (op)))
23
24
25
static PyMemberDef module_members[] = {
26
    {"__dict__", _Py_T_OBJECT, offsetof(PyModuleObject, md_dict), Py_READONLY},
27
    {0}
28
};
29
30
static void
31
assert_def_missing_or_redundant(PyModuleObject *m)
32
102k
{
33
    /* We copy all relevant info into the module object.
34
     * Modules created using a def keep a reference to that (statically
35
     * allocated) def; the info there should match what we have in the module.
36
     */
37
#ifndef NDEBUG
38
    if (m->md_token_is_def) {
39
        PyModuleDef *def = (PyModuleDef *)m->md_token;
40
        assert(def);
41
#define DO_ASSERT(F) assert (def->m_ ## F == m->md_state_ ## F);
42
        DO_ASSERT(size);
43
        DO_ASSERT(traverse);
44
        DO_ASSERT(clear);
45
        DO_ASSERT(free);
46
#undef DO_ASSERT
47
    }
48
#endif // NDEBUG
49
102k
}
50
51
52
PyTypeObject PyModuleDef_Type = {
53
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
54
    "moduledef",                                /* tp_name */
55
    sizeof(PyModuleDef),                        /* tp_basicsize */
56
    0,                                          /* tp_itemsize */
57
};
58
59
60
int
61
_PyModule_IsExtension(PyObject *obj)
62
0
{
63
0
    if (!PyModule_Check(obj)) {
64
0
        return 0;
65
0
    }
66
0
    PyModuleObject *module = (PyModuleObject*)obj;
67
68
0
    if (module->md_exec) {
69
0
        return 1;
70
0
    }
71
0
    if (module->md_token_is_def) {
72
0
        PyModuleDef *def = (PyModuleDef *)module->md_token;
73
0
        return (module->md_token_is_def && def->m_methods != NULL);
74
0
    }
75
0
    return 0;
76
0
}
77
78
79
PyObject*
80
PyModuleDef_Init(PyModuleDef* def)
81
842
{
82
#ifdef Py_GIL_DISABLED
83
    // Check that this def does not come from a non-free-threading ABI.
84
    //
85
    // This is meant as a "sanity check"; users should never rely on it.
86
    // In particular, if we run out of ob_flags bits, or otherwise need to
87
    // change some of the internals, this check can go away. Still, it
88
    // would be nice to keep it for the free-threading transition.
89
    //
90
    // A PyModuleDef must be initialized with PyModuleDef_HEAD_INIT,
91
    // which (via PyObject_HEAD_INIT) sets _Py_STATICALLY_ALLOCATED_FLAG
92
    // and not _Py_LEGACY_ABI_CHECK_FLAG. For PyModuleDef, these flags never
93
    // change.
94
    // This means that the lower nibble of a valid PyModuleDef's ob_flags is
95
    // always `_10_` (in binary; `_` is don't care).
96
    //
97
    // So, a check for these bits won't reject valid PyModuleDef.
98
    // Rejecting incompatible extensions is slightly less important; here's
99
    // how that works:
100
    //
101
    // In the pre-free-threading stable ABI, PyModuleDef_HEAD_INIT is big
102
    // enough to overlap with free-threading ABI's ob_flags, is all zeros
103
    // except for the refcount field.
104
    // The refcount field can be:
105
    // - 1 (3.11 and below)
106
    // - UINT_MAX >> 2 (32-bit 3.12 & 3.13)
107
    // - UINT_MAX (64-bit 3.12 & 3.13)
108
    // - 7L << 28 (3.14)
109
    //
110
    // This means that the lower nibble of *any byte* in PyModuleDef_HEAD_INIT
111
    // is not `_10_` -- it can be:
112
    // - 0b0000
113
    // - 0b0001
114
    // - 0b0011 (from UINT_MAX >> 2)
115
    // - 0b0111 (from 7L << 28)
116
    // - 0b1111 (e.g. from UINT_MAX)
117
    // (The values may change at runtime as the PyModuleDef is used, but
118
    // PyModuleDef_Init is required before using the def as a Python object,
119
    // so we check at least once with the initial values.
120
    uint16_t flags = ((PyObject*)def)->ob_flags;
121
    uint16_t bits = _Py_STATICALLY_ALLOCATED_FLAG | _Py_LEGACY_ABI_CHECK_FLAG;
122
    if ((flags & bits) != _Py_STATICALLY_ALLOCATED_FLAG) {
123
        const char *message = "invalid PyModuleDef, extension possibly "
124
            "compiled for non-free-threaded Python";
125
        // Write the error as unraisable: if the extension tries calling
126
        // any API, it's likely to segfault and lose the exception.
127
        PyErr_SetString(PyExc_SystemError, message);
128
        PyErr_WriteUnraisable(NULL);
129
        // But also raise the exception normally -- this is technically
130
        // a recoverable state.
131
        PyErr_SetString(PyExc_SystemError, message);
132
        return NULL;
133
    }
134
#endif
135
842
    assert(PyModuleDef_Type.tp_flags & Py_TPFLAGS_READY);
136
842
    if (def->m_base.m_index == 0) {
137
453
        Py_SET_REFCNT(def, 1);
138
453
        Py_SET_TYPE(def, &PyModuleDef_Type);
139
453
        def->m_base.m_index = _PyImport_GetNextModuleIndex();
140
453
    }
141
842
    return (PyObject*)def;
142
842
}
143
144
static int
145
module_init_dict(PyModuleObject *mod, PyObject *md_dict,
146
                 PyObject *name, PyObject *doc)
147
1.48k
{
148
1.48k
    assert(md_dict != NULL);
149
1.48k
    if (doc == NULL)
150
501
        doc = Py_None;
151
152
1.48k
    if (PyDict_SetItem(md_dict, &_Py_ID(__name__), name) != 0)
153
0
        return -1;
154
1.48k
    if (PyDict_SetItem(md_dict, &_Py_ID(__doc__), doc) != 0)
155
0
        return -1;
156
1.48k
    if (PyDict_SetItem(md_dict, &_Py_ID(__package__), Py_None) != 0)
157
0
        return -1;
158
1.48k
    if (PyDict_SetItem(md_dict, &_Py_ID(__loader__), Py_None) != 0)
159
0
        return -1;
160
1.48k
    if (PyDict_SetItem(md_dict, &_Py_ID(__spec__), Py_None) != 0)
161
0
        return -1;
162
1.48k
    if (PyUnicode_CheckExact(name)) {
163
1.48k
        Py_XSETREF(mod->md_name, Py_NewRef(name));
164
1.48k
    }
165
166
1.48k
    return 0;
167
1.48k
}
168
169
static PyModuleObject *
170
new_module_notrack(PyTypeObject *mt)
171
1.48k
{
172
1.48k
    PyModuleObject *m;
173
1.48k
    m = (PyModuleObject *)_PyType_AllocNoTrack(mt, 0);
174
1.48k
    if (m == NULL)
175
0
        return NULL;
176
1.48k
    m->md_state = NULL;
177
1.48k
    m->md_weaklist = NULL;
178
1.48k
    m->md_name = NULL;
179
1.48k
    m->md_token_is_def = false;
180
#ifdef Py_GIL_DISABLED
181
    m->md_gil = Py_MOD_GIL_USED;
182
#endif
183
1.48k
    m->md_state_size = 0;
184
1.48k
    m->md_state_traverse = NULL;
185
1.48k
    m->md_state_clear = NULL;
186
1.48k
    m->md_state_free = NULL;
187
1.48k
    m->md_exec = NULL;
188
1.48k
    m->md_token = NULL;
189
1.48k
    m->md_dict = PyDict_New();
190
1.48k
    if (m->md_dict == NULL) {
191
0
        Py_DECREF(m);
192
0
        return NULL;
193
0
    }
194
1.48k
    return m;
195
1.48k
}
196
197
static void
198
track_module(PyModuleObject *m)
199
1.48k
{
200
1.48k
    _PyDict_EnablePerThreadRefcounting(m->md_dict);
201
1.48k
    _PyObject_SetDeferredRefcount((PyObject *)m);
202
1.48k
    PyObject_GC_Track(m);
203
1.48k
}
204
205
static PyObject *
206
new_module(PyTypeObject *mt, PyObject *args, PyObject *kws)
207
984
{
208
984
    PyModuleObject *m = new_module_notrack(mt);
209
984
    if (m != NULL) {
210
984
        track_module(m);
211
984
    }
212
984
    return (PyObject *)m;
213
984
}
214
215
PyObject *
216
PyModule_NewObject(PyObject *name)
217
501
{
218
501
    PyModuleObject *m = new_module_notrack(&PyModule_Type);
219
501
    if (m == NULL)
220
0
        return NULL;
221
501
    if (module_init_dict(m, m->md_dict, name, NULL) != 0)
222
0
        goto fail;
223
501
    track_module(m);
224
501
    return (PyObject *)m;
225
226
0
 fail:
227
0
    Py_DECREF(m);
228
0
    return NULL;
229
501
}
230
231
PyObject *
232
PyModule_New(const char *name)
233
80
{
234
80
    PyObject *nameobj, *module;
235
80
    nameobj = PyUnicode_FromString(name);
236
80
    if (nameobj == NULL)
237
0
        return NULL;
238
80
    module = PyModule_NewObject(nameobj);
239
80
    Py_DECREF(nameobj);
240
80
    return module;
241
80
}
242
243
/* Check API/ABI version
244
 * Issues a warning on mismatch, which is usually not fatal.
245
 * Returns 0 if an exception is raised.
246
 */
247
static int
248
check_api_version(const char *name, int module_api_version)
249
453
{
250
453
    if (module_api_version != PYTHON_API_VERSION && module_api_version != PYTHON_ABI_VERSION) {
251
0
        int err;
252
0
        err = PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
253
0
            "Python C API version mismatch for module %.100s: "
254
0
            "This Python has API version %d, module %.100s has version %d.",
255
0
             name,
256
0
             PYTHON_API_VERSION, name, module_api_version);
257
0
        if (err)
258
0
            return 0;
259
0
    }
260
453
    return 1;
261
453
}
262
263
static int
264
_add_methods_to_object(PyObject *module, PyObject *name, PyMethodDef *functions)
265
429
{
266
429
    PyObject *func;
267
429
    PyMethodDef *fdef;
268
269
9.43k
    for (fdef = functions; fdef->ml_name != NULL; fdef++) {
270
9.00k
        if ((fdef->ml_flags & METH_CLASS) ||
271
9.00k
            (fdef->ml_flags & METH_STATIC)) {
272
0
            PyErr_SetString(PyExc_ValueError,
273
0
                            "module functions cannot set"
274
0
                            " METH_CLASS or METH_STATIC");
275
0
            return -1;
276
0
        }
277
9.00k
        func = PyCFunction_NewEx(fdef, (PyObject*)module, name);
278
9.00k
        if (func == NULL) {
279
0
            return -1;
280
0
        }
281
9.00k
        _PyObject_SetDeferredRefcount(func);
282
9.00k
        if (PyObject_SetAttrString(module, fdef->ml_name, func) != 0) {
283
0
            Py_DECREF(func);
284
0
            return -1;
285
0
        }
286
9.00k
        Py_DECREF(func);
287
9.00k
    }
288
289
429
    return 0;
290
429
}
291
292
PyObject *
293
PyModule_Create2(PyModuleDef* module, int module_api_version)
294
0
{
295
0
    if (!_PyImport_IsInitialized(_PyInterpreterState_GET())) {
296
0
        PyErr_SetString(PyExc_SystemError,
297
0
                        "Python import machinery not initialized");
298
0
        return NULL;
299
0
    }
300
0
    return _PyModule_CreateInitialized(module, module_api_version);
301
0
}
302
303
static void
304
module_copy_members_from_deflike(
305
    PyModuleObject *md,
306
    PyModuleDef *def_like /* not necessarily a valid Python object */)
307
453
{
308
453
    md->md_state_size = def_like->m_size;
309
453
    md->md_state_traverse = def_like->m_traverse;
310
453
    md->md_state_clear = def_like->m_clear;
311
453
    md->md_state_free = def_like->m_free;
312
453
}
313
314
PyObject *
315
_PyModule_CreateInitialized(PyModuleDef* module, int module_api_version)
316
64
{
317
64
    const char* name;
318
64
    PyModuleObject *m;
319
320
64
    if (!PyModuleDef_Init(module))
321
0
        return NULL;
322
64
    name = module->m_name;
323
64
    if (!check_api_version(name, module_api_version)) {
324
0
        return NULL;
325
0
    }
326
64
    if (module->m_slots) {
327
0
        PyErr_Format(
328
0
            PyExc_SystemError,
329
0
            "module %s: PyModule_Create is incompatible with m_slots", name);
330
0
        return NULL;
331
0
    }
332
64
    name = _PyImport_ResolveNameWithPackageContext(name);
333
334
64
    m = (PyModuleObject*)PyModule_New(name);
335
64
    if (m == NULL)
336
0
        return NULL;
337
338
64
    if (module->m_size > 0) {
339
0
        m->md_state = PyMem_Malloc(module->m_size);
340
0
        if (!m->md_state) {
341
0
            PyErr_NoMemory();
342
0
            Py_DECREF(m);
343
0
            return NULL;
344
0
        }
345
0
        memset(m->md_state, 0, module->m_size);
346
0
    }
347
348
64
    if (module->m_methods != NULL) {
349
64
        if (PyModule_AddFunctions((PyObject *) m, module->m_methods) != 0) {
350
0
            Py_DECREF(m);
351
0
            return NULL;
352
0
        }
353
64
    }
354
64
    if (module->m_doc != NULL) {
355
48
        if (PyModule_SetDocString((PyObject *) m, module->m_doc) != 0) {
356
0
            Py_DECREF(m);
357
0
            return NULL;
358
0
        }
359
48
    }
360
64
    m->md_token = module;
361
64
    m->md_token_is_def = true;
362
64
    module_copy_members_from_deflike(m, module);
363
#ifdef Py_GIL_DISABLED
364
    m->md_gil = Py_MOD_GIL_USED;
365
#endif
366
64
    return (PyObject*)m;
367
64
}
368
369
static PyObject *
370
module_from_def_and_spec(
371
    PyModuleDef* def_like, /* not necessarily a valid Python object */
372
    PyObject *spec,
373
    int module_api_version,
374
    PyModuleDef* original_def /* NULL if not defined by a def */)
375
389
{
376
389
    PyModuleDef_Slot* cur_slot;
377
389
    PyObject *(*create)(PyObject *, PyModuleDef*) = NULL;
378
389
    PyObject *nameobj;
379
389
    PyObject *m = NULL;
380
389
    int has_multiple_interpreters_slot = 0;
381
389
    void *multiple_interpreters = (void *)0;
382
389
    int has_gil_slot = 0;
383
389
    void *gil_slot = Py_MOD_GIL_USED;
384
389
    int has_execution_slots = 0;
385
389
    const char *name;
386
389
    int ret;
387
389
    void *token = NULL;
388
389
    _Py_modexecfunc m_exec = NULL;
389
389
    PyInterpreterState *interp = _PyInterpreterState_GET();
390
391
389
    nameobj = PyObject_GetAttrString(spec, "name");
392
389
    if (nameobj == NULL) {
393
0
        return NULL;
394
0
    }
395
389
    name = PyUnicode_AsUTF8(nameobj);
396
389
    if (name == NULL) {
397
0
        goto error;
398
0
    }
399
400
389
    if (!check_api_version(name, module_api_version)) {
401
0
        goto error;
402
0
    }
403
404
389
    if (def_like->m_size < 0) {
405
0
        PyErr_Format(
406
0
            PyExc_SystemError,
407
0
            "module %s: m_size may not be negative for multi-phase initialization",
408
0
            name);
409
0
        goto error;
410
0
    }
411
412
1.58k
    for (cur_slot = def_like->m_slots; cur_slot && cur_slot->slot; cur_slot++) {
413
        // Macro to copy a non-NULL, non-repeatable slot that's unusable with
414
        // PyModuleDef. The destination must be initially NULL.
415
1.19k
#define COPY_COMMON_SLOT(SLOT, TYPE, DEST)                              \
416
1.19k
        do {                                                            \
417
0
            if (!(TYPE)(cur_slot->value)) {                             \
418
0
                PyErr_Format(                                           \
419
0
                    PyExc_SystemError,                                  \
420
0
                    "module %s: " #SLOT " must not be NULL",            \
421
0
                    name);                                              \
422
0
                goto error;                                             \
423
0
            }                                                           \
424
0
            if (original_def) {                                         \
425
0
                PyErr_Format(                                           \
426
0
                    PyExc_SystemError,                                  \
427
0
                    "module %s: " #SLOT " used with PyModuleDef",       \
428
0
                    name);                                              \
429
0
                goto error;                                             \
430
0
            }                                                           \
431
0
            if (DEST) {                                                 \
432
0
                PyErr_Format(                                           \
433
0
                    PyExc_SystemError,                                  \
434
0
                    "module %s has more than one " #SLOT " slot",       \
435
0
                    name);                                              \
436
0
                goto error;                                             \
437
0
            }                                                           \
438
0
            DEST = (TYPE)(cur_slot->value);                             \
439
0
        } while (0);                                                    \
440
        /////////////////////////////////////////////////////////////////
441
1.19k
        switch (cur_slot->slot) {
442
0
            case Py_mod_create:
443
0
                if (create) {
444
0
                    PyErr_Format(
445
0
                        PyExc_SystemError,
446
0
                        "module %s has multiple create slots",
447
0
                        name);
448
0
                    goto error;
449
0
                }
450
0
                create = cur_slot->value;
451
0
                break;
452
409
            case Py_mod_exec:
453
409
                has_execution_slots = 1;
454
409
                if (!original_def) {
455
0
                    COPY_COMMON_SLOT(Py_mod_exec, _Py_modexecfunc, m_exec);
456
0
                }
457
409
                break;
458
409
            case Py_mod_multiple_interpreters:
459
389
                if (has_multiple_interpreters_slot) {
460
0
                    PyErr_Format(
461
0
                        PyExc_SystemError,
462
0
                        "module %s has more than one 'multiple interpreters' slots",
463
0
                        name);
464
0
                    goto error;
465
0
                }
466
389
                multiple_interpreters = cur_slot->value;
467
389
                has_multiple_interpreters_slot = 1;
468
389
                break;
469
389
            case Py_mod_gil:
470
389
                if (has_gil_slot) {
471
0
                    PyErr_Format(
472
0
                       PyExc_SystemError,
473
0
                       "module %s has more than one 'gil' slot",
474
0
                       name);
475
0
                    goto error;
476
0
                }
477
389
                gil_slot = cur_slot->value;
478
389
                has_gil_slot = 1;
479
389
                break;
480
6
            case Py_mod_abi:
481
6
                if (PyABIInfo_Check((PyABIInfo *)cur_slot->value, name) < 0) {
482
0
                    goto error;
483
0
                }
484
6
                break;
485
6
            case Py_mod_name:
486
0
                COPY_COMMON_SLOT(Py_mod_name, char*, def_like->m_name);
487
0
                break;
488
0
            case Py_mod_doc:
489
0
                COPY_COMMON_SLOT(Py_mod_doc, char*, def_like->m_doc);
490
0
                break;
491
0
            case Py_mod_state_size:
492
0
                COPY_COMMON_SLOT(Py_mod_state_size, Py_ssize_t,
493
0
                                 def_like->m_size);
494
0
                break;
495
0
            case Py_mod_methods:
496
0
                COPY_COMMON_SLOT(Py_mod_methods, PyMethodDef*,
497
0
                                 def_like->m_methods);
498
0
                break;
499
0
            case Py_mod_state_traverse:
500
0
                COPY_COMMON_SLOT(Py_mod_state_traverse, traverseproc,
501
0
                                 def_like->m_traverse);
502
0
                break;
503
0
            case Py_mod_state_clear:
504
0
                COPY_COMMON_SLOT(Py_mod_state_clear, inquiry,
505
0
                                 def_like->m_clear);
506
0
                break;
507
0
            case Py_mod_state_free:
508
0
                COPY_COMMON_SLOT(Py_mod_state_free, freefunc,
509
0
                                 def_like->m_free);
510
0
                break;
511
0
            case Py_mod_token:
512
0
                COPY_COMMON_SLOT(Py_mod_token, void*, token);
513
0
                break;
514
0
            default:
515
0
                assert(cur_slot->slot < 0 || cur_slot->slot > _Py_mod_LAST_SLOT);
516
0
                PyErr_Format(
517
0
                    PyExc_SystemError,
518
0
                    "module %s uses unknown slot ID %i",
519
0
                    name, cur_slot->slot);
520
0
                goto error;
521
1.19k
        }
522
1.19k
#undef COPY_COMMON_SLOT
523
1.19k
    }
524
525
    /* By default, multi-phase init modules are expected
526
       to work under multiple interpreters. */
527
389
    if (!has_multiple_interpreters_slot) {
528
0
        multiple_interpreters = Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED;
529
0
    }
530
389
    if (multiple_interpreters == Py_MOD_MULTIPLE_INTERPRETERS_NOT_SUPPORTED) {
531
0
        if (!_Py_IsMainInterpreter(interp)
532
0
            && _PyImport_CheckSubinterpIncompatibleExtensionAllowed(name) < 0)
533
0
        {
534
0
            goto error;
535
0
        }
536
0
    }
537
389
    else if (multiple_interpreters != Py_MOD_PER_INTERPRETER_GIL_SUPPORTED
538
0
             && interp->ceval.own_gil
539
0
             && !_Py_IsMainInterpreter(interp)
540
0
             && _PyImport_CheckSubinterpIncompatibleExtensionAllowed(name) < 0)
541
0
    {
542
0
        goto error;
543
0
    }
544
545
389
    if (create) {
546
0
        m = create(spec, original_def);
547
0
        if (m == NULL) {
548
0
            if (!PyErr_Occurred()) {
549
0
                PyErr_Format(
550
0
                    PyExc_SystemError,
551
0
                    "creation of module %s failed without setting an exception",
552
0
                    name);
553
0
            }
554
0
            goto error;
555
0
        } else {
556
0
            if (PyErr_Occurred()) {
557
0
                _PyErr_FormatFromCause(
558
0
                    PyExc_SystemError,
559
0
                    "creation of module %s raised unreported exception",
560
0
                    name);
561
0
                goto error;
562
0
            }
563
0
        }
564
389
    } else {
565
389
        m = PyModule_NewObject(nameobj);
566
389
        if (m == NULL) {
567
0
            goto error;
568
0
        }
569
389
    }
570
571
389
    if (PyModule_Check(m)) {
572
389
        PyModuleObject *mod = (PyModuleObject*)m;
573
389
        mod->md_state = NULL;
574
389
        module_copy_members_from_deflike(mod, def_like);
575
389
        if (original_def) {
576
389
            assert (!token);
577
389
            mod->md_token = original_def;
578
389
            mod->md_token_is_def = 1;
579
389
        }
580
0
        else {
581
0
            mod->md_token = token;
582
0
        }
583
#ifdef Py_GIL_DISABLED
584
        mod->md_gil = gil_slot;
585
#else
586
389
        (void)gil_slot;
587
389
#endif
588
389
        mod->md_exec = m_exec;
589
389
    } else {
590
0
        if (def_like->m_size > 0 || def_like->m_traverse || def_like->m_clear
591
0
            || def_like->m_free)
592
0
        {
593
0
            PyErr_Format(
594
0
                PyExc_SystemError,
595
0
                "module %s is not a module object, but requests module state",
596
0
                name);
597
0
            goto error;
598
0
        }
599
0
        if (has_execution_slots) {
600
0
            PyErr_Format(
601
0
                PyExc_SystemError,
602
0
                "module %s specifies execution slots, but did not create "
603
0
                    "a ModuleType instance",
604
0
                name);
605
0
            goto error;
606
0
        }
607
0
        if (token) {
608
0
            PyErr_Format(
609
0
                PyExc_SystemError,
610
0
                "module %s specifies a token, but did not create "
611
0
                    "a ModuleType instance",
612
0
                name);
613
0
            goto error;
614
0
        }
615
0
    }
616
617
389
    if (def_like->m_methods != NULL) {
618
365
        ret = _add_methods_to_object(m, nameobj, def_like->m_methods);
619
365
        if (ret != 0) {
620
0
            goto error;
621
0
        }
622
365
    }
623
624
389
    if (def_like->m_doc != NULL) {
625
333
        ret = PyModule_SetDocString(m, def_like->m_doc);
626
333
        if (ret != 0) {
627
0
            goto error;
628
0
        }
629
333
    }
630
631
389
    Py_DECREF(nameobj);
632
389
    return m;
633
634
0
error:
635
0
    Py_DECREF(nameobj);
636
0
    Py_XDECREF(m);
637
0
    return NULL;
638
389
}
639
640
PyObject *
641
PyModule_FromDefAndSpec2(PyModuleDef* def, PyObject *spec, int module_api_version)
642
389
{
643
389
    PyModuleDef_Init(def);
644
389
    return module_from_def_and_spec(def, spec, module_api_version, def);
645
389
}
646
647
PyObject *
648
PyModule_FromSlotsAndSpec(const PyModuleDef_Slot *slots, PyObject *spec)
649
0
{
650
0
    if (!slots) {
651
0
        PyErr_SetString(
652
0
            PyExc_SystemError,
653
0
            "PyModule_FromSlotsAndSpec called with NULL slots");
654
0
        return NULL;
655
0
    }
656
    // Fill in enough of a PyModuleDef to pass to common machinery
657
0
    PyModuleDef def_like = {.m_slots = (PyModuleDef_Slot *)slots};
658
659
0
    return module_from_def_and_spec(&def_like, spec, PYTHON_API_VERSION,
660
0
                                    NULL);
661
0
}
662
663
#ifdef Py_GIL_DISABLED
664
int
665
PyUnstable_Module_SetGIL(PyObject *module, void *gil)
666
{
667
    if (!PyModule_Check(module)) {
668
        PyErr_BadInternalCall();
669
        return -1;
670
    }
671
    ((PyModuleObject *)module)->md_gil = gil;
672
    return 0;
673
}
674
#endif
675
676
static int
677
run_exec_func(PyObject *module, int (*exec)(PyObject *))
678
409
{
679
409
    int ret = exec(module);
680
409
    if (ret != 0) {
681
0
        if (!PyErr_Occurred()) {
682
0
            PyErr_Format(
683
0
                PyExc_SystemError,
684
0
                "execution of %R failed without setting an exception",
685
0
                module);
686
0
        }
687
0
        return -1;
688
0
    }
689
409
    if (PyErr_Occurred()) {
690
0
        _PyErr_FormatFromCause(
691
0
            PyExc_SystemError,
692
0
            "execution of module %R raised unreported exception",
693
0
            module);
694
0
        return -1;
695
0
    }
696
409
    return 0;
697
409
}
698
699
static int
700
alloc_state(PyObject *module)
701
778
{
702
778
    if (!PyModule_Check(module)) {
703
0
        PyErr_Format(PyExc_TypeError, "expected module, got %T", module);
704
0
        return -1;
705
0
    }
706
778
    PyModuleObject *md = (PyModuleObject*)module;
707
708
778
    if (md->md_state_size >= 0) {
709
778
        if (md->md_state == NULL) {
710
            /* Always set a state pointer; this serves as a marker to skip
711
             * multiple initialization (importlib.reload() is no-op) */
712
389
            md->md_state = PyMem_Malloc(md->md_state_size);
713
389
            if (!md->md_state) {
714
0
                PyErr_NoMemory();
715
0
                return -1;
716
0
            }
717
389
            memset(md->md_state, 0, md->md_state_size);
718
389
        }
719
778
    }
720
778
    return 0;
721
778
}
722
723
int
724
PyModule_Exec(PyObject *module)
725
389
{
726
389
    if (alloc_state(module) < 0) {
727
0
        return -1;
728
0
    }
729
389
    PyModuleObject *md = (PyModuleObject*)module;
730
389
    if (md->md_exec) {
731
0
        assert(!md->md_token_is_def);
732
0
        return run_exec_func(module, md->md_exec);
733
0
    }
734
735
389
    PyModuleDef *def = _PyModule_GetDefOrNull(module);
736
389
    if (def) {
737
389
        return PyModule_ExecDef(module, def);
738
389
    }
739
0
    return 0;
740
389
}
741
742
int
743
PyModule_ExecDef(PyObject *module, PyModuleDef *def)
744
389
{
745
389
    PyModuleDef_Slot *cur_slot;
746
747
389
    if (alloc_state(module) < 0) {
748
0
        return -1;
749
0
    }
750
751
389
    assert(PyModule_Check(module));
752
753
389
    if (def->m_slots == NULL) {
754
0
        return 0;
755
0
    }
756
757
1.58k
    for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) {
758
1.19k
        if (cur_slot->slot == Py_mod_exec) {
759
409
            int (*func)(PyObject *) = cur_slot->value;
760
409
            if (run_exec_func(module, func) < 0) {
761
0
                return -1;
762
0
            }
763
409
            continue;
764
409
        }
765
1.19k
    }
766
389
    return 0;
767
389
}
768
769
int
770
PyModule_AddFunctions(PyObject *m, PyMethodDef *functions)
771
64
{
772
64
    int res;
773
64
    PyObject *name = PyModule_GetNameObject(m);
774
64
    if (name == NULL) {
775
0
        return -1;
776
0
    }
777
778
64
    res = _add_methods_to_object(m, name, functions);
779
64
    Py_DECREF(name);
780
64
    return res;
781
64
}
782
783
int
784
PyModule_SetDocString(PyObject *m, const char *doc)
785
381
{
786
381
    PyObject *v;
787
788
381
    v = PyUnicode_FromString(doc);
789
381
    if (v == NULL || PyObject_SetAttr(m, &_Py_ID(__doc__), v) != 0) {
790
0
        Py_XDECREF(v);
791
0
        return -1;
792
0
    }
793
381
    Py_DECREF(v);
794
381
    return 0;
795
381
}
796
797
PyObject *
798
PyModule_GetDict(PyObject *m)
799
9.48k
{
800
9.48k
    if (!PyModule_Check(m)) {
801
0
        PyErr_BadInternalCall();
802
0
        return NULL;
803
0
    }
804
9.48k
    return _PyModule_GetDict(m);  // borrowed reference
805
9.48k
}
806
807
int
808
PyModule_GetStateSize(PyObject *m, Py_ssize_t *size_p)
809
0
{
810
0
    *size_p = -1;
811
0
    if (!PyModule_Check(m)) {
812
0
        PyErr_Format(PyExc_TypeError, "expected module, got %T", m);
813
0
        return -1;
814
0
    }
815
0
    PyModuleObject *mod = (PyModuleObject *)m;
816
0
    *size_p = mod->md_state_size;
817
0
    return 0;
818
0
}
819
820
int
821
PyModule_GetToken(PyObject *m, void **token_p)
822
0
{
823
0
    *token_p = NULL;
824
0
    if (!PyModule_Check(m)) {
825
0
        PyErr_Format(PyExc_TypeError, "expected module, got %T", m);
826
0
        return -1;
827
0
    }
828
0
    *token_p = _PyModule_GetToken(m);
829
0
    return 0;
830
0
}
831
832
PyObject*
833
PyModule_GetNameObject(PyObject *mod)
834
64
{
835
64
    if (!PyModule_Check(mod)) {
836
0
        PyErr_BadArgument();
837
0
        return NULL;
838
0
    }
839
64
    PyObject *dict = ((PyModuleObject *)mod)->md_dict;  // borrowed reference
840
64
    if (dict == NULL || !PyDict_Check(dict)) {
841
0
        goto error;
842
0
    }
843
64
    PyObject *name;
844
64
    if (PyDict_GetItemRef(dict, &_Py_ID(__name__), &name) <= 0) {
845
        // error or not found
846
0
        goto error;
847
0
    }
848
64
    if (!PyUnicode_Check(name)) {
849
0
        Py_DECREF(name);
850
0
        goto error;
851
0
    }
852
64
    return name;
853
854
0
error:
855
0
    if (!PyErr_Occurred()) {
856
0
        PyErr_SetString(PyExc_SystemError, "nameless module");
857
0
    }
858
0
    return NULL;
859
64
}
860
861
const char *
862
PyModule_GetName(PyObject *m)
863
0
{
864
0
    PyObject *name = PyModule_GetNameObject(m);
865
0
    if (name == NULL) {
866
0
        return NULL;
867
0
    }
868
0
    assert(Py_REFCNT(name) >= 2);
869
0
    Py_DECREF(name);   /* module dict has still a reference */
870
0
    return PyUnicode_AsUTF8(name);
871
0
}
872
873
PyObject*
874
_PyModule_GetFilenameObject(PyObject *mod)
875
48
{
876
    // We return None to indicate "not found" or "bogus".
877
48
    if (!PyModule_Check(mod)) {
878
0
        PyErr_BadArgument();
879
0
        return NULL;
880
0
    }
881
48
    PyObject *dict = ((PyModuleObject *)mod)->md_dict;  // borrowed reference
882
48
    if (dict == NULL) {
883
        // The module has been tampered with.
884
0
        Py_RETURN_NONE;
885
0
    }
886
48
    PyObject *fileobj;
887
48
    int res = PyDict_GetItemRef(dict, &_Py_ID(__file__), &fileobj);
888
48
    if (res < 0) {
889
0
        return NULL;
890
0
    }
891
48
    if (res == 0) {
892
        // __file__ isn't set.  There are several reasons why this might
893
        // be so, most of them valid reasons.  If it's the __main__
894
        // module then we're running the REPL or with -c.  Otherwise
895
        // it's a namespace package or other module with a loader that
896
        // isn't disk-based.  It could also be that a user created
897
        // a module manually but without manually setting __file__.
898
12
        Py_RETURN_NONE;
899
12
    }
900
36
    if (!PyUnicode_Check(fileobj)) {
901
0
        Py_DECREF(fileobj);
902
0
        Py_RETURN_NONE;
903
0
    }
904
36
    return fileobj;
905
36
}
906
907
PyObject*
908
PyModule_GetFilenameObject(PyObject *mod)
909
48
{
910
48
    PyObject *fileobj = _PyModule_GetFilenameObject(mod);
911
48
    if (fileobj == NULL) {
912
0
        return NULL;
913
0
    }
914
48
    if (fileobj == Py_None) {
915
12
        PyErr_SetString(PyExc_SystemError, "module filename missing");
916
12
        return NULL;
917
12
    }
918
36
    return fileobj;
919
48
}
920
921
const char *
922
PyModule_GetFilename(PyObject *m)
923
0
{
924
0
    PyObject *fileobj;
925
0
    const char *utf8;
926
0
    fileobj = PyModule_GetFilenameObject(m);
927
0
    if (fileobj == NULL)
928
0
        return NULL;
929
0
    utf8 = PyUnicode_AsUTF8(fileobj);
930
0
    Py_DECREF(fileobj);   /* module dict has still a reference */
931
0
    return utf8;
932
0
}
933
934
Py_ssize_t
935
_PyModule_GetFilenameUTF8(PyObject *mod, char *buffer, Py_ssize_t maxlen)
936
0
{
937
    // We "return" an empty string for an invalid module
938
    // and for a missing, empty, or invalid filename.
939
0
    assert(maxlen >= 0);
940
0
    Py_ssize_t size = -1;
941
0
    PyObject *filenameobj = _PyModule_GetFilenameObject(mod);
942
0
    if (filenameobj == NULL) {
943
0
        return -1;
944
0
    }
945
0
    if (filenameobj == Py_None) {
946
        // It is missing or invalid.
947
0
        buffer[0] = '\0';
948
0
        size = 0;
949
0
    }
950
0
    else {
951
0
        const char *filename = PyUnicode_AsUTF8AndSize(filenameobj, &size);
952
0
        assert(size >= 0);
953
0
        if (size > maxlen) {
954
0
            size = -1;
955
0
            PyErr_SetString(PyExc_ValueError, "__file__ too long");
956
0
        }
957
0
        else {
958
0
            (void)strcpy(buffer, filename);
959
0
        }
960
0
    }
961
0
    Py_DECREF(filenameobj);
962
0
    return size;
963
0
}
964
965
PyModuleDef*
966
PyModule_GetDef(PyObject* m)
967
2
{
968
2
    if (!PyModule_Check(m)) {
969
0
        PyErr_BadArgument();
970
0
        return NULL;
971
0
    }
972
2
    return _PyModule_GetDefOrNull(m);
973
2
}
974
975
void*
976
PyModule_GetState(PyObject* m)
977
22.8M
{
978
22.8M
    if (!PyModule_Check(m)) {
979
0
        PyErr_BadArgument();
980
0
        return NULL;
981
0
    }
982
22.8M
    return _PyModule_GetState(m);
983
22.8M
}
984
985
void
986
_PyModule_Clear(PyObject *m)
987
0
{
988
0
    PyObject *d = ((PyModuleObject *)m)->md_dict;
989
0
    if (d != NULL)
990
0
        _PyModule_ClearDict(d);
991
0
}
992
993
void
994
_PyModule_ClearDict(PyObject *d)
995
0
{
996
    /* To make the execution order of destructors for global
997
       objects a bit more predictable, we first zap all objects
998
       whose name starts with a single underscore, before we clear
999
       the entire dictionary.  We zap them by replacing them with
1000
       None, rather than deleting them from the dictionary, to
1001
       avoid rehashing the dictionary (to some extent). */
1002
1003
0
    Py_ssize_t pos;
1004
0
    PyObject *key, *value;
1005
1006
0
    int verbose = _Py_GetConfig()->verbose;
1007
1008
    /* First, clear only names starting with a single underscore */
1009
0
    pos = 0;
1010
0
    while (PyDict_Next(d, &pos, &key, &value)) {
1011
0
        if (value != Py_None && PyUnicode_Check(key)) {
1012
0
            if (PyUnicode_READ_CHAR(key, 0) == '_' &&
1013
0
                PyUnicode_READ_CHAR(key, 1) != '_') {
1014
0
                if (verbose > 1) {
1015
0
                    const char *s = PyUnicode_AsUTF8(key);
1016
0
                    if (s != NULL)
1017
0
                        PySys_WriteStderr("#   clear[1] %s\n", s);
1018
0
                    else
1019
0
                        PyErr_Clear();
1020
0
                }
1021
0
                if (PyDict_SetItem(d, key, Py_None) != 0) {
1022
0
                    PyErr_FormatUnraisable("Exception ignored while "
1023
0
                                           "clearing module dict");
1024
0
                }
1025
0
            }
1026
0
        }
1027
0
    }
1028
1029
    /* Next, clear all names except for __builtins__ */
1030
0
    pos = 0;
1031
0
    while (PyDict_Next(d, &pos, &key, &value)) {
1032
0
        if (value != Py_None && PyUnicode_Check(key)) {
1033
0
            if (PyUnicode_READ_CHAR(key, 0) != '_' ||
1034
0
                !_PyUnicode_EqualToASCIIString(key, "__builtins__"))
1035
0
            {
1036
0
                if (verbose > 1) {
1037
0
                    const char *s = PyUnicode_AsUTF8(key);
1038
0
                    if (s != NULL)
1039
0
                        PySys_WriteStderr("#   clear[2] %s\n", s);
1040
0
                    else
1041
0
                        PyErr_Clear();
1042
0
                }
1043
0
                if (PyDict_SetItem(d, key, Py_None) != 0) {
1044
0
                    PyErr_FormatUnraisable("Exception ignored while "
1045
0
                                           "clearing module dict");
1046
0
                }
1047
0
            }
1048
0
        }
1049
0
    }
1050
1051
    /* Note: we leave __builtins__ in place, so that destructors
1052
       of non-global objects defined in this module can still use
1053
       builtins, in particularly 'None'. */
1054
1055
0
}
1056
1057
/*[clinic input]
1058
class module "PyModuleObject *" "&PyModule_Type"
1059
[clinic start generated code]*/
1060
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3e35d4f708ecb6af]*/
1061
1062
#include "clinic/moduleobject.c.h"
1063
1064
/* Methods */
1065
1066
/*[clinic input]
1067
module.__init__
1068
    name: unicode
1069
    doc: object = None
1070
1071
Create a module object.
1072
1073
The name must be a string; the optional doc argument can have any type.
1074
[clinic start generated code]*/
1075
1076
static int
1077
module___init___impl(PyModuleObject *self, PyObject *name, PyObject *doc)
1078
/*[clinic end generated code: output=e7e721c26ce7aad7 input=57f9e177401e5e1e]*/
1079
984
{
1080
984
    return module_init_dict(self, self->md_dict, name, doc);
1081
984
}
1082
1083
static void
1084
module_dealloc(PyObject *self)
1085
40
{
1086
40
    PyModuleObject *m = _PyModule_CAST(self);
1087
1088
40
    PyObject_GC_UnTrack(m);
1089
1090
40
    int verbose = _Py_GetConfig()->verbose;
1091
40
    if (verbose && m->md_name) {
1092
0
        PySys_FormatStderr("# destroy %U\n", m->md_name);
1093
0
    }
1094
40
    FT_CLEAR_WEAKREFS(self, m->md_weaklist);
1095
1096
40
    assert_def_missing_or_redundant(m);
1097
    /* bpo-39824: Don't call m_free() if m_size > 0 and md_state=NULL */
1098
40
    if (m->md_state_free && (m->md_state_size <= 0 || m->md_state != NULL))
1099
0
    {
1100
0
        m->md_state_free(m);
1101
0
    }
1102
1103
40
    Py_XDECREF(m->md_dict);
1104
40
    Py_XDECREF(m->md_name);
1105
40
    if (m->md_state != NULL) {
1106
0
        PyMem_Free(m->md_state);
1107
0
    }
1108
40
    Py_TYPE(m)->tp_free((PyObject *)m);
1109
40
}
1110
1111
static PyObject *
1112
module_repr(PyObject *self)
1113
0
{
1114
0
    PyModuleObject *m = _PyModule_CAST(self);
1115
0
    PyInterpreterState *interp = _PyInterpreterState_GET();
1116
0
    return _PyImport_ImportlibModuleRepr(interp, (PyObject *)m);
1117
0
}
1118
1119
/* Check if the "_initializing" attribute of the module spec is set to true.
1120
 */
1121
int
1122
_PyModuleSpec_IsInitializing(PyObject *spec)
1123
42.2k
{
1124
42.2k
    if (spec == NULL) {
1125
0
        return 0;
1126
0
    }
1127
42.2k
    PyObject *value;
1128
42.2k
    int rc = PyObject_GetOptionalAttr(spec, &_Py_ID(_initializing), &value);
1129
42.2k
    if (rc > 0) {
1130
41.5k
        rc = PyObject_IsTrue(value);
1131
41.5k
        Py_DECREF(value);
1132
41.5k
    }
1133
42.2k
    return rc;
1134
42.2k
}
1135
1136
/* Check if the submodule name is in the "_uninitialized_submodules" attribute
1137
   of the module spec.
1138
 */
1139
int
1140
_PyModuleSpec_IsUninitializedSubmodule(PyObject *spec, PyObject *name)
1141
6.11k
{
1142
6.11k
    if (spec == NULL) {
1143
0
         return 0;
1144
0
    }
1145
1146
6.11k
    PyObject *value;
1147
6.11k
    int rc = PyObject_GetOptionalAttr(spec, &_Py_ID(_uninitialized_submodules), &value);
1148
6.11k
    if (rc > 0) {
1149
5.92k
        rc = PySequence_Contains(value, name);
1150
5.92k
        Py_DECREF(value);
1151
5.92k
    }
1152
6.11k
    return rc;
1153
6.11k
}
1154
1155
int
1156
_PyModuleSpec_GetFileOrigin(PyObject *spec, PyObject **p_origin)
1157
6.16k
{
1158
6.16k
    PyObject *has_location = NULL;
1159
6.16k
    int rc = PyObject_GetOptionalAttr(spec, &_Py_ID(has_location), &has_location);
1160
6.16k
    if (rc <= 0) {
1161
192
        return rc;
1162
192
    }
1163
    // If origin is not a location, or doesn't exist, or is not a str, we could consider falling
1164
    // back to module.__file__. But the cases in which module.__file__ is not __spec__.origin
1165
    // are cases in which we probably shouldn't be guessing.
1166
5.96k
    rc = PyObject_IsTrue(has_location);
1167
5.96k
    Py_DECREF(has_location);
1168
5.96k
    if (rc <= 0) {
1169
64
        return rc;
1170
64
    }
1171
    // has_location is true, so origin is a location
1172
5.90k
    PyObject *origin = NULL;
1173
5.90k
    rc = PyObject_GetOptionalAttr(spec, &_Py_ID(origin), &origin);
1174
5.90k
    if (rc <= 0) {
1175
0
        return rc;
1176
0
    }
1177
5.90k
    assert(origin != NULL);
1178
5.90k
    if (!PyUnicode_Check(origin)) {
1179
0
        Py_DECREF(origin);
1180
0
        return 0;
1181
0
    }
1182
5.90k
    *p_origin = origin;
1183
5.90k
    return 1;
1184
5.90k
}
1185
1186
int
1187
_PyModule_IsPossiblyShadowing(PyObject *origin)
1188
6.16k
{
1189
    // origin must be a unicode subtype
1190
    // Returns 1 if the module at origin could be shadowing a module of the
1191
    // same name later in the module search path. The condition we check is basically:
1192
    // root = os.path.dirname(origin.removesuffix(os.sep + "__init__.py"))
1193
    // return not sys.flags.safe_path and root == (sys.path[0] or os.getcwd())
1194
    // Returns 0 otherwise (or if we aren't sure)
1195
    // Returns -1 if an error occurred that should be propagated
1196
6.16k
    if (origin == NULL) {
1197
256
        return 0;
1198
256
    }
1199
1200
    // not sys.flags.safe_path
1201
5.90k
    const PyConfig *config = _Py_GetConfig();
1202
5.90k
    if (config->safe_path) {
1203
0
        return 0;
1204
0
    }
1205
1206
    // root = os.path.dirname(origin.removesuffix(os.sep + "__init__.py"))
1207
5.90k
    wchar_t root[MAXPATHLEN + 1];
1208
5.90k
    Py_ssize_t size = PyUnicode_AsWideChar(origin, root, MAXPATHLEN);
1209
5.90k
    if (size < 0) {
1210
0
        return -1;
1211
0
    }
1212
5.90k
    assert(size <= MAXPATHLEN);
1213
5.90k
    root[size] = L'\0';
1214
1215
5.90k
    wchar_t *sep = wcsrchr(root, SEP);
1216
5.90k
    if (sep == NULL) {
1217
0
        return 0;
1218
0
    }
1219
    // If it's a package then we need to look one directory further up
1220
5.90k
    if (wcscmp(sep + 1, L"__init__.py") == 0) {
1221
0
        *sep = L'\0';
1222
0
        sep = wcsrchr(root, SEP);
1223
0
        if (sep == NULL) {
1224
0
            return 0;
1225
0
        }
1226
0
    }
1227
5.90k
    *sep = L'\0';
1228
1229
    // sys.path[0] or os.getcwd()
1230
5.90k
    wchar_t *sys_path_0 = config->sys_path_0;
1231
5.90k
    if (!sys_path_0) {
1232
5.90k
        return 0;
1233
5.90k
    }
1234
1235
0
    wchar_t sys_path_0_buf[MAXPATHLEN];
1236
0
    if (sys_path_0[0] == L'\0') {
1237
        // if sys.path[0] == "", treat it as if it were the current directory
1238
0
        if (!_Py_wgetcwd(sys_path_0_buf, MAXPATHLEN)) {
1239
            // If we failed to getcwd, don't raise an exception and instead
1240
            // let the caller proceed assuming no shadowing
1241
0
            return 0;
1242
0
        }
1243
0
        sys_path_0 = sys_path_0_buf;
1244
0
    }
1245
1246
0
    int result = wcscmp(sys_path_0, root) == 0;
1247
0
    return result;
1248
0
}
1249
1250
PyObject*
1251
_Py_module_getattro_impl(PyModuleObject *m, PyObject *name, int suppress)
1252
3.01M
{
1253
    // When suppress=1, this function suppresses AttributeError.
1254
3.01M
    PyObject *attr, *mod_name, *getattr;
1255
3.01M
    attr = _PyObject_GenericGetAttrWithDict((PyObject *)m, name, NULL, suppress);
1256
3.01M
    if (attr) {
1257
2.99M
        return attr;
1258
2.99M
    }
1259
16.6k
    if (suppress == 1) {
1260
10.5k
        if (PyErr_Occurred()) {
1261
            // pass up non-AttributeError exception
1262
0
            return NULL;
1263
0
        }
1264
10.5k
    }
1265
6.11k
    else {
1266
6.11k
        if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
1267
            // pass up non-AttributeError exception
1268
0
            return NULL;
1269
0
        }
1270
6.11k
        PyErr_Clear();
1271
6.11k
    }
1272
16.6k
    assert(m->md_dict != NULL);
1273
16.6k
    if (PyDict_GetItemRef(m->md_dict, &_Py_ID(__getattr__), &getattr) < 0) {
1274
0
        return NULL;
1275
0
    }
1276
16.6k
    if (getattr) {
1277
66
        PyObject *result = PyObject_CallOneArg(getattr, name);
1278
66
        if (result == NULL && suppress == 1 && PyErr_ExceptionMatches(PyExc_AttributeError)) {
1279
            // suppress AttributeError
1280
66
            PyErr_Clear();
1281
66
        }
1282
66
        Py_DECREF(getattr);
1283
66
        return result;
1284
66
    }
1285
1286
    // The attribute was not found.  We make a best effort attempt at a useful error message,
1287
    // but only if we're not suppressing AttributeError.
1288
16.6k
    if (suppress == 1) {
1289
10.5k
        return NULL;
1290
10.5k
    }
1291
6.11k
    if (PyDict_GetItemRef(m->md_dict, &_Py_ID(__name__), &mod_name) < 0) {
1292
0
        return NULL;
1293
0
    }
1294
6.11k
    if (!mod_name || !PyUnicode_Check(mod_name)) {
1295
0
        Py_XDECREF(mod_name);
1296
0
        PyErr_Format(PyExc_AttributeError,
1297
0
                    "module has no attribute '%U'", name);
1298
0
        return NULL;
1299
0
    }
1300
6.11k
    PyObject *spec;
1301
6.11k
    if (PyDict_GetItemRef(m->md_dict, &_Py_ID(__spec__), &spec) < 0) {
1302
0
        Py_DECREF(mod_name);
1303
0
        return NULL;
1304
0
    }
1305
6.11k
    if (spec == NULL) {
1306
0
        PyErr_Format(PyExc_AttributeError,
1307
0
                     "module '%U' has no attribute '%U'",
1308
0
                     mod_name, name);
1309
0
        Py_DECREF(mod_name);
1310
0
        return NULL;
1311
0
    }
1312
1313
6.11k
    PyObject *origin = NULL;
1314
6.11k
    if (_PyModuleSpec_GetFileOrigin(spec, &origin) < 0) {
1315
0
        goto done;
1316
0
    }
1317
1318
6.11k
    int is_possibly_shadowing = _PyModule_IsPossiblyShadowing(origin);
1319
6.11k
    if (is_possibly_shadowing < 0) {
1320
0
        goto done;
1321
0
    }
1322
6.11k
    int is_possibly_shadowing_stdlib = 0;
1323
6.11k
    if (is_possibly_shadowing) {
1324
0
        PyObject *stdlib_modules;
1325
0
        if (PySys_GetOptionalAttrString("stdlib_module_names", &stdlib_modules) < 0) {
1326
0
            goto done;
1327
0
        }
1328
0
        if (stdlib_modules && PyAnySet_Check(stdlib_modules)) {
1329
0
            is_possibly_shadowing_stdlib = PySet_Contains(stdlib_modules, mod_name);
1330
0
            if (is_possibly_shadowing_stdlib < 0) {
1331
0
                Py_DECREF(stdlib_modules);
1332
0
                goto done;
1333
0
            }
1334
0
        }
1335
0
        Py_XDECREF(stdlib_modules);
1336
0
    }
1337
1338
6.11k
    if (is_possibly_shadowing_stdlib) {
1339
0
        assert(origin);
1340
0
        PyErr_Format(PyExc_AttributeError,
1341
0
                    "module '%U' has no attribute '%U' "
1342
0
                    "(consider renaming '%U' since it has the same "
1343
0
                    "name as the standard library module named '%U' "
1344
0
                    "and prevents importing that standard library module)",
1345
0
                    mod_name, name, origin, mod_name);
1346
0
    }
1347
6.11k
    else {
1348
6.11k
        int rc = _PyModuleSpec_IsInitializing(spec);
1349
6.11k
        if (rc < 0) {
1350
0
            goto done;
1351
0
        }
1352
6.11k
        else if (rc > 0) {
1353
0
            if (is_possibly_shadowing) {
1354
0
                assert(origin);
1355
                // For non-stdlib modules, only mention the possibility of
1356
                // shadowing if the module is being initialized.
1357
0
                PyErr_Format(PyExc_AttributeError,
1358
0
                            "module '%U' has no attribute '%U' "
1359
0
                            "(consider renaming '%U' if it has the same name "
1360
0
                            "as a library you intended to import)",
1361
0
                            mod_name, name, origin);
1362
0
            }
1363
0
            else if (origin) {
1364
0
                PyErr_Format(PyExc_AttributeError,
1365
0
                            "partially initialized "
1366
0
                            "module '%U' from '%U' has no attribute '%U' "
1367
0
                            "(most likely due to a circular import)",
1368
0
                            mod_name, origin, name);
1369
0
            }
1370
0
            else {
1371
0
                PyErr_Format(PyExc_AttributeError,
1372
0
                            "partially initialized "
1373
0
                            "module '%U' has no attribute '%U' "
1374
0
                            "(most likely due to a circular import)",
1375
0
                            mod_name, name);
1376
0
            }
1377
0
        }
1378
6.11k
        else {
1379
6.11k
            assert(rc == 0);
1380
6.11k
            rc = _PyModuleSpec_IsUninitializedSubmodule(spec, name);
1381
6.11k
            if (rc > 0) {
1382
0
                PyErr_Format(PyExc_AttributeError,
1383
0
                            "cannot access submodule '%U' of module '%U' "
1384
0
                            "(most likely due to a circular import)",
1385
0
                            name, mod_name);
1386
0
            }
1387
6.11k
            else if (rc == 0) {
1388
6.11k
                PyErr_Format(PyExc_AttributeError,
1389
6.11k
                            "module '%U' has no attribute '%U'",
1390
6.11k
                            mod_name, name);
1391
6.11k
            }
1392
6.11k
        }
1393
6.11k
    }
1394
1395
6.11k
done:
1396
6.11k
    Py_XDECREF(origin);
1397
6.11k
    Py_DECREF(spec);
1398
6.11k
    Py_DECREF(mod_name);
1399
6.11k
    return NULL;
1400
6.11k
}
1401
1402
1403
PyObject*
1404
_Py_module_getattro(PyObject *self, PyObject *name)
1405
2.93M
{
1406
2.93M
    PyModuleObject *m = _PyModule_CAST(self);
1407
2.93M
    return _Py_module_getattro_impl(m, name, 0);
1408
2.93M
}
1409
1410
static int
1411
module_traverse(PyObject *self, visitproc visit, void *arg)
1412
102k
{
1413
102k
    PyModuleObject *m = _PyModule_CAST(self);
1414
1415
102k
    assert_def_missing_or_redundant(m);
1416
    /* bpo-39824: Don't call m_traverse() if m_size > 0 and md_state=NULL */
1417
102k
    if (m->md_state_traverse && (m->md_state_size <= 0 || m->md_state != NULL))
1418
13.6k
    {
1419
13.6k
        int res = m->md_state_traverse((PyObject*)m, visit, arg);
1420
13.6k
        if (res)
1421
0
            return res;
1422
13.6k
    }
1423
1424
102k
    Py_VISIT(m->md_dict);
1425
102k
    return 0;
1426
102k
}
1427
1428
static int
1429
module_clear(PyObject *self)
1430
0
{
1431
0
    PyModuleObject *m = _PyModule_CAST(self);
1432
1433
0
    assert_def_missing_or_redundant(m);
1434
    /* bpo-39824: Don't call m_clear() if m_size > 0 and md_state=NULL */
1435
0
    if (m->md_state_clear && (m->md_state_size <= 0 || m->md_state != NULL))
1436
0
    {
1437
0
        int res = m->md_state_clear((PyObject*)m);
1438
0
        if (PyErr_Occurred()) {
1439
0
            PyErr_FormatUnraisable("Exception ignored in m_clear of module%s%V",
1440
0
                                   m->md_name ? " " : "",
1441
0
                                   m->md_name, "");
1442
0
        }
1443
0
        if (res) {
1444
0
            return res;
1445
0
        }
1446
0
    }
1447
0
    Py_CLEAR(m->md_dict);
1448
0
    return 0;
1449
0
}
1450
1451
static PyObject *
1452
module_dir(PyObject *self, PyObject *args)
1453
20
{
1454
20
    PyObject *result = NULL;
1455
20
    PyObject *dict = PyObject_GetAttr(self, &_Py_ID(__dict__));
1456
1457
20
    if (dict != NULL) {
1458
20
        if (PyDict_Check(dict)) {
1459
20
            PyObject *dirfunc = PyDict_GetItemWithError(dict, &_Py_ID(__dir__));
1460
20
            if (dirfunc) {
1461
0
                result = _PyObject_CallNoArgs(dirfunc);
1462
0
            }
1463
20
            else if (!PyErr_Occurred()) {
1464
20
                result = PyDict_Keys(dict);
1465
20
            }
1466
20
        }
1467
0
        else {
1468
0
            PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary");
1469
0
        }
1470
20
    }
1471
1472
20
    Py_XDECREF(dict);
1473
20
    return result;
1474
20
}
1475
1476
static PyMethodDef module_methods[] = {
1477
    {"__dir__", module_dir, METH_NOARGS,
1478
     PyDoc_STR("__dir__() -> list\nspecialized dir() implementation")},
1479
    {0}
1480
};
1481
1482
static PyObject *
1483
module_get_dict(PyModuleObject *m)
1484
0
{
1485
0
    PyObject *dict = PyObject_GetAttr((PyObject *)m, &_Py_ID(__dict__));
1486
0
    if (dict == NULL) {
1487
0
        return NULL;
1488
0
    }
1489
0
    if (!PyDict_Check(dict)) {
1490
0
        PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary");
1491
0
        Py_DECREF(dict);
1492
0
        return NULL;
1493
0
    }
1494
0
    return dict;
1495
0
}
1496
1497
static PyObject *
1498
module_get_annotate(PyObject *self, void *Py_UNUSED(ignored))
1499
0
{
1500
0
    PyModuleObject *m = _PyModule_CAST(self);
1501
1502
0
    PyObject *dict = module_get_dict(m);
1503
0
    if (dict == NULL) {
1504
0
        return NULL;
1505
0
    }
1506
1507
0
    PyObject *annotate;
1508
0
    if (PyDict_GetItemRef(dict, &_Py_ID(__annotate__), &annotate) == 0) {
1509
0
        annotate = Py_None;
1510
0
        if (PyDict_SetItem(dict, &_Py_ID(__annotate__), annotate) == -1) {
1511
0
            Py_CLEAR(annotate);
1512
0
        }
1513
0
    }
1514
0
    Py_DECREF(dict);
1515
0
    return annotate;
1516
0
}
1517
1518
static int
1519
module_set_annotate(PyObject *self, PyObject *value, void *Py_UNUSED(ignored))
1520
0
{
1521
0
    PyModuleObject *m = _PyModule_CAST(self);
1522
0
    if (value == NULL) {
1523
0
        PyErr_SetString(PyExc_TypeError, "cannot delete __annotate__ attribute");
1524
0
        return -1;
1525
0
    }
1526
1527
0
    PyObject *dict = module_get_dict(m);
1528
0
    if (dict == NULL) {
1529
0
        return -1;
1530
0
    }
1531
1532
0
    if (!Py_IsNone(value) && !PyCallable_Check(value)) {
1533
0
        PyErr_SetString(PyExc_TypeError, "__annotate__ must be callable or None");
1534
0
        Py_DECREF(dict);
1535
0
        return -1;
1536
0
    }
1537
1538
0
    if (PyDict_SetItem(dict, &_Py_ID(__annotate__), value) == -1) {
1539
0
        Py_DECREF(dict);
1540
0
        return -1;
1541
0
    }
1542
0
    if (!Py_IsNone(value)) {
1543
0
        if (PyDict_Pop(dict, &_Py_ID(__annotations__), NULL) == -1) {
1544
0
            Py_DECREF(dict);
1545
0
            return -1;
1546
0
        }
1547
0
    }
1548
0
    Py_DECREF(dict);
1549
0
    return 0;
1550
0
}
1551
1552
static PyObject *
1553
module_get_annotations(PyObject *self, void *Py_UNUSED(ignored))
1554
0
{
1555
0
    PyModuleObject *m = _PyModule_CAST(self);
1556
1557
0
    PyObject *dict = module_get_dict(m);
1558
0
    if (dict == NULL) {
1559
0
        return NULL;
1560
0
    }
1561
1562
0
    PyObject *annotations;
1563
0
    if (PyDict_GetItemRef(dict, &_Py_ID(__annotations__), &annotations) == 0) {
1564
0
        PyObject *spec;
1565
0
        if (PyDict_GetItemRef(m->md_dict, &_Py_ID(__spec__), &spec) < 0) {
1566
0
            Py_DECREF(dict);
1567
0
            return NULL;
1568
0
        }
1569
0
        bool is_initializing = false;
1570
0
        if (spec != NULL) {
1571
0
            int rc = _PyModuleSpec_IsInitializing(spec);
1572
0
            if (rc < 0) {
1573
0
                Py_DECREF(spec);
1574
0
                Py_DECREF(dict);
1575
0
                return NULL;
1576
0
            }
1577
0
            Py_DECREF(spec);
1578
0
            if (rc) {
1579
0
                is_initializing = true;
1580
0
            }
1581
0
        }
1582
1583
0
        PyObject *annotate;
1584
0
        int annotate_result = PyDict_GetItemRef(dict, &_Py_ID(__annotate__), &annotate);
1585
0
        if (annotate_result < 0) {
1586
0
            Py_DECREF(dict);
1587
0
            return NULL;
1588
0
        }
1589
0
        if (annotate_result == 1 && PyCallable_Check(annotate)) {
1590
0
            PyObject *one = _PyLong_GetOne();
1591
0
            annotations = _PyObject_CallOneArg(annotate, one);
1592
0
            if (annotations == NULL) {
1593
0
                Py_DECREF(annotate);
1594
0
                Py_DECREF(dict);
1595
0
                return NULL;
1596
0
            }
1597
0
            if (!PyDict_Check(annotations)) {
1598
0
                PyErr_Format(PyExc_TypeError,
1599
0
                             "__annotate__() must return a dict, not %T",
1600
0
                             annotations);
1601
0
                Py_DECREF(annotate);
1602
0
                Py_DECREF(annotations);
1603
0
                Py_DECREF(dict);
1604
0
                return NULL;
1605
0
            }
1606
0
        }
1607
0
        else {
1608
0
            annotations = PyDict_New();
1609
0
        }
1610
0
        Py_XDECREF(annotate);
1611
        // Do not cache annotations if the module is still initializing
1612
0
        if (annotations && !is_initializing) {
1613
0
            int result = PyDict_SetItem(
1614
0
                    dict, &_Py_ID(__annotations__), annotations);
1615
0
            if (result) {
1616
0
                Py_CLEAR(annotations);
1617
0
            }
1618
0
        }
1619
0
    }
1620
0
    Py_DECREF(dict);
1621
0
    return annotations;
1622
0
}
1623
1624
static int
1625
module_set_annotations(PyObject *self, PyObject *value, void *Py_UNUSED(ignored))
1626
0
{
1627
0
    PyModuleObject *m = _PyModule_CAST(self);
1628
1629
0
    PyObject *dict = module_get_dict(m);
1630
0
    if (dict == NULL) {
1631
0
        return -1;
1632
0
    }
1633
1634
0
    int ret = -1;
1635
0
    if (value != NULL) {
1636
        /* set */
1637
0
        ret = PyDict_SetItem(dict, &_Py_ID(__annotations__), value);
1638
0
    }
1639
0
    else {
1640
        /* delete */
1641
0
        ret = PyDict_Pop(dict, &_Py_ID(__annotations__), NULL);
1642
0
        if (ret == 0) {
1643
0
            PyErr_SetObject(PyExc_AttributeError, &_Py_ID(__annotations__));
1644
0
            ret = -1;
1645
0
        }
1646
0
        else if (ret > 0) {
1647
0
            ret = 0;
1648
0
        }
1649
0
    }
1650
0
    if (ret == 0 && PyDict_Pop(dict, &_Py_ID(__annotate__), NULL) < 0) {
1651
0
        ret = -1;
1652
0
    }
1653
1654
0
    Py_DECREF(dict);
1655
0
    return ret;
1656
0
}
1657
1658
1659
static PyGetSetDef module_getsets[] = {
1660
    {"__annotations__", module_get_annotations, module_set_annotations},
1661
    {"__annotate__", module_get_annotate, module_set_annotate},
1662
    {NULL}
1663
};
1664
1665
PyTypeObject PyModule_Type = {
1666
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
1667
    "module",                                   /* tp_name */
1668
    sizeof(PyModuleObject),                     /* tp_basicsize */
1669
    0,                                          /* tp_itemsize */
1670
    module_dealloc,                             /* tp_dealloc */
1671
    0,                                          /* tp_vectorcall_offset */
1672
    0,                                          /* tp_getattr */
1673
    0,                                          /* tp_setattr */
1674
    0,                                          /* tp_as_async */
1675
    module_repr,                                /* tp_repr */
1676
    0,                                          /* tp_as_number */
1677
    0,                                          /* tp_as_sequence */
1678
    0,                                          /* tp_as_mapping */
1679
    0,                                          /* tp_hash */
1680
    0,                                          /* tp_call */
1681
    0,                                          /* tp_str */
1682
    _Py_module_getattro,                        /* tp_getattro */
1683
    PyObject_GenericSetAttr,                    /* tp_setattro */
1684
    0,                                          /* tp_as_buffer */
1685
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1686
        Py_TPFLAGS_BASETYPE,                    /* tp_flags */
1687
    module___init____doc__,                     /* tp_doc */
1688
    module_traverse,                            /* tp_traverse */
1689
    module_clear,                               /* tp_clear */
1690
    0,                                          /* tp_richcompare */
1691
    offsetof(PyModuleObject, md_weaklist),      /* tp_weaklistoffset */
1692
    0,                                          /* tp_iter */
1693
    0,                                          /* tp_iternext */
1694
    module_methods,                             /* tp_methods */
1695
    module_members,                             /* tp_members */
1696
    module_getsets,                             /* tp_getset */
1697
    0,                                          /* tp_base */
1698
    0,                                          /* tp_dict */
1699
    0,                                          /* tp_descr_get */
1700
    0,                                          /* tp_descr_set */
1701
    offsetof(PyModuleObject, md_dict),          /* tp_dictoffset */
1702
    module___init__,                            /* tp_init */
1703
    0,                                          /* tp_alloc */
1704
    new_module,                                 /* tp_new */
1705
    PyObject_GC_Del,                            /* tp_free */
1706
};