Coverage Report

Created: 2026-01-10 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython3/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
639k
    (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
520k
{
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
520k
#ifndef NDEBUG
39
520k
    if (m->md_token_is_def) {
40
140k
        PyModuleDef *def = (PyModuleDef *)m->md_token;
41
140k
        assert(def);
42
563k
#define DO_ASSERT(F) assert (def->m_ ## F == m->md_state_ ## F);
43
140k
        DO_ASSERT(size);
44
140k
        DO_ASSERT(traverse);
45
140k
        DO_ASSERT(clear);
46
140k
        DO_ASSERT(free);
47
140k
#undef DO_ASSERT
48
140k
    }
49
520k
#endif // NDEBUG
50
520k
}
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
784
{
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
784
    assert(PyModuleDef_Type.tp_flags & Py_TPFLAGS_READY);
137
784
    if (def->m_base.m_index == 0) {
138
436
        Py_SET_REFCNT(def, 1);
139
436
        Py_SET_TYPE(def, &PyModuleDef_Type);
140
436
        def->m_base.m_index = _PyImport_GetNextModuleIndex();
141
436
    }
142
784
    return (PyObject*)def;
143
784
}
144
145
static int
146
module_init_dict(PyModuleObject *mod, PyObject *md_dict,
147
                 PyObject *name, PyObject *doc)
148
1.07k
{
149
1.07k
    assert(md_dict != NULL);
150
1.07k
    if (doc == NULL)
151
482
        doc = Py_None;
152
153
1.07k
    if (PyDict_SetItem(md_dict, &_Py_ID(__name__), name) != 0)
154
0
        return -1;
155
1.07k
    if (PyDict_SetItem(md_dict, &_Py_ID(__doc__), doc) != 0)
156
0
        return -1;
157
1.07k
    if (PyDict_SetItem(md_dict, &_Py_ID(__package__), Py_None) != 0)
158
0
        return -1;
159
1.07k
    if (PyDict_SetItem(md_dict, &_Py_ID(__loader__), Py_None) != 0)
160
0
        return -1;
161
1.07k
    if (PyDict_SetItem(md_dict, &_Py_ID(__spec__), Py_None) != 0)
162
0
        return -1;
163
1.07k
    if (PyUnicode_CheckExact(name)) {
164
1.07k
        Py_XSETREF(mod->md_name, Py_NewRef(name));
165
1.07k
    }
166
167
1.07k
    return 0;
168
1.07k
}
169
170
static PyModuleObject *
171
new_module_notrack(PyTypeObject *mt)
172
1.07k
{
173
1.07k
    PyModuleObject *m;
174
1.07k
    m = (PyModuleObject *)_PyType_AllocNoTrack(mt, 0);
175
1.07k
    if (m == NULL)
176
0
        return NULL;
177
1.07k
    m->md_state = NULL;
178
1.07k
    m->md_weaklist = NULL;
179
1.07k
    m->md_name = NULL;
180
1.07k
    m->md_token_is_def = false;
181
#ifdef Py_GIL_DISABLED
182
    m->md_requires_gil = true;
183
#endif
184
1.07k
    m->md_state_size = 0;
185
1.07k
    m->md_state_traverse = NULL;
186
1.07k
    m->md_state_clear = NULL;
187
1.07k
    m->md_state_free = NULL;
188
1.07k
    m->md_exec = NULL;
189
1.07k
    m->md_token = NULL;
190
1.07k
    m->md_dict = PyDict_New();
191
1.07k
    if (m->md_dict == NULL) {
192
0
        Py_DECREF(m);
193
0
        return NULL;
194
0
    }
195
1.07k
    return m;
196
1.07k
}
197
198
static void
199
track_module(PyModuleObject *m)
200
1.07k
{
201
1.07k
    _PyDict_EnablePerThreadRefcounting(m->md_dict);
202
1.07k
    _PyObject_SetDeferredRefcount((PyObject *)m);
203
1.07k
    PyObject_GC_Track(m);
204
1.07k
}
205
206
static PyObject *
207
new_module(PyTypeObject *mt, PyObject *args, PyObject *kws)
208
590
{
209
590
    PyModuleObject *m = new_module_notrack(mt);
210
590
    if (m != NULL) {
211
590
        track_module(m);
212
590
    }
213
590
    return (PyObject *)m;
214
590
}
215
216
PyObject *
217
PyModule_NewObject(PyObject *name)
218
482
{
219
482
    PyModuleObject *m = new_module_notrack(&PyModule_Type);
220
482
    if (m == NULL)
221
0
        return NULL;
222
482
    if (module_init_dict(m, m->md_dict, name, NULL) != 0)
223
0
        goto fail;
224
482
    track_module(m);
225
482
    return (PyObject *)m;
226
227
0
 fail:
228
0
    Py_DECREF(m);
229
0
    return NULL;
230
482
}
231
232
PyObject *
233
PyModule_New(const char *name)
234
90
{
235
90
    PyObject *nameobj, *module;
236
90
    nameobj = PyUnicode_FromString(name);
237
90
    if (nameobj == NULL)
238
0
        return NULL;
239
90
    module = PyModule_NewObject(nameobj);
240
90
    Py_DECREF(nameobj);
241
90
    return module;
242
90
}
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
436
{
251
436
    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
436
    return 1;
262
436
}
263
264
static int
265
_add_methods_to_object(PyObject *module, PyObject *name, PyMethodDef *functions)
266
427
{
267
427
    PyObject *func;
268
427
    PyMethodDef *fdef;
269
270
10.9k
    for (fdef = functions; fdef->ml_name != NULL; fdef++) {
271
10.4k
        if ((fdef->ml_flags & METH_CLASS) ||
272
10.4k
            (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
10.4k
        func = PyCFunction_NewEx(fdef, (PyObject*)module, name);
279
10.4k
        if (func == NULL) {
280
0
            return -1;
281
0
        }
282
10.4k
        _PyObject_SetDeferredRefcount(func);
283
10.4k
        if (PyObject_SetAttrString(module, fdef->ml_name, func) != 0) {
284
0
            Py_DECREF(func);
285
0
            return -1;
286
0
        }
287
10.4k
        Py_DECREF(func);
288
10.4k
    }
289
290
427
    return 0;
291
427
}
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
436
{
309
436
    md->md_state_size = def_like->m_size;
310
436
    md->md_state_traverse = def_like->m_traverse;
311
436
    md->md_state_clear = def_like->m_clear;
312
436
    md->md_state_free = def_like->m_free;
313
436
}
314
315
PyObject *
316
_PyModule_CreateInitialized(PyModuleDef* module, int module_api_version)
317
88
{
318
88
    const char* name;
319
88
    PyModuleObject *m;
320
321
88
    if (!PyModuleDef_Init(module))
322
0
        return NULL;
323
88
    name = module->m_name;
324
88
    if (!check_api_version(name, module_api_version)) {
325
0
        return NULL;
326
0
    }
327
88
    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
88
    name = _PyImport_ResolveNameWithPackageContext(name);
334
335
88
    m = (PyModuleObject*)PyModule_New(name);
336
88
    if (m == NULL)
337
0
        return NULL;
338
339
88
    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
88
    if (module->m_methods != NULL) {
350
88
        if (PyModule_AddFunctions((PyObject *) m, module->m_methods) != 0) {
351
0
            Py_DECREF(m);
352
0
            return NULL;
353
0
        }
354
88
    }
355
88
    if (module->m_doc != NULL) {
356
66
        if (PyModule_SetDocString((PyObject *) m, module->m_doc) != 0) {
357
0
            Py_DECREF(m);
358
0
            return NULL;
359
0
        }
360
66
    }
361
88
    m->md_token = module;
362
88
    m->md_token_is_def = true;
363
88
    module_copy_members_from_deflike(m, module);
364
#ifdef Py_GIL_DISABLED
365
    m->md_requires_gil = true;
366
#endif
367
88
    return (PyObject*)m;
368
88
}
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
348
{
377
348
    PyModuleDef_Slot* cur_slot;
378
348
    PyObject *(*create)(PyObject *, PyModuleDef*) = NULL;
379
348
    PyObject *nameobj;
380
348
    PyObject *m = NULL;
381
348
    int has_multiple_interpreters_slot = 0;
382
348
    void *multiple_interpreters = (void *)0;
383
348
    int has_gil_slot = 0;
384
348
    bool requires_gil = true;
385
348
    int has_execution_slots = 0;
386
348
    const char *name;
387
348
    int ret;
388
348
    void *token = NULL;
389
348
    _Py_modexecfunc m_exec = NULL;
390
348
    PyInterpreterState *interp = _PyInterpreterState_GET();
391
392
348
    nameobj = PyObject_GetAttrString(spec, "name");
393
348
    if (nameobj == NULL) {
394
0
        return NULL;
395
0
    }
396
348
    name = PyUnicode_AsUTF8(nameobj);
397
348
    if (name == NULL) {
398
0
        goto error;
399
0
    }
400
401
348
    if (!check_api_version(name, module_api_version)) {
402
0
        goto error;
403
0
    }
404
405
348
    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
1.37k
    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
1.02k
#define COPY_COMMON_SLOT(SLOT, TYPE, DEST)                              \
417
1.02k
        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
1.02k
        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
326
            case Py_mod_exec:
454
326
                has_execution_slots = 1;
455
326
                if (!original_def) {
456
0
                    COPY_COMMON_SLOT(Py_mod_exec, _Py_modexecfunc, m_exec);
457
0
                }
458
326
                break;
459
348
            case Py_mod_multiple_interpreters:
460
348
                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
348
                multiple_interpreters = cur_slot->value;
468
348
                has_multiple_interpreters_slot = 1;
469
348
                break;
470
348
            case Py_mod_gil:
471
348
                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
348
                requires_gil = (cur_slot->value != Py_MOD_GIL_NOT_USED);
479
348
                has_gil_slot = 1;
480
348
                break;
481
0
            case Py_mod_abi:
482
0
                if (PyABIInfo_Check((PyABIInfo *)cur_slot->value, name) < 0) {
483
0
                    goto error;
484
0
                }
485
0
                break;
486
0
            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
1.02k
        }
523
1.02k
#undef COPY_COMMON_SLOT
524
1.02k
    }
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
348
    if (!has_multiple_interpreters_slot) {
545
0
        multiple_interpreters = Py_MOD_MULTIPLE_INTERPRETERS_SUPPORTED;
546
0
    }
547
348
    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
348
    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
348
    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
348
    } else {
582
348
        m = PyModule_NewObject(nameobj);
583
348
        if (m == NULL) {
584
0
            goto error;
585
0
        }
586
348
    }
587
588
348
    if (PyModule_Check(m)) {
589
348
        PyModuleObject *mod = (PyModuleObject*)m;
590
348
        mod->md_state = NULL;
591
348
        module_copy_members_from_deflike(mod, def_like);
592
348
        if (original_def) {
593
348
            assert (!token);
594
348
            mod->md_token = original_def;
595
348
            mod->md_token_is_def = 1;
596
348
        }
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
348
        (void)requires_gil;
604
348
#endif
605
348
        mod->md_exec = m_exec;
606
348
    } 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
348
    if (def_like->m_methods != NULL) {
635
339
        ret = _add_methods_to_object(m, nameobj, def_like->m_methods);
636
339
        if (ret != 0) {
637
0
            goto error;
638
0
        }
639
339
    }
640
641
348
    if (def_like->m_doc != NULL) {
642
304
        ret = PyModule_SetDocString(m, def_like->m_doc);
643
304
        if (ret != 0) {
644
0
            goto error;
645
0
        }
646
304
    }
647
648
348
    Py_DECREF(nameobj);
649
348
    return m;
650
651
0
error:
652
0
    Py_DECREF(nameobj);
653
0
    Py_XDECREF(m);
654
0
    return NULL;
655
348
}
656
657
PyObject *
658
PyModule_FromDefAndSpec2(PyModuleDef* def, PyObject *spec, int module_api_version)
659
348
{
660
348
    PyModuleDef_Init(def);
661
348
    return module_from_def_and_spec(def, spec, module_api_version, def);
662
348
}
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
326
{
697
326
    int ret = exec(module);
698
326
    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
326
    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
326
    return 0;
715
326
}
716
717
static int
718
alloc_state(PyObject *module)
719
696
{
720
696
    if (!PyModule_Check(module)) {
721
0
        PyErr_Format(PyExc_TypeError, "expected module, got %T", module);
722
0
        return -1;
723
0
    }
724
696
    PyModuleObject *md = (PyModuleObject*)module;
725
726
696
    if (md->md_state_size >= 0) {
727
696
        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
348
            md->md_state = PyMem_Malloc(md->md_state_size);
731
348
            if (!md->md_state) {
732
0
                PyErr_NoMemory();
733
0
                return -1;
734
0
            }
735
348
            memset(md->md_state, 0, md->md_state_size);
736
348
        }
737
696
    }
