Coverage Report

Created: 2025-11-24 06:11

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.57M
    (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
132k
{
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
132k
}
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
1.47k
{
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
1.47k
    assert(PyModuleDef_Type.tp_flags & Py_TPFLAGS_READY);
136
1.47k
    if (def->m_base.m_index == 0) {
137
795
        Py_SET_REFCNT(def, 1);
138
795
        Py_SET_TYPE(def, &PyModuleDef_Type);
139
795
        def->m_base.m_index = _PyImport_GetNextModuleIndex();
140
795
    }
141
1.47k
    return (PyObject*)def;
142
1.47k
}
143
144
static int
145
module_init_dict(PyModuleObject *mod, PyObject *md_dict,
146
                 PyObject *name, PyObject *doc)
147
7.70k
{
148
7.70k
    assert(md_dict != NULL);
149
7.70k
    if (doc == NULL)
150
883
        doc = Py_None;
151
152
7.70k
    if (PyDict_SetItem(md_dict, &_Py_ID(__name__), name) != 0)
153
0
        return -1;
154
7.70k
    if (PyDict_SetItem(md_dict, &_Py_ID(__doc__), doc) != 0)
155
0
        return -1;
156
7.70k
    if (PyDict_SetItem(md_dict, &_Py_ID(__package__), Py_None) != 0)
157
0
        return -1;
158
7.70k
    if (PyDict_SetItem(md_dict, &_Py_ID(__loader__), Py_None) != 0)
159
0
        return -1;
160
7.70k
    if (PyDict_SetItem(md_dict, &_Py_ID(__spec__), Py_None) != 0)
161
0
        return -1;
162
7.70k
    if (PyUnicode_CheckExact(name)) {
163
7.70k
        Py_XSETREF(mod->md_name, Py_NewRef(name));
164
7.70k
    }
165
166
7.70k
    return 0;
167
7.70k
}
168
169
static PyModuleObject *
170
new_module_notrack(PyTypeObject *mt)
171
7.70k
{
172
7.70k
    PyModuleObject *m;
173
7.70k
    m = (PyModuleObject *)_PyType_AllocNoTrack(mt, 0);
174
7.70k
    if (m == NULL)
175
0
        return NULL;
176
7.70k
    m->md_state = NULL;
177
7.70k
    m->md_weaklist = NULL;
178
7.70k
    m->md_name = NULL;
179
7.70k
    m->md_token_is_def = false;
180
#ifdef Py_GIL_DISABLED
181
    m->md_requires_gil = true;
182
#endif
183
7.70k
    m->md_state_size = 0;
184
7.70k
    m->md_state_traverse = NULL;
185
7.70k
    m->md_state_clear = NULL;
186
7.70k
    m->md_state_free = NULL;
187
7.70k
    m->md_exec = NULL;
188
7.70k
    m->md_token = NULL;
189
7.70k
    m->md_dict = PyDict_New();
190
7.70k
    if (m->md_dict == NULL) {
191
0
        Py_DECREF(m);
192
0
        return NULL;
193
0
    }
194
7.70k
    return m;
195
7.70k
}
196
197
static void
198
track_module(PyModuleObject *m)
199
7.70k
{
200
7.70k
    _PyDict_EnablePerThreadRefcounting(m->md_dict);
201
7.70k
    _PyObject_SetDeferredRefcount((PyObject *)m);
202
7.70k
    PyObject_GC_Track(m);
203
7.70k
}
204
205
static PyObject *
206
new_module(PyTypeObject *mt, PyObject *args, PyObject *kws)
207
6.82k
{
208
6.82k
    PyModuleObject *m = new_module_notrack(mt);
209
6.82k
    if (m != NULL) {
210
6.82k
        track_module(m);
211
6.82k
    }
212
6.82k
    return (PyObject *)m;
213
6.82k
}
214
215
PyObject *
216
PyModule_NewObject(PyObject *name)
217
883
{
218
883
    PyModuleObject *m = new_module_notrack(&PyModule_Type);
219
883
    if (m == NULL)
220
0
        return NULL;
221
883
    if (module_init_dict(m, m->md_dict, name, NULL) != 0)
222
0
        goto fail;
223
883
    track_module(m);
224
883
    return (PyObject *)m;
225
226
0
 fail:
227
0
    Py_DECREF(m);
228
0
    return NULL;
229
883
}
230
231
PyObject *
232
PyModule_New(const char *name)
233
144
{
234
144
    PyObject *nameobj, *module;
235
144
    nameobj = PyUnicode_FromString(name);
236
144
    if (nameobj == NULL)
237
0
        return NULL;
238
144
    module = PyModule_NewObject(nameobj);
239
144
    Py_DECREF(nameobj);
240
144
    return module;
241
144
}
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
795
{
250
795
    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
795
    return 1;
261
795
}
262
263
static int
264
_add_methods_to_object(PyObject *module, PyObject *name, PyMethodDef *functions)
265
751
{
266
751
    PyObject *func;
267
751
    PyMethodDef *fdef;
268
269
16.4k
    for (fdef = functions; fdef->ml_name != NULL; fdef++) {
270
15.7k
        if ((fdef->ml_flags & METH_CLASS) ||
271
15.7k
            (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
15.7k
        func = PyCFunction_NewEx(fdef, (PyObject*)module, name);
278
15.7k
        if (func == NULL) {
279
0
            return -1;
280
0
        }
281
15.7k
        _PyObject_SetDeferredRefcount(func);
282
15.7k
        if (PyObject_SetAttrString(module, fdef->ml_name, func) != 0) {
283
0
            Py_DECREF(func);
284
0
            return -1;
285
0
        }
286
15.7k
        Py_DECREF(func);
287
15.7k
    }
288
289
751
    return 0;
290
751
}
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
795
{
308
795
    md->md_state_size = def_like->m_size;
309
795
    md->md_state_traverse = def_like->m_traverse;
310
795
    md->md_state_clear = def_like->m_clear;
311
795
    md->md_state_free = def_like->m_free;
312
795
}
313
314
PyObject *
315
_PyModule_CreateInitialized(PyModuleDef* module, int module_api_version)
316
112
{
317
112
    const char* name;
318
112
    PyModuleObject *m;
319
320
112
    if (!PyModuleDef_Init(module))
321
0
        return NULL;
322
112
    name = module->m_name;
323
112
    if (!check_api_version(name, module_api_version)) {
324
0
        return NULL;
325
0
    }
326
112
    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
112
    name = _PyImport_ResolveNameWithPackageContext(name);
333
334
112
    m = (PyModuleObject*)PyModule_New(name);
335
112
    if (m == NULL)
336
0
        return NULL;
337
338
112
    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
112
    if (module->m_methods != NULL) {
349
112
        if (PyModule_AddFunctions((PyObject *) m, module->m_methods) != 0) {
350
0
            Py_DECREF(m);
351
0
            return NULL;
352
0
        }
353
112
    }
354
112
    if (module->m_doc != NULL) {
355
84
        if (PyModule_SetDocString((PyObject *) m, module->m_doc) != 0) {
356
0
            Py_DECREF(m);
357
0
            return NULL;
358
0
        }
359
84
    }
360
112
    m->md_token = module;
361
112
    m->md_token_is_def = true;
362
112
    module_copy_members_from_deflike(m, module);
363
#ifdef Py_GIL_DISABLED
364
    m->md_requires_gil = true;
365
#endif
366
112
    return (PyObject*)m;
367
112
}
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
683
{
376
683
    PyModuleDef_Slot* cur_slot;
377
683
    PyObject *(*create)(PyObject *, PyModuleDef*) = NULL;
378
683
    PyObject *nameobj;
379
683
    PyObject *m = NULL;
380
683
    int has_multiple_interpreters_slot = 0;
381
683
    void *multiple_interpreters = (void *)0;
382
683
    int has_gil_slot = 0;
383
683
    bool requires_gil = true;
384
683
    int has_execution_slots = 0;
385
683
    const char *name;
386
683
    int ret;
387
683
    void *token = NULL;
388
683
    _Py_modexecfunc m_exec = NULL;
389
683
    PyInterpreterState *interp = _PyInterpreterState_GET();
390
391
683
    nameobj = PyObject_GetAttrString(spec, "name");
392
683
    if (nameobj == NULL) {
393
0
        return NULL;
394
0
    }
395
683
    name = PyUnicode_AsUTF8(nameobj);
396
683
    if (name == NULL) {
397
0
        goto error;
398
0
    }
399
400
683
    if (!check_api_version(name, module_api_version)) {
401
0
        goto error;
402
0
    }
403
404
683
    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
2.74k
    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
2.06k
#define COPY_COMMON_SLOT(SLOT, TYPE, DEST)                              \
416
2.06k
        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
2.06k
        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
689
            case Py_mod_exec:
453
689
                has_execution_slots = 1;
454
689
                if (!original_def) {
455
0
                    COPY_COMMON_SLOT(Py_mod_exec, _Py_modexecfunc, m_exec);
456
0
                }
457
689
                break;
458
689
            case Py_mod_multiple_interpreters:
459
683
                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
683
                multiple_interpreters = cur_slot->value;
467
683
                has_multiple_interpreters_slot = 1;
468
683
                break;
469
683
            case Py_mod_gil:
470
683
                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
683
                requires_gil = (cur_slot->value != Py_MOD_GIL_NOT_USED);
478
683
                has_gil_slot = 1;
479
683
                break;
480
10
            case Py_mod_abi:
481
10
                if (PyABIInfo_Check((PyABIInfo *)cur_slot->value, name) < 0) {
482
0
                    goto error;
483
0
                }
484
10
                break;
485
10
            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
2.06k
        }
522
2.06k
#undef COPY_COMMON_SLOT
523
2.06k
    }
524
525
    /* By default, multi-phase init modules are expected
526
       to work under multiple interpreters. */
527
683
    if (!has_multiple_interpreters_slot) {
528
0
        multiple_interpreters = Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED;
529
0
    }
530
683
    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
683
    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
683
    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
683
    } else {
565
683
        m = PyModule_NewObject(nameobj);
566
683
        if (m == NULL) {
567
0
            goto error;
568
0
        }
569
683
    }
570
571
683
    if (PyModule_Check(m)) {
572
683
        PyModuleObject *mod = (PyModuleObject*)m;
573
683
        mod->md_state = NULL;
574
683
        module_copy_members_from_deflike(mod, def_like);
575
683
        if (original_def) {
576
683
            assert (!token);
577
683
            mod->md_token = original_def;
578
683
            mod->md_token_is_def = 1;
579
683
        }
580
0
        else {
581
0
            mod->md_token = token;
582
0
        }
583
#ifdef Py_GIL_DISABLED
584
        mod->md_requires_gil = requires_gil;
585
#else
586
683
        (void)requires_gil;
587
683
#endif
588
683
        mod->md_exec = m_exec;
589
683
    } 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
683
    if (def_like->m_methods != NULL) {
618
639
        ret = _add_methods_to_object(m, nameobj, def_like->m_methods);
619
639
        if (ret != 0) {
620
0
            goto error;
621
0
        }
622
639
    }
623
624
683
    if (def_like->m_doc != NULL) {
625
598
        ret = PyModule_SetDocString(m, def_like->m_doc);
626
598
        if (ret != 0) {
627
0
            goto error;
628
0
        }
629
598
    }
630
631
683
    Py_DECREF(nameobj);
632
683
    return m;
633
634
0
error:
635
0
    Py_DECREF(nameobj);
636
0
    Py_XDECREF(m);
637
0
    return NULL;
638
683
}
639
640
PyObject *
641
PyModule_FromDefAndSpec2(PyModuleDef* def, PyObject *spec, int module_api_version)
642
683
{
643
683
    PyModuleDef_Init(def);
644
683
    return module_from_def_and_spec(def, spec, module_api_version, def);
645
683
}
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
    bool requires_gil = (gil != Py_MOD_GIL_NOT_USED);
