Coverage Report

Created: 2025-11-30 06:38

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