738
696
    return 0;
739
696
}
740
741
int
742
PyModule_Exec(PyObject *module)
743
348
{
744
348
    if (alloc_state(module) < 0) {
745
0
        return -1;
746
0
    }
747
348
    PyModuleObject *md = (PyModuleObject*)module;
748
348
    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
348
    PyModuleDef *def = _PyModule_GetDefOrNull(module);
754
348
    if (def) {
755
348
        return PyModule_ExecDef(module, def);
756
348
    }
757
0
    return 0;
758
348
}
759
760
int
761
PyModule_ExecDef(PyObject *module, PyModuleDef *def)
762
348
{
763
348
    PyModuleDef_Slot *cur_slot;
764
765
348
    if (alloc_state(module) < 0) {
766
0
        return -1;
767
0
    }
768
769
348
    assert(PyModule_Check(module));
770
771
348
    if (def->m_slots == NULL) {
772
0
        return 0;
773
0
    }
774
775
1.37k
    for (cur_slot = def->m_slots; cur_slot && cur_slot->slot; cur_slot++) {
776
1.02k
        if (cur_slot->slot == Py_mod_exec) {
777
326
            int (*func)(PyObject *) = cur_slot->value;
778
326
            if (run_exec_func(module, func) < 0) {
779
0
                return -1;
780
0
            }
781
326
            continue;
782
326
        }
783
1.02k
    }
784
348
    return 0;
785
348
}
786
787
int
788
PyModule_AddFunctions(PyObject *m, PyMethodDef *functions)
789
88
{
790
88
    int res;
791
88
    PyObject *name = PyModule_GetNameObject(m);
792
88
    if (name == NULL) {
793
0
        return -1;
794
0
    }
795
796
88
    res = _add_methods_to_object(m, name, functions);
797
88
    Py_DECREF(name);
798
88
    return res;
799
88
}
800
801
int
802
PyModule_SetDocString(PyObject *m, const char *doc)
803
370
{
804
370
    PyObject *v;
805
806
370
    v = PyUnicode_FromString(doc);
807
370
    if (v == NULL || PyObject_SetAttr(m, &_Py_ID(__doc__), v) != 0) {
808
0
        Py_XDECREF(v);
809
0
        return -1;
810
0
    }
811
370
    Py_DECREF(v);
812
370
    return 0;
813
370
}
814
815
PyObject *
816
PyModule_GetDict(PyObject *m)
817
7.75k
{
818
7.75k
    if (!PyModule_Check(m)) {
819
0
        PyErr_BadInternalCall();
820
0
        return NULL;
821
0
    }
822
7.75k
    return _PyModule_GetDict(m);  // borrowed reference
823
7.75k
}
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
88
{
853
88
    if (!PyModule_Check(mod)) {
854
0
        PyErr_BadArgument();
855
0
        return NULL;
856
0
    }
857
88
    PyObject *dict = ((PyModuleObject *)mod)->md_dict;  // borrowed reference
858
88
    if (dict == NULL || !PyDict_Check(dict)) {
859
0
        goto error;
860
0
    }
861
88
    PyObject *name;
862
88
    if (PyDict_GetItemRef(dict, &_Py_ID(__name__), &name) <= 0) {
863
        // error or not found
864
0
        goto error;
865
0
    }
866
88
    if (!PyUnicode_Check(name)) {
867
0
        Py_DECREF(name);
868
0
        goto error;
869
0
    }
870
88
    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
88
}
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
5
{
894
    // We return None to indicate "not found" or "bogus".
895
5
    if (!PyModule_Check(mod)) {
896
0
        PyErr_BadArgument();
897
0
        return NULL;
898
0
    }
899
5
    PyObject *dict = ((PyModuleObject *)mod)->md_dict;  // borrowed reference
900
5
    if (dict == NULL) {
901
        // The module has been tampered with.
902
0
        Py_RETURN_NONE;
903
0
    }
904
5
    PyObject *fileobj;
905
5
    int res = PyDict_GetItemRef(dict, &_Py_ID(__file__), &fileobj);
906
5
    if (res < 0) {
907
0
        return NULL;
908
0
    }
909
5
    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
5
        Py_RETURN_NONE;
917
5
    }