668
    if (!PyModule_Check(module)) {
669
        PyErr_BadInternalCall();
670
        return -1;
671
    }
672
    ((PyModuleObject *)module)->md_requires_gil = requires_gil;
673
    return 0;
674
}
675
#endif
676
677
static int
678
run_exec_func(PyObject *module, int (*exec)(PyObject *))
679
689
{
680
689
    int ret = exec(module);
681
689
    if (ret != 0) {
682
0
        if (!PyErr_Occurred()) {
683
0
            PyErr_Format(
684
0
                PyExc_SystemError,
685
0
                "execution of %R failed without setting an exception",
686
0
                module);
687
0
        }
688
0
        return -1;
689
0
    }
690
689
    if (PyErr_Occurred()) {
691
0
        _PyErr_FormatFromCause(
692
0
            PyExc_SystemError,
693
0
            "execution of module %R raised unreported exception",
694
0
            module);
695
0
        return -1;
696
0
    }
697
689
    return 0;
698
689
}
699
700
static int
701
alloc_state(PyObject *module)
702
1.36k
{
703
1.36k
    if (!PyModule_Check(module)) {
704
0
        PyErr_Format(PyExc_TypeError, "expected module, got %T", module);
705
0
        return -1;
706
0
    }
707
1.36k
    PyModuleObject *md = (PyModuleObject*)module;
708
709
1.36k
    if (md->md_state_size >= 0) {
710
1.36k
        if (md->md_state == NULL) {
711
            /* Always set a state pointer; this serves as a marker to skip
712
             * multiple initialization (importlib.reload() is no-op) */
713
683
            md->md_state = PyMem_Malloc(md->md_state_size);
714
683
            if (!md->md_state) {
715
0
                PyErr_NoMemory();
716
0
                return -1;
717
0
            }
718
683
            memset(md->md_state, 0, md->md_state_size);
719
683
        }
720
1.36k
    }
721
1.36k
    return 0;
722
1.36k
}
723
724
int
725
PyModule_Exec(PyObject *module)
726
683
{
727
683
    if (alloc_state(module) < 0) {
728
0
        return -1;
729
0
    }
730
683
    PyModuleObject *md = (PyModuleObject*)module;
731
683
    if (md->md_exec) {
732
0
        assert(!md->md_token_is_def);
733
0
        return run_exec_func(module, md->md_exec);
734
0
    }
735
736
683
    PyModuleDef *def = _PyModule_GetDefOrNull(module);
737
683
    if (def) {
738
683
        return PyModule_ExecDef(module, def);
739
683
    }
740
0
    return 0;
741
683
}
742
743
int
744
PyModule_ExecDef(PyObject *module, PyModuleDef *def)
745
683
{
746
683
    PyModuleDef_Slot *cur_slot;
747
748
683
    if (alloc_state(module) < 0) {
749
0
        return -1;
750
0
    }
751
752
683
    assert(PyModule_Check(module));
753
754
683
    if (def->m_slots == NULL) {
755
0
        return 0;
756
0
    }
757
758
2.74k
    for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) {
759
2.06k
        if (cur_slot->slot == Py_mod_exec) {
760
689
            int (*func)(PyObject *) = cur_slot->value;
761
689
            if (run_exec_func(module, func) < 0) {
762
0
                return -1;
763
0
            }
764
689
            continue;
765
689
        }
766
2.06k
    }