918
0
    if (!PyUnicode_Check(fileobj)) {
919
0
        Py_DECREF(fileobj);
920
0
        Py_RETURN_NONE;
921
0
    }
922
0
    return fileobj;
923
0
}
924
925
PyObject*
926
PyModule_GetFilenameObject(PyObject *mod)
927
5
{
928
5
    PyObject *fileobj = _PyModule_GetFilenameObject(mod);
929
5
    if (fileobj == NULL) {
930
0
        return NULL;
931
0
    }
932
5
    if (fileobj == Py_None) {
933
5
        PyErr_SetString(PyExc_SystemError, "module filename missing");
934
5
        return NULL;
935
5
    }
936
0
    return fileobj;
937
5
}
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
0
{
986
0
    if (!PyModule_Check(m)) {
987
0
        PyErr_BadArgument();
988
0
        return NULL;
989
0
    }
990
0
    return _PyModule_GetDefOrNull(m);
991
0
}
992
993
void*
994
PyModule_GetState(PyObject* m)
995
24.5M
{
996
24.5M
    if (!PyModule_Check(m)) {
997
0
        PyErr_BadArgument();
998
0
        return NULL;
999
0
    }
1000
24.5M
    return _PyModule_GetState(m);
1001
24.5M
}
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
590
{
1098
590
    return module_init_dict(self, self->md_dict, name, doc);
1099
590
}
1100
1101
static void
1102
module_dealloc(PyObject *self)
1103
4
{
1104
4
    PyModuleObject *m = _PyModule_CAST(self);
1105
1106
0
    PyObject_GC_UnTrack(m);
1107
1108
4
    int verbose = _Py_GetConfig()->verbose;
1109
4
    if (verbose && m->md_name) {
1110
0
        PySys_FormatStderr("# destroy %U\n", m->md_name);
1111
0
    }
1112
4
    FT_CLEAR_WEAKREFS(self, m->md_weaklist);
1113
1114
4
    assert_def_missing_or_redundant(m);
1115
    /* bpo-39824: Don't call m_free() if m_size > 0 and md_state=NULL */
1116
4
    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
4
    Py_XDECREF(m->md_dict);
1122
4
    Py_XDECREF(m->md_name);
1123
4
    if (m->md_state != NULL) {
1124
0
        PyMem_Free(m->md_state);
1125
0
    }
1126
4
    Py_TYPE(m)->tp_free((PyObject *)m);
1127
4
}
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
4.01M
{
1142
4.01M
    if (spec == NULL) {
1143
0
        return 0;
1144
0
    }
1145
4.01M
    PyObject *value;
1146
4.01M
    int rc = PyObject_GetOptionalAttr(spec, &_Py_ID(_initializing), &value);
1147
4.01M
    if (rc > 0) {
1148
3.70M
        rc = PyObject_IsTrue(value);
1149
3.70M
        Py_DECREF(value);
1150
3.70M
    }
1151
4.01M
    return rc;
1152
4.01M
}
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
467
{
1160
467
    if (spec == NULL) {
1161
0
         return 0;
1162
0
    }
1163
1164
467
    PyObject *value;
1165
467
    int rc = PyObject_GetOptionalAttr(spec, &_Py_ID(_uninitialized_submodules), &value);
1166
467
    if (rc > 0) {
1167
291
        rc = PySequence_Contains(value, name);
1168
291
        Py_DECREF(value);
1169
291
    }
1170
467
    return rc;
1171
467
}
1172
1173
int
1174
_PyModuleSpec_GetFileOrigin(PyObject *spec, PyObject **p_origin)
1175
472
{
1176
472
    PyObject *has_location = NULL;
1177
472
    int rc = PyObject_GetOptionalAttr(spec, &_Py_ID(has_location), &has_location);
1178
472
    if (rc <= 0) {
1179
176
        return rc;
1180
176
    }
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
296
    rc = PyObject_IsTrue(has_location);
1185
296
    Py_DECREF(has_location);
1186
296
    if (rc <= 0) {
1187
27
        return rc;
1188
27
    }
1189
    // has_location is true, so origin is a location
1190
269
    PyObject *origin = NULL;
1191
269
    rc = PyObject_GetOptionalAttr(spec, &_Py_ID(origin), &origin);
1192
269
    if (rc <= 0) {
1193
0
        return rc;
1194
0
    }
1195
269
    assert(origin != NULL);
1196
269
    if (!PyUnicode_Check(origin)) {
1197
0
        Py_DECREF(origin);
1198
0
        return 0;
1199
0
    }
1200
269
    *p_origin = origin;
1201
269
    return 1;
1202
269
}
1203
1204
int
1205
_PyModule_IsPossiblyShadowing(PyObject *origin)
1206
472
{
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
472
    if (origin == NULL) {
1215
203
        return 0;
1216
203
    }
1217
1218
    // not sys.flags.safe_path
1219
269
    const PyConfig *config = _Py_GetConfig();
1220
269
    if (config->safe_path) {
1221
0
        return 0;
1222
0
    }
1223
1224
    // root = os.path.dirname(origin.removesuffix(os.sep + "__init__.py"))
1225
269
    wchar_t root[MAXPATHLEN + 1];
1226
269
    Py_ssize_t size = PyUnicode_AsWideChar(origin, root, MAXPATHLEN);
1227
269
    if (size < 0) {
1228
0
        return -1;
1229
0
    }
1230
269
    assert(size <= MAXPATHLEN);
1231
269
    root[size] = L'\0';
1232
1233
269
    wchar_t *sep = wcsrchr(root, SEP);
1234
269
    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
269
    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
269
    *sep = L'\0';
1246
1247
    // sys.path[0] or os.getcwd()
1248
269
    wchar_t *sys_path_0 = config->sys_path_0;
1249
269
    if (!sys_path_0) {
1250
269
        return 0;
1251
269
    }
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.24M
{
1271
    // When suppress=1, this function suppresses AttributeError.
1272
5.24M
    PyObject *attr, *mod_name, *getattr;
1273
5.24M
    attr = _PyObject_GenericGetAttrWithDict((PyObject *)m, name, NULL, suppress);
1274
5.24M
    if (attr) {
1275
5.24M
        return attr;
1276
5.24M
    }
1277
3.01k
    if (suppress == 1) {
1278
2.54k
        if (PyErr_Occurred()) {
1279
            // pass up non-AttributeError exception
1280
0
            return NULL;
1281
0
        }
1282
2.54k
    }
1283
467
    else {
1284
467
        if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
1285
            // pass up non-AttributeError exception
1286
0
            return NULL;
1287
0
        }
1288
467
        PyErr_Clear();
1289
467
    }
1290
3.01k
    assert(m->md_dict != NULL);
1291
3.01k
    if (PyDict_GetItemRef(m->md_dict, &_Py_ID(__getattr__), &getattr) < 0) {
1292
0
        return NULL;
1293
0
    }
1294
3.01k
    if (getattr) {
1295
51
        PyObject *result = PyObject_CallOneArg(getattr, name);
1296
51
        if (result == NULL && suppress == 1 && PyErr_ExceptionMatches(PyExc_AttributeError)) {
1297
            // suppress AttributeError
1298
51
            PyErr_Clear();
1299
51
        }
1300
51
        Py_DECREF(getattr);
1301
51
        return result;
1302
51
    }
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
2.96k
    if (suppress == 1) {
1307
2.49k
        return NULL;
1308
2.49k
    }
1309
467
    if (PyDict_GetItemRef(m->md_dict, &_Py_ID(__name__), &mod_name) < 0) {
1310
0
        return NULL;
1311
0
    }
1312
467
    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
467
    PyObject *spec;
1319
467
    if (PyDict_GetItemRef(m->md_dict, &_Py_ID(__spec__), &spec) < 0) {
1320
0
        Py_DECREF(mod_name);
1321
0
        return NULL;
1322
0
    }
1323
467
    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
467
    PyObject *origin = NULL;
1332
467
    if (_PyModuleSpec_GetFileOrigin(spec, &origin) < 0) {
1333
0
        goto done;
1334
0
    }
1335
1336
467
    int is_possibly_shadowing = _PyModule_IsPossiblyShadowing(origin);
1337
467
    if (is_possibly_shadowing < 0) {
1338
0
        goto done;
1339
0
    }
1340
467
    int is_possibly_shadowing_stdlib = 0;
1341
467
    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
467
    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
467
    else {
1366
467
        int rc = _PyModuleSpec_IsInitializing(spec);
1367
467
        if (rc < 0) {
1368
0
            goto done;
1369
0
        }
1370
467
        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
467
        else {
1397
467
            assert(rc == 0);
1398
467
            rc = _PyModuleSpec_IsUninitializedSubmodule(spec, name);
1399
467
            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
467
            else if (rc == 0) {
1406
467
                PyErr_Format(PyExc_AttributeError,
1407
467
                            "module '%U' has no attribute '%U'",
1408
467
                            mod_name, name);
1409
467
            }
1410
467
        }
1411
467
    }
1412
1413
467
done:
1414
467
    Py_XDECREF(origin);
1415
467
    Py_DECREF(spec);
1416
467
    Py_DECREF(mod_name);
1417
467
    return NULL;
1418
467
}
1419
1420
1421
PyObject*
1422
_Py_module_getattro(PyObject *self, PyObject *name)
1423
119k
{
1424
119k
    PyModuleObject *m = _PyModule_CAST(self);
1425
0
    return _Py_module_getattro_impl(m, name, 0);
1426
119k
}
1427
1428
static int
1429
module_traverse(PyObject *self, visitproc visit, void *arg)
1430
520k
{
1431
520k
    PyModuleObject *m = _PyModule_CAST(self);
1432
1433
0
    assert_def_missing_or_redundant(m);
1434
    /* bpo-39824: Don't call m_traverse() if m_size > 0 and md_state=NULL */
1435
520k
    if (m->md_state_traverse && (m->md_state_size <= 0 || m->md_state != NULL))
1436
60.6k
    {
1437
60.6k
        int res = m->md_state_traverse((PyObject*)m, visit, arg);
1438
60.6k
        if (res)
1439
0
            return res;
1440
60.6k
    }
1441
1442
520k
    Py_VISIT(m->md_dict);
1443
520k
    return 0;
1444
520k
}
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
22
{
1472
22
    PyObject *result = NULL;
1473
22
    PyObject *dict = PyObject_GetAttr(self, &_Py_ID(__dict__));
1474
1475
22
    if (dict != NULL) {
1476
22
        if (PyDict_Check(dict)) {
1477
22
            PyObject *dirfunc = PyDict_GetItemWithError(dict, &_Py_ID(__dir__));
1478
22
            if (dirfunc) {
1479
0
                result = _PyObject_CallNoArgs(dirfunc);
1480
0
            }
1481
22
            else if (!PyErr_Occurred()) {
1482
22
                result = PyDict_Keys(dict);
1483
22
            }
1484
22
        }
1485
0
        else {
1486
0
            PyErr_Format(PyExc_TypeError, "<module>.__dict__ is not a dictionary");
1487
0
        }
1488
22
    }
1489
1490
22
    Py_XDECREF(dict);
1491
22
    return result;
1492
22
}
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
};