767
683
    return 0;
768
683
}
769
770
int
771
PyModule_AddFunctions(PyObject *m, PyMethodDef *functions)
772
112
{
773
112
    int res;
774
112
    PyObject *name = PyModule_GetNameObject(m);
775
112
    if (name == NULL) {
776
0
        return -1;
777
0
    }
778
779
112
    res = _add_methods_to_object(m, name, functions);
780
112
    Py_DECREF(name);
781
112
    return res;
782
112
}
783
784
int
785
PyModule_SetDocString(PyObject *m, const char *doc)
786
682
{
787
682
    PyObject *v;
788
789
682
    v = PyUnicode_FromString(doc);
790
682
    if (v == NULL || PyObject_SetAttr(m, &_Py_ID(__doc__), v) != 0) {
791
0
        Py_XDECREF(v);
792
0
        return -1;
793
0
    }
794
682
    Py_DECREF(v);
795
682
    return 0;
796
682
}
797
798
PyObject *
799
PyModule_GetDict(PyObject *m)
800
15.2k
{
801
15.2k
    if (!PyModule_Check(m)) {
802
0
        PyErr_BadInternalCall();
803
0
        return NULL;
804
0
    }
805
15.2k
    return _PyModule_GetDict(m);  // borrowed reference
806
15.2k
}
807
808
int
809
PyModule_GetStateSize(PyObject *m, Py_ssize_t *size_p)
810
0
{
811
0
    *size_p = -1;
812
0
    if (!PyModule_Check(m)) {
813
0
        PyErr_Format(PyExc_TypeError, "expected module, got %T", m);
814
0
        return -1;
815
0
    }
816
0
    PyModuleObject *mod = (PyModuleObject *)m;
817
0
    *size_p = mod->md_state_size;
818
0
    return 0;
819
0
}
820
821
int
822
PyModule_GetToken(PyObject *m, void **token_p)
823
0
{
824
0
    *token_p = NULL;
825
0
    if (!PyModule_Check(m)) {
826
0
        PyErr_Format(PyExc_TypeError, "expected module, got %T", m);
827
0
        return -1;
828
0
    }
829
0
    *token_p = _PyModule_GetToken(m);
830
0
    return 0;
831
0
}
832
833
PyObject*
834
PyModule_GetNameObject(PyObject *mod)
835
112
{
836
112
    if (!PyModule_Check(mod)) {
837
0
        PyErr_BadArgument();
838
0
        return NULL;
839
0
    }
840
112
    PyObject *dict = ((PyModuleObject *)mod)->md_dict;  // borrowed reference
841
112
    if (dict == NULL || !PyDict_Check(dict)) {
842
0
        goto error;
843
0
    }
844
112
    PyObject *name;
845
112
    if (PyDict_GetItemRef(dict, &_Py_ID(__name__), &name) <= 0) {
846
        // error or not found
847
0
        goto error;
848
0
    }
849
112
    if (!PyUnicode_Check(name)) {
850
0
        Py_DECREF(name);
851
0
        goto error;
852
0
    }
853
112
    return name;
854
855
0
error:
856
0
    if (!PyErr_Occurred()) {
857
0
        PyErr_SetString(PyExc_SystemError, "nameless module");
858
0
    }
859
0
    return NULL;
860
112
}
861
862
const char *
863
PyModule_GetName(PyObject *m)
864
0
{
865
0
    PyObject *name = PyModule_GetNameObject(m);
866
0
    if (name == NULL) {
867
0
        return NULL;
868
0
    }
869
0
    assert(Py_REFCNT(name) >= 2);
870
0
    Py_DECREF(name);   /* module dict has still a reference */
871
0
    return PyUnicode_AsUTF8(name);
872
0
}
873
874
PyObject*
875
_PyModule_GetFilenameObject(PyObject *mod)
876
49
{
877
    // We return None to indicate "not found" or "bogus".
878
49
    if (!PyModule_Check(mod)) {
879
0
        PyErr_BadArgument();
880
0
        return NULL;
881
0
    }
882
49
    PyObject *dict = ((PyModuleObject *)mod)->md_dict;  // borrowed reference
883
49
    if (dict == NULL) {
884
        // The module has been tampered with.
885
0
        Py_RETURN_NONE;
886
0
    }
887
49
    PyObject *fileobj;
888
49
    int res = PyDict_GetItemRef(dict, &_Py_ID(__file__), &fileobj);
889
49
    if (res < 0) {
890
0
        return NULL;
891
0
    }
892
49
    if (res == 0) {
893
        // __file__ isn't set.  There are several reasons why this might
894
        // be so, most of them valid reasons.  If it's the __main__
895
        // module then we're running the REPL or with -c.  Otherwise
896
        // it's a namespace package or other module with a loader that
897
        // isn't disk-based.  It could also be that a user created
898
        // a module manually but without manually setting __file__.
899
21
        Py_RETURN_NONE;
900
21
    }
901
28
    if (!PyUnicode_Check(fileobj)) {
902
0
        Py_DECREF(fileobj);
903
0
        Py_RETURN_NONE;
904
0
    }
905
28
    return fileobj;
906
28
}
907
908
PyObject*
909
PyModule_GetFilenameObject(PyObject *mod)
910
49
{
911
49
    PyObject *fileobj = _PyModule_GetFilenameObject(mod);
912
49
    if (fileobj == NULL) {
913
0
        return NULL;
914
0
    }
915
49
    if (fileobj == Py_None) {
916
21
        PyErr_SetString(PyExc_SystemError, "module filename missing");
917
21
        return NULL;
918
21
    }
919
28
    return fileobj;
920
49
}
921
922
const char *
923
PyModule_GetFilename(PyObject *m)
924
0
{
925
0
    PyObject *fileobj;
926
0
    const char *utf8;
927
0
    fileobj = PyModule_GetFilenameObject(m);
928
0
    if (fileobj == NULL)
929
0
        return NULL;
930
0
    utf8 = PyUnicode_AsUTF8(fileobj);
931
0
    Py_DECREF(fileobj);   /* module dict has still a reference */
932
0
    return utf8;
933
0
}
934
935
Py_ssize_t
936
_PyModule_GetFilenameUTF8(PyObject *mod, char *buffer, Py_ssize_t maxlen)
937
0
{
938
    // We "return" an empty string for an invalid module
939
    // and for a missing, empty, or invalid filename.
940
0
    assert(maxlen >= 0);
941
0
    Py_ssize_t size = -1;
942
0
    PyObject *filenameobj = _PyModule_GetFilenameObject(mod);
943
0
    if (filenameobj == NULL) {
944
0
        return -1;
945
0
    }
946
0
    if (filenameobj == Py_None) {
947
        // It is missing or invalid.
948
0
        buffer[0] = '\0';
949
0
        size = 0;
950
0
    }
951
0
    else {
952
0
        const char *filename = PyUnicode_AsUTF8AndSize(filenameobj, &size);
953
0
        assert(size >= 0);
954
0
        if (size > maxlen) {
955
0
            size = -1;
956
0
            PyErr_SetString(PyExc_ValueError, "__file__ too long");
957
0
        }
958
0
        else {
959
0
            (void)strcpy(buffer, filename);
960
0
        }
961
0
    }
962
0
    Py_DECREF(filenameobj);
963
0
    return size;
964
0
}
965
966
PyModuleDef*
967
PyModule_GetDef(PyObject* m)
968
2
{
969
2
    if (!PyModule_Check(m)) {
970
0
        PyErr_BadArgument();
971
0
        return NULL;
972
0
    }
973
2
    return _PyModule_GetDefOrNull(m);
974
2
}
975
976
void*
977
PyModule_GetState(PyObject* m)
978
23.5M
{
979
23.5M
    if (!PyModule_Check(m)) {
980
0
        PyErr_BadArgument();
981
0
        return NULL;
982
0
    }
983
23.5M
    return _PyModule_GetState(m);
984
23.5M
}
985
986
void
987
_PyModule_Clear(PyObject *m)
988
0
{
989
0
    PyObject *d = ((PyModuleObject *)m)->md_dict;
990
0
    if (d != NULL)
991
0
        _PyModule_ClearDict(d);
992
0
}
993
994
void
995
_PyModule_ClearDict(PyObject *d)
996
0
{
997
    /* To make the execution order of destructors for global
998
       objects a bit more predictable, we first zap all objects
999
       whose name starts with a single underscore, before we clear
1000
       the entire dictionary.  We zap them by replacing them with
1001
       None, rather than deleting them from the dictionary, to
1002
       avoid rehashing the dictionary (to some extent). */
1003
1004
0
    Py_ssize_t pos;
1005
0
    PyObject *key, *value;
1006
1007
0
    int verbose = _Py_GetConfig()->verbose;
1008
1009
    /* First, clear only names starting with a single underscore */
1010
0
    pos = 0;
1011
0
    while (PyDict_Next(d, &pos, &key, &value)) {
1012
0
        if (value != Py_None && PyUnicode_Check(key)) {
1013
0
            if (PyUnicode_READ_CHAR(key, 0) == '_' &&
1014
0
                PyUnicode_READ_CHAR(key, 1) != '_') {
1015
0
                if (verbose > 1) {
1016
0
                    const char *s = PyUnicode_AsUTF8(key);
1017
0
                    if (s != NULL)
1018
0
                        PySys_WriteStderr("#   clear[1] %s\n", s);
1019
0
                    else
1020
0
                        PyErr_Clear();
1021
0
                }
1022
0
                if (PyDict_SetItem(d, key, Py_None) != 0) {
1023
0
                    PyErr_FormatUnraisable("Exception ignored while "
1024
0
                                           "clearing module dict");
1025
0
                }
1026
0
            }
1027
0
        }
1028
0
    }
1029
1030
    /* Next, clear all names except for __builtins__ */
1031
0
    pos = 0;
1032
0
    while (PyDict_Next(d, &pos, &key, &value)) {
1033
0
        if (value != Py_None && PyUnicode_Check(key)) {
1034
0
            if (PyUnicode_READ_CHAR(key, 0) != '_' ||
1035
0
                !_PyUnicode_EqualToASCIIString(key, "__builtins__"))
1036
0
            {
1037
0
                if (verbose > 1) {
1038
0
                    const char *s = PyUnicode_AsUTF8(key);
1039
0
                    if (s != NULL)
1040
0
                        PySys_WriteStderr("#   clear[2] %s\n", s);
1041
0
                    else
1042
0
                        PyErr_Clear();
1043
0
                }
1044
0
                if (PyDict_SetItem(d, key, Py_None) != 0) {
1045
0
                    PyErr_FormatUnraisable("Exception ignored while "
1046
0
                                           "clearing module dict");
1047
0
                }
1048
0
            }
1049
0
        }
1050
0
    }
1051
1052
    /* Note: we leave __builtins__ in place, so that destructors
1053
       of non-global objects defined in this module can still use
1054
       builtins, in particularly 'None'. */
1055
1056
0
}
1057
1058
/*[clinic input]
1059
class module "PyModuleObject *" "&PyModule_Type"
1060
[clinic start generated code]*/
1061
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3e35d4f708ecb6af]*/
1062
1063
#include "clinic/moduleobject.c.h"
1064
1065
/* Methods */
1066
1067
/*[clinic input]
1068
module.__init__
1069
    name: unicode
1070
    doc: object = None
1071
1072
Create a module object.
1073
1074
The name must be a string; the optional doc argument can have any type.
1075
[clinic start generated code]*/
1076
1077
static int
1078
module___init___impl(PyModuleObject *self, PyObject *name, PyObject *doc)
1079
/*[clinic end generated code: output=e7e721c26ce7aad7 input=57f9e177401e5e1e]*/
1080
6.82k
{
1081
6.82k
    return module_init_dict(self, self->md_dict, name, doc);
1082
6.82k
}
1083
1084
static void
1085
module_dealloc(PyObject *self)
1086
5.40k
{
1087
5.40k
    PyModuleObject *m = _PyModule_CAST(self);
1088
1089
5.40k
    PyObject_GC_UnTrack(m);
1090
1091
5.40k
    int verbose = _Py_GetConfig()->verbose;
1092
5.40k
    if (verbose && m->md_name) {
1093
0
        PySys_FormatStderr("# destroy %U\n", m->md_name);
1094
0
    }
1095
5.40k
    FT_CLEAR_WEAKREFS(self, m->md_weaklist);
1096
1097
5.40k
    assert_def_missing_or_redundant(m);
1098
    /* bpo-39824: Don't call m_free() if m_size > 0 and md_state=NULL */
1099
5.40k
    if (m->md_state_free && (m->md_state_size <= 0 || m->md_state != NULL))
1100
0
    {
1101
0
        m->md_state_free(m);
1102
0
    }
1103
1104
5.40k
    Py_XDECREF(m->md_dict);
1105
5.40k
    Py_XDECREF(m->md_name);
1106
5.40k
    if (m->md_state != NULL) {
1107
0
        PyMem_Free(m->md_state);
1108
0
    }
1109
5.40k
    Py_TYPE(m)->tp_free((PyObject *)m);
1110
5.40k
}
1111
1112
static PyObject *
1113
module_repr(PyObject *self)
1114
0
{
1115
0
    PyModuleObject *m = _PyModule_CAST(self);
1116
0
    PyInterpreterState *interp = _PyInterpreterState_GET();
1117
0
    return _PyImport_ImportlibModuleRepr(interp, (PyObject *)m);
1118
0
}
1119
1120
/* Check if the "_initializing" attribute of the module spec is set to true.
1121
 */
1122
int
1123
_PyModuleSpec_IsInitializing(PyObject *spec)
1124
2.29M
{
1125
2.29M
    if (spec == NULL) {
1126
0
        return 0;
1127
0
    }
1128
2.29M
    PyObject *value;
1129
2.29M
    int rc = PyObject_GetOptionalAttr(spec, &_Py_ID(_initializing), &value);
1130
2.29M
    if (rc > 0) {
1131
2.13M
        rc = PyObject_IsTrue(value);
1132
2.13M
        Py_DECREF(value);
1133
2.13M
    }
1134
2.29M
    return rc;
1135
2.29M
}
1136
1137
/* Check if the submodule name is in the "_uninitialized_submodules" attribute
1138
   of the module spec.
1139
 */
1140
int
1141
_PyModuleSpec_IsUninitializedSubmodule(PyObject *spec, PyObject *name)
1142
6.07k
{
1143
6.07k
    if (spec == NULL) {
1144
0
         return 0;
1145
0
    }
1146
1147
6.07k
    PyObject *value;
1148
6.07k
    int rc = PyObject_GetOptionalAttr(spec, &_Py_ID(_uninitialized_submodules), &value);
1149
6.07k
    if (rc > 0) {
1150
5.73k
        rc = PySequence_Contains(value, name);
1151
5.73k
        Py_DECREF(value);
1152
5.73k
    }
1153
6.07k
    return rc;
1154
6.07k
}
1155
1156
int
1157
_PyModuleSpec_GetFileOrigin(PyObject *spec, PyObject **p_origin)
1158
6.12k
{
1159
6.12k
    PyObject *has_location = NULL;
1160
6.12k
    int rc = PyObject_GetOptionalAttr(spec, &_Py_ID(has_location), &has_location);
1161
6.12k
    if (rc <= 0) {
1162
336
        return rc;
1163
336
    }
1164
    // If origin is not a location, or doesn't exist, or is not a str, we could consider falling
1165
    // back to module.__file__. But the cases in which module.__file__ is not __spec__.origin
1166
    // are cases in which we probably shouldn't be guessing.
1167
5.78k
    rc = PyObject_IsTrue(has_location);
1168
5.78k
    Py_DECREF(has_location);
1169
5.78k
    if (rc <= 0) {
1170
77
        return rc;
1171
77
    }
1172
    // has_location is true, so origin is a location
1173
5.70k
    PyObject *origin = NULL;
1174
5.70k
    rc = PyObject_GetOptionalAttr(spec, &_Py_ID(origin), &origin);
1175
5.70k
    if (rc <= 0) {
1176
0
        return rc;
1177
0
    }
1178
5.70k
    assert(origin != NULL);
1179
5.70k
    if (!PyUnicode_Check(origin)) {
1180
0
        Py_DECREF(origin);
1181
0
        return 0;
1182
0
    }
1183
5.70k
    *p_origin = origin;
1184
5.70k
    return 1;
1185
5.70k
}
1186
1187
int
1188
_PyModule_IsPossiblyShadowing(PyObject *origin)
1189
6.12k
{
1190
    // origin must be a unicode subtype
1191
    // Returns 1 if the module at origin could be shadowing a module of the
1192
    // same name later in the module search path. The condition we check is basically:
1193
    // root = os.path.dirname(origin.removesuffix(os.sep + "__init__.py"))
1194
    // return not sys.flags.safe_path and root == (sys.path[0] or os.getcwd())
1195
    // Returns 0 otherwise (or if we aren't sure)
1196
    // Returns -1 if an error occurred that should be propagated
1197
6.12k
    if (origin == NULL) {
1198
413
        return 0;
1199
413
    }
1200
1201
    // not sys.flags.safe_path
1202
5.70k
    const PyConfig *config = _Py_GetConfig();
1203
5.70k
    if (config->safe_path) {
1204
0
        return 0;
1205
0
    }
1206
1207
    // root = os.path.dirname(origin.removesuffix(os.sep + "__init__.py"))
1208
5.70k
    wchar_t root[MAXPATHLEN + 1];
1209
5.70k
    Py_ssize_t size = PyUnicode_AsWideChar(origin, root, MAXPATHLEN);
1210
5.70k
    if (size < 0) {
1211
0
        return -1;
1212
0
    }
1213
5.70k
    assert(size <= MAXPATHLEN);
1214
5.70k
    root[size] = L'\0';
1215
1216
5.70k
    wchar_t *sep = wcsrchr(root, SEP);
1217
5.70k
    if (sep == NULL) {
1218
0
        return 0;
1219
0
    }
1220
    // If it's a package then we need to look one directory further up
1221
5.70k
    if (wcscmp(sep + 1, L"__init__.py") == 0) {
1222
0
        *sep = L'\0';
1223
0
        sep = wcsrchr(root, SEP);
1224
0
        if (sep == NULL) {
1225
0
            return 0;
1226
0
        }
1227
0
    }
1228
5.70k
    *sep = L'\0';
1229
1230
    // sys.path[0] or os.getcwd()
1231
5.70k
    wchar_t *sys_path_0 = config->sys_path_0;
1232
5.70k
    if (!sys_path_0) {
1233
5.70k
        return 0;
1234
5.70k
    }
1235
1236
0
    wchar_t sys_path_0_buf[MAXPATHLEN];
1237
0
    if (sys_path_0[0] == L'\0') {
1238
        // if sys.path[0] == "", treat it as if it were the current directory
1239
0
        if (!_Py_wgetcwd(sys_path_0_buf, MAXPATHLEN)) {
1240
            // If we failed to getcwd, don't raise an exception and instead
1241
            // let the caller proceed assuming no shadowing
1242
0
            return 0;
1243
0
        }
1244
0
        sys_path_0 = sys_path_0_buf;
1245
0
    }
1246
1247
0
    int result = wcscmp(sys_path_0, root) == 0;
1248
0
    return result;
1249
0
}
1250
1251
PyObject*
1252
_Py_module_getattro_impl(PyModuleObject *m, PyObject *name, int suppress)
1253
6.51M
{
1254
    // When suppress=1, this function suppresses AttributeError.
1255
6.51M
    PyObject *attr, *mod_name, *getattr;
1256
6.51M
    attr = _PyObject_GenericGetAttrWithDict((PyObject *)m, name, NULL, suppress);
1257
6.51M
    if (attr) {
1258
6.47M
        return attr;
1259
6.47M
    }
1260
41.5k
    if (suppress == 1) {
1261
35.4k
        if (PyErr_Occurred()) {
1262
            // pass up non-AttributeError exception
1263
0
            return NULL;
1264
0
        }
1265
35.4k
    }
1266
6.07k
    else {
1267
6.07k
        if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
1268
            // pass up non-AttributeError exception
1269
0
            return NULL;
1270
0
        }
1271
6.07k
        PyErr_Clear();
1272
6.07k
    }
1273
41.5k
    assert(m->md_dict != NULL);
1274
41.5k
    if (PyDict_GetItemRef(m->md_dict, &_Py_ID(__getattr__), &getattr) < 0) {
1275
0
        return NULL;
1276
0
    }
1277
41.5k
    if (getattr) {
1278
121
        PyObject *result = PyObject_CallOneArg(getattr, name);
1279
121
        if (result == NULL && suppress == 1 && PyErr_ExceptionMatches(PyExc_AttributeError)) {
1280
            // suppress AttributeError
1281
121
            PyErr_Clear();
1282
121
        }
1283
121
        Py_DECREF(getattr);
1284
121
        return result;
1285
121
    }
1286
1287
    // The attribute was not found.  We make a best effort attempt at a useful error message,
1288
    // but only if we're not suppressing AttributeError.
1289
41.4k
    if (suppress == 1) {
1290
35.3k
        return NULL;
1291
35.3k
    }
1292
6.07k
    if (PyDict_GetItemRef(m->md_dict, &_Py_ID(__name__), &mod_name) < 0) {
1293
0
        return NULL;
1294
0
    }
1295
6.07k
    if (!mod_name || !PyUnicode_Check(mod_name)) {
1296
0
        Py_XDECREF(mod_name);
1297
0
        PyErr_Format(PyExc_AttributeError,
1298
0
                    "module has no attribute '%U'", name);
1299
0
        return NULL;
1300
0
    }
1301
6.07k
    PyObject *spec;
1302
6.07k
    if (PyDict_GetItemRef(m->md_dict, &_Py_ID(__spec__), &spec) < 0) {
1303
0
        Py_DECREF(mod_name);
1304
0
        return NULL;
1305
0
    }
1306
6.07k
    if (spec == NULL) {
1307
0
        PyErr_Format(PyExc_AttributeError,
1308
0
                     "module '%U' has no attribute '%U'",
1309
0
                     mod_name, name);
1310
0
        Py_DECREF(mod_name);
1311
0
        return NULL;
1312
0
    }
1313
1314
6.07k
    PyObject *origin = NULL;
1315
6.07k
    if (_PyModuleSpec_GetFileOrigin(spec, &origin) < 0) {
1316
0
        goto done;
1317
0
    }
1318
1319
6.07k
    int is_possibly_shadowing = _PyModule_IsPossiblyShadowing(origin);
1320
6.07k
    if (is_possibly_shadowing < 0) {
1321
0
        goto done;
1322
0
    }
1323
6.07k
    int is_possibly_shadowing_stdlib = 0;
1324
6.07k
    if (is_possibly_shadowing) {
1325
0
        PyObject *stdlib_modules;
1326
0
        if (PySys_GetOptionalAttrString("stdlib_module_names", &stdlib_modules) < 0) {
1327
0
            goto done;
1328
0
        }
1329
0
        if (stdlib_modules && PyAnySet_Check(stdlib_modules)) {
1330
0
            is_possibly_shadowing_stdlib = PySet_Contains(stdlib_modules, mod_name);
1331
0
            if (is_possibly_shadowing_stdlib < 0) {
1332
0
                Py_DECREF(stdlib_modules);
1333
0
                goto done;
1334
0
            }
1335
0
        }
1336
0
        Py_XDECREF(stdlib_modules);
1337
0
    }
1338
1339
6.07k
    if (is_possibly_shadowing_stdlib) {
1340
0
        assert(origin);
1341
0
        PyErr_Format(PyExc_AttributeError,
1342
0
                    "module '%U' has no attribute '%U' "
1343
0
                    "(consider renaming '%U' since it has the same "
1344
0
                    "name as the standard library module named '%U' "
1345
0
                    "and prevents importing that standard library module)",
1346
0
                    mod_name, name, origin, mod_name);
1347
0
    }
1348
6.07k
    else {
1349
6.07k
        int rc = _PyModuleSpec_IsInitializing(spec);
1350
6.07k
        if (rc < 0) {
1351
0
            goto done;
1352
0
        }
1353
6.07k
        else if (rc > 0) {
1354
0
            if (is_possibly_shadowing) {
1355
0
                assert(origin);
1356
                // For non-stdlib modules, only mention the possibility of
1357
                // shadowing if the module is being initialized.
1358
0
                PyErr_Format(PyExc_AttributeError,
1359
0
                            "module '%U' has no attribute '%U' "
1360
0
                            "(consider renaming '%U' if it has the same name "
1361
0
                            "as a library you intended to import)",
1362
0
                            mod_name, name, origin);
1363
0
            }
1364
0
            else if (origin) {
1365
0
                PyErr_Format(PyExc_AttributeError,
1366
0
                            "partially initialized "
1367
0
                            "module '%U' from '%U' has no attribute '%U' "
1368
0
                            "(most likely due to a circular import)",
1369
0
                            mod_name, origin, name);
1370
0
            }
1371
0
            else {
1372
0
                PyErr_Format(PyExc_AttributeError,
1373
0
                            "partially initialized "
1374
0
                            "module '%U' has no attribute '%U' "
1375
0
                            "(most likely due to a circular import)",
1376
0
                            mod_name, name);
1377
0
            }
1378
0
        }
1379
6.07k
        else {
1380
6.07k
            assert(rc == 0);
1381
6.07k
            rc = _PyModuleSpec_IsUninitializedSubmodule(spec, name);
1382
6.07k
            if (rc > 0) {
1383
0
                PyErr_Format(PyExc_AttributeError,
1384
0
                            "cannot access submodule '%U' of module '%U' "
1385
0
                            "(most likely due to a circular import)",
1386
0
                            name, mod_name);
1387
0
            }
1388
6.07k
            else if (rc == 0) {
1389
6.07k
                PyErr_Format(PyExc_AttributeError,
1390
6.07k
                            "module '%U' has no attribute '%U'",
1391
6.07k
                            mod_name, name);
1392
6.07k
            }
1393
6.07k
        }
1394
6.07k
    }
1395
1396
6.07k
done:
1397
6.07k
    Py_XDECREF(origin);
1398
6.07k
    Py_DECREF(spec);
1399
6.07k
    Py_DECREF(mod_name);
1400
6.07k
    return NULL;
1401
6.07k
}
1402
1403
1404
PyObject*
1405
_Py_module_getattro(PyObject *self, PyObject *name)
1406
3.44M
{
1407
3.44M
    PyModuleObject *m = _PyModule_CAST(self);
1408
3.44M
    return _Py_module_getattro_impl(m, name, 0);
1409
3.44M
}
1410
1411
static int
1412
module_traverse(PyObject *self, visitproc visit, void *arg)
1413
126k
{
1414
126k
    PyModuleObject *m = _PyModule_CAST(self);
1415
1416
126k
    assert_def_missing_or_redundant(m);
1417
    /* bpo-39824: Don't call m_traverse() if m_size > 0 and md_state=NULL */
1418
126k
    if (m->md_state_traverse && (m->md_state_size <= 0 || m->md_state != NULL))
1419
18.6k
    {
1420
18.6k
        int res = m->md_state_traverse((PyObject*)m, visit, arg);
1421
18.6k
        if (res)
1422
0
            return res;
1423
18.6k
    }
1424
1425
126k
    Py_VISIT(m->md_dict);
1426
126k
    return 0;
1427
126k
}
1428
1429
static int
1430
module_clear(PyObject *self)
1431
0
{
1432
0
    PyModuleObject *m = _PyModule_CAST(self);
1433
1434
0
    assert_def_missing_or_redundant(m);
1435
    /* bpo-39824: Don't call m_clear() if m_size > 0 and md_state=NULL */
1436
0
    if (m->md_state_clear && (m->md_state_size <= 0 || m->md_state != NULL))
1437
0
    {
1438
0
        int res = m->md_state_clear((PyObject*)m);
1439
0
        if (PyErr_Occurred()) {
1440
0
            PyErr_FormatUnraisable("Exception ignored in m_clear of module%s%V",
1441
0
                                   m->md_name ? " " : "",
1442
0
                                   m->md_name, "");
1443
0
        }
1444
0
        if (res) {
1445
0
            return res;
1446
0
        }
1447
0
    }
1448
0
    Py_CLEAR(m->md_dict);
1449
0
    return 0;
1450
0
}
1451
1452
static PyObject *
1453
module_dir(PyObject *self, PyObject *args)
1454
32
{
1455
32
    PyObject *result = NULL;
1456
32
    PyObject *dict = PyObject_GetAttr(self, &_Py_ID(__dict__));
1457
1458
32
    if (dict != NULL) {
1459
32
        if (PyDict_Check(dict)) {
1460
32
            PyObject *dirfunc = PyDict_GetItemWithError(dict, &_Py_ID(__dir__));
1461
32
            if (dirfunc) {
1462
0
                result = _PyObject_CallNoArgs(dirfunc);
1463
0
            }
1464
32
            else if (!PyErr_Occurred()) {
1465
32
                result = PyDict_Keys(dict);
1466
32
            }
1467
32
        }
1468
0
        else {
1469
0
            PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary");
1470
0
        }
1471
32
    }
1472
1473
32
    Py_XDECREF(dict);
1474
32
    return result;
1475
32
}
1476
1477
static PyMethodDef module_methods[] = {
1478
    {"__dir__", module_dir, METH_NOARGS,
1479
     PyDoc_STR("__dir__() -> list\nspecialized dir() implementation")},
1480
    {0}
1481
};
1482
1483
static PyObject *
1484
module_get_dict(PyModuleObject *m)
1485
0
{
1486
0
    PyObject *dict = PyObject_GetAttr((PyObject *)m, &_Py_ID(__dict__));
1487
0
    if (dict == NULL) {
1488
0
        return NULL;
1489
0
    }
1490
0
    if (!PyDict_Check(dict)) {
1491
0
        PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary");
1492
0
        Py_DECREF(dict);
1493
0
        return NULL;
1494
0
    }
1495
0
    return dict;
1496
0
}
1497
1498
static PyObject *
1499
module_get_annotate(PyObject *self, void *Py_UNUSED(ignored))
1500
0
{
1501
0
    PyModuleObject *m = _PyModule_CAST(self);
1502
1503
0
    PyObject *dict = module_get_dict(m);
1504
0
    if (dict == NULL) {
1505
0
        return NULL;
1506
0
    }
1507
1508
0
    PyObject *annotate;
1509
0
    if (PyDict_GetItemRef(dict, &_Py_ID(__annotate__), &annotate) == 0) {
1510
0
        annotate = Py_None;
1511
0
        if (PyDict_SetItem(dict, &_Py_ID(__annotate__), annotate) == -1) {
1512
0
            Py_CLEAR(annotate);
1513
0
        }
1514
0
    }
1515
0
    Py_DECREF(dict);
1516
0
    return annotate;
1517
0
}
1518
1519
static int
1520
module_set_annotate(PyObject *self, PyObject *value, void *Py_UNUSED(ignored))
1521
0
{
1522
0
    PyModuleObject *m = _PyModule_CAST(self);
1523
0
    if (value == NULL) {
1524
0
        PyErr_SetString(PyExc_TypeError, "cannot delete __annotate__ attribute");
1525
0
        return -1;
1526
0
    }
1527
1528
0
    PyObject *dict = module_get_dict(m);
1529
0
    if (dict == NULL) {
1530
0
        return -1;
1531
0
    }
1532
1533
0
    if (!Py_IsNone(value) && !PyCallable_Check(value)) {
1534
0
        PyErr_SetString(PyExc_TypeError, "__annotate__ must be callable or None");
1535
0
        Py_DECREF(dict);
1536
0
        return -1;
1537
0
    }
1538
1539
0
    if (PyDict_SetItem(dict, &_Py_ID(__annotate__), value) == -1) {
1540
0
        Py_DECREF(dict);
1541
0
        return -1;
1542
0
    }
1543
0
    if (!Py_IsNone(value)) {
1544
0
        if (PyDict_Pop(dict, &_Py_ID(__annotations__), NULL) == -1) {
1545
0
            Py_DECREF(dict);
1546
0
            return -1;
1547
0
        }
1548
0
    }
1549
0
    Py_DECREF(dict);
1550
0
    return 0;
1551
0
}
1552
1553
static PyObject *
1554
module_get_annotations(PyObject *self, void *Py_UNUSED(ignored))
1555
0
{
1556
0
    PyModuleObject *m = _PyModule_CAST(self);
1557
1558
0
    PyObject *dict = module_get_dict(m);
1559
0
    if (dict == NULL) {
1560
0
        return NULL;
1561
0
    }
1562
1563
0
    PyObject *annotations;
1564
0
    if (PyDict_GetItemRef(dict, &_Py_ID(__annotations__), &annotations) == 0) {
1565
0
        PyObject *spec;
1566
0
        if (PyDict_GetItemRef(m->md_dict, &_Py_ID(__spec__), &spec) < 0) {
1567
0
            Py_DECREF(dict);
1568
0
            return NULL;
1569
0
        }
1570
0
        bool is_initializing = false;
1571
0
        if (spec != NULL) {
1572
0
            int rc = _PyModuleSpec_IsInitializing(spec);
1573
0
            if (rc < 0) {
1574
0
                Py_DECREF(spec);
1575
0
                Py_DECREF(dict);
1576
0
                return NULL;
1577
0
            }
1578
0
            Py_DECREF(spec);
1579
0
            if (rc) {
1580
0
                is_initializing = true;
1581
0
            }
1582
0
        }
1583
1584
0
        PyObject *annotate;
1585
0
        int annotate_result = PyDict_GetItemRef(dict, &_Py_ID(__annotate__), &annotate);
1586
0
        if (annotate_result < 0) {
1587
0
            Py_DECREF(dict);
1588
0
            return NULL;
1589
0
        }
1590
0
        if (annotate_result == 1 && PyCallable_Check(annotate)) {
1591
0
            PyObject *one = _PyLong_GetOne();
1592
0
            annotations = _PyObject_CallOneArg(annotate, one);
1593
0
            if (annotations == NULL) {
1594
0
                Py_DECREF(annotate);
1595
0
                Py_DECREF(dict);
1596
0
                return NULL;
1597
0
            }
1598
0
            if (!PyDict_Check(annotations)) {
1599
0
                PyErr_Format(PyExc_TypeError,
1600
0
                             "__annotate__() must return a dict, not %T",
1601
0
                             annotations);
1602
0
                Py_DECREF(annotate);
1603
0
                Py_DECREF(annotations);
1604
0
                Py_DECREF(dict);
1605
0
                return NULL;
1606
0
            }
1607
0
        }
1608
0
        else {
1609
0
            annotations = PyDict_New();
1610
0
        }
1611
0
        Py_XDECREF(annotate);
1612
        // Do not cache annotations if the module is still initializing
1613
0
        if (annotations && !is_initializing) {
1614
0
            int result = PyDict_SetItem(
1615
0
                    dict, &_Py_ID(__annotations__), annotations);
1616
0
            if (result) {
1617
0
                Py_CLEAR(annotations);
1618
0
            }
1619
0
        }
1620
0
    }
1621
0
    Py_DECREF(dict);
1622
0
    return annotations;
1623
0
}
1624
1625
static int
1626
module_set_annotations(PyObject *self, PyObject *value, void *Py_UNUSED(ignored))
1627
0
{
1628
0
    PyModuleObject *m = _PyModule_CAST(self);
1629
1630
0
    PyObject *dict = module_get_dict(m);
1631
0
    if (dict == NULL) {
1632
0
        return -1;
1633
0
    }
1634
1635
0
    int ret = -1;
1636
0
    if (value != NULL) {
1637
        /* set */
1638
0
        ret = PyDict_SetItem(dict, &_Py_ID(__annotations__), value);
1639
0
    }
1640
0
    else {
1641
        /* delete */
1642
0
        ret = PyDict_Pop(dict, &_Py_ID(__annotations__), NULL);
1643
0
        if (ret == 0) {
1644
0
            PyErr_SetObject(PyExc_AttributeError, &_Py_ID(__annotations__));
1645
0
            ret = -1;
1646
0
        }
1647
0
        else if (ret > 0) {
1648
0
            ret = 0;
1649
0
        }
1650
0
    }
1651
0
    if (ret == 0 && PyDict_Pop(dict, &_Py_ID(__annotate__), NULL) < 0) {
1652
0
        ret = -1;
1653
0
    }
1654
1655
0
    Py_DECREF(dict);
1656
0
    return ret;
1657
0
}
1658
1659
1660
static PyGetSetDef module_getsets[] = {
1661
    {"__annotations__", module_get_annotations, module_set_annotations},
1662
    {"__annotate__", module_get_annotate, module_set_annotate},
1663
    {NULL}
1664
};
1665
1666
PyTypeObject PyModule_Type = {
1667
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
1668
    "module",                                   /* tp_name */
1669
    sizeof(PyModuleObject),                     /* tp_basicsize */
1670
    0,                                          /* tp_itemsize */
1671
    module_dealloc,                             /* tp_dealloc */
1672
    0,                                          /* tp_vectorcall_offset */
1673
    0,                                          /* tp_getattr */
1674
    0,                                          /* tp_setattr */
1675
    0,                                          /* tp_as_async */
1676
    module_repr,                                /* tp_repr */
1677
    0,                                          /* tp_as_number */
1678
    0,                                          /* tp_as_sequence */
1679
    0,                                          /* tp_as_mapping */
1680
    0,                                          /* tp_hash */
1681
    0,                                          /* tp_call */
1682
    0,                                          /* tp_str */
1683
    _Py_module_getattro,                        /* tp_getattro */
1684
    PyObject_GenericSetAttr,                    /* tp_setattro */
1685
    0,                                          /* tp_as_buffer */
1686
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1687
        Py_TPFLAGS_BASETYPE,                    /* tp_flags */
1688
    module___init____doc__,                     /* tp_doc */
1689
    module_traverse,                            /* tp_traverse */
1690
    module_clear,                               /* tp_clear */
1691
    0,                                          /* tp_richcompare */
1692
    offsetof(PyModuleObject, md_weaklist),      /* tp_weaklistoffset */
1693
    0,                                          /* tp_iter */
1694
    0,                                          /* tp_iternext */
1695
    module_methods,                             /* tp_methods */
1696
    module_members,                             /* tp_members */
1697
    module_getsets,                             /* tp_getset */
1698
    0,                                          /* tp_base */
1699
    0,                                          /* tp_dict */
1700
    0,                                          /* tp_descr_get */
1701
    0,                                          /* tp_descr_set */
1702
    offsetof(PyModuleObject, md_dict),          /* tp_dictoffset */
1703
    module___init__,                            /* tp_init */
1704
    0,                                          /* tp_alloc */
1705
    new_module,                                 /* tp_new */
1706
    PyObject_GC_Del,                            /* tp_free */
1707
};