Coverage Report

Created: 2025-11-02 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Python/import.c
Line
Count
Source
1
/* Module definition and import implementation */
2
3
#include "Python.h"
4
#include "pycore_audit.h"         // _PySys_Audit()
5
#include "pycore_ceval.h"
6
#include "pycore_hashtable.h"     // _Py_hashtable_new_full()
7
#include "pycore_import.h"        // _PyImport_BootstrapImp()
8
#include "pycore_initconfig.h"    // _PyStatus_OK()
9
#include "pycore_interp.h"        // struct _import_runtime_state
10
#include "pycore_magic_number.h"  // PYC_MAGIC_NUMBER_TOKEN
11
#include "pycore_moduleobject.h"  // _PyModule_GetDef()
12
#include "pycore_namespace.h"     // _PyNamespace_Type
13
#include "pycore_object.h"        // _Py_SetImmortal()
14
#include "pycore_pyerrors.h"      // _PyErr_SetString()
15
#include "pycore_pyhash.h"        // _Py_KeyedHash()
16
#include "pycore_pylifecycle.h"
17
#include "pycore_pymem.h"         // _PyMem_DefaultRawFree()
18
#include "pycore_pystate.h"       // _PyInterpreterState_GET()
19
#include "pycore_sysmodule.h"     // _PySys_ClearAttrString()
20
#include "pycore_time.h"          // _PyTime_AsMicroseconds()
21
#include "pycore_unicodeobject.h" // _PyUnicode_AsUTF8NoNUL()
22
#include "pycore_weakref.h"       // _PyWeakref_GET_REF()
23
24
#include "marshal.h"              // PyMarshal_ReadObjectFromString()
25
#include "pycore_importdl.h"      // _PyImport_DynLoadFiletab
26
#include "pydtrace.h"             // PyDTrace_IMPORT_FIND_LOAD_START_ENABLED()
27
#include <stdbool.h>              // bool
28
29
#ifdef HAVE_FCNTL_H
30
#include <fcntl.h>
31
#endif
32
33
34
/*[clinic input]
35
module _imp
36
[clinic start generated code]*/
37
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=9c332475d8686284]*/
38
39
#include "clinic/import.c.h"
40
41
42
#ifndef NDEBUG
43
static bool
44
is_interpreter_isolated(PyInterpreterState *interp)
45
{
46
    return !_Py_IsMainInterpreter(interp)
47
        && !(interp->feature_flags & Py_RTFLAGS_USE_MAIN_OBMALLOC)
48
        && interp->ceval.own_gil;
49
}
50
#endif
51
52
53
/*******************************/
54
/* process-global import state */
55
/*******************************/
56
57
/* This table is defined in config.c: */
58
extern struct _inittab _PyImport_Inittab[];
59
60
// This is not used after Py_Initialize() is called.
61
// (See _PyRuntimeState.imports.inittab.)
62
struct _inittab *PyImport_Inittab = _PyImport_Inittab;
63
// When we dynamically allocate a larger table for PyImport_ExtendInittab(),
64
// we track the pointer here so we can deallocate it during finalization.
65
static struct _inittab *inittab_copy = NULL;
66
67
68
/*******************************/
69
/* runtime-global import state */
70
/*******************************/
71
72
3.31k
#define INITTAB _PyRuntime.imports.inittab
73
453
#define LAST_MODULE_INDEX _PyRuntime.imports.last_module_index
74
986
#define EXTENSIONS _PyRuntime.imports.extensions
75
76
#define PKGCONTEXT (_PyRuntime.imports.pkgcontext)
77
78
79
/*******************************/
80
/* interpreter import state */
81
/*******************************/
82
83
#define MODULES(interp) \
84
58.4k
    (interp)->imports.modules
85
#define MODULES_BY_INDEX(interp) \
86
176
    (interp)->imports.modules_by_index
87
#define IMPORTLIB(interp) \
88
3.31k
    (interp)->imports.importlib
89
#define OVERRIDE_MULTI_INTERP_EXTENSIONS_CHECK(interp) \
90
0
    (interp)->imports.override_multi_interp_extensions_check
91
#define OVERRIDE_FROZEN_MODULES(interp) \
92
2.78k
    (interp)->imports.override_frozen_modules
93
#ifdef HAVE_DLOPEN
94
#  define DLOPENFLAGS(interp) \
95
79
        (interp)->imports.dlopenflags
96
#endif
97
#define IMPORT_FUNC(interp) \
98
12.0k
    (interp)->imports.import_func
99
100
#define IMPORT_LOCK(interp) \
101
42.1k
    (interp)->imports.lock
102
103
#define FIND_AND_LOAD(interp) \
104
2.91k
    (interp)->imports.find_and_load
105
106
#define _IMPORT_TIME_HEADER(interp)                                           \
107
0
    do {                                                                      \
108
0
        if (FIND_AND_LOAD((interp)).header) {                                 \
109
0
            fputs("import time: self [us] | cumulative | imported package\n", \
110
0
                  stderr);                                                    \
111
0
            FIND_AND_LOAD((interp)).header = 0;                               \
112
0
        }                                                                     \
113
0
    } while (0)
114
115
116
/*******************/
117
/* the import lock */
118
/*******************/
119
120
/* Locking primitives to prevent parallel imports of the same module
121
   in different threads to return with a partially loaded module.
122
   These calls are serialized by the global interpreter lock. */
123
124
void
125
_PyImport_AcquireLock(PyInterpreterState *interp)
126
14.0k
{
127
14.0k
    _PyRecursiveMutex_Lock(&IMPORT_LOCK(interp));
128
14.0k
}
129
130
void
131
_PyImport_ReleaseLock(PyInterpreterState *interp)
132
14.0k
{
133
14.0k
    _PyRecursiveMutex_Unlock(&IMPORT_LOCK(interp));
134
14.0k
}
135
136
void
137
_PyImport_ReInitLock(PyInterpreterState *interp)
138
0
{
139
    // gh-126688: Thread id may change after fork() on some operating systems.
140
0
    IMPORT_LOCK(interp).thread = PyThread_get_thread_ident_ex();
141
0
}
142
143
144
/***************/
145
/* sys.modules */
146
/***************/
147
148
PyObject *
149
_PyImport_InitModules(PyInterpreterState *interp)
150
16
{
151
16
    assert(MODULES(interp) == NULL);
152
16
    MODULES(interp) = PyDict_New();
153
16
    if (MODULES(interp) == NULL) {
154
0
        return NULL;
155
0
    }
156
16
    return MODULES(interp);
157
16
}
158
159
PyObject *
160
_PyImport_GetModules(PyInterpreterState *interp)
161
14.3k
{
162
14.3k
    return MODULES(interp);
163
14.3k
}
164
165
PyObject *
166
_PyImport_GetModulesRef(PyInterpreterState *interp)
167
0
{
168
0
    _PyImport_AcquireLock(interp);
169
0
    PyObject *modules = MODULES(interp);
170
0
    if (modules == NULL) {
171
        /* The interpreter hasn't been initialized yet. */
172
0
        modules = Py_None;
173
0
    }
174
0
    Py_INCREF(modules);
175
0
    _PyImport_ReleaseLock(interp);
176
0
    return modules;
177
0
}
178
179
void
180
_PyImport_ClearModules(PyInterpreterState *interp)
181
0
{
182
0
    Py_SETREF(MODULES(interp), NULL);
183
0
}
184
185
static inline PyObject *
186
get_modules_dict(PyThreadState *tstate, bool fatal)
187
44.0k
{
188
    /* Technically, it would make sense to incref the dict,
189
     * since sys.modules could be swapped out and decref'ed to 0
190
     * before the caller is done using it.  However, that is highly
191
     * unlikely, especially since we can rely on a global lock
192
     * (i.e. the GIL) for thread-safety. */
193
44.0k
    PyObject *modules = MODULES(tstate->interp);
194
44.0k
    if (modules == NULL) {
195
0
        if (fatal) {
196
0
            Py_FatalError("interpreter has no modules dictionary");
197
0
        }
198
0
        _PyErr_SetString(tstate, PyExc_RuntimeError,
199
0
                         "unable to get sys.modules");
200
0
        return NULL;
201
0
    }
202
44.0k
    return modules;
203
44.0k
}
204
205
PyObject *
206
PyImport_GetModuleDict(void)
207
0
{
208
0
    PyThreadState *tstate = _PyThreadState_GET();
209
0
    return get_modules_dict(tstate, true);
210
0
}
211
212
int
213
_PyImport_SetModule(PyObject *name, PyObject *m)
214
0
{
215
0
    PyThreadState *tstate = _PyThreadState_GET();
216
0
    PyObject *modules = get_modules_dict(tstate, true);
217
0
    return PyObject_SetItem(modules, name, m);
218
0
}
219
220
int
221
_PyImport_SetModuleString(const char *name, PyObject *m)
222
22
{
223
22
    PyThreadState *tstate = _PyThreadState_GET();
224
22
    PyObject *modules = get_modules_dict(tstate, true);
225
22
    return PyMapping_SetItemString(modules, name, m);
226
22
}
227
228
static PyObject *
229
import_get_module(PyThreadState *tstate, PyObject *name)
230
43.5k
{
231
43.5k
    PyObject *modules = get_modules_dict(tstate, false);
232
43.5k
    if (modules == NULL) {
233
0
        return NULL;
234
0
    }
235
236
43.5k
    PyObject *m;
237
43.5k
    Py_INCREF(modules);
238
43.5k
    (void)PyMapping_GetOptionalItem(modules, name, &m);
239
43.5k
    Py_DECREF(modules);
240
43.5k
    return m;
241
43.5k
}
242
243
static int
244
import_ensure_initialized(PyInterpreterState *interp, PyObject *mod, PyObject *name)
245
35.4k
{
246
35.4k
    PyObject *spec;
247
248
    /* Optimization: only call _bootstrap._lock_unlock_module() if
249
       __spec__._initializing is true.
250
       NOTE: because of this, initializing must be set *before*
251
       stuffing the new module in sys.modules.
252
    */
253
35.4k
    int rc = PyObject_GetOptionalAttr(mod, &_Py_ID(__spec__), &spec);
254
35.4k
    if (rc > 0) {
255
35.4k
        rc = _PyModuleSpec_IsInitializing(spec);
256
35.4k
        Py_DECREF(spec);
257
35.4k
    }
258
35.4k
    if (rc == 0) {
259
35.2k
        goto done;
260
35.2k
    }
261
188
    else if (rc < 0) {
262
0
        return rc;
263
0
    }
264
265
    /* Wait until module is done importing. */
266
188
    PyObject *value = PyObject_CallMethodOneArg(
267
188
        IMPORTLIB(interp), &_Py_ID(_lock_unlock_module), name);
268
188
    if (value == NULL) {
269
0
        return -1;
270
0
    }
271
188
    Py_DECREF(value);
272
273
35.4k
done:
274
    /* When -X importtime=2, print an import time entry even if an
275
       imported module has already been loaded.
276
     */
277
35.4k
    if (_PyInterpreterState_GetConfig(interp)->import_time == 2) {
278
0
        _IMPORT_TIME_HEADER(interp);
279
0
#define import_level FIND_AND_LOAD(interp).import_level
280
0
        fprintf(stderr, "import time: cached    | cached     | %*s\n",
281
0
                import_level*2, PyUnicode_AsUTF8(name));
282
0
#undef import_level
283
0
    }
284
285
35.4k
    return 0;
286
188
}
287
288
static void remove_importlib_frames(PyThreadState *tstate);
289
290
PyObject *
291
PyImport_GetModule(PyObject *name)
292
14.3k
{
293
14.3k
    PyThreadState *tstate = _PyThreadState_GET();
294
14.3k
    PyObject *mod;
295
296
14.3k
    mod = import_get_module(tstate, name);
297
14.3k
    if (mod != NULL && mod != Py_None) {
298
14.3k
        if (import_ensure_initialized(tstate->interp, mod, name) < 0) {
299
0
            Py_DECREF(mod);
300
0
            remove_importlib_frames(tstate);
301
0
            return NULL;
302
0
        }
303
14.3k
    }
304
14.3k
    return mod;
305
14.3k
}
306
307
/* Get the module object corresponding to a module name.
308
   First check the modules dictionary if there's one there,
309
   if not, create a new one and insert it in the modules dictionary. */
310
311
static PyObject *
312
import_add_module(PyThreadState *tstate, PyObject *name)
313
80
{
314
80
    PyObject *modules = get_modules_dict(tstate, false);
315
80
    if (modules == NULL) {
316
0
        return NULL;
317
0
    }
318
319
80
    PyObject *m;
320
80
    if (PyMapping_GetOptionalItem(modules, name, &m) < 0) {
321
0
        return NULL;
322
0
    }
323
80
    if (m != NULL && PyModule_Check(m)) {
324
48
        return m;
325
48
    }
326
32
    Py_XDECREF(m);
327
32
    m = PyModule_NewObject(name);
328
32
    if (m == NULL)
329
0
        return NULL;
330
32
    if (PyObject_SetItem(modules, name, m) != 0) {
331
0
        Py_DECREF(m);
332
0
        return NULL;
333
0
    }
334
335
32
    return m;
336
32
}
337
338
PyObject *
339
PyImport_AddModuleRef(const char *name)
340
48
{
341
48
    PyObject *name_obj = PyUnicode_FromString(name);
342
48
    if (name_obj == NULL) {
343
0
        return NULL;
344
0
    }
345
48
    PyThreadState *tstate = _PyThreadState_GET();
346
48
    PyObject *module = import_add_module(tstate, name_obj);
347
48
    Py_DECREF(name_obj);
348
48
    return module;
349
48
}
350
351
352
PyObject *
353
PyImport_AddModuleObject(PyObject *name)
354
16
{
355
16
    PyThreadState *tstate = _PyThreadState_GET();
356
16
    PyObject *mod = import_add_module(tstate, name);
357
16
    if (!mod) {
358
0
        return NULL;
359
0
    }
360
361
    // gh-86160: PyImport_AddModuleObject() returns a borrowed reference.
362
    // Create a weak reference to produce a borrowed reference, since it can
363
    // become NULL. sys.modules type can be different than dict and it is not
364
    // guaranteed that it keeps a strong reference to the module. It can be a
365
    // custom mapping with __getitem__() which returns a new object or removes
366
    // returned object, or __setitem__ which does nothing. There is so much
367
    // unknown.  With weakref we can be sure that we get either a reference to
368
    // live object or NULL.
369
    //
370
    // Use PyImport_AddModuleRef() to avoid these issues.
371
16
    PyObject *ref = PyWeakref_NewRef(mod, NULL);
372
16
    Py_DECREF(mod);
373
16
    if (ref == NULL) {
374
0
        return NULL;
375
0
    }
376
16
    mod = _PyWeakref_GET_REF(ref);
377
16
    Py_DECREF(ref);
378
16
    Py_XDECREF(mod);
379
380
16
    if (mod == NULL && !PyErr_Occurred()) {
381
0
        PyErr_SetString(PyExc_RuntimeError,
382
0
                        "sys.modules does not hold a strong reference "
383
0
                        "to the module");
384
0
    }
385
16
    return mod; /* borrowed reference */
386
16
}
387
388
389
PyObject *
390
PyImport_AddModule(const char *name)
391
0
{
392
0
    PyObject *nameobj = PyUnicode_FromString(name);
393
0
    if (nameobj == NULL) {
394
0
        return NULL;
395
0
    }
396
0
    PyObject *module = PyImport_AddModuleObject(nameobj);
397
0
    Py_DECREF(nameobj);
398
0
    return module;
399
0
}
400
401
402
/* Remove name from sys.modules, if it's there.
403
 * Can be called with an exception raised.
404
 * If fail to remove name a new exception will be chained with the old
405
 * exception, otherwise the old exception is preserved.
406
 */
407
static void
408
remove_module(PyThreadState *tstate, PyObject *name)
409
0
{
410
0
    PyObject *exc = _PyErr_GetRaisedException(tstate);
411
412
0
    PyObject *modules = get_modules_dict(tstate, true);
413
0
    if (PyDict_CheckExact(modules)) {
414
        // Error is reported to the caller
415
0
        (void)PyDict_Pop(modules, name, NULL);
416
0
    }
417
0
    else if (PyMapping_DelItem(modules, name) < 0) {
418
0
        if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
419
0
            _PyErr_Clear(tstate);
420
0
        }
421
0
    }
422
423
0
    _PyErr_ChainExceptions1(exc);
424
0
}
425
426
427
/************************************/
428
/* per-interpreter modules-by-index */
429
/************************************/
430
431
Py_ssize_t
432
_PyImport_GetNextModuleIndex(void)
433
453
{
434
453
    return _Py_atomic_add_ssize(&LAST_MODULE_INDEX, 1) + 1;
435
453
}
436
437
#ifndef NDEBUG
438
struct extensions_cache_value;
439
static struct extensions_cache_value * _find_cached_def(PyModuleDef *);
440
static Py_ssize_t _get_cached_module_index(struct extensions_cache_value *);
441
#endif
442
443
static Py_ssize_t
444
_get_module_index_from_def(PyModuleDef *def)
445
0
{
446
0
    Py_ssize_t index = def->m_base.m_index;
447
#ifndef NDEBUG
448
    struct extensions_cache_value *cached = _find_cached_def(def);
449
    assert(cached == NULL || index == _get_cached_module_index(cached));
450
#endif
451
0
    return index;
452
0
}
453
454
static void
455
_set_module_index(PyModuleDef *def, Py_ssize_t index)
456
32
{
457
32
    assert(index > 0);
458
32
    if (index == def->m_base.m_index) {
459
        /* There's nothing to do. */
460
32
    }
461
0
    else if (def->m_base.m_index == 0) {
462
        /* It should have been initialized by PyModuleDef_Init().
463
         * We assert here to catch this in dev, but keep going otherwise. */
464
0
        assert(def->m_base.m_index != 0);
465
0
        def->m_base.m_index = index;
466
0
    }
467
0
    else {
468
        /* It was already set for a different module.
469
         * We replace the old value. */
470
0
        assert(def->m_base.m_index > 0);
471
0
        def->m_base.m_index = index;
472
0
    }
473
32
}
474
475
static const char *
476
_modules_by_index_check(PyInterpreterState *interp, Py_ssize_t index)
477
0
{
478
0
    if (index <= 0) {
479
0
        return "invalid module index";
480
0
    }
481
0
    if (MODULES_BY_INDEX(interp) == NULL) {
482
0
        return "Interpreters module-list not accessible.";
483
0
    }
484
0
    if (index >= PyList_GET_SIZE(MODULES_BY_INDEX(interp))) {
485
0
        return "Module index out of bounds.";
486
0
    }
487
0
    return NULL;
488
0
}
489
490
static PyObject *
491
_modules_by_index_get(PyInterpreterState *interp, Py_ssize_t index)
492
0
{
493
0
    if (_modules_by_index_check(interp, index) != NULL) {
494
0
        return NULL;
495
0
    }
496
0
    PyObject *res = PyList_GET_ITEM(MODULES_BY_INDEX(interp), index);
497
0
    return res==Py_None ? NULL : res;
498
0
}
499
500
static int
501
_modules_by_index_set(PyInterpreterState *interp,
502
                      Py_ssize_t index, PyObject *module)
503
32
{
504
32
    assert(index > 0);
505
506
32
    if (MODULES_BY_INDEX(interp) == NULL) {
507
16
        MODULES_BY_INDEX(interp) = PyList_New(0);
508
16
        if (MODULES_BY_INDEX(interp) == NULL) {
509
0
            return -1;
510
0
        }
511
16
    }
512
513
112
    while (PyList_GET_SIZE(MODULES_BY_INDEX(interp)) <= index) {
514
80
        if (PyList_Append(MODULES_BY_INDEX(interp), Py_None) < 0) {
515
0
            return -1;
516
0
        }
517
80
    }
518
519
32
    return PyList_SetItem(MODULES_BY_INDEX(interp), index, Py_NewRef(module));
520
32
}
521
522
static int
523
_modules_by_index_clear_one(PyInterpreterState *interp, Py_ssize_t index)
524
0
{
525
0
    const char *err = _modules_by_index_check(interp, index);
526
0
    if (err != NULL) {
527
0
        Py_FatalError(err);
528
0
        return -1;
529
0
    }
530
0
    return PyList_SetItem(MODULES_BY_INDEX(interp), index, Py_NewRef(Py_None));
531
0
}
532
533
534
PyObject*
535
PyState_FindModule(PyModuleDef* module)
536
0
{
537
0
    PyInterpreterState *interp = _PyInterpreterState_GET();
538
0
    if (module->m_slots) {
539
0
        return NULL;
540
0
    }
541
0
    Py_ssize_t index = _get_module_index_from_def(module);
542
0
    return _modules_by_index_get(interp, index);
543
0
}
544
545
/* _PyState_AddModule() has been completely removed from the C-API
546
   (and was removed from the limited API in 3.6).  However, we're
547
   playing it safe and keeping it around for any stable ABI extensions
548
   built against 3.2-3.5. */
549
int
550
_PyState_AddModule(PyThreadState *tstate, PyObject* module, PyModuleDef* def)
551
0
{
552
0
    if (!def) {
553
0
        assert(_PyErr_Occurred(tstate));
554
0
        return -1;
555
0
    }
556
0
    if (def->m_slots) {
557
0
        _PyErr_SetString(tstate,
558
0
                         PyExc_SystemError,
559
0
                         "PyState_AddModule called on module with slots");
560
0
        return -1;
561
0
    }
562
0
    assert(def->m_slots == NULL);
563
0
    Py_ssize_t index = _get_module_index_from_def(def);
564
0
    return _modules_by_index_set(tstate->interp, index, module);
565
0
}
566
567
int
568
PyState_AddModule(PyObject* module, PyModuleDef* def)
569
0
{
570
0
    if (!def) {
571
0
        Py_FatalError("module definition is NULL");
572
0
        return -1;
573
0
    }
574
575
0
    PyThreadState *tstate = _PyThreadState_GET();
576
0
    if (def->m_slots) {
577
0
        _PyErr_SetString(tstate,
578
0
                         PyExc_SystemError,
579
0
                         "PyState_AddModule called on module with slots");
580
0
        return -1;
581
0
    }
582
583
0
    PyInterpreterState *interp = tstate->interp;
584
0
    Py_ssize_t index = _get_module_index_from_def(def);
585
0
    if (MODULES_BY_INDEX(interp) &&
586
0
        index < PyList_GET_SIZE(MODULES_BY_INDEX(interp)) &&
587
0
        module == PyList_GET_ITEM(MODULES_BY_INDEX(interp), index))
588
0
    {
589
0
        _Py_FatalErrorFormat(__func__, "module %p already added", module);
590
0
        return -1;
591
0
    }
592
593
0
    assert(def->m_slots == NULL);
594
0
    return _modules_by_index_set(interp, index, module);
595
0
}
596
597
int
598
PyState_RemoveModule(PyModuleDef* def)
599
0
{
600
0
    PyThreadState *tstate = _PyThreadState_GET();
601
0
    if (def->m_slots) {
602
0
        _PyErr_SetString(tstate,
603
0
                         PyExc_SystemError,
604
0
                         "PyState_RemoveModule called on module with slots");
605
0
        return -1;
606
0
    }
607
0
    Py_ssize_t index = _get_module_index_from_def(def);
608
0
    return _modules_by_index_clear_one(tstate->interp, index);
609
0
}
610
611
612
// Used by finalize_modules()
613
void
614
_PyImport_ClearModulesByIndex(PyInterpreterState *interp)
615
0
{
616
0
    if (!MODULES_BY_INDEX(interp)) {
617
0
        return;
618
0
    }
619
620
0
    Py_ssize_t i;
621
0
    for (i = 0; i < PyList_GET_SIZE(MODULES_BY_INDEX(interp)); i++) {
622
0
        PyObject *m = PyList_GET_ITEM(MODULES_BY_INDEX(interp), i);
623
0
        if (PyModule_Check(m)) {
624
            /* cleanup the saved copy of module dicts */
625
0
            PyModuleDef *md = PyModule_GetDef(m);
626
0
            if (md) {
627
                // XXX Do this more carefully.  The dict might be owned
628
                // by another interpreter.
629
0
                Py_CLEAR(md->m_base.m_copy);
630
0
            }
631
0
        }
632
0
    }
633
634
    /* Setting modules_by_index to NULL could be dangerous, so we
635
       clear the list instead. */
636
0
    if (PyList_SetSlice(MODULES_BY_INDEX(interp),
637
0
                        0, PyList_GET_SIZE(MODULES_BY_INDEX(interp)),
638
0
                        NULL)) {
639
0
        PyErr_FormatUnraisable("Exception ignored while "
640
0
                               "clearing interpreters module list");
641
0
    }
642
0
}
643
644
645
/*********************/
646
/* extension modules */
647
/*********************/
648
649
/*
650
    It may help to have a big picture view of what happens
651
    when an extension is loaded.  This includes when it is imported
652
    for the first time.
653
654
    Here's a summary, using importlib._bootstrap._load() as a starting point.
655
656
    1.  importlib._bootstrap._load()
657
    2.    _load():  acquire import lock
658
    3.    _load() -> importlib._bootstrap._load_unlocked()
659
    4.      _load_unlocked() -> importlib._bootstrap.module_from_spec()
660
    5.        module_from_spec() -> ExtensionFileLoader.create_module()
661
    6.          create_module() -> _imp.create_dynamic()
662
                    (see below)
663
    7.        module_from_spec() -> importlib._bootstrap._init_module_attrs()
664
    8.      _load_unlocked():  sys.modules[name] = module
665
    9.      _load_unlocked() -> ExtensionFileLoader.exec_module()
666
    10.       exec_module() -> _imp.exec_dynamic()
667
                  (see below)
668
    11.   _load():  release import lock
669
670
671
    ...for single-phase init modules, where m_size == -1:
672
673
    (6). first time  (not found in _PyRuntime.imports.extensions):
674
       A. _imp_create_dynamic_impl() -> import_find_extension()
675
       B. _imp_create_dynamic_impl() -> _PyImport_GetModInitFunc()
676
       C.   _PyImport_GetModInitFunc():  load <module init func>
677
       D. _imp_create_dynamic_impl() -> import_run_extension()
678
       E.   import_run_extension() -> _PyImport_RunModInitFunc()
679
       F.     _PyImport_RunModInitFunc():  call <module init func>
680
       G.       <module init func> -> PyModule_Create() -> PyModule_Create2()
681
                                          -> PyModule_CreateInitialized()
682
       H.         PyModule_CreateInitialized() -> PyModule_New()
683
       I.         PyModule_CreateInitialized():  allocate mod->md_state
684
       J.         PyModule_CreateInitialized() -> PyModule_AddFunctions()
685
       K.         PyModule_CreateInitialized() -> PyModule_SetDocString()
686
       L.       PyModule_CreateInitialized():  set mod->md_def
687
       M.       <module init func>:  initialize the module, etc.
688
       N.   import_run_extension()
689
                -> _PyImport_CheckSubinterpIncompatibleExtensionAllowed()
690
       O.   import_run_extension():  set __file__
691
       P.   import_run_extension() -> update_global_state_for_extension()
692
       Q.     update_global_state_for_extension():
693
                      copy __dict__ into def->m_base.m_copy
694
       R.     update_global_state_for_extension():
695
                      add it to _PyRuntime.imports.extensions
696
       S.   import_run_extension() -> finish_singlephase_extension()
697
       T.     finish_singlephase_extension():
698
                      add it to interp->imports.modules_by_index
699
       U.     finish_singlephase_extension():  add it to sys.modules
700
701
       Step (Q) is skipped for core modules (sys/builtins).
702
703
    (6). subsequent times  (found in _PyRuntime.imports.extensions):
704
       A. _imp_create_dynamic_impl() -> import_find_extension()
705
       B.   import_find_extension() -> reload_singlephase_extension()
706
       C.     reload_singlephase_extension()
707
                  -> _PyImport_CheckSubinterpIncompatibleExtensionAllowed()
708
       D.     reload_singlephase_extension() -> import_add_module()
709
       E.       if name in sys.modules:  use that module
710
       F.       else:
711
                  1. import_add_module() -> PyModule_NewObject()
712
                  2. import_add_module():  set it on sys.modules
713
       G.     reload_singlephase_extension():  copy the "m_copy" dict into __dict__
714
       H.     reload_singlephase_extension():  add to modules_by_index
715
716
    (10). (every time):
717
       A. noop
718
719
720
    ...for single-phase init modules, where m_size >= 0:
721
722
    (6). not main interpreter and never loaded there - every time  (not found in _PyRuntime.imports.extensions):
723
       A-P. (same as for m_size == -1)
724
       Q.     _PyImport_RunModInitFunc():  set def->m_base.m_init
725
       R. (skipped)
726
       S-U. (same as for m_size == -1)
727
728
    (6). main interpreter - first time  (not found in _PyRuntime.imports.extensions):
729
       A-P. (same as for m_size == -1)
730
       Q.     _PyImport_RunModInitFunc():  set def->m_base.m_init
731
       R-U. (same as for m_size == -1)
732
733
    (6). subsequent times  (found in _PyRuntime.imports.extensions):
734
       A. _imp_create_dynamic_impl() -> import_find_extension()
735
       B.   import_find_extension() -> reload_singlephase_extension()
736
       C.     reload_singlephase_extension()
737
                  -> _PyImport_CheckSubinterpIncompatibleExtensionAllowed()
738
       D.     reload_singlephase_extension():  call def->m_base.m_init  (see above)
739
       E.     reload_singlephase_extension():  add the module to sys.modules
740
       F.     reload_singlephase_extension():  add to modules_by_index
741
742
    (10). every time:
743
       A. noop
744
745
746
    ...for multi-phase init modules:
747
748
    (6). every time:
749
       A. _imp_create_dynamic_impl() -> import_find_extension()  (not found)
750
       B. _imp_create_dynamic_impl() -> _PyImport_GetModInitFunc()
751
       C.   _PyImport_GetModInitFunc():  load <module init func>
752
       D. _imp_create_dynamic_impl() -> import_run_extension()
753
       E.   import_run_extension() -> _PyImport_RunModInitFunc()
754
       F.     _PyImport_RunModInitFunc():  call <module init func>
755
       G.   import_run_extension() -> PyModule_FromDefAndSpec()
756
       H.      PyModule_FromDefAndSpec(): gather/check moduledef slots
757
       I.      if there's a Py_mod_create slot:
758
                 1. PyModule_FromDefAndSpec():  call its function
759
       J.      else:
760
                 1. PyModule_FromDefAndSpec() -> PyModule_NewObject()
761
       K:      PyModule_FromDefAndSpec():  set mod->md_def
762
       L.      PyModule_FromDefAndSpec() -> _add_methods_to_object()
763
       M.      PyModule_FromDefAndSpec() -> PyModule_SetDocString()
764
765
    (10). every time:
766
       A. _imp_exec_dynamic_impl() -> exec_builtin_or_dynamic()
767
       B.   if mod->md_state == NULL (including if m_size == 0):
768
            1. exec_builtin_or_dynamic() -> PyModule_ExecDef()
769
            2.   PyModule_ExecDef():  allocate mod->md_state
770
            3.   if there's a Py_mod_exec slot:
771
                 1. PyModule_ExecDef():  call its function
772
 */
773
774
775
/* Make sure name is fully qualified.
776
777
   This is a bit of a hack: when the shared library is loaded,
778
   the module name is "package.module", but the module calls
779
   PyModule_Create*() with just "module" for the name.  The shared
780
   library loader squirrels away the true name of the module in
781
   _PyRuntime.imports.pkgcontext, and PyModule_Create*() will
782
   substitute this (if the name actually matches).
783
*/
784
785
_Py_thread_local const char *pkgcontext = NULL;
786
# undef PKGCONTEXT
787
1.62k
# define PKGCONTEXT pkgcontext
788
789
const char *
790
_PyImport_ResolveNameWithPackageContext(const char *name)
791
64
{
792
64
    if (PKGCONTEXT != NULL) {
793
0
        const char *p = strrchr(PKGCONTEXT, '.');
794
0
        if (p != NULL && strcmp(name, p+1) == 0) {
795
0
            name = PKGCONTEXT;
796
0
            PKGCONTEXT = NULL;
797
0
        }
798
0
    }
799
64
    return name;
800
64
}
801
802
const char *
803
_PyImport_SwapPackageContext(const char *newcontext)
804
778
{
805
778
    const char *oldcontext = PKGCONTEXT;
806
778
    PKGCONTEXT = newcontext;
807
778
    return oldcontext;
808
778
}
809
810
#ifdef HAVE_DLOPEN
811
int
812
_PyImport_GetDLOpenFlags(PyInterpreterState *interp)
813
79
{
814
79
    return DLOPENFLAGS(interp);
815
79
}
816
817
void
818
_PyImport_SetDLOpenFlags(PyInterpreterState *interp, int new_val)
819
0
{
820
0
    DLOPENFLAGS(interp) = new_val;
821
0
}
822
#endif  // HAVE_DLOPEN
823
824
825
/* Common implementation for _imp.exec_dynamic and _imp.exec_builtin */
826
static int
827
389
exec_builtin_or_dynamic(PyObject *mod) {
828
389
    PyModuleDef *def;
829
389
    void *state;
830
831
389
    if (!PyModule_Check(mod)) {
832
0
        return 0;
833
0
    }
834
835
389
    def = PyModule_GetDef(mod);
836
389
    if (def == NULL) {
837
0
        return 0;
838
0
    }
839
840
389
    state = PyModule_GetState(mod);
841
389
    if (state) {
842
        /* Already initialized; skip reload */
843
0
        return 0;
844
0
    }
845
846
389
    return PyModule_ExecDef(mod, def);
847
389
}
848
849
850
static int clear_singlephase_extension(PyInterpreterState *interp,
851
                                       PyObject *name, PyObject *filename);
852
853
// Currently, this is only used for testing.
854
// (See _testinternalcapi.clear_extension().)
855
// If adding another use, be careful about modules that import themselves
856
// recursively (see gh-123880).
857
int
858
_PyImport_ClearExtension(PyObject *name, PyObject *filename)
859
0
{
860
0
    PyInterpreterState *interp = _PyInterpreterState_GET();
861
862
    /* Clearing a module's C globals is up to the module. */
863
0
    if (clear_singlephase_extension(interp, name, filename) < 0) {
864
0
        return -1;
865
0
    }
866
867
    // In the future we'll probably also make sure the extension's
868
    // file handle (and DL handle) is closed (requires saving it).
869
870
0
    return 0;
871
0
}
872
873
874
/*****************************/
875
/* single-phase init modules */
876
/*****************************/
877
878
/*
879
We support a number of kinds of single-phase init builtin/extension modules:
880
881
* "basic"
882
    * no module state (PyModuleDef.m_size == -1)
883
    * does not support repeated init (we use PyModuleDef.m_base.m_copy)
884
    * may have process-global state
885
    * the module's def is cached in _PyRuntime.imports.extensions,
886
      by (name, filename)
887
* "reinit"
888
    * no module state (PyModuleDef.m_size == 0)
889
    * supports repeated init (m_copy is never used)
890
    * should not have any process-global state
891
    * its def is never cached in _PyRuntime.imports.extensions
892
      (except, currently, under the main interpreter, for some reason)
893
* "with state"  (almost the same as reinit)
894
    * has module state (PyModuleDef.m_size > 0)
895
    * supports repeated init (m_copy is never used)
896
    * should not have any process-global state
897
    * its def is never cached in _PyRuntime.imports.extensions
898
      (except, currently, under the main interpreter, for some reason)
899
900
There are also variants within those classes:
901
902
* two or more modules share a PyModuleDef
903
    * a module's init func uses another module's PyModuleDef
904
    * a module's init func calls another's module's init func
905
    * a module's init "func" is actually a variable statically initialized
906
      to another module's init func
907
* two or modules share "methods"
908
    * a module's init func copies another module's PyModuleDef
909
      (with a different name)
910
* (basic-only) two or modules share process-global state
911
912
In the first case, where modules share a PyModuleDef, the following
913
notable weirdness happens:
914
915
* the module's __name__ matches the def, not the requested name
916
* the last module (with the same def) to be imported for the first time wins
917
    * returned by PyState_Find_Module() (via interp->modules_by_index)
918
    * (non-basic-only) its init func is used when re-loading any of them
919
      (via the def's m_init)
920
    * (basic-only) the copy of its __dict__ is used when re-loading any of them
921
      (via the def's m_copy)
922
923
However, the following happens as expected:
924
925
* a new module object (with its own __dict__) is created for each request
926
* the module's __spec__ has the requested name
927
* the loaded module is cached in sys.modules under the requested name
928
* the m_index field of the shared def is not changed,
929
  so at least PyState_FindModule() will always look in the same place
930
931
For "basic" modules there are other quirks:
932
933
* (whether sharing a def or not) when loaded the first time,
934
  m_copy is set before _init_module_attrs() is called
935
  in importlib._bootstrap.module_from_spec(),
936
  so when the module is re-loaded, the previous value
937
  for __wpec__ (and others) is reset, possibly unexpectedly.
938
939
Generally, when multiple interpreters are involved, some of the above
940
gets even messier.
941
*/
942
943
static inline void
944
extensions_lock_acquire(void)
945
453
{
946
453
    PyMutex_Lock(&_PyRuntime.imports.extensions.mutex);
947
453
}
948
949
static inline void
950
extensions_lock_release(void)
951
453
{
952
453
    PyMutex_Unlock(&_PyRuntime.imports.extensions.mutex);
953
453
}
954
955
956
/* Magic for extension modules (built-in as well as dynamically
957
   loaded).  To prevent initializing an extension module more than
958
   once, we keep a static dictionary 'extensions' keyed by the tuple
959
   (module name, module name)  (for built-in modules) or by
960
   (filename, module name) (for dynamically loaded modules), containing these
961
   modules.  A copy of the module's dictionary is stored by calling
962
   fix_up_extension() immediately after the module initialization
963
   function succeeds.  A copy can be retrieved from there by calling
964
   import_find_extension().
965
966
   Modules which do support multiple initialization set their m_size
967
   field to a non-negative number (indicating the size of the
968
   module-specific state). They are still recorded in the extensions
969
   dictionary, to avoid loading shared libraries twice.
970
*/
971
972
typedef struct cached_m_dict {
973
    /* A shallow copy of the original module's __dict__. */
974
    PyObject *copied;
975
    /* The interpreter that owns the copy. */
976
    int64_t interpid;
977
} *cached_m_dict_t;
978
979
struct extensions_cache_value {
980
    PyModuleDef *def;
981
982
    /* The function used to re-initialize the module.
983
       This is only set for legacy (single-phase init) extension modules
984
       and only used for those that support multiple initializations
985
       (m_size >= 0).
986
       It is set by update_global_state_for_extension(). */
987
    PyModInitFunction m_init;
988
989
    /* The module's index into its interpreter's modules_by_index cache.
990
       This is set for all extension modules but only used for legacy ones.
991
       (See PyInterpreterState.modules_by_index for more info.) */
992
    Py_ssize_t m_index;
993
994
    /* A copy of the module's __dict__ after the first time it was loaded.
995
       This is only set/used for legacy modules that do not support
996
       multiple initializations.
997
       It is set exclusively by fixup_cached_def(). */
998
    cached_m_dict_t m_dict;
999
    struct cached_m_dict _m_dict;
1000
1001
    _Py_ext_module_origin origin;
1002
1003
#ifdef Py_GIL_DISABLED
1004
    /* The module's md_gil slot, for legacy modules that are reinitialized from
1005
       m_dict rather than calling their initialization function again. */
1006
    void *md_gil;
1007
#endif
1008
};
1009
1010
static struct extensions_cache_value *
1011
alloc_extensions_cache_value(void)
1012
32
{
1013
32
    struct extensions_cache_value *value
1014
32
            = PyMem_RawMalloc(sizeof(struct extensions_cache_value));
1015
32
    if (value == NULL) {
1016
0
        PyErr_NoMemory();
1017
0
        return NULL;
1018
0
    }
1019
32
    *value = (struct extensions_cache_value){0};
1020
32
    return value;
1021
32
}
1022
1023
static void
1024
free_extensions_cache_value(struct extensions_cache_value *value)
1025
0
{
1026
0
    PyMem_RawFree(value);
1027
0
}
1028
1029
static Py_ssize_t
1030
_get_cached_module_index(struct extensions_cache_value *cached)
1031
32
{
1032
32
    assert(cached->m_index > 0);
1033
32
    return cached->m_index;
1034
32
}
1035
1036
static void
1037
fixup_cached_def(struct extensions_cache_value *value)
1038
32
{
1039
    /* For the moment, the values in the def's m_base may belong
1040
     * to another module, and we're replacing them here.  This can
1041
     * cause problems later if the old module is reloaded.
1042
     *
1043
     * Also, we don't decref any old cached values first when we
1044
     * replace them here, in case we need to restore them in the
1045
     * near future.  Instead, the caller is responsible for wrapping
1046
     * this up by calling cleanup_old_cached_def() or
1047
     * restore_old_cached_def() if there was an error. */
1048
32
    PyModuleDef *def = value->def;
1049
32
    assert(def != NULL);
1050
1051
    /* We assume that all module defs are statically allocated
1052
       and will never be freed.  Otherwise, we would incref here. */
1053
32
    _Py_SetImmortalUntracked((PyObject *)def);
1054
1055
32
    def->m_base.m_init = value->m_init;
1056
1057
32
    assert(value->m_index > 0);
1058
32
    _set_module_index(def, value->m_index);
1059
1060
    /* Different modules can share the same def, so we can't just
1061
     * expect m_copy to be NULL. */
1062
32
    assert(def->m_base.m_copy == NULL
1063
32
           || def->m_base.m_init == NULL
1064
32
           || value->m_dict != NULL);
1065
32
    if (value->m_dict != NULL) {
1066
0
        assert(value->m_dict->copied != NULL);
1067
        /* As noted above, we don't first decref the old value, if any. */
1068
0
        def->m_base.m_copy = Py_NewRef(value->m_dict->copied);
1069
0
    }
1070
32
}
1071
1072
static void
1073
restore_old_cached_def(PyModuleDef *def, PyModuleDef_Base *oldbase)
1074
0
{
1075
0
    def->m_base = *oldbase;
1076
0
}
1077
1078
static void
1079
cleanup_old_cached_def(PyModuleDef_Base *oldbase)
1080
32
{
1081
32
    Py_XDECREF(oldbase->m_copy);
1082
32
}
1083
1084
static void
1085
del_cached_def(struct extensions_cache_value *value)
1086
0
{
1087
    /* If we hadn't made the stored defs immortal, we would decref here.
1088
       However, this decref would be problematic if the module def were
1089
       dynamically allocated, it were the last ref, and this function
1090
       were called with an interpreter other than the def's owner. */
1091
0
    assert(value->def == NULL || _Py_IsImmortal(value->def));
1092
1093
0
    Py_XDECREF(value->def->m_base.m_copy);
1094
0
    value->def->m_base.m_copy = NULL;
1095
0
}
1096
1097
static int
1098
init_cached_m_dict(struct extensions_cache_value *value, PyObject *m_dict)
1099
32
{
1100
32
    assert(value != NULL);
1101
    /* This should only have been called without an m_dict already set. */
1102
32
    assert(value->m_dict == NULL);
1103
32
    if (m_dict == NULL) {
1104
32
        return 0;
1105
32
    }
1106
32
    assert(PyDict_Check(m_dict));
1107
0
    assert(value->origin != _Py_ext_module_origin_CORE);
1108
1109
0
    PyInterpreterState *interp = _PyInterpreterState_GET();
1110
0
    assert(!is_interpreter_isolated(interp));
1111
1112
    /* XXX gh-88216: The copied dict is owned by the current
1113
     * interpreter.  That's a problem if the interpreter has
1114
     * its own obmalloc state or if the module is successfully
1115
     * imported into such an interpreter.  If the interpreter
1116
     * has its own GIL then there may be data races and
1117
     * PyImport_ClearModulesByIndex() can crash.  Normally,
1118
     * a single-phase init module cannot be imported in an
1119
     * isolated interpreter, but there are ways around that.
1120
     * Hence, heere be dragons!  Ideally we would instead do
1121
     * something like make a read-only, immortal copy of the
1122
     * dict using PyMem_RawMalloc() and store *that* in m_copy.
1123
     * Then we'd need to make sure to clear that when the
1124
     * runtime is finalized, rather than in
1125
     * PyImport_ClearModulesByIndex(). */
1126
0
    PyObject *copied = PyDict_Copy(m_dict);
1127
0
    if (copied == NULL) {
1128
        /* We expect this can only be "out of memory". */
1129
0
        return -1;
1130
0
    }
1131
    // XXX We may want to make the copy immortal.
1132
1133
0
    value->_m_dict = (struct cached_m_dict){
1134
0
        .copied=copied,
1135
0
        .interpid=PyInterpreterState_GetID(interp),
1136
0
    };
1137
1138
0
    value->m_dict = &value->_m_dict;
1139
0
    return 0;
1140
0
}
1141
1142
static void
1143
del_cached_m_dict(struct extensions_cache_value *value)
1144
0
{
1145
0
    if (value->m_dict != NULL) {
1146
0
        assert(value->m_dict == &value->_m_dict);
1147
0
        assert(value->m_dict->copied != NULL);
1148
        /* In the future we can take advantage of m_dict->interpid
1149
         * to decref the dict using the owning interpreter. */
1150
0
        Py_XDECREF(value->m_dict->copied);
1151
0
        value->m_dict = NULL;
1152
0
    }
1153
0
}
1154
1155
static PyObject * get_core_module_dict(
1156
        PyInterpreterState *interp, PyObject *name, PyObject *path);
1157
1158
static PyObject *
1159
get_cached_m_dict(struct extensions_cache_value *value,
1160
                  PyObject *name, PyObject *path)
1161
0
{
1162
0
    assert(value != NULL);
1163
0
    PyInterpreterState *interp = _PyInterpreterState_GET();
1164
    /* It might be a core module (e.g. sys & builtins),
1165
       for which we don't cache m_dict. */
1166
0
    if (value->origin == _Py_ext_module_origin_CORE) {
1167
0
        return get_core_module_dict(interp, name, path);
1168
0
    }
1169
0
    assert(value->def != NULL);
1170
    // XXX Switch to value->m_dict.
1171
0
    PyObject *m_dict = value->def->m_base.m_copy;
1172
0
    Py_XINCREF(m_dict);
1173
0
    return m_dict;
1174
0
}
1175
1176
static void
1177
del_extensions_cache_value(void *raw)
1178
0
{
1179
0
    struct extensions_cache_value *value = raw;
1180
0
    if (value != NULL) {
1181
0
        del_cached_m_dict(value);
1182
0
        del_cached_def(value);
1183
0
        free_extensions_cache_value(value);
1184
0
    }
1185
0
}
1186
1187
static void *
1188
hashtable_key_from_2_strings(PyObject *str1, PyObject *str2, const char sep)
1189
437
{
1190
437
    const char *str1_data = _PyUnicode_AsUTF8NoNUL(str1);
1191
437
    const char *str2_data = _PyUnicode_AsUTF8NoNUL(str2);
1192
437
    if (str1_data == NULL || str2_data == NULL) {
1193
0
        return NULL;
1194
0
    }
1195
437
    Py_ssize_t str1_len = strlen(str1_data);
1196
437
    Py_ssize_t str2_len = strlen(str2_data);
1197
1198
    /* Make sure sep and the NULL byte won't cause an overflow. */
1199
437
    assert(SIZE_MAX - str1_len - str2_len > 2);
1200
437
    size_t size = str1_len + 1 + str2_len + 1;
1201
1202
    // XXX Use a buffer if it's a temp value (every case but "set").
1203
437
    char *key = PyMem_RawMalloc(size);
1204
437
    if (key == NULL) {
1205
0
        PyErr_NoMemory();
1206
0
        return NULL;
1207
0
    }
1208
1209
437
    memcpy(key, str1_data, str1_len);
1210
437
    key[str1_len] = sep;
1211
437
    memcpy(key + str1_len + 1, str2_data, str2_len);
1212
437
    key[size - 1] = '\0';
1213
437
    assert(strlen(key) == size - 1);
1214
437
    return key;
1215
437
}
1216
1217
static Py_uhash_t
1218
hashtable_hash_str(const void *key)
1219
469
{
1220
469
    return Py_HashBuffer(key, strlen((const char *)key));
1221
469
}
1222
1223
static int
1224
hashtable_compare_str(const void *key1, const void *key2)
1225
0
{
1226
0
    return strcmp((const char *)key1, (const char *)key2) == 0;
1227
0
}
1228
1229
static void
1230
hashtable_destroy_str(void *ptr)
1231
405
{
1232
405
    PyMem_RawFree(ptr);
1233
405
}
1234
1235
#ifndef NDEBUG
1236
struct hashtable_next_match_def_data {
1237
    PyModuleDef *def;
1238
    struct extensions_cache_value *matched;
1239
};
1240
1241
static int
1242
hashtable_next_match_def(_Py_hashtable_t *ht,
1243
                         const void *key, const void *value, void *user_data)
1244
{
1245
    if (value == NULL) {
1246
        /* It was previously deleted. */
1247
        return 0;
1248
    }
1249
    struct hashtable_next_match_def_data *data
1250
            = (struct hashtable_next_match_def_data *)user_data;
1251
    struct extensions_cache_value *cur
1252
            = (struct extensions_cache_value *)value;
1253
    if (cur->def == data->def) {
1254
        data->matched = cur;
1255
        return 1;
1256
    }
1257
    return 0;
1258
}
1259
1260
static struct extensions_cache_value *
1261
_find_cached_def(PyModuleDef *def)
1262
{
1263
    struct hashtable_next_match_def_data data = {0};
1264
    (void)_Py_hashtable_foreach(
1265
            EXTENSIONS.hashtable, hashtable_next_match_def, &data);
1266
    return data.matched;
1267
}
1268
#endif
1269
1270
437
#define HTSEP ':'
1271
1272
static int
1273
_extensions_cache_init(void)
1274
16
{
1275
16
    _Py_hashtable_allocator_t alloc = {PyMem_RawMalloc, PyMem_RawFree};
1276
16
    EXTENSIONS.hashtable = _Py_hashtable_new_full(
1277
16
        hashtable_hash_str,
1278
16
        hashtable_compare_str,
1279
16
        hashtable_destroy_str,  // key
1280
16
        del_extensions_cache_value,  // value
1281
16
        &alloc
1282
16
    );
1283
16
    if (EXTENSIONS.hashtable == NULL) {
1284
0
        PyErr_NoMemory();
1285
0
        return -1;
1286
0
    }
1287
16
    return 0;
1288
16
}
1289
1290
static _Py_hashtable_entry_t *
1291
_extensions_cache_find_unlocked(PyObject *path, PyObject *name,
1292
                                void **p_key)
1293
453
{
1294
453
    if (EXTENSIONS.hashtable == NULL) {
1295
16
        return NULL;
1296
16
    }
1297
437
    void *key = hashtable_key_from_2_strings(path, name, HTSEP);
1298
437
    if (key == NULL) {
1299
0
        return NULL;
1300
0
    }
1301
437
    _Py_hashtable_entry_t *entry =
1302
437
            _Py_hashtable_get_entry(EXTENSIONS.hashtable, key);
1303
437
    if (p_key != NULL) {
1304
32
        *p_key = key;
1305
32
    }
1306
405
    else {
1307
405
        hashtable_destroy_str(key);
1308
405
    }
1309
437
    return entry;
1310
437
}
1311
1312
/* This can only fail with "out of memory". */
1313
static struct extensions_cache_value *
1314
_extensions_cache_get(PyObject *path, PyObject *name)
1315
421
{
1316
421
    struct extensions_cache_value *value = NULL;
1317
421
    extensions_lock_acquire();
1318
1319
421
    _Py_hashtable_entry_t *entry =
1320
421
            _extensions_cache_find_unlocked(path, name, NULL);
1321
421
    if (entry == NULL) {
1322
        /* It was never added. */
1323
421
        goto finally;
1324
421
    }
1325
0
    value = (struct extensions_cache_value *)entry->value;
1326
1327
421
finally:
1328
421
    extensions_lock_release();
1329
421
    return value;
1330
0
}
1331
1332
/* This can only fail with "out of memory". */
1333
static struct extensions_cache_value *
1334
_extensions_cache_set(PyObject *path, PyObject *name,
1335
                      PyModuleDef *def, PyModInitFunction m_init,
1336
                      Py_ssize_t m_index, PyObject *m_dict,
1337
                      _Py_ext_module_origin origin, void *md_gil)
1338
32
{
1339
32
    struct extensions_cache_value *value = NULL;
1340
32
    void *key = NULL;
1341
32
    struct extensions_cache_value *newvalue = NULL;
1342
32
    PyModuleDef_Base olddefbase = def->m_base;
1343
1344
32
    assert(def != NULL);
1345
32
    assert(m_init == NULL || m_dict == NULL);
1346
    /* We expect the same symbol to be used and the shared object file
1347
     * to have remained loaded, so it must be the same pointer. */
1348
32
    assert(def->m_base.m_init == NULL || def->m_base.m_init == m_init);
1349
    /* For now we don't worry about comparing value->m_copy. */
1350
32
    assert(def->m_base.m_copy == NULL || m_dict != NULL);
1351
32
    assert((origin == _Py_ext_module_origin_DYNAMIC) == (name != path));
1352
32
    assert(origin != _Py_ext_module_origin_CORE || m_dict == NULL);
1353
1354
32
    extensions_lock_acquire();
1355
1356
32
    if (EXTENSIONS.hashtable == NULL) {
1357
16
        if (_extensions_cache_init() < 0) {
1358
0
            goto finally;
1359
0
        }
1360
16
    }
1361
1362
    /* Create a cached value to populate for the module. */
1363
32
    _Py_hashtable_entry_t *entry =
1364
32
            _extensions_cache_find_unlocked(path, name, &key);
1365
32
    value = entry == NULL
1366
32
        ? NULL
1367
32
        : (struct extensions_cache_value *)entry->value;
1368
32
    if (value != NULL) {
1369
        /* gh-123880: If there's an existing cache value, it means a module is
1370
         * being imported recursively from its PyInit_* or Py_mod_* function.
1371
         * (That function presumably handles returning a partially
1372
         *  constructed module in such a case.)
1373
         * We can reuse the existing cache value; it is owned by the cache.
1374
         * (Entries get removed from it in exceptional circumstances,
1375
         *  after interpreter shutdown, and in runtime shutdown.)
1376
         */
1377
0
        goto finally_oldvalue;
1378
0
    }
1379
32
    newvalue = alloc_extensions_cache_value();
1380
32
    if (newvalue == NULL) {
1381
0
        goto finally;
1382
0
    }
1383
1384
    /* Populate the new cache value data. */
1385
32
    *newvalue = (struct extensions_cache_value){
1386
32
        .def=def,
1387
32
        .m_init=m_init,
1388
32
        .m_index=m_index,
1389
        /* m_dict is set by set_cached_m_dict(). */
1390
32
        .origin=origin,
1391
#ifdef Py_GIL_DISABLED
1392
        .md_gil=md_gil,
1393
#endif
1394
32
    };
1395
32
#ifndef Py_GIL_DISABLED
1396
32
    (void)md_gil;
1397
32
#endif
1398
32
    if (init_cached_m_dict(newvalue, m_dict) < 0) {
1399
0
        goto finally;
1400
0
    }
1401
32
    fixup_cached_def(newvalue);
1402
1403
32
    if (entry == NULL) {
1404
        /* It was never added. */
1405
32
        if (_Py_hashtable_set(EXTENSIONS.hashtable, key, newvalue) < 0) {
1406
0
            PyErr_NoMemory();
1407
0
            goto finally;
1408
0
        }
1409
        /* The hashtable owns the key now. */
1410
32
        key = NULL;
1411
32
    }
1412
0
    else if (value == NULL) {
1413
        /* It was previously deleted. */
1414
0
        entry->value = newvalue;
1415
0
    }
1416
0
    else {
1417
        /* We are updating the entry for an existing module. */
1418
        /* We expect def to be static, so it must be the same pointer. */
1419
0
        assert(value->def == def);
1420
        /* We expect the same symbol to be used and the shared object file
1421
         * to have remained loaded, so it must be the same pointer. */
1422
0
        assert(value->m_init == m_init);
1423
        /* The same module can't switch between caching __dict__ and not. */
1424
0
        assert((value->m_dict == NULL) == (m_dict == NULL));
1425
        /* This shouldn't ever happen. */
1426
0
        Py_UNREACHABLE();
1427
0
    }
1428
1429
32
    value = newvalue;
1430
1431
32
finally:
1432
32
    if (value == NULL) {
1433
0
        restore_old_cached_def(def, &olddefbase);
1434
0
        if (newvalue != NULL) {
1435
0
            del_extensions_cache_value(newvalue);
1436
0
        }
1437
0
    }
1438
32
    else {
1439
32
        cleanup_old_cached_def(&olddefbase);
1440
32
    }
1441
1442
32
finally_oldvalue:
1443
32
    extensions_lock_release();
1444
32
    if (key != NULL) {
1445
0
        hashtable_destroy_str(key);
1446
0
    }
1447
1448
32
    return value;
1449
32
}
1450
1451
static void
1452
_extensions_cache_delete(PyObject *path, PyObject *name)
1453
0
{
1454
0
    extensions_lock_acquire();
1455
1456
0
    if (EXTENSIONS.hashtable == NULL) {
1457
        /* It was never added. */
1458
0
        goto finally;
1459
0
    }
1460
1461
0
    _Py_hashtable_entry_t *entry =
1462
0
            _extensions_cache_find_unlocked(path, name, NULL);
1463
0
    if (entry == NULL) {
1464
        /* It was never added. */
1465
0
        goto finally;
1466
0
    }
1467
0
    if (entry->value == NULL) {
1468
        /* It was already removed. */
1469
0
        goto finally;
1470
0
    }
1471
0
    struct extensions_cache_value *value = entry->value;
1472
0
    entry->value = NULL;
1473
1474
0
    del_extensions_cache_value(value);
1475
1476
0
finally:
1477
0
    extensions_lock_release();
1478
0
}
1479
1480
static void
1481
_extensions_cache_clear_all(void)
1482
0
{
1483
    /* The runtime (i.e. main interpreter) must be finalizing,
1484
       so we don't need to worry about the lock. */
1485
0
    _Py_hashtable_destroy(EXTENSIONS.hashtable);
1486
0
    EXTENSIONS.hashtable = NULL;
1487
0
}
1488
1489
#undef HTSEP
1490
1491
1492
static bool
1493
check_multi_interp_extensions(PyInterpreterState *interp)
1494
0
{
1495
0
    int override = OVERRIDE_MULTI_INTERP_EXTENSIONS_CHECK(interp);
1496
0
    if (override < 0) {
1497
0
        return false;
1498
0
    }
1499
0
    else if (override > 0) {
1500
0
        return true;
1501
0
    }
1502
0
    else if (_PyInterpreterState_HasFeature(
1503
0
                interp, Py_RTFLAGS_MULTI_INTERP_EXTENSIONS)) {
1504
0
        return true;
1505
0
    }
1506
0
    return false;
1507
0
}
1508
1509
int
1510
_PyImport_CheckSubinterpIncompatibleExtensionAllowed(const char *name)
1511
0
{
1512
0
    PyInterpreterState *interp = _PyInterpreterState_GET();
1513
0
    if (check_multi_interp_extensions(interp)) {
1514
0
        assert(!_Py_IsMainInterpreter(interp));
1515
0
        PyErr_Format(PyExc_ImportError,
1516
0
                     "module %s does not support loading in subinterpreters",
1517
0
                     name);
1518
0
        return -1;
1519
0
    }
1520
0
    return 0;
1521
0
}
1522
1523
#ifdef Py_GIL_DISABLED
1524
int
1525
_PyImport_CheckGILForModule(PyObject* module, PyObject *module_name)
1526
{
1527
    PyThreadState *tstate = _PyThreadState_GET();
1528
    if (module == NULL) {
1529
        _PyEval_DisableGIL(tstate);
1530
        return 0;
1531
    }
1532
1533
    if (!PyModule_Check(module) ||
1534
        ((PyModuleObject *)module)->md_gil == Py_MOD_GIL_USED) {
1535
        if (_PyEval_EnableGILPermanent(tstate)) {
1536
            int warn_result = PyErr_WarnFormat(
1537
                PyExc_RuntimeWarning,
1538
                1,
1539
                "The global interpreter lock (GIL) has been enabled to load "
1540
                "module '%U', which has not declared that it can run safely "
1541
                "without the GIL. To override this behavior and keep the GIL "
1542
                "disabled (at your own risk), run with PYTHON_GIL=0 or -Xgil=0.",
1543
                module_name
1544
            );
1545
            if (warn_result < 0) {
1546
                return warn_result;
1547
            }
1548
        }
1549
1550
        const PyConfig *config = _PyInterpreterState_GetConfig(tstate->interp);
1551
        if (config->enable_gil == _PyConfig_GIL_DEFAULT && config->verbose) {
1552
            PySys_FormatStderr("# loading module '%U', which requires the GIL\n",
1553
                               module_name);
1554
        }
1555
    }
1556
    else {
1557
        _PyEval_DisableGIL(tstate);
1558
    }
1559
1560
    return 0;
1561
}
1562
#endif
1563
1564
static PyThreadState *
1565
switch_to_main_interpreter(PyThreadState *tstate)
1566
389
{
1567
389
    if (_Py_IsMainInterpreter(tstate->interp)) {
1568
389
        return tstate;
1569
389
    }
1570
0
    PyThreadState *main_tstate = _PyThreadState_NewBound(
1571
0
            _PyInterpreterState_Main(), _PyThreadState_WHENCE_EXEC);
1572
0
    if (main_tstate == NULL) {
1573
0
        return NULL;
1574
0
    }
1575
#ifndef NDEBUG
1576
    PyThreadState *old_tstate = PyThreadState_Swap(main_tstate);
1577
    assert(old_tstate == tstate);
1578
#else
1579
0
    (void)PyThreadState_Swap(main_tstate);
1580
0
#endif
1581
0
    return main_tstate;
1582
0
}
1583
1584
static void
1585
switch_back_from_main_interpreter(PyThreadState *tstate,
1586
                                  PyThreadState *main_tstate,
1587
                                  PyObject *tempobj)
1588
0
{
1589
0
    assert(main_tstate == PyThreadState_GET());
1590
0
    assert(_Py_IsMainInterpreter(main_tstate->interp));
1591
0
    assert(tstate->interp != main_tstate->interp);
1592
1593
    /* Handle any exceptions, which we cannot propagate directly
1594
     * to the subinterpreter. */
1595
0
    if (PyErr_Occurred()) {
1596
0
        if (PyErr_ExceptionMatches(PyExc_MemoryError)) {
1597
            /* We trust it will be caught again soon. */
1598
0
            PyErr_Clear();
1599
0
        }
1600
0
        else {
1601
            /* Printing the exception should be sufficient. */
1602
0
            PyErr_PrintEx(0);
1603
0
        }
1604
0
    }
1605
1606
0
    Py_XDECREF(tempobj);
1607
1608
0
    PyThreadState_Clear(main_tstate);
1609
0
    (void)PyThreadState_Swap(tstate);
1610
0
    PyThreadState_Delete(main_tstate);
1611
0
}
1612
1613
static PyObject *
1614
get_core_module_dict(PyInterpreterState *interp,
1615
                     PyObject *name, PyObject *path)
1616
0
{
1617
    /* Only builtin modules are core. */
1618
0
    if (path == name) {
1619
0
        assert(!PyErr_Occurred());
1620
0
        if (PyUnicode_CompareWithASCIIString(name, "sys") == 0) {
1621
0
            return Py_NewRef(interp->sysdict_copy);
1622
0
        }
1623
0
        assert(!PyErr_Occurred());
1624
0
        if (PyUnicode_CompareWithASCIIString(name, "builtins") == 0) {
1625
0
            return Py_NewRef(interp->builtins_copy);
1626
0
        }
1627
0
        assert(!PyErr_Occurred());
1628
0
    }
1629
0
    return NULL;
1630
0
}
1631
1632
#ifndef NDEBUG
1633
static inline int
1634
is_core_module(PyInterpreterState *interp, PyObject *name, PyObject *path)
1635
{
1636
    /* This might be called before the core dict copies are in place,
1637
       so we can't rely on get_core_module_dict() here. */
1638
    if (path == name) {
1639
        if (PyUnicode_CompareWithASCIIString(name, "sys") == 0) {
1640
            return 1;
1641
        }
1642
        if (PyUnicode_CompareWithASCIIString(name, "builtins") == 0) {
1643
            return 1;
1644
        }
1645
    }
1646
    return 0;
1647
}
1648
1649
1650
static _Py_ext_module_kind
1651
_get_extension_kind(PyModuleDef *def, bool check_size)
1652
{
1653
    _Py_ext_module_kind kind;
1654
    if (def == NULL) {
1655
        /* It must be a module created by reload_singlephase_extension()
1656
         * from m_copy.  Ideally we'd do away with this case. */
1657
        kind = _Py_ext_module_kind_SINGLEPHASE;
1658
    }
1659
    else if (def->m_slots != NULL) {
1660
        kind = _Py_ext_module_kind_MULTIPHASE;
1661
    }
1662
    else if (check_size && def->m_size == -1) {
1663
        kind = _Py_ext_module_kind_SINGLEPHASE;
1664
    }
1665
    else if (def->m_base.m_init != NULL) {
1666
        kind = _Py_ext_module_kind_SINGLEPHASE;
1667
    }
1668
    else {
1669
        // This is probably single-phase init, but a multi-phase
1670
        // module *can* have NULL m_slots.
1671
        kind = _Py_ext_module_kind_UNKNOWN;
1672
    }
1673
    return kind;
1674
}
1675
1676
/* The module might not be fully initialized yet
1677
 * and PyModule_FromDefAndSpec() checks m_size
1678
 * so we skip m_size. */
1679
#define assert_multiphase_def(def)                                  \
1680
    do {                                                            \
1681
        _Py_ext_module_kind kind = _get_extension_kind(def, false); \
1682
        assert(kind == _Py_ext_module_kind_MULTIPHASE               \
1683
                /* m_slots can be NULL. */                          \
1684
                || kind == _Py_ext_module_kind_UNKNOWN);            \
1685
    } while (0)
1686
1687
#define assert_singlephase_def(def)                                 \
1688
    do {                                                            \
1689
        _Py_ext_module_kind kind = _get_extension_kind(def, true);  \
1690
        assert(kind == _Py_ext_module_kind_SINGLEPHASE              \
1691
                || kind == _Py_ext_module_kind_UNKNOWN);            \
1692
    } while (0)
1693
1694
#define assert_singlephase(cached)                                          \
1695
    do {                                                                    \
1696
        _Py_ext_module_kind kind = _get_extension_kind(cached->def, true);  \
1697
        assert(kind == _Py_ext_module_kind_SINGLEPHASE);                    \
1698
    } while (0)
1699
1700
#else  /* defined(NDEBUG) */
1701
#define assert_multiphase_def(def)
1702
#define assert_singlephase_def(def)
1703
#define assert_singlephase(cached)
1704
#endif
1705
1706
1707
struct singlephase_global_update {
1708
    PyModInitFunction m_init;
1709
    Py_ssize_t m_index;
1710
    PyObject *m_dict;
1711
    _Py_ext_module_origin origin;
1712
    void *md_gil;
1713
};
1714
1715
static struct extensions_cache_value *
1716
update_global_state_for_extension(PyThreadState *tstate,
1717
                                  PyObject *path, PyObject *name,
1718
                                  PyModuleDef *def,
1719
                                  struct singlephase_global_update *singlephase)
1720
32
{
1721
32
    struct extensions_cache_value *cached = NULL;
1722
32
    PyModInitFunction m_init = NULL;
1723
32
    PyObject *m_dict = NULL;
1724
1725
    /* Set up for _extensions_cache_set(). */
1726
32
    if (singlephase == NULL) {
1727
0
        assert(def->m_base.m_init == NULL);
1728
0
        assert(def->m_base.m_copy == NULL);
1729
0
    }
1730
32
    else {
1731
32
        if (singlephase->m_init != NULL) {
1732
0
            assert(singlephase->m_dict == NULL);
1733
0
            assert(def->m_base.m_copy == NULL);
1734
0
            assert(def->m_size >= 0);
1735
            /* Remember pointer to module init function. */
1736
            // XXX If two modules share a def then def->m_base will
1737
            // reflect the last one added (here) to the global cache.
1738
            // We should prevent this somehow.  The simplest solution
1739
            // is probably to store m_copy/m_init in the cache along
1740
            // with the def, rather than within the def.
1741
0
            m_init = singlephase->m_init;
1742
0
        }
1743
32
        else if (singlephase->m_dict == NULL) {
1744
            /* It must be a core builtin module. */
1745
32
            assert(is_core_module(tstate->interp, name, path));
1746
32
            assert(def->m_size == -1);
1747
32
            assert(def->m_base.m_copy == NULL);
1748
32
            assert(def->m_base.m_init == NULL);
1749
32
        }
1750
0
        else {
1751
0
            assert(PyDict_Check(singlephase->m_dict));
1752
            // gh-88216: Extensions and def->m_base.m_copy can be updated
1753
            // when the extension module doesn't support sub-interpreters.
1754
0
            assert(def->m_size == -1);
1755
0
            assert(!is_core_module(tstate->interp, name, path));
1756
0
            assert(PyUnicode_CompareWithASCIIString(name, "sys") != 0);
1757
0
            assert(PyUnicode_CompareWithASCIIString(name, "builtins") != 0);
1758
0
            m_dict = singlephase->m_dict;
1759
0
        }
1760
32
    }
1761
1762
    /* Add the module's def to the global cache. */
1763
    // XXX Why special-case the main interpreter?
1764
32
    if (_Py_IsMainInterpreter(tstate->interp) || def->m_size == -1) {
1765
#ifndef NDEBUG
1766
        cached = _extensions_cache_get(path, name);
1767
        assert(cached == NULL || cached->def == def);
1768
#endif
1769
32
        cached = _extensions_cache_set(
1770
32
                path, name, def, m_init, singlephase->m_index, m_dict,
1771
32
                singlephase->origin, singlephase->md_gil);
1772
32
        if (cached == NULL) {
1773
            // XXX Ignore this error?  Doing so would effectively
1774
            // mark the module as not loadable.
1775
0
            return NULL;
1776
0
        }
1777
32
    }
1778
1779
32
    return cached;
1780
32
}
1781
1782
/* For multi-phase init modules, the module is finished
1783
 * by PyModule_FromDefAndSpec(). */
1784
static int
1785
finish_singlephase_extension(PyThreadState *tstate, PyObject *mod,
1786
                             struct extensions_cache_value *cached,
1787
                             PyObject *name, PyObject *modules)
1788
32
{
1789
32
    assert(mod != NULL && PyModule_Check(mod));
1790
32
    assert(cached->def == _PyModule_GetDef(mod));
1791
1792
32
    Py_ssize_t index = _get_cached_module_index(cached);
1793
32
    if (_modules_by_index_set(tstate->interp, index, mod) < 0) {
1794
0
        return -1;
1795
0
    }
1796
1797
32
    if (modules != NULL) {
1798
32
        if (PyObject_SetItem(modules, name, mod) < 0) {
1799
0
            return -1;
1800
0
        }
1801
32
    }
1802
1803
32
    return 0;
1804
32
}
1805
1806
1807
static PyObject *
1808
reload_singlephase_extension(PyThreadState *tstate,
1809
                             struct extensions_cache_value *cached,
1810
                             struct _Py_ext_module_loader_info *info)
1811
0
{
1812
0
    PyModuleDef *def = cached->def;
1813
0
    assert(def != NULL);
1814
0
    assert_singlephase(cached);
1815
0
    PyObject *mod = NULL;
1816
1817
    /* It may have been successfully imported previously
1818
       in an interpreter that allows legacy modules
1819
       but is not allowed in the current interpreter. */
1820
0
    const char *name_buf = PyUnicode_AsUTF8(info->name);
1821
0
    assert(name_buf != NULL);
1822
0
    if (_PyImport_CheckSubinterpIncompatibleExtensionAllowed(name_buf) < 0) {
1823
0
        return NULL;
1824
0
    }
1825
1826
0
    PyObject *modules = get_modules_dict(tstate, true);
1827
0
    if (def->m_size == -1) {
1828
        /* Module does not support repeated initialization */
1829
0
        assert(cached->m_init == NULL);
1830
0
        assert(def->m_base.m_init == NULL);
1831
        // XXX Copying the cached dict may break interpreter isolation.
1832
        // We could solve this by temporarily acquiring the original
1833
        // interpreter's GIL.
1834
0
        PyObject *m_copy = get_cached_m_dict(cached, info->name, info->path);
1835
0
        if (m_copy == NULL) {
1836
0
            assert(!PyErr_Occurred());
1837
0
            return NULL;
1838
0
        }
1839
0
        mod = import_add_module(tstate, info->name);
1840
0
        if (mod == NULL) {
1841
0
            Py_DECREF(m_copy);
1842
0
            return NULL;
1843
0
        }
1844
0
        PyObject *mdict = PyModule_GetDict(mod);
1845
0
        if (mdict == NULL) {
1846
0
            Py_DECREF(m_copy);
1847
0
            Py_DECREF(mod);
1848
0
            return NULL;
1849
0
        }
1850
0
        int rc = PyDict_Update(mdict, m_copy);
1851
0
        Py_DECREF(m_copy);
1852
0
        if (rc < 0) {
1853
0
            Py_DECREF(mod);
1854
0
            return NULL;
1855
0
        }
1856
#ifdef Py_GIL_DISABLED
1857
        if (def->m_base.m_copy != NULL) {
1858
            // For non-core modules, fetch the GIL slot that was stored by
1859
            // import_run_extension().
1860
            ((PyModuleObject *)mod)->md_gil = cached->md_gil;
1861
        }
1862
#endif
1863
        /* We can't set mod->md_def if it's missing,
1864
         * because _PyImport_ClearModulesByIndex() might break
1865
         * due to violating interpreter isolation.
1866
         * See the note in set_cached_m_dict().
1867
         * Until that is solved, we leave md_def set to NULL. */
1868
0
        assert(_PyModule_GetDef(mod) == NULL
1869
0
               || _PyModule_GetDef(mod) == def);
1870
0
    }
1871
0
    else {
1872
0
        assert(cached->m_dict == NULL);
1873
0
        assert(def->m_base.m_copy == NULL);
1874
        // XXX Use cached->m_init.
1875
0
        PyModInitFunction p0 = def->m_base.m_init;
1876
0
        if (p0 == NULL) {
1877
0
            assert(!PyErr_Occurred());
1878
0
            return NULL;
1879
0
        }
1880
0
        struct _Py_ext_module_loader_result res;
1881
0
        if (_PyImport_RunModInitFunc(p0, info, &res) < 0) {
1882
0
            _Py_ext_module_loader_result_apply_error(&res, name_buf);
1883
0
            return NULL;
1884
0
        }
1885
0
        assert(!PyErr_Occurred());
1886
0
        assert(res.err == NULL);
1887
0
        assert(res.kind == _Py_ext_module_kind_SINGLEPHASE);
1888
0
        mod = res.module;
1889
        /* Tchnically, the init function could return a different module def.
1890
         * Then we would probably need to update the global cache.
1891
         * However, we don't expect anyone to change the def. */
1892
0
        assert(res.def == def);
1893
0
        _Py_ext_module_loader_result_clear(&res);
1894
1895
        /* Remember the filename as the __file__ attribute */
1896
0
        if (info->filename != NULL) {
1897
0
            if (PyModule_AddObjectRef(mod, "__file__", info->filename) < 0) {
1898
0
                PyErr_Clear(); /* Not important enough to report */
1899
0
            }
1900
0
        }
1901
1902
0
        if (PyObject_SetItem(modules, info->name, mod) == -1) {
1903
0
            Py_DECREF(mod);
1904
0
            return NULL;
1905
0
        }
1906
0
    }
1907
1908
0
    Py_ssize_t index = _get_cached_module_index(cached);
1909
0
    if (_modules_by_index_set(tstate->interp, index, mod) < 0) {
1910
0
        PyMapping_DelItem(modules, info->name);
1911
0
        Py_DECREF(mod);
1912
0
        return NULL;
1913
0
    }
1914
1915
0
    return mod;
1916
0
}
1917
1918
static PyObject *
1919
import_find_extension(PyThreadState *tstate,
1920
                      struct _Py_ext_module_loader_info *info,
1921
                      struct extensions_cache_value **p_cached)
1922
389
{
1923
    /* Only single-phase init modules will be in the cache. */
1924
389
    struct extensions_cache_value *cached
1925
389
            = _extensions_cache_get(info->path, info->name);
1926
389
    if (cached == NULL) {
1927
389
        return NULL;
1928
389
    }
1929
389
    assert(cached->def != NULL);
1930
0
    assert_singlephase(cached);
1931
0
    *p_cached = cached;
1932
1933
    /* It may have been successfully imported previously
1934
       in an interpreter that allows legacy modules
1935
       but is not allowed in the current interpreter. */
1936
0
    const char *name_buf = PyUnicode_AsUTF8(info->name);
1937
0
    assert(name_buf != NULL);
1938
0
    if (_PyImport_CheckSubinterpIncompatibleExtensionAllowed(name_buf) < 0) {
1939
0
        return NULL;
1940
0
    }
1941
1942
0
    PyObject *mod = reload_singlephase_extension(tstate, cached, info);
1943
0
    if (mod == NULL) {
1944
0
        return NULL;
1945
0
    }
1946
1947
0
    int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose;
1948
0
    if (verbose) {
1949
0
        PySys_FormatStderr("import %U # previously loaded (%R)\n",
1950
0
                           info->name, info->path);
1951
0
    }
1952
1953
0
    return mod;
1954
0
}
1955
1956
static PyObject *
1957
import_run_extension(PyThreadState *tstate, PyModInitFunction p0,
1958
                     struct _Py_ext_module_loader_info *info,
1959
                     PyObject *spec, PyObject *modules)
1960
389
{
1961
    /* Core modules go through _PyImport_FixupBuiltin(). */
1962
389
    assert(!is_core_module(tstate->interp, info->name, info->path));
1963
1964
389
    PyObject *mod = NULL;
1965
389
    PyModuleDef *def = NULL;
1966
389
    struct extensions_cache_value *cached = NULL;
1967
389
    const char *name_buf = PyBytes_AS_STRING(info->name_encoded);
1968
1969
    /* We cannot know if the module is single-phase init or
1970
     * multi-phase init until after we call its init function. Even
1971
     * in isolated interpreters (that do not support single-phase init),
1972
     * the init function will run without restriction.  For multi-phase
1973
     * init modules that isn't a problem because the init function only
1974
     * runs PyModuleDef_Init() on the module's def and then returns it.
1975
     *
1976
     * However, for single-phase init the module's init function will
1977
     * create the module, create other objects (and allocate other
1978
     * memory), populate it and its module state, and initialize static
1979
     * types.  Some modules store other objects and data in global C
1980
     * variables and register callbacks with the runtime/stdlib or
1981
     * even external libraries (which is part of why we can't just
1982
     * dlclose() the module in the error case).  That's a problem
1983
     * for isolated interpreters since all of the above happens
1984
     * and only then * will the import fail.  Memory will leak,
1985
     * callbacks will still get used, and sometimes there
1986
     * will be crashes (memory access violations
1987
     * and use-after-free).
1988
     *
1989
     * To put it another way, if the module is single-phase init
1990
     * then the import will probably break interpreter isolation
1991
     * and should fail ASAP.  However, the module's init function
1992
     * will still get run.  That means it may still store state
1993
     * in the shared-object/DLL address space (which never gets
1994
     * closed/cleared), including objects (e.g. static types).
1995
     * This is a problem for isolated subinterpreters since each
1996
     * has its own object allocator.  If the loaded shared-object
1997
     * still holds a reference to an object after the corresponding
1998
     * interpreter has finalized then either we must let it leak
1999
     * or else any later use of that object by another interpreter
2000
     * (or across multiple init-fini cycles) will crash the process.
2001
     *
2002
     * To avoid all of that, we make sure the module's init function
2003
     * is always run first with the main interpreter active.  If it was
2004
     * already the main interpreter then we can continue loading the
2005
     * module like normal.  Otherwise, right after the init function,
2006
     * we take care of some import state bookkeeping, switch back
2007
     * to the subinterpreter, check for single-phase init,
2008
     * and then continue loading like normal. */
2009
2010
389
    bool switched = false;
2011
    /* We *could* leave in place a legacy interpreter here
2012
     * (one that shares obmalloc/GIL with main interp),
2013
     * but there isn't a big advantage, we anticipate
2014
     * such interpreters will be increasingly uncommon,
2015
     * and the code is a bit simpler if we always switch
2016
     * to the main interpreter. */
2017
389
    PyThreadState *main_tstate = switch_to_main_interpreter(tstate);
2018
389
    if (main_tstate == NULL) {
2019
0
        return NULL;
2020
0
    }
2021
389
    else if (main_tstate != tstate) {
2022
0
        switched = true;
2023
        /* In the switched case, we could play it safe
2024
         * by getting the main interpreter's import lock here.
2025
         * It's unlikely to matter though. */
2026
0
    }
2027
2028
389
    struct _Py_ext_module_loader_result res;
2029
389
    int rc = _PyImport_RunModInitFunc(p0, info, &res);
2030
389
    if (rc < 0) {
2031
        /* We discard res.def. */
2032
0
        assert(res.module == NULL);
2033
0
    }
2034
389
    else {
2035
389
        assert(!PyErr_Occurred());
2036
389
        assert(res.err == NULL);
2037
2038
389
        mod = res.module;
2039
389
        res.module = NULL;
2040
389
        def = res.def;
2041
389
        assert(def != NULL);
2042
2043
        /* Do anything else that should be done
2044
         * while still using the main interpreter. */
2045
389
        if (res.kind == _Py_ext_module_kind_SINGLEPHASE) {
2046
            /* Remember the filename as the __file__ attribute */
2047
0
            if (info->filename != NULL) {
2048
0
                PyObject *filename = NULL;
2049
0
                if (switched) {
2050
                    // The original filename may be allocated by subinterpreter's
2051
                    // obmalloc, so we create a copy here.
2052
0
                    filename = _PyUnicode_Copy(info->filename);
2053
0
                    if (filename == NULL) {
2054
0
                        return NULL;
2055
0
                    }
2056
0
                } else {
2057
0
                    filename = Py_NewRef(info->filename);
2058
0
                }
2059
                // XXX There's a refleak somewhere with the filename.
2060
                // Until we can track it down, we immortalize it.
2061
0
                PyInterpreterState *interp = _PyInterpreterState_GET();
2062
0
                _PyUnicode_InternImmortal(interp, &filename);
2063
2064
0
                if (PyModule_AddObjectRef(mod, "__file__", filename) < 0) {
2065
0
                    PyErr_Clear(); /* Not important enough to report */
2066
0
                }
2067
0
            }
2068
2069
            /* Update global import state. */
2070
0
            assert(def->m_base.m_index != 0);
2071
0
            struct singlephase_global_update singlephase = {
2072
                // XXX Modules that share a def should each get their own index,
2073
                // whereas currently they share (which means the per-interpreter
2074
                // cache is less reliable than it should be).
2075
0
                .m_index=def->m_base.m_index,
2076
0
                .origin=info->origin,
2077
#ifdef Py_GIL_DISABLED
2078
                .md_gil=((PyModuleObject *)mod)->md_gil,
2079
#endif
2080
0
            };
2081
            // gh-88216: Extensions and def->m_base.m_copy can be updated
2082
            // when the extension module doesn't support sub-interpreters.
2083
0
            if (def->m_size == -1) {
2084
                /* We will reload from m_copy. */
2085
0
                assert(def->m_base.m_init == NULL);
2086
0
                singlephase.m_dict = PyModule_GetDict(mod);
2087
0
                assert(singlephase.m_dict != NULL);
2088
0
            }
2089
0
            else {
2090
                /* We will reload via the init function. */
2091
0
                assert(def->m_size >= 0);
2092
0
                assert(def->m_base.m_copy == NULL);
2093
0
                singlephase.m_init = p0;
2094
0
            }
2095
0
            cached = update_global_state_for_extension(
2096
0
                    main_tstate, info->path, info->name, def, &singlephase);
2097
0
            if (cached == NULL) {
2098
0
                assert(PyErr_Occurred());
2099
0
                goto main_finally;
2100
0
            }
2101
0
        }
2102
389
    }
2103
2104
389
main_finally:
2105
    /* Switch back to the subinterpreter. */
2106
389
    if (switched) {
2107
0
        assert(main_tstate != tstate);
2108
0
        switch_back_from_main_interpreter(tstate, main_tstate, mod);
2109
        /* Any module we got from the init function will have to be
2110
         * reloaded in the subinterpreter. */
2111
0
        mod = NULL;
2112
0
    }
2113
2114
    /*****************************************************************/
2115
    /* At this point we are back to the interpreter we started with. */
2116
    /*****************************************************************/
2117
2118
    /* Finally we handle the error return from _PyImport_RunModInitFunc(). */
2119
389
    if (rc < 0) {
2120
0
        _Py_ext_module_loader_result_apply_error(&res, name_buf);
2121
0
        goto error;
2122
0
    }
2123
2124
389
    if (res.kind == _Py_ext_module_kind_MULTIPHASE) {
2125
389
        assert_multiphase_def(def);
2126
389
        assert(mod == NULL);
2127
        /* Note that we cheat a little by not repeating the calls
2128
         * to _PyImport_GetModInitFunc() and _PyImport_RunModInitFunc(). */
2129
389
        mod = PyModule_FromDefAndSpec(def, spec);
2130
389
        if (mod == NULL) {
2131
0
            goto error;
2132
0
        }
2133
389
    }
2134
0
    else {
2135
0
        assert(res.kind == _Py_ext_module_kind_SINGLEPHASE);
2136
0
        assert_singlephase_def(def);
2137
2138
0
        if (_PyImport_CheckSubinterpIncompatibleExtensionAllowed(name_buf) < 0) {
2139
0
            goto error;
2140
0
        }
2141
0
        assert(!PyErr_Occurred());
2142
2143
0
        if (switched) {
2144
            /* We switched to the main interpreter to run the init
2145
             * function, so now we will "reload" the module from the
2146
             * cached data using the original subinterpreter. */
2147
0
            assert(mod == NULL);
2148
0
            mod = reload_singlephase_extension(tstate, cached, info);
2149
0
            if (mod == NULL) {
2150
0
                goto error;
2151
0
            }
2152
0
            assert(!PyErr_Occurred());
2153
0
            assert(PyModule_Check(mod));
2154
0
        }
2155
0
        else {
2156
0
            assert(mod != NULL);
2157
0
            assert(PyModule_Check(mod));
2158
2159
            /* Update per-interpreter import state. */
2160
0
            PyObject *modules = get_modules_dict(tstate, true);
2161
0
            if (finish_singlephase_extension(
2162
0
                    tstate, mod, cached, info->name, modules) < 0)
2163
0
            {
2164
0
                goto error;
2165
0
            }
2166
0
        }
2167
0
    }
2168
2169
389
    _Py_ext_module_loader_result_clear(&res);
2170
389
    return mod;
2171
2172
0
error:
2173
0
    Py_XDECREF(mod);
2174
0
    _Py_ext_module_loader_result_clear(&res);
2175
0
    return NULL;
2176
389
}
2177
2178
2179
// Used in _PyImport_ClearExtension; see notes there.
2180
static int
2181
clear_singlephase_extension(PyInterpreterState *interp,
2182
                            PyObject *name, PyObject *path)
2183
0
{
2184
0
    struct extensions_cache_value *cached = _extensions_cache_get(path, name);
2185
0
    if (cached == NULL) {
2186
0
        if (PyErr_Occurred()) {
2187
0
            return -1;
2188
0
        }
2189
0
        return 0;
2190
0
    }
2191
0
    PyModuleDef *def = cached->def;
2192
2193
    /* Clear data set when the module was initially loaded. */
2194
0
    def->m_base.m_init = NULL;
2195
0
    Py_CLEAR(def->m_base.m_copy);
2196
0
    def->m_base.m_index = 0;
2197
2198
    /* Clear the PyState_*Module() cache entry. */
2199
0
    Py_ssize_t index = _get_cached_module_index(cached);
2200
0
    if (_modules_by_index_check(interp, index) == NULL) {
2201
0
        if (_modules_by_index_clear_one(interp, index) < 0) {
2202
0
            return -1;
2203
0
        }
2204
0
    }
2205
2206
    /* We must use the main interpreter to clean up the cache.
2207
     * See the note in import_run_extension(). */
2208
0
    PyThreadState *tstate = PyThreadState_GET();
2209
0
    PyThreadState *main_tstate = switch_to_main_interpreter(tstate);
2210
0
    if (main_tstate == NULL) {
2211
0
        return -1;
2212
0
    }
2213
2214
    /* Clear the cached module def. */
2215
0
    _extensions_cache_delete(path, name);
2216
2217
0
    if (main_tstate != tstate) {
2218
0
        switch_back_from_main_interpreter(tstate, main_tstate, NULL);
2219
0
    }
2220
2221
0
    return 0;
2222
0
}
2223
2224
2225
/*******************/
2226
/* builtin modules */
2227
/*******************/
2228
2229
int
2230
_PyImport_FixupBuiltin(PyThreadState *tstate, PyObject *mod, const char *name,
2231
                       PyObject *modules)
2232
32
{
2233
32
    int res = -1;
2234
32
    assert(mod != NULL && PyModule_Check(mod));
2235
2236
32
    PyObject *nameobj;
2237
32
    nameobj = PyUnicode_InternFromString(name);
2238
32
    if (nameobj == NULL) {
2239
0
        return -1;
2240
0
    }
2241
2242
32
    PyModuleDef *def = PyModule_GetDef(mod);
2243
32
    if (def == NULL) {
2244
0
        PyErr_BadInternalCall();
2245
0
        goto finally;
2246
0
    }
2247
2248
    /* We only use _PyImport_FixupBuiltin() for the core builtin modules
2249
     * (sys and builtins).  These modules are single-phase init with no
2250
     * module state, but we also don't populate def->m_base.m_copy
2251
     * for them. */
2252
32
    assert(is_core_module(tstate->interp, nameobj, nameobj));
2253
32
    assert_singlephase_def(def);
2254
32
    assert(def->m_size == -1);
2255
32
    assert(def->m_base.m_copy == NULL);
2256
32
    assert(def->m_base.m_index >= 0);
2257
2258
    /* We aren't using import_find_extension() for core modules,
2259
     * so we have to do the extra check to make sure the module
2260
     * isn't already in the global cache before calling
2261
     * update_global_state_for_extension(). */
2262
32
    struct extensions_cache_value *cached
2263
32
            = _extensions_cache_get(nameobj, nameobj);
2264
32
    if (cached == NULL) {
2265
32
        struct singlephase_global_update singlephase = {
2266
32
            .m_index=def->m_base.m_index,
2267
            /* We don't want def->m_base.m_copy populated. */
2268
32
            .m_dict=NULL,
2269
32
            .origin=_Py_ext_module_origin_CORE,
2270
#ifdef Py_GIL_DISABLED
2271
            /* Unused when m_dict == NULL. */
2272
            .md_gil=NULL,
2273
#endif
2274
32
        };
2275
32
        cached = update_global_state_for_extension(
2276
32
                tstate, nameobj, nameobj, def, &singlephase);
2277
32
        if (cached == NULL) {
2278
0
            goto finally;
2279
0
        }
2280
32
    }
2281
2282
32
    if (finish_singlephase_extension(tstate, mod, cached, nameobj, modules) < 0) {
2283
0
        goto finally;
2284
0
    }
2285
2286
32
    res = 0;
2287
2288
32
finally:
2289
32
    Py_DECREF(nameobj);
2290
32
    return res;
2291
32
}
2292
2293
/* Helper to test for built-in module */
2294
2295
static int
2296
is_builtin(PyObject *name)
2297
2.95k
{
2298
2.95k
    int i;
2299
2.95k
    struct _inittab *inittab = INITTAB;
2300
109k
    for (i = 0; inittab[i].name != NULL; i++) {
2301
106k
        if (_PyUnicode_EqualToASCIIString(name, inittab[i].name)) {
2302
294
            if (inittab[i].initfunc == NULL)
2303
0
                return -1;
2304
294
            else
2305
294
                return 1;
2306
294
        }
2307
106k
    }
2308
2.66k
    return 0;
2309
2.95k
}
2310
2311
static PyObject*
2312
create_builtin(PyThreadState *tstate, PyObject *name, PyObject *spec)
2313
310
{
2314
310
    struct _Py_ext_module_loader_info info;
2315
310
    if (_Py_ext_module_loader_info_init_for_builtin(&info, name) < 0) {
2316
0
        return NULL;
2317
0
    }
2318
2319
310
    struct extensions_cache_value *cached = NULL;
2320
310
    PyObject *mod = import_find_extension(tstate, &info, &cached);
2321
310
    if (mod != NULL) {
2322
0
        assert(!_PyErr_Occurred(tstate));
2323
0
        assert(cached != NULL);
2324
        /* The module might not have md_def set in certain reload cases. */
2325
0
        assert(_PyModule_GetDef(mod) == NULL
2326
0
                || cached->def == _PyModule_GetDef(mod));
2327
0
        assert_singlephase(cached);
2328
0
        goto finally;
2329
0
    }
2330
310
    else if (_PyErr_Occurred(tstate)) {
2331
0
        goto finally;
2332
0
    }
2333
2334
    /* If the module was added to the global cache
2335
     * but def->m_base.m_copy was cleared (e.g. subinterp fini)
2336
     * then we have to do a little dance here. */
2337
310
    if (cached != NULL) {
2338
0
        assert(cached->def->m_base.m_copy == NULL);
2339
        /* For now we clear the cache and move on. */
2340
0
        _extensions_cache_delete(info.path, info.name);
2341
0
    }
2342
2343
310
    struct _inittab *found = NULL;
2344
5.85k
    for (struct _inittab *p = INITTAB; p->name != NULL; p++) {
2345
5.85k
        if (_PyUnicode_EqualToASCIIString(info.name, p->name)) {
2346
310
            found = p;
2347
310
            break;
2348
310
        }
2349
5.85k
    }
2350
310
    if (found == NULL) {
2351
        // not found
2352
0
        mod = Py_NewRef(Py_None);
2353
0
        goto finally;
2354
0
    }
2355
2356
310
    PyModInitFunction p0 = (PyModInitFunction)found->initfunc;
2357
310
    if (p0 == NULL) {
2358
        /* Cannot re-init internal module ("sys" or "builtins") */
2359
0
        assert(is_core_module(tstate->interp, info.name, info.path));
2360
0
        mod = import_add_module(tstate, info.name);
2361
0
        goto finally;
2362
0
    }
2363
2364
#ifdef Py_GIL_DISABLED
2365
    // This call (and the corresponding call to _PyImport_CheckGILForModule())
2366
    // would ideally be inside import_run_extension(). They are kept in the
2367
    // callers for now because that would complicate the control flow inside
2368
    // import_run_extension(). It should be possible to restructure
2369
    // import_run_extension() to address this.
2370
    _PyEval_EnableGILTransient(tstate);
2371
#endif
2372
    /* Now load it. */
2373
310
    mod = import_run_extension(
2374
310
                    tstate, p0, &info, spec, get_modules_dict(tstate, true));
2375
#ifdef Py_GIL_DISABLED
2376
    if (_PyImport_CheckGILForModule(mod, info.name) < 0) {
2377
        Py_CLEAR(mod);
2378
        goto finally;
2379
    }
2380
#endif
2381
2382
310
finally:
2383
310
    _Py_ext_module_loader_info_clear(&info);
2384
310
    return mod;
2385
310
}
2386
2387
2388
/*****************************/
2389
/* the builtin modules table */
2390
/*****************************/
2391
2392
/* API for embedding applications that want to add their own entries
2393
   to the table of built-in modules.  This should normally be called
2394
   *before* Py_Initialize().  When the table resize fails, -1 is
2395
   returned and the existing table is unchanged.
2396
2397
   After a similar function by Just van Rossum. */
2398
2399
int
2400
PyImport_ExtendInittab(struct _inittab *newtab)
2401
0
{
2402
0
    struct _inittab *p;
2403
0
    size_t i, n;
2404
0
    int res = 0;
2405
2406
0
    if (INITTAB != NULL) {
2407
0
        Py_FatalError("PyImport_ExtendInittab() may not be called after Py_Initialize()");
2408
0
    }
2409
2410
    /* Count the number of entries in both tables */
2411
0
    for (n = 0; newtab[n].name != NULL; n++)
2412
0
        ;
2413
0
    if (n == 0)
2414
0
        return 0; /* Nothing to do */
2415
0
    for (i = 0; PyImport_Inittab[i].name != NULL; i++)
2416
0
        ;
2417
2418
    /* Force default raw memory allocator to get a known allocator to be able
2419
       to release the memory in _PyImport_Fini2() */
2420
    /* Allocate new memory for the combined table */
2421
0
    p = NULL;
2422
0
    if (i + n <= SIZE_MAX / sizeof(struct _inittab) - 1) {
2423
0
        size_t size = sizeof(struct _inittab) * (i + n + 1);
2424
0
        p = _PyMem_DefaultRawRealloc(inittab_copy, size);
2425
0
    }
2426
0
    if (p == NULL) {
2427
0
        res = -1;
2428
0
        goto done;
2429
0
    }
2430
2431
    /* Copy the tables into the new memory at the first call
2432
       to PyImport_ExtendInittab(). */
2433
0
    if (inittab_copy != PyImport_Inittab) {
2434
0
        memcpy(p, PyImport_Inittab, (i+1) * sizeof(struct _inittab));
2435
0
    }
2436
0
    memcpy(p + i, newtab, (n + 1) * sizeof(struct _inittab));
2437
0
    PyImport_Inittab = inittab_copy = p;
2438
0
done:
2439
0
    return res;
2440
0
}
2441
2442
/* Shorthand to add a single entry given a name and a function */
2443
2444
int
2445
PyImport_AppendInittab(const char *name, PyObject* (*initfunc)(void))
2446
0
{
2447
0
    struct _inittab newtab[2];
2448
2449
0
    if (INITTAB != NULL) {
2450
0
        Py_FatalError("PyImport_AppendInittab() may not be called after Py_Initialize()");
2451
0
    }
2452
2453
0
    memset(newtab, '\0', sizeof newtab);
2454
2455
0
    newtab[0].name = name;
2456
0
    newtab[0].initfunc = initfunc;
2457
2458
0
    return PyImport_ExtendInittab(newtab);
2459
0
}
2460
2461
2462
/* the internal table */
2463
2464
static int
2465
init_builtin_modules_table(void)
2466
16
{
2467
16
    size_t size;
2468
624
    for (size = 0; PyImport_Inittab[size].name != NULL; size++)
2469
608
        ;
2470
16
    size++;
2471
2472
    /* Make the copy. */
2473
16
    struct _inittab *copied = _PyMem_DefaultRawMalloc(size * sizeof(struct _inittab));
2474
16
    if (copied == NULL) {
2475
0
        return -1;
2476
0
    }
2477
16
    memcpy(copied, PyImport_Inittab, size * sizeof(struct _inittab));
2478
16
    INITTAB = copied;
2479
16
    return 0;
2480
16
}
2481
2482
static void
2483
fini_builtin_modules_table(void)
2484
0
{
2485
0
    struct _inittab *inittab = INITTAB;
2486
0
    INITTAB = NULL;
2487
0
    _PyMem_DefaultRawFree(inittab);
2488
0
}
2489
2490
PyObject *
2491
_PyImport_GetBuiltinModuleNames(void)
2492
16
{
2493
16
    PyObject *list = PyList_New(0);
2494
16
    if (list == NULL) {
2495
0
        return NULL;
2496
0
    }
2497
16
    struct _inittab *inittab = INITTAB;
2498
624
    for (Py_ssize_t i = 0; inittab[i].name != NULL; i++) {
2499
608
        PyObject *name = PyUnicode_FromString(inittab[i].name);
2500
608
        if (name == NULL) {
2501
0
            Py_DECREF(list);
2502
0
            return NULL;
2503
0
        }
2504
608
        if (PyList_Append(list, name) < 0) {
2505
0
            Py_DECREF(name);
2506
0
            Py_DECREF(list);
2507
0
            return NULL;
2508
0
        }
2509
608
        Py_DECREF(name);
2510
608
    }
2511
16
    return list;
2512
16
}
2513
2514
2515
/********************/
2516
/* the magic number */
2517
/********************/
2518
2519
/* Helper for pythonrun.c -- return magic number and tag. */
2520
2521
long
2522
PyImport_GetMagicNumber(void)
2523
0
{
2524
0
    return PYC_MAGIC_NUMBER_TOKEN;
2525
0
}
2526
2527
extern const char * _PySys_ImplCacheTag;
2528
2529
const char *
2530
PyImport_GetMagicTag(void)
2531
0
{
2532
0
    return _PySys_ImplCacheTag;
2533
0
}
2534
2535
2536
/*********************************/
2537
/* a Python module's code object */
2538
/*********************************/
2539
2540
/* Execute a code object in a module and return the module object
2541
 * WITH INCREMENTED REFERENCE COUNT.  If an error occurs, name is
2542
 * removed from sys.modules, to avoid leaving damaged module objects
2543
 * in sys.modules.  The caller may wish to restore the original
2544
 * module object (if any) in this case; PyImport_ReloadModule is an
2545
 * example.
2546
 *
2547
 * Note that PyImport_ExecCodeModuleWithPathnames() is the preferred, richer
2548
 * interface.  The other two exist primarily for backward compatibility.
2549
 */
2550
PyObject *
2551
PyImport_ExecCodeModule(const char *name, PyObject *co)
2552
0
{
2553
0
    return PyImport_ExecCodeModuleWithPathnames(
2554
0
        name, co, (char *)NULL, (char *)NULL);
2555
0
}
2556
2557
PyObject *
2558
PyImport_ExecCodeModuleEx(const char *name, PyObject *co, const char *pathname)
2559
0
{
2560
0
    return PyImport_ExecCodeModuleWithPathnames(
2561
0
        name, co, pathname, (char *)NULL);
2562
0
}
2563
2564
PyObject *
2565
PyImport_ExecCodeModuleWithPathnames(const char *name, PyObject *co,
2566
                                     const char *pathname,
2567
                                     const char *cpathname)
2568
0
{
2569
0
    PyObject *m = NULL;
2570
0
    PyObject *nameobj, *pathobj = NULL, *cpathobj = NULL, *external= NULL;
2571
2572
0
    nameobj = PyUnicode_FromString(name);
2573
0
    if (nameobj == NULL)
2574
0
        return NULL;
2575
2576
0
    if (cpathname != NULL) {
2577
0
        cpathobj = PyUnicode_DecodeFSDefault(cpathname);
2578
0
        if (cpathobj == NULL)
2579
0
            goto error;
2580
0
    }
2581
0
    else
2582
0
        cpathobj = NULL;
2583
2584
0
    if (pathname != NULL) {
2585
0
        pathobj = PyUnicode_DecodeFSDefault(pathname);
2586
0
        if (pathobj == NULL)
2587
0
            goto error;
2588
0
    }
2589
0
    else if (cpathobj != NULL) {
2590
0
        PyInterpreterState *interp = _PyInterpreterState_GET();
2591
2592
0
        if (interp == NULL) {
2593
0
            Py_FatalError("no current interpreter");
2594
0
        }
2595
2596
0
        external= PyObject_GetAttrString(IMPORTLIB(interp),
2597
0
                                         "_bootstrap_external");
2598
0
        if (external != NULL) {
2599
0
            pathobj = PyObject_CallMethodOneArg(
2600
0
                external, &_Py_ID(_get_sourcefile), cpathobj);
2601
0
            Py_DECREF(external);
2602
0
        }
2603
0
        if (pathobj == NULL)
2604
0
            PyErr_Clear();
2605
0
    }
2606
0
    else
2607
0
        pathobj = NULL;
2608
2609
0
    m = PyImport_ExecCodeModuleObject(nameobj, co, pathobj, cpathobj);
2610
0
error:
2611
0
    Py_DECREF(nameobj);
2612
0
    Py_XDECREF(pathobj);
2613
0
    Py_XDECREF(cpathobj);
2614
0
    return m;
2615
0
}
2616
2617
static PyObject *
2618
module_dict_for_exec(PyThreadState *tstate, PyObject *name)
2619
16
{
2620
16
    PyObject *m, *d;
2621
2622
16
    m = import_add_module(tstate, name);
2623
16
    if (m == NULL)
2624
0
        return NULL;
2625
    /* If the module is being reloaded, we get the old module back
2626
       and re-use its dict to exec the new code. */
2627
16
    d = PyModule_GetDict(m);
2628
16
    int r = PyDict_Contains(d, &_Py_ID(__builtins__));
2629
16
    if (r == 0) {
2630
16
        r = PyDict_SetItem(d, &_Py_ID(__builtins__), PyEval_GetBuiltins());
2631
16
    }
2632
16
    if (r < 0) {
2633
0
        remove_module(tstate, name);
2634
0
        Py_DECREF(m);
2635
0
        return NULL;
2636
0
    }
2637
2638
16
    Py_INCREF(d);
2639
16
    Py_DECREF(m);
2640
16
    return d;
2641
16
}
2642
2643
static PyObject *
2644
exec_code_in_module(PyThreadState *tstate, PyObject *name,
2645
                    PyObject *module_dict, PyObject *code_object)
2646
16
{
2647
16
    PyObject *v, *m;
2648
2649
16
    v = PyEval_EvalCode(code_object, module_dict, module_dict);
2650
16
    if (v == NULL) {
2651
0
        remove_module(tstate, name);
2652
0
        return NULL;
2653
0
    }
2654
16
    Py_DECREF(v);
2655
2656
16
    m = import_get_module(tstate, name);
2657
16
    if (m == NULL && !_PyErr_Occurred(tstate)) {
2658
0
        _PyErr_Format(tstate, PyExc_ImportError,
2659
0
                      "Loaded module %R not found in sys.modules",
2660
0
                      name);
2661
0
    }
2662
2663
16
    return m;
2664
16
}
2665
2666
PyObject*
2667
PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname,
2668
                              PyObject *cpathname)
2669
0
{
2670
0
    PyThreadState *tstate = _PyThreadState_GET();
2671
0
    PyObject *d, *external, *res;
2672
2673
0
    d = module_dict_for_exec(tstate, name);
2674
0
    if (d == NULL) {
2675
0
        return NULL;
2676
0
    }
2677
2678
0
    if (pathname == NULL) {
2679
0
        pathname = ((PyCodeObject *)co)->co_filename;
2680
0
    }
2681
0
    external = PyObject_GetAttrString(IMPORTLIB(tstate->interp),
2682
0
                                      "_bootstrap_external");
2683
0
    if (external == NULL) {
2684
0
        Py_DECREF(d);
2685
0
        return NULL;
2686
0
    }
2687
0
    res = PyObject_CallMethodObjArgs(external, &_Py_ID(_fix_up_module),
2688
0
                                     d, name, pathname, cpathname, NULL);
2689
0
    Py_DECREF(external);
2690
0
    if (res != NULL) {
2691
0
        Py_DECREF(res);
2692
0
        res = exec_code_in_module(tstate, name, d, co);
2693
0
    }
2694
0
    Py_DECREF(d);
2695
0
    return res;
2696
0
}
2697
2698
2699
static void
2700
update_code_filenames(PyCodeObject *co, PyObject *oldname, PyObject *newname)
2701
0
{
2702
0
    PyObject *constants, *tmp;
2703
0
    Py_ssize_t i, n;
2704
2705
0
    if (PyUnicode_Compare(co->co_filename, oldname))
2706
0
        return;
2707
2708
0
    Py_XSETREF(co->co_filename, Py_NewRef(newname));
2709
2710
0
    constants = co->co_consts;
2711
0
    n = PyTuple_GET_SIZE(constants);
2712
0
    for (i = 0; i < n; i++) {
2713
0
        tmp = PyTuple_GET_ITEM(constants, i);
2714
0
        if (PyCode_Check(tmp))
2715
0
            update_code_filenames((PyCodeObject *)tmp,
2716
0
                                  oldname, newname);
2717
0
    }
2718
0
}
2719
2720
static void
2721
update_compiled_module(PyCodeObject *co, PyObject *newname)
2722
545
{
2723
545
    PyObject *oldname;
2724
2725
545
    if (PyUnicode_Compare(co->co_filename, newname) == 0)
2726
545
        return;
2727
2728
0
    oldname = co->co_filename;
2729
0
    Py_INCREF(oldname);
2730
0
    update_code_filenames(co, oldname, newname);
2731
0
    Py_DECREF(oldname);
2732
0
}
2733
2734
2735
/******************/
2736
/* frozen modules */
2737
/******************/
2738
2739
/* Return true if the name is an alias.  In that case, "alias" is set
2740
   to the original module name.  If it is an alias but the original
2741
   module isn't known then "alias" is set to NULL while true is returned. */
2742
static bool
2743
resolve_module_alias(const char *name, const struct _module_alias *aliases,
2744
                     const char **alias)
2745
428
{
2746
428
    const struct _module_alias *entry;
2747
3.24k
    for (entry = aliases; ; entry++) {
2748
3.24k
        if (entry->name == NULL) {
2749
            /* It isn't an alias. */
2750
348
            return false;
2751
348
        }
2752
2.89k
        if (strcmp(name, entry->name) == 0) {
2753
80
            if (alias != NULL) {
2754
80
                *alias = entry->orig;
2755
80
            }
2756
80
            return true;
2757
80
        }
2758
2.89k
    }
2759
428
}
2760
2761
static bool
2762
use_frozen(void)
2763
2.78k
{
2764
2.78k
    PyInterpreterState *interp = _PyInterpreterState_GET();
2765
2.78k
    int override = OVERRIDE_FROZEN_MODULES(interp);
2766
2.78k
    if (override > 0) {
2767
0
        return true;
2768
0
    }
2769
2.78k
    else if (override < 0) {
2770
0
        return false;
2771
0
    }
2772
2.78k
    else {
2773
2.78k
        return interp->config.use_frozen_modules;
2774
2.78k
    }
2775
2.78k
}
2776
2777
static PyObject *
2778
list_frozen_module_names(void)
2779
0
{
2780
0
    PyObject *names = PyList_New(0);
2781
0
    if (names == NULL) {
2782
0
        return NULL;
2783
0
    }
2784
0
    bool enabled = use_frozen();
2785
0
    const struct _frozen *p;
2786
0
#define ADD_MODULE(name) \
2787
0
    do { \
2788
0
        PyObject *nameobj = PyUnicode_FromString(name); \
2789
0
        if (nameobj == NULL) { \
2790
0
            goto error; \
2791
0
        } \
2792
0
        int res = PyList_Append(names, nameobj); \
2793
0
        Py_DECREF(nameobj); \
2794
0
        if (res != 0) { \
2795
0
            goto error; \
2796
0
        } \
2797
0
    } while(0)
2798
    // We always use the bootstrap modules.
2799
0
    for (p = _PyImport_FrozenBootstrap; ; p++) {
2800
0
        if (p->name == NULL) {
2801
0
            break;
2802
0
        }
2803
0
        ADD_MODULE(p->name);
2804
0
    }
2805
    // Frozen stdlib modules may be disabled.
2806
0
    for (p = _PyImport_FrozenStdlib; ; p++) {
2807
0
        if (p->name == NULL) {
2808
0
            break;
2809
0
        }
2810
0
        if (enabled) {
2811
0
            ADD_MODULE(p->name);
2812
0
        }
2813
0
    }
2814
0
    for (p = _PyImport_FrozenTest; ; p++) {
2815
0
        if (p->name == NULL) {
2816
0
            break;
2817
0
        }
2818
0
        if (enabled) {
2819
0
            ADD_MODULE(p->name);
2820
0
        }
2821
0
    }
2822
0
#undef ADD_MODULE
2823
    // Add any custom modules.
2824
0
    if (PyImport_FrozenModules != NULL) {
2825
0
        for (p = PyImport_FrozenModules; ; p++) {
2826
0
            if (p->name == NULL) {
2827
0
                break;
2828
0
            }
2829
0
            PyObject *nameobj = PyUnicode_FromString(p->name);
2830
0
            if (nameobj == NULL) {
2831
0
                goto error;
2832
0
            }
2833
0
            int found = PySequence_Contains(names, nameobj);
2834
0
            if (found < 0) {
2835
0
                Py_DECREF(nameobj);
2836
0
                goto error;
2837
0
            }
2838
0
            else if (found) {
2839
0
                Py_DECREF(nameobj);
2840
0
            }
2841
0
            else {
2842
0
                int res = PyList_Append(names, nameobj);
2843
0
                Py_DECREF(nameobj);
2844
0
                if (res != 0) {
2845
0
                    goto error;
2846
0
                }
2847
0
            }
2848
0
        }
2849
0
    }
2850
0
    return names;
2851
2852
0
error:
2853
0
    Py_DECREF(names);
2854
0
    return NULL;
2855
0
}
2856
2857
typedef enum {
2858
    FROZEN_OKAY,
2859
    FROZEN_BAD_NAME,    // The given module name wasn't valid.
2860
    FROZEN_NOT_FOUND,   // It wasn't in PyImport_FrozenModules.
2861
    FROZEN_DISABLED,    // -X frozen_modules=off (and not essential)
2862
    FROZEN_EXCLUDED,    /* The PyImport_FrozenModules entry has NULL "code"
2863
                           (module is present but marked as unimportable, stops search). */
2864
    FROZEN_INVALID,     /* The PyImport_FrozenModules entry is bogus
2865
                           (eg. does not contain executable code). */
2866
} frozen_status;
2867
2868
static inline void
2869
set_frozen_error(frozen_status status, PyObject *modname)
2870
0
{
2871
0
    const char *err = NULL;
2872
0
    switch (status) {
2873
0
        case FROZEN_BAD_NAME:
2874
0
        case FROZEN_NOT_FOUND:
2875
0
            err = "No such frozen object named %R";
2876
0
            break;
2877
0
        case FROZEN_DISABLED:
2878
0
            err = "Frozen modules are disabled and the frozen object named %R is not essential";
2879
0
            break;
2880
0
        case FROZEN_EXCLUDED:
2881
0
            err = "Excluded frozen object named %R";
2882
0
            break;
2883
0
        case FROZEN_INVALID:
2884
0
            err = "Frozen object named %R is invalid";
2885
0
            break;
2886
0
        case FROZEN_OKAY:
2887
            // There was no error.
2888
0
            break;
2889
0
        default:
2890
0
            Py_UNREACHABLE();
2891
0
    }
2892
0
    if (err != NULL) {
2893
0
        PyObject *msg = PyUnicode_FromFormat(err, modname);
2894
0
        if (msg == NULL) {
2895
0
            PyErr_Clear();
2896
0
        }
2897
0
        PyErr_SetImportError(msg, modname, NULL);
2898
0
        Py_XDECREF(msg);
2899
0
    }
2900
0
}
2901
2902
static const struct _frozen *
2903
look_up_frozen(const char *name)
2904
2.90k
{
2905
2.90k
    const struct _frozen *p;
2906
    // We always use the bootstrap modules.
2907
11.3k
    for (p = _PyImport_FrozenBootstrap; ; p++) {
2908
11.3k
        if (p->name == NULL) {
2909
            // We hit the end-of-list sentinel value.
2910
2.78k
            break;
2911
2.78k
        }
2912
8.57k
        if (strcmp(name, p->name) == 0) {
2913
112
            return p;
2914
112
        }
2915
8.57k
    }
2916
    // Prefer custom modules, if any.  Frozen stdlib modules can be
2917
    // disabled here by setting "code" to NULL in the array entry.
2918
2.78k
    if (PyImport_FrozenModules != NULL) {
2919
0
        for (p = PyImport_FrozenModules; ; p++) {
2920
0
            if (p->name == NULL) {
2921
0
                break;
2922
0
            }
2923
0
            if (strcmp(name, p->name) == 0) {
2924
0
                return p;
2925
0
            }
2926
0
        }
2927
0
    }
2928
    // Frozen stdlib modules may be disabled.
2929
2.78k
    if (use_frozen()) {
2930
39.0k
        for (p = _PyImport_FrozenStdlib; ; p++) {
2931
39.0k
            if (p->name == NULL) {
2932
2.47k
                break;
2933
2.47k
            }
2934
36.5k
            if (strcmp(name, p->name) == 0) {
2935
316
                return p;
2936
316
            }
2937
36.5k
        }
2938
29.6k
        for (p = _PyImport_FrozenTest; ; p++) {
2939
29.6k
            if (p->name == NULL) {
2940
2.47k
                break;
2941
2.47k
            }
2942
27.2k
            if (strcmp(name, p->name) == 0) {
2943
0
                return p;
2944
0
            }
2945
27.2k
        }
2946
2.47k
    }
2947
2.47k
    return NULL;
2948
2.78k
}
2949
2950
struct frozen_info {
2951
    PyObject *nameobj;
2952
    const char *data;
2953
    Py_ssize_t size;
2954
    bool is_package;
2955
    bool is_alias;
2956
    const char *origname;
2957
};
2958
2959
static frozen_status
2960
find_frozen(PyObject *nameobj, struct frozen_info *info)
2961
2.90k
{
2962
2.90k
    if (info != NULL) {
2963
2.90k
        memset(info, 0, sizeof(*info));
2964
2.90k
    }
2965
2966
2.90k
    if (nameobj == NULL || nameobj == Py_None) {
2967
0
        return FROZEN_BAD_NAME;
2968
0
    }
2969
2.90k
    const char *name = PyUnicode_AsUTF8(nameobj);
2970
2.90k
    if (name == NULL) {
2971
        // Note that this function previously used
2972
        // _PyUnicode_EqualToASCIIString().  We clear the error here
2973
        // (instead of propagating it) to match the earlier behavior
2974
        // more closely.
2975
0
        PyErr_Clear();
2976
0
        return FROZEN_BAD_NAME;
2977
0
    }
2978
2979
2.90k
    const struct _frozen *p = look_up_frozen(name);
2980
2.90k
    if (p == NULL) {
2981
2.47k
        return FROZEN_NOT_FOUND;
2982
2.47k
    }
2983
428
    if (info != NULL) {
2984
428
        info->nameobj = nameobj;  // borrowed
2985
428
        info->data = (const char *)p->code;
2986
428
        info->size = p->size;
2987
428
        info->is_package = p->is_package;
2988
428
        if (p->size < 0) {
2989
            // backward compatibility with negative size values
2990
0
            info->size = -(p->size);
2991
0
            info->is_package = true;
2992
0
        }
2993
428
        info->origname = name;
2994
428
        info->is_alias = resolve_module_alias(name, _PyImport_FrozenAliases,
2995
428
                                              &info->origname);
2996
428
    }
2997
428
    if (p->code == NULL) {
2998
        /* It is frozen but marked as un-importable. */
2999
0
        return FROZEN_EXCLUDED;
3000
0
    }
3001
428
    if (p->code[0] == '\0' || p->size == 0) {
3002
        /* Does not contain executable code. */
3003
0
        return FROZEN_INVALID;
3004
0
    }
3005
428
    return FROZEN_OKAY;
3006
428
}
3007
3008
static PyObject *
3009
unmarshal_frozen_code(PyInterpreterState *interp, struct frozen_info *info)
3010
206
{
3011
206
    PyObject *co = PyMarshal_ReadObjectFromString(info->data, info->size);
3012
206
    if (co == NULL) {
3013
        /* Does not contain executable code. */
3014
0
        PyErr_Clear();
3015
0
        set_frozen_error(FROZEN_INVALID, info->nameobj);
3016
0
        return NULL;
3017
0
    }
3018
206
    if (!PyCode_Check(co)) {
3019
        // We stick with TypeError for backward compatibility.
3020
0
        PyErr_Format(PyExc_TypeError,
3021
0
                     "frozen object %R is not a code object",
3022
0
                     info->nameobj);
3023
0
        Py_DECREF(co);
3024
0
        return NULL;
3025
0
    }
3026
206
    return co;
3027
206
}
3028
3029
3030
/* Initialize a frozen module.
3031
   Return 1 for success, 0 if the module is not found, and -1 with
3032
   an exception set if the initialization failed.
3033
   This function is also used from frozenmain.c */
3034
3035
int
3036
PyImport_ImportFrozenModuleObject(PyObject *name)
3037
16
{
3038
16
    PyThreadState *tstate = _PyThreadState_GET();
3039
16
    PyObject *co, *m, *d = NULL;
3040
16
    int err;
3041
3042
16
    struct frozen_info info;
3043
16
    frozen_status status = find_frozen(name, &info);
3044
16
    if (status == FROZEN_NOT_FOUND || status == FROZEN_DISABLED) {
3045
0
        return 0;
3046
0
    }
3047
16
    else if (status == FROZEN_BAD_NAME) {
3048
0
        return 0;
3049
0
    }
3050
16
    else if (status != FROZEN_OKAY) {
3051
0
        set_frozen_error(status, name);
3052
0
        return -1;
3053
0
    }
3054
16
    co = unmarshal_frozen_code(tstate->interp, &info);
3055
16
    if (co == NULL) {
3056
0
        return -1;
3057
0
    }
3058
16
    if (info.is_package) {
3059
        /* Set __path__ to the empty list */
3060
0
        PyObject *l;
3061
0
        m = import_add_module(tstate, name);
3062
0
        if (m == NULL)
3063
0
            goto err_return;
3064
0
        d = PyModule_GetDict(m);
3065
0
        l = PyList_New(0);
3066
0
        if (l == NULL) {
3067
0
            Py_DECREF(m);
3068
0
            goto err_return;
3069
0
        }
3070
0
        err = PyDict_SetItemString(d, "__path__", l);
3071
0
        Py_DECREF(l);
3072
0
        Py_DECREF(m);
3073
0
        if (err != 0)
3074
0
            goto err_return;
3075
0
    }
3076
16
    d = module_dict_for_exec(tstate, name);
3077
16
    if (d == NULL) {
3078
0
        goto err_return;
3079
0
    }
3080
16
    m = exec_code_in_module(tstate, name, d, co);
3081
16
    if (m == NULL) {
3082
0
        goto err_return;
3083
0
    }
3084
16
    Py_DECREF(m);
3085
    /* Set __origname__ (consumed in FrozenImporter._setup_module()). */
3086
16
    PyObject *origname;
3087
16
    if (info.origname) {
3088
16
        origname = PyUnicode_FromString(info.origname);
3089
16
        if (origname == NULL) {
3090
0
            goto err_return;
3091
0
        }
3092
16
    }
3093
0
    else {
3094
0
        origname = Py_NewRef(Py_None);
3095
0
    }
3096
16
    err = PyDict_SetItemString(d, "__origname__", origname);
3097
16
    Py_DECREF(origname);
3098
16
    if (err != 0) {
3099
0
        goto err_return;
3100
0
    }
3101
16
    Py_DECREF(d);
3102
16
    Py_DECREF(co);
3103
16
    return 1;
3104
3105
0
err_return:
3106
0
    Py_XDECREF(d);
3107
0
    Py_DECREF(co);
3108
0
    return -1;
3109
16
}
3110
3111
int
3112
PyImport_ImportFrozenModule(const char *name)
3113
16
{
3114
16
    PyObject *nameobj;
3115
16
    int ret;
3116
16
    nameobj = PyUnicode_InternFromString(name);
3117
16
    if (nameobj == NULL)
3118
0
        return -1;
3119
16
    ret = PyImport_ImportFrozenModuleObject(nameobj);
3120
16
    Py_DECREF(nameobj);
3121
16
    return ret;
3122
16
}
3123
3124
3125
/*************/
3126
/* importlib */
3127
/*************/
3128
3129
/* Import the _imp extension by calling manually _imp.create_builtin() and
3130
   _imp.exec_builtin() since importlib is not initialized yet. Initializing
3131
   importlib requires the _imp module: this function fix the bootstrap issue.
3132
 */
3133
static PyObject*
3134
bootstrap_imp(PyThreadState *tstate)
3135
16
{
3136
16
    PyObject *name = PyUnicode_FromString("_imp");
3137
16
    if (name == NULL) {
3138
0
        return NULL;
3139
0
    }
3140
3141
    // Mock a ModuleSpec object just good enough for PyModule_FromDefAndSpec():
3142
    // an object with just a name attribute.
3143
    //
3144
    // _imp.__spec__ is overridden by importlib._bootstrap._instal() anyway.
3145
16
    PyObject *attrs = Py_BuildValue("{sO}", "name", name);
3146
16
    if (attrs == NULL) {
3147
0
        goto error;
3148
0
    }
3149
16
    PyObject *spec = _PyNamespace_New(attrs);
3150
16
    Py_DECREF(attrs);
3151
16
    if (spec == NULL) {
3152
0
        goto error;
3153
0
    }
3154
3155
    // Create the _imp module from its definition.
3156
16
    PyObject *mod = create_builtin(tstate, name, spec);
3157
16
    Py_CLEAR(name);
3158
16
    Py_DECREF(spec);
3159
16
    if (mod == NULL) {
3160
0
        goto error;
3161
0
    }
3162
16
    assert(mod != Py_None);  // not found
3163
3164
    // Execute the _imp module: call imp_module_exec().
3165
16
    if (exec_builtin_or_dynamic(mod) < 0) {
3166
0
        Py_DECREF(mod);
3167
0
        goto error;
3168
0
    }
3169
16
    return mod;
3170
3171
0
error:
3172
0
    Py_XDECREF(name);
3173
0
    return NULL;
3174
16
}
3175
3176
/* Global initializations.  Can be undone by Py_FinalizeEx().  Don't
3177
   call this twice without an intervening Py_FinalizeEx() call.  When
3178
   initializations fail, a fatal error is issued and the function does
3179
   not return.  On return, the first thread and interpreter state have
3180
   been created.
3181
3182
   Locking: you must hold the interpreter lock while calling this.
3183
   (If the lock has not yet been initialized, that's equivalent to
3184
   having the lock, but you cannot use multiple threads.)
3185
3186
*/
3187
static int
3188
init_importlib(PyThreadState *tstate, PyObject *sysmod)
3189
16
{
3190
16
    assert(!_PyErr_Occurred(tstate));
3191
3192
16
    PyInterpreterState *interp = tstate->interp;
3193
16
    int verbose = _PyInterpreterState_GetConfig(interp)->verbose;
3194
3195
    // Import _importlib through its frozen version, _frozen_importlib.
3196
16
    if (verbose) {
3197
0
        PySys_FormatStderr("import _frozen_importlib # frozen\n");
3198
0
    }
3199
16
    if (PyImport_ImportFrozenModule("_frozen_importlib") <= 0) {
3200
0
        return -1;
3201
0
    }
3202
3203
16
    PyObject *importlib = PyImport_AddModuleRef("_frozen_importlib");
3204
16
    if (importlib == NULL) {
3205
0
        return -1;
3206
0
    }
3207
16
    IMPORTLIB(interp) = importlib;
3208
3209
    // Import the _imp module
3210
16
    if (verbose) {
3211
0
        PySys_FormatStderr("import _imp # builtin\n");
3212
0
    }
3213
16
    PyObject *imp_mod = bootstrap_imp(tstate);
3214
16
    if (imp_mod == NULL) {
3215
0
        return -1;
3216
0
    }
3217
16
    if (_PyImport_SetModuleString("_imp", imp_mod) < 0) {
3218
0
        Py_DECREF(imp_mod);
3219
0
        return -1;
3220
0
    }
3221
3222
    // Install importlib as the implementation of import
3223
16
    PyObject *value = PyObject_CallMethod(importlib, "_install",
3224
16
                                          "OO", sysmod, imp_mod);
3225
16
    Py_DECREF(imp_mod);
3226
16
    if (value == NULL) {
3227
0
        return -1;
3228
0
    }
3229
16
    Py_DECREF(value);
3230
3231
16
    assert(!_PyErr_Occurred(tstate));
3232
16
    return 0;
3233
16
}
3234
3235
3236
static int
3237
init_importlib_external(PyInterpreterState *interp)
3238
16
{
3239
16
    PyObject *value;
3240
16
    value = PyObject_CallMethod(IMPORTLIB(interp),
3241
16
                                "_install_external_importers", "");
3242
16
    if (value == NULL) {
3243
0
        return -1;
3244
0
    }
3245
16
    Py_DECREF(value);
3246
16
    return 0;
3247
16
}
3248
3249
PyObject *
3250
_PyImport_GetImportlibLoader(PyInterpreterState *interp,
3251
                             const char *loader_name)
3252
16
{
3253
16
    return PyObject_GetAttrString(IMPORTLIB(interp), loader_name);
3254
16
}
3255
3256
PyObject *
3257
_PyImport_GetImportlibExternalLoader(PyInterpreterState *interp,
3258
                                     const char *loader_name)
3259
0
{
3260
0
    PyObject *bootstrap = PyObject_GetAttrString(IMPORTLIB(interp),
3261
0
                                                 "_bootstrap_external");
3262
0
    if (bootstrap == NULL) {
3263
0
        return NULL;
3264
0
    }
3265
3266
0
    PyObject *loader_type = PyObject_GetAttrString(bootstrap, loader_name);
3267
0
    Py_DECREF(bootstrap);
3268
0
    return loader_type;
3269
0
}
3270
3271
PyObject *
3272
_PyImport_BlessMyLoader(PyInterpreterState *interp, PyObject *module_globals)
3273
0
{
3274
0
    PyObject *external = PyObject_GetAttrString(IMPORTLIB(interp),
3275
0
                                                "_bootstrap_external");
3276
0
    if (external == NULL) {
3277
0
        return NULL;
3278
0
    }
3279
3280
0
    PyObject *loader = PyObject_CallMethod(external, "_bless_my_loader",
3281
0
                                           "O", module_globals, NULL);
3282
0
    Py_DECREF(external);
3283
0
    return loader;
3284
0
}
3285
3286
PyObject *
3287
_PyImport_ImportlibModuleRepr(PyInterpreterState *interp, PyObject *m)
3288
0
{
3289
0
    return PyObject_CallMethod(IMPORTLIB(interp), "_module_repr", "O", m);
3290
0
}
3291
3292
3293
/*******************/
3294
3295
/* Return a finder object for a sys.path/pkg.__path__ item 'p',
3296
   possibly by fetching it from the path_importer_cache dict. If it
3297
   wasn't yet cached, traverse path_hooks until a hook is found
3298
   that can handle the path item. Return None if no hook could;
3299
   this tells our caller that the path based finder could not find
3300
   a finder for this path item. Cache the result in
3301
   path_importer_cache. */
3302
3303
static PyObject *
3304
get_path_importer(PyThreadState *tstate, PyObject *path_importer_cache,
3305
                  PyObject *path_hooks, PyObject *p)
3306
0
{
3307
0
    PyObject *importer;
3308
0
    Py_ssize_t j, nhooks;
3309
3310
0
    if (!PyList_Check(path_hooks)) {
3311
0
        PyErr_SetString(PyExc_RuntimeError, "sys.path_hooks is not a list");
3312
0
        return NULL;
3313
0
    }
3314
0
    if (!PyDict_Check(path_importer_cache)) {
3315
0
        PyErr_SetString(PyExc_RuntimeError, "sys.path_importer_cache is not a dict");
3316
0
        return NULL;
3317
0
    }
3318
3319
0
    nhooks = PyList_Size(path_hooks);
3320
0
    if (nhooks < 0)
3321
0
        return NULL; /* Shouldn't happen */
3322
3323
0
    if (PyDict_GetItemRef(path_importer_cache, p, &importer) != 0) {
3324
        // found or error
3325
0
        return importer;
3326
0
    }
3327
    // not found
3328
    /* set path_importer_cache[p] to None to avoid recursion */
3329
0
    if (PyDict_SetItem(path_importer_cache, p, Py_None) != 0)
3330
0
        return NULL;
3331
3332
0
    for (j = 0; j < nhooks; j++) {
3333
0
        PyObject *hook = PyList_GetItem(path_hooks, j);
3334
0
        if (hook == NULL)
3335
0
            return NULL;
3336
0
        importer = PyObject_CallOneArg(hook, p);
3337
0
        if (importer != NULL)
3338
0
            break;
3339
3340
0
        if (!_PyErr_ExceptionMatches(tstate, PyExc_ImportError)) {
3341
0
            return NULL;
3342
0
        }
3343
0
        _PyErr_Clear(tstate);
3344
0
    }
3345
0
    if (importer == NULL) {
3346
0
        Py_RETURN_NONE;
3347
0
    }
3348
0
    if (PyDict_SetItem(path_importer_cache, p, importer) < 0) {
3349
0
        Py_DECREF(importer);
3350
0
        return NULL;
3351
0
    }
3352
0
    return importer;
3353
0
}
3354
3355
PyObject *
3356
PyImport_GetImporter(PyObject *path)
3357
0
{
3358
0
    PyThreadState *tstate = _PyThreadState_GET();
3359
0
    PyObject *path_importer_cache = PySys_GetAttrString("path_importer_cache");
3360
0
    if (path_importer_cache == NULL) {
3361
0
        return NULL;
3362
0
    }
3363
0
    PyObject *path_hooks = PySys_GetAttrString("path_hooks");
3364
0
    if (path_hooks == NULL) {
3365
0
        Py_DECREF(path_importer_cache);
3366
0
        return NULL;
3367
0
    }
3368
0
    PyObject *importer = get_path_importer(tstate, path_importer_cache, path_hooks, path);
3369
0
    Py_DECREF(path_hooks);
3370
0
    Py_DECREF(path_importer_cache);
3371
0
    return importer;
3372
0
}
3373
3374
3375
/*********************/
3376
/* importing modules */
3377
/*********************/
3378
3379
int
3380
_PyImport_InitDefaultImportFunc(PyInterpreterState *interp)
3381
16
{
3382
    // Get the __import__ function
3383
16
    PyObject *import_func;
3384
16
    if (PyDict_GetItemStringRef(interp->builtins, "__import__", &import_func) <= 0) {
3385
0
        return -1;
3386
0
    }
3387
16
    IMPORT_FUNC(interp) = import_func;
3388
16
    return 0;
3389
16
}
3390
3391
int
3392
_PyImport_IsDefaultImportFunc(PyInterpreterState *interp, PyObject *func)
3393
8.91k
{
3394
8.91k
    return func == IMPORT_FUNC(interp);
3395
8.91k
}
3396
3397
3398
/* Import a module, either built-in, frozen, or external, and return
3399
   its module object WITH INCREMENTED REFERENCE COUNT */
3400
3401
PyObject *
3402
PyImport_ImportModule(const char *name)
3403
274
{
3404
274
    PyObject *pname;
3405
274
    PyObject *result;
3406
3407
274
    pname = PyUnicode_FromString(name);
3408
274
    if (pname == NULL)
3409
0
        return NULL;
3410
274
    result = PyImport_Import(pname);
3411
274
    Py_DECREF(pname);
3412
274
    return result;
3413
274
}
3414
3415
3416
/* Import a module without blocking
3417
 *
3418
 * At first it tries to fetch the module from sys.modules. If the module was
3419
 * never loaded before it loads it with PyImport_ImportModule() unless another
3420
 * thread holds the import lock. In the latter case the function raises an
3421
 * ImportError instead of blocking.
3422
 *
3423
 * Returns the module object with incremented ref count.
3424
 *
3425
 * Removed in 3.15, but kept for stable ABI compatibility.
3426
 */
3427
PyAPI_FUNC(PyObject *)
3428
PyImport_ImportModuleNoBlock(const char *name)
3429
0
{
3430
0
    if (PyErr_WarnEx(PyExc_DeprecationWarning,
3431
0
        "PyImport_ImportModuleNoBlock() is deprecated and scheduled for "
3432
0
        "removal in Python 3.15. Use PyImport_ImportModule() instead.", 1))
3433
0
    {
3434
0
        return NULL;
3435
0
    }
3436
0
    return PyImport_ImportModule(name);
3437
0
}
3438
3439
3440
/* Remove importlib frames from the traceback,
3441
 * except in Verbose mode. */
3442
static void
3443
remove_importlib_frames(PyThreadState *tstate)
3444
1.64k
{
3445
1.64k
    const char *importlib_filename = "<frozen importlib._bootstrap>";
3446
1.64k
    const char *external_filename = "<frozen importlib._bootstrap_external>";
3447
1.64k
    const char *remove_frames = "_call_with_frames_removed";
3448
1.64k
    int always_trim = 0;
3449
1.64k
    int in_importlib = 0;
3450
1.64k
    PyObject **prev_link, **outer_link = NULL;
3451
1.64k
    PyObject *base_tb = NULL;
3452
3453
    /* Synopsis: if it's an ImportError, we trim all importlib chunks
3454
       from the traceback. We always trim chunks
3455
       which end with a call to "_call_with_frames_removed". */
3456
3457
1.64k
    PyObject *exc = _PyErr_GetRaisedException(tstate);
3458
1.64k
    if (exc == NULL || _PyInterpreterState_GetConfig(tstate->interp)->verbose) {
3459
0
        goto done;
3460
0
    }
3461
3462
1.64k
    if (PyType_IsSubtype(Py_TYPE(exc), (PyTypeObject *) PyExc_ImportError)) {
3463
1.64k
        always_trim = 1;
3464
1.64k
    }
3465
3466
1.64k
    assert(PyExceptionInstance_Check(exc));
3467
1.64k
    base_tb = PyException_GetTraceback(exc);
3468
1.64k
    prev_link = &base_tb;
3469
1.64k
    PyObject *tb = base_tb;
3470
5.05k
    while (tb != NULL) {
3471
3.41k
        assert(PyTraceBack_Check(tb));
3472
3.41k
        PyTracebackObject *traceback = (PyTracebackObject *)tb;
3473
3.41k
        PyObject *next = (PyObject *) traceback->tb_next;
3474
3.41k
        PyFrameObject *frame = traceback->tb_frame;
3475
3.41k
        PyCodeObject *code = PyFrame_GetCode(frame);
3476
3.41k
        int now_in_importlib;
3477
3478
3.41k
        now_in_importlib = _PyUnicode_EqualToASCIIString(code->co_filename, importlib_filename) ||
3479
66
                           _PyUnicode_EqualToASCIIString(code->co_filename, external_filename);
3480
3.41k
        if (now_in_importlib && !in_importlib) {
3481
            /* This is the link to this chunk of importlib tracebacks */
3482
1.64k
            outer_link = prev_link;
3483
1.64k
        }
3484
3.41k
        in_importlib = now_in_importlib;
3485
3486
3.41k
        if (in_importlib &&
3487
3.38k
            (always_trim ||
3488
3.38k
             _PyUnicode_EqualToASCIIString(code->co_name, remove_frames))) {
3489
3.38k
            Py_XSETREF(*outer_link, Py_XNewRef(next));
3490
3.38k
            prev_link = outer_link;
3491
3.38k
        }
3492
34
        else {
3493
34
            prev_link = (PyObject **) &traceback->tb_next;
3494
34
        }
3495
3.41k
        Py_DECREF(code);
3496
3.41k
        tb = next;
3497
3.41k
    }
3498
1.64k
    if (base_tb == NULL) {
3499
1.60k
        base_tb = Py_None;
3500
1.60k
        Py_INCREF(Py_None);
3501
1.60k
    }
3502
1.64k
    PyException_SetTraceback(exc, base_tb);
3503
1.64k
done:
3504
1.64k
    Py_XDECREF(base_tb);
3505
1.64k
    _PyErr_SetRaisedException(tstate, exc);
3506
1.64k
}
3507
3508
3509
static PyObject *
3510
resolve_name(PyThreadState *tstate, PyObject *name, PyObject *globals, int level)
3511
108
{
3512
108
    PyObject *abs_name;
3513
108
    PyObject *package = NULL;
3514
108
    PyObject *spec = NULL;
3515
108
    Py_ssize_t last_dot;
3516
108
    PyObject *base;
3517
108
    int level_up;
3518
3519
108
    if (globals == NULL) {
3520
0
        _PyErr_SetString(tstate, PyExc_KeyError, "'__name__' not in globals");
3521
0
        goto error;
3522
0
    }
3523
108
    if (!PyDict_Check(globals)) {
3524
0
        _PyErr_SetString(tstate, PyExc_TypeError, "globals must be a dict");
3525
0
        goto error;
3526
0
    }
3527
108
    if (PyDict_GetItemRef(globals, &_Py_ID(__package__), &package) < 0) {
3528
0
        goto error;
3529
0
    }
3530
108
    if (package == Py_None) {
3531
0
        Py_DECREF(package);
3532
0
        package = NULL;
3533
0
    }
3534
108
    if (PyDict_GetItemRef(globals, &_Py_ID(__spec__), &spec) < 0) {
3535
0
        goto error;
3536
0
    }
3537
3538
108
    if (package != NULL) {
3539
108
        if (!PyUnicode_Check(package)) {
3540
0
            _PyErr_SetString(tstate, PyExc_TypeError,
3541
0
                             "package must be a string");
3542
0
            goto error;
3543
0
        }
3544
108
        else if (spec != NULL && spec != Py_None) {
3545
108
            int equal;
3546
108
            PyObject *parent = PyObject_GetAttr(spec, &_Py_ID(parent));
3547
108
            if (parent == NULL) {
3548
0
                goto error;
3549
0
            }
3550
3551
108
            equal = PyObject_RichCompareBool(package, parent, Py_EQ);
3552
108
            Py_DECREF(parent);
3553
108
            if (equal < 0) {
3554
0
                goto error;
3555
0
            }
3556
108
            else if (equal == 0) {
3557
0
                if (PyErr_WarnEx(PyExc_DeprecationWarning,
3558
0
                        "__package__ != __spec__.parent", 1) < 0) {
3559
0
                    goto error;
3560
0
                }
3561
0
            }
3562
108
        }
3563
108
    }
3564
0
    else if (spec != NULL && spec != Py_None) {
3565
0
        package = PyObject_GetAttr(spec, &_Py_ID(parent));
3566
0
        if (package == NULL) {
3567
0
            goto error;
3568
0
        }
3569
0
        else if (!PyUnicode_Check(package)) {
3570
0
            _PyErr_SetString(tstate, PyExc_TypeError,
3571
0
                             "__spec__.parent must be a string");
3572
0
            goto error;
3573
0
        }
3574
0
    }
3575
0
    else {
3576
0
        if (PyErr_WarnEx(PyExc_ImportWarning,
3577
0
                    "can't resolve package from __spec__ or __package__, "
3578
0
                    "falling back on __name__ and __path__", 1) < 0) {
3579
0
            goto error;
3580
0
        }
3581
3582
0
        if (PyDict_GetItemRef(globals, &_Py_ID(__name__), &package) < 0) {
3583
0
            goto error;
3584
0
        }
3585
0
        if (package == NULL) {
3586
0
            _PyErr_SetString(tstate, PyExc_KeyError,
3587
0
                             "'__name__' not in globals");
3588
0
            goto error;
3589
0
        }
3590
3591
0
        if (!PyUnicode_Check(package)) {
3592
0
            _PyErr_SetString(tstate, PyExc_TypeError,
3593
0
                             "__name__ must be a string");
3594
0
            goto error;
3595
0
        }
3596
3597
0
        int haspath = PyDict_Contains(globals, &_Py_ID(__path__));
3598
0
        if (haspath < 0) {
3599
0
            goto error;
3600
0
        }
3601
0
        if (!haspath) {
3602
0
            Py_ssize_t dot;
3603
3604
0
            dot = PyUnicode_FindChar(package, '.',
3605
0
                                        0, PyUnicode_GET_LENGTH(package), -1);
3606
0
            if (dot == -2) {
3607
0
                goto error;
3608
0
            }
3609
0
            else if (dot == -1) {
3610
0
                goto no_parent_error;
3611
0
            }
3612
0
            PyObject *substr = PyUnicode_Substring(package, 0, dot);
3613
0
            if (substr == NULL) {
3614
0
                goto error;
3615
0
            }
3616
0
            Py_SETREF(package, substr);
3617
0
        }
3618
0
    }
3619
3620
108
    last_dot = PyUnicode_GET_LENGTH(package);
3621
108
    if (last_dot == 0) {
3622
0
        goto no_parent_error;
3623
0
    }
3624
3625
108
    for (level_up = 1; level_up < level; level_up += 1) {
3626
0
        last_dot = PyUnicode_FindChar(package, '.', 0, last_dot, -1);
3627
0
        if (last_dot == -2) {
3628
0
            goto error;
3629
0
        }
3630
0
        else if (last_dot == -1) {
3631
0
            _PyErr_SetString(tstate, PyExc_ImportError,
3632
0
                             "attempted relative import beyond top-level "
3633
0
                             "package");
3634
0
            goto error;
3635
0
        }
3636
0
    }
3637
3638
108
    Py_XDECREF(spec);
3639
108
    base = PyUnicode_Substring(package, 0, last_dot);
3640
108
    Py_DECREF(package);
3641
108
    if (base == NULL || PyUnicode_GET_LENGTH(name) == 0) {
3642
40
        return base;
3643
40
    }
3644
3645
68
    abs_name = PyUnicode_FromFormat("%U.%U", base, name);
3646
68
    Py_DECREF(base);
3647
68
    return abs_name;
3648
3649
0
  no_parent_error:
3650
0
    _PyErr_SetString(tstate, PyExc_ImportError,
3651
0
                     "attempted relative import "
3652
0
                     "with no known parent package");
3653
3654
0
  error:
3655
0
    Py_XDECREF(spec);
3656
0
    Py_XDECREF(package);
3657
0
    return NULL;
3658
0
}
3659
3660
static PyObject *
3661
import_find_and_load(PyThreadState *tstate, PyObject *abs_name)
3662
2.91k
{
3663
2.91k
    PyObject *mod = NULL;
3664
2.91k
    PyInterpreterState *interp = tstate->interp;
3665
2.91k
    int import_time = _PyInterpreterState_GetConfig(interp)->import_time;
3666
2.91k
#define import_level FIND_AND_LOAD(interp).import_level
3667
2.91k
#define accumulated FIND_AND_LOAD(interp).accumulated
3668
3669
2.91k
    PyTime_t t1 = 0, accumulated_copy = accumulated;
3670
3671
    /* XOptions is initialized after first some imports.
3672
     * So we can't have negative cache before completed initialization.
3673
     * Anyway, importlib._find_and_load is much slower than
3674
     * _PyDict_GetItemIdWithError().
3675
     */
3676
2.91k
    if (import_time) {
3677
0
        _IMPORT_TIME_HEADER(interp);
3678
3679
0
        import_level++;
3680
        // ignore error: don't block import if reading the clock fails
3681
0
        (void)PyTime_PerfCounterRaw(&t1);
3682
0
        accumulated = 0;
3683
0
    }
3684
3685
2.91k
    if (PyDTrace_IMPORT_FIND_LOAD_START_ENABLED())
3686
0
        PyDTrace_IMPORT_FIND_LOAD_START(PyUnicode_AsUTF8(abs_name));
3687
3688
2.91k
    mod = PyObject_CallMethodObjArgs(IMPORTLIB(interp), &_Py_ID(_find_and_load),
3689
2.91k
                                     abs_name, IMPORT_FUNC(interp), NULL);
3690
3691
2.91k
    if (PyDTrace_IMPORT_FIND_LOAD_DONE_ENABLED())
3692
0
        PyDTrace_IMPORT_FIND_LOAD_DONE(PyUnicode_AsUTF8(abs_name),
3693
0
                                       mod != NULL);
3694
3695
2.91k
    if (import_time) {
3696
0
        PyTime_t t2;
3697
0
        (void)PyTime_PerfCounterRaw(&t2);
3698
0
        PyTime_t cum = t2 - t1;
3699
3700
0
        import_level--;
3701
0
        fprintf(stderr, "import time: %9ld | %10ld | %*s%s\n",
3702
0
                (long)_PyTime_AsMicroseconds(cum - accumulated, _PyTime_ROUND_CEILING),
3703
0
                (long)_PyTime_AsMicroseconds(cum, _PyTime_ROUND_CEILING),
3704
0
                import_level*2, "", PyUnicode_AsUTF8(abs_name));
3705
3706
0
        accumulated = accumulated_copy + cum;
3707
0
    }
3708
3709
2.91k
    return mod;
3710
2.91k
#undef import_level
3711
2.91k
#undef accumulated
3712
2.91k
}
3713
3714
PyObject *
3715
PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
3716
                                 PyObject *locals, PyObject *fromlist,
3717
                                 int level)
3718
24.0k
{
3719
24.0k
    PyThreadState *tstate = _PyThreadState_GET();
3720
24.0k
    PyObject *abs_name = NULL;
3721
24.0k
    PyObject *final_mod = NULL;
3722
24.0k
    PyObject *mod = NULL;
3723
24.0k
    PyObject *package = NULL;
3724
24.0k
    PyInterpreterState *interp = tstate->interp;
3725
24.0k
    int has_from;
3726
3727
24.0k
    if (name == NULL) {
3728
0
        _PyErr_SetString(tstate, PyExc_ValueError, "Empty module name");
3729
0
        goto error;
3730
0
    }
3731
3732
    /* The below code is importlib.__import__() & _gcd_import(), ported to C
3733
       for added performance. */
3734
3735
24.0k
    if (!PyUnicode_Check(name)) {
3736
0
        _PyErr_SetString(tstate, PyExc_TypeError,
3737
0
                         "module name must be a string");
3738
0
        goto error;
3739
0
    }
3740
24.0k
    if (level < 0) {
3741
0
        _PyErr_SetString(tstate, PyExc_ValueError, "level must be >= 0");
3742
0
        goto error;
3743
0
    }
3744
3745
24.0k
    if (level > 0) {
3746
108
        abs_name = resolve_name(tstate, name, globals, level);
3747
108
        if (abs_name == NULL)
3748
0
            goto error;
3749
108
    }
3750
23.9k
    else {  /* level == 0 */
3751
23.9k
        if (PyUnicode_GET_LENGTH(name) == 0) {
3752
0
            _PyErr_SetString(tstate, PyExc_ValueError, "Empty module name");
3753
0
            goto error;
3754
0
        }
3755
23.9k
        abs_name = Py_NewRef(name);
3756
23.9k
    }
3757
3758
24.0k
    mod = import_get_module(tstate, abs_name);
3759
24.0k
    if (mod == NULL && _PyErr_Occurred(tstate)) {
3760
0
        goto error;
3761
0
    }
3762
3763
24.0k
    if (mod != NULL && mod != Py_None) {
3764
21.1k
        if (import_ensure_initialized(tstate->interp, mod, abs_name) < 0) {
3765
0
            goto error;
3766
0
        }
3767
21.1k
    }
3768
2.91k
    else {
3769
2.91k
        Py_XDECREF(mod);
3770
2.91k
        mod = import_find_and_load(tstate, abs_name);
3771
2.91k
        if (mod == NULL) {
3772
1.64k
            goto error;
3773
1.64k
        }
3774
2.91k
    }
3775
3776
22.3k
    has_from = 0;
3777
22.3k
    if (fromlist != NULL && fromlist != Py_None) {
3778
12.1k
        has_from = PyObject_IsTrue(fromlist);
3779
12.1k
        if (has_from < 0)
3780
0
            goto error;
3781
12.1k
    }
3782
22.3k
    if (!has_from) {
3783
15.4k
        Py_ssize_t len = PyUnicode_GET_LENGTH(name);
3784
15.4k
        if (level == 0 || len > 0) {
3785
15.4k
            Py_ssize_t dot;
3786
3787
15.4k
            dot = PyUnicode_FindChar(name, '.', 0, len, 1);
3788
15.4k
            if (dot == -2) {
3789
0
                goto error;
3790
0
            }
3791
3792
15.4k
            if (dot == -1) {
3793
                /* No dot in module name, simple exit */
3794
12.9k
                final_mod = Py_NewRef(mod);
3795
12.9k
                goto error;
3796
12.9k
            }
3797
3798
2.45k
            if (level == 0) {
3799
2.45k
                PyObject *front = PyUnicode_Substring(name, 0, dot);
3800
2.45k
                if (front == NULL) {
3801
0
                    goto error;
3802
0
                }
3803
3804
2.45k
                final_mod = PyImport_ImportModuleLevelObject(front, NULL, NULL, NULL, 0);
3805
2.45k
                Py_DECREF(front);
3806
2.45k
            }
3807
0
            else {
3808
0
                Py_ssize_t cut_off = len - dot;
3809
0
                Py_ssize_t abs_name_len = PyUnicode_GET_LENGTH(abs_name);
3810
0
                PyObject *to_return = PyUnicode_Substring(abs_name, 0,
3811
0
                                                        abs_name_len - cut_off);
3812
0
                if (to_return == NULL) {
3813
0
                    goto error;
3814
0
                }
3815
3816
0
                final_mod = import_get_module(tstate, to_return);
3817
0
                if (final_mod == NULL) {
3818
0
                    if (!_PyErr_Occurred(tstate)) {
3819
0
                        _PyErr_Format(tstate, PyExc_KeyError,
3820
0
                                      "%R not in sys.modules as expected",
3821
0
                                      to_return);
3822
0
                    }
3823
0
                    Py_DECREF(to_return);
3824
0
                    goto error;
3825
0
                }
3826
3827
0
                Py_DECREF(to_return);
3828
0
            }
3829
2.45k
        }
3830
0
        else {
3831
0
            final_mod = Py_NewRef(mod);
3832
0
        }
3833
15.4k
    }
3834
6.97k
    else {
3835
6.97k
        int has_path = PyObject_HasAttrWithError(mod, &_Py_ID(__path__));
3836
6.97k
        if (has_path < 0) {
3837
0
            goto error;
3838
0
        }
3839
6.97k
        if (has_path) {
3840
171
            final_mod = PyObject_CallMethodObjArgs(
3841
171
                        IMPORTLIB(interp), &_Py_ID(_handle_fromlist),
3842
171
                        mod, fromlist, IMPORT_FUNC(interp), NULL);
3843
171
        }
3844
6.80k
        else {
3845
6.80k
            final_mod = Py_NewRef(mod);
3846
6.80k
        }
3847
6.97k
    }
3848
3849
24.0k
  error:
3850
24.0k
    Py_XDECREF(abs_name);
3851
24.0k
    Py_XDECREF(mod);
3852
24.0k
    Py_XDECREF(package);
3853
24.0k
    if (final_mod == NULL) {
3854
1.64k
        remove_importlib_frames(tstate);
3855
1.64k
    }
3856
24.0k
    return final_mod;
3857
22.3k
}
3858
3859
PyObject *
3860
PyImport_ImportModuleLevel(const char *name, PyObject *globals, PyObject *locals,
3861
                           PyObject *fromlist, int level)
3862
128
{
3863
128
    PyObject *nameobj, *mod;
3864
128
    nameobj = PyUnicode_FromString(name);
3865
128
    if (nameobj == NULL)
3866
0
        return NULL;
3867
128
    mod = PyImport_ImportModuleLevelObject(nameobj, globals, locals,
3868
128
                                           fromlist, level);
3869
128
    Py_DECREF(nameobj);
3870
128
    return mod;
3871
128
}
3872
3873
3874
/* Re-import a module of any kind and return its module object, WITH
3875
   INCREMENTED REFERENCE COUNT */
3876
3877
PyObject *
3878
PyImport_ReloadModule(PyObject *m)
3879
0
{
3880
0
    PyObject *reloaded_module = NULL;
3881
0
    PyObject *importlib = PyImport_GetModule(&_Py_ID(importlib));
3882
0
    if (importlib == NULL) {
3883
0
        if (PyErr_Occurred()) {
3884
0
            return NULL;
3885
0
        }
3886
3887
0
        importlib = PyImport_ImportModule("importlib");
3888
0
        if (importlib == NULL) {
3889
0
            return NULL;
3890
0
        }
3891
0
    }
3892
3893
0
    reloaded_module = PyObject_CallMethodOneArg(importlib, &_Py_ID(reload), m);
3894
0
    Py_DECREF(importlib);
3895
0
    return reloaded_module;
3896
0
}
3897
3898
3899
/* Higher-level import emulator which emulates the "import" statement
3900
   more accurately -- it invokes the __import__() function from the
3901
   builtins of the current globals.  This means that the import is
3902
   done using whatever import hooks are installed in the current
3903
   environment.
3904
   A dummy list ["__doc__"] is passed as the 4th argument so that
3905
   e.g. PyImport_Import(PyUnicode_FromString("win32com.client.gencache"))
3906
   will return <module "gencache"> instead of <module "win32com">. */
3907
3908
PyObject *
3909
PyImport_Import(PyObject *module_name)
3910
5.16k
{
3911
5.16k
    PyThreadState *tstate = _PyThreadState_GET();
3912
5.16k
    PyObject *globals = NULL;
3913
5.16k
    PyObject *import = NULL;
3914
5.16k
    PyObject *builtins = NULL;
3915
5.16k
    PyObject *r = NULL;
3916
3917
5.16k
    PyObject *from_list = PyList_New(0);
3918
5.16k
    if (from_list == NULL) {
3919
0
        goto err;
3920
0
    }
3921
3922
    /* Get the builtins from current globals */
3923
5.16k
    globals = PyEval_GetGlobals();  // borrowed
3924
5.16k
    if (globals != NULL) {
3925
5.03k
        Py_INCREF(globals);
3926
        // XXX Use _PyEval_EnsureBuiltins()?
3927
5.03k
        builtins = PyObject_GetItem(globals, &_Py_ID(__builtins__));
3928
5.03k
        if (builtins == NULL) {
3929
            // XXX Fall back to interp->builtins or sys.modules['builtins']?
3930
0
            goto err;
3931
0
        }
3932
5.03k
    }
3933
128
    else if (_PyErr_Occurred(tstate)) {
3934
0
        goto err;
3935
0
    }
3936
128
    else {
3937
        /* No globals -- use standard builtins, and fake globals */
3938
128
        globals = PyDict_New();
3939
128
        if (globals == NULL) {
3940
0
            goto err;
3941
0
        }
3942
128
        if (_PyEval_EnsureBuiltinsWithModule(tstate, globals, &builtins) < 0) {
3943
0
            goto err;
3944
0
        }
3945
128
    }
3946
3947
    /* Get the __import__ function from the builtins */
3948
5.16k
    if (PyDict_Check(builtins)) {
3949
5.03k
        import = PyObject_GetItem(builtins, &_Py_ID(__import__));
3950
5.03k
        if (import == NULL) {
3951
0
            _PyErr_SetObject(tstate, PyExc_KeyError, &_Py_ID(__import__));
3952
0
        }
3953
5.03k
    }
3954
128
    else
3955
128
        import = PyObject_GetAttr(builtins, &_Py_ID(__import__));
3956
5.16k
    if (import == NULL)
3957
0
        goto err;
3958
3959
    /* Call the __import__ function with the proper argument list
3960
       Always use absolute import here.
3961
       Calling for side-effect of import. */
3962
5.16k
    r = PyObject_CallFunction(import, "OOOOi", module_name, globals,
3963
5.16k
                              globals, from_list, 0, NULL);
3964
5.16k
    if (r == NULL)
3965
0
        goto err;
3966
5.16k
    Py_DECREF(r);
3967
3968
5.16k
    r = import_get_module(tstate, module_name);
3969
5.16k
    if (r == NULL && !_PyErr_Occurred(tstate)) {
3970
0
        _PyErr_SetObject(tstate, PyExc_KeyError, module_name);
3971
0
    }
3972
3973
5.16k
  err:
3974
5.16k
    Py_XDECREF(globals);
3975
5.16k
    Py_XDECREF(builtins);
3976
5.16k
    Py_XDECREF(import);
3977
5.16k
    Py_XDECREF(from_list);
3978
3979
5.16k
    return r;
3980
5.16k
}
3981
3982
3983
/*********************/
3984
/* runtime lifecycle */
3985
/*********************/
3986
3987
PyStatus
3988
_PyImport_Init(void)
3989
16
{
3990
16
    if (INITTAB != NULL) {
3991
0
        return _PyStatus_ERR("global import state already initialized");
3992
0
    }
3993
16
    if (init_builtin_modules_table() != 0) {
3994
0
        return PyStatus_NoMemory();
3995
0
    }
3996
16
    return _PyStatus_OK();
3997
16
}
3998
3999
void
4000
_PyImport_Fini(void)
4001
0
{
4002
    /* Destroy the database used by _PyImport_{Fixup,Find}Extension */
4003
    // XXX Should we actually leave them (mostly) intact, since we don't
4004
    // ever dlclose() the module files?
4005
0
    _extensions_cache_clear_all();
4006
4007
    /* Free memory allocated by _PyImport_Init() */
4008
0
    fini_builtin_modules_table();
4009
0
}
4010
4011
void
4012
_PyImport_Fini2(void)
4013
0
{
4014
    // Reset PyImport_Inittab
4015
0
    PyImport_Inittab = _PyImport_Inittab;
4016
4017
    /* Free memory allocated by PyImport_ExtendInittab() */
4018
0
    _PyMem_DefaultRawFree(inittab_copy);
4019
0
    inittab_copy = NULL;
4020
0
}
4021
4022
4023
/*************************/
4024
/* interpreter lifecycle */
4025
/*************************/
4026
4027
PyStatus
4028
_PyImport_InitCore(PyThreadState *tstate, PyObject *sysmod, int importlib)
4029
16
{
4030
    // XXX Initialize here: interp->modules and interp->import_func.
4031
    // XXX Initialize here: sys.modules and sys.meta_path.
4032
4033
16
    if (importlib) {
4034
        /* This call sets up builtin and frozen import support */
4035
16
        if (init_importlib(tstate, sysmod) < 0) {
4036
0
            return _PyStatus_ERR("failed to initialize importlib");
4037
0
        }
4038
16
    }
4039
4040
16
    return _PyStatus_OK();
4041
16
}
4042
4043
/* In some corner cases it is important to be sure that the import
4044
   machinery has been initialized (or not cleaned up yet).  For
4045
   example, see issue #4236 and PyModule_Create2(). */
4046
4047
int
4048
_PyImport_IsInitialized(PyInterpreterState *interp)
4049
0
{
4050
0
    if (MODULES(interp) == NULL)
4051
0
        return 0;
4052
0
    return 1;
4053
0
}
4054
4055
/* Clear the direct per-interpreter import state, if not cleared already. */
4056
void
4057
_PyImport_ClearCore(PyInterpreterState *interp)
4058
0
{
4059
    /* interp->modules should have been cleaned up and cleared already
4060
       by _PyImport_FiniCore(). */
4061
0
    Py_CLEAR(MODULES(interp));
4062
0
    Py_CLEAR(MODULES_BY_INDEX(interp));
4063
0
    Py_CLEAR(IMPORTLIB(interp));
4064
0
    Py_CLEAR(IMPORT_FUNC(interp));
4065
0
}
4066
4067
void
4068
_PyImport_FiniCore(PyInterpreterState *interp)
4069
0
{
4070
0
    int verbose = _PyInterpreterState_GetConfig(interp)->verbose;
4071
4072
0
    if (_PySys_ClearAttrString(interp, "meta_path", verbose) < 0) {
4073
0
        PyErr_FormatUnraisable("Exception ignored while "
4074
0
                               "clearing sys.meta_path");
4075
0
    }
4076
4077
    // XXX Pull in most of finalize_modules() in pylifecycle.c.
4078
4079
0
    if (_PySys_ClearAttrString(interp, "modules", verbose) < 0) {
4080
0
        PyErr_FormatUnraisable("Exception ignored while "
4081
0
                               "clearing sys.modules");
4082
0
    }
4083
4084
0
    _PyImport_ClearCore(interp);
4085
0
}
4086
4087
// XXX Add something like _PyImport_Disable() for use early in interp fini?
4088
4089
4090
/* "external" imports */
4091
4092
static int
4093
init_zipimport(PyThreadState *tstate, int verbose)
4094
16
{
4095
16
    PyObject *path_hooks = PySys_GetAttrString("path_hooks");
4096
16
    if (path_hooks == NULL) {
4097
0
        return -1;
4098
0
    }
4099
4100
16
    if (verbose) {
4101
0
        PySys_WriteStderr("# installing zipimport hook\n");
4102
0
    }
4103
4104
16
    PyObject *zipimporter = PyImport_ImportModuleAttrString("zipimport", "zipimporter");
4105
16
    if (zipimporter == NULL) {
4106
0
        _PyErr_Clear(tstate); /* No zipimporter object -- okay */
4107
0
        if (verbose) {
4108
0
            PySys_WriteStderr("# can't import zipimport.zipimporter\n");
4109
0
        }
4110
0
    }
4111
16
    else {
4112
        /* sys.path_hooks.insert(0, zipimporter) */
4113
16
        int err = PyList_Insert(path_hooks, 0, zipimporter);
4114
16
        Py_DECREF(zipimporter);
4115
16
        if (err < 0) {
4116
0
            Py_DECREF(path_hooks);
4117
0
            return -1;
4118
0
        }
4119
16
        if (verbose) {
4120
0
            PySys_WriteStderr("# installed zipimport hook\n");
4121
0
        }
4122
16
    }
4123
16
    Py_DECREF(path_hooks);
4124
4125
16
    return 0;
4126
16
}
4127
4128
PyStatus
4129
_PyImport_InitExternal(PyThreadState *tstate)
4130
16
{
4131
16
    int verbose = _PyInterpreterState_GetConfig(tstate->interp)->verbose;
4132
4133
    // XXX Initialize here: sys.path_hooks and sys.path_importer_cache.
4134
4135
16
    if (init_importlib_external(tstate->interp) != 0) {
4136
0
        _PyErr_Print(tstate);
4137
0
        return _PyStatus_ERR("external importer setup failed");
4138
0
    }
4139
4140
16
    if (init_zipimport(tstate, verbose) != 0) {
4141
0
        PyErr_Print();
4142
0
        return _PyStatus_ERR("initializing zipimport failed");
4143
0
    }
4144
4145
16
    return _PyStatus_OK();
4146
16
}
4147
4148
void
4149
_PyImport_FiniExternal(PyInterpreterState *interp)
4150
0
{
4151
0
    int verbose = _PyInterpreterState_GetConfig(interp)->verbose;
4152
4153
    // XXX Uninstall importlib metapath importers here?
4154
4155
0
    if (_PySys_ClearAttrString(interp, "path_importer_cache", verbose) < 0) {
4156
0
        PyErr_FormatUnraisable("Exception ignored while "
4157
0
                               "clearing sys.path_importer_cache");
4158
0
    }
4159
0
    if (_PySys_ClearAttrString(interp, "path_hooks", verbose) < 0) {
4160
0
        PyErr_FormatUnraisable("Exception ignored while "
4161
0
                               "clearing sys.path_hooks");
4162
0
    }
4163
0
}
4164
4165
4166
/******************/
4167
/* module helpers */
4168
/******************/
4169
4170
PyObject *
4171
PyImport_ImportModuleAttr(PyObject *modname, PyObject *attrname)
4172
4.89k
{
4173
4.89k
    PyObject *mod = PyImport_Import(modname);
4174
4.89k
    if (mod == NULL) {
4175
0
        return NULL;
4176
0
    }
4177
4.89k
    PyObject *result = PyObject_GetAttr(mod, attrname);
4178
4.89k
    Py_DECREF(mod);
4179
4.89k
    return result;
4180
4.89k
}
4181
4182
PyObject *
4183
PyImport_ImportModuleAttrString(const char *modname, const char *attrname)
4184
2.56k
{
4185
2.56k
    PyObject *pmodname = PyUnicode_FromString(modname);
4186
2.56k
    if (pmodname == NULL) {
4187
0
        return NULL;
4188
0
    }
4189
2.56k
    PyObject *pattrname = PyUnicode_FromString(attrname);
4190
2.56k
    if (pattrname == NULL) {
4191
0
        Py_DECREF(pmodname);
4192
0
        return NULL;
4193
0
    }
4194
2.56k
    PyObject *result = PyImport_ImportModuleAttr(pmodname, pattrname);
4195
2.56k
    Py_DECREF(pattrname);
4196
2.56k
    Py_DECREF(pmodname);
4197
2.56k
    return result;
4198
2.56k
}
4199
4200
4201
/**************/
4202
/* the module */
4203
/**************/
4204
4205
/*[clinic input]
4206
_imp.lock_held
4207
4208
Return True if the import lock is currently held, else False.
4209
4210
On platforms without threads, return False.
4211
[clinic start generated code]*/
4212
4213
static PyObject *
4214
_imp_lock_held_impl(PyObject *module)
4215
/*[clinic end generated code: output=8b89384b5e1963fc input=9b088f9b217d9bdf]*/
4216
0
{
4217
0
    PyInterpreterState *interp = _PyInterpreterState_GET();
4218
0
    return PyBool_FromLong(PyMutex_IsLocked(&IMPORT_LOCK(interp).mutex));
4219
0
}
4220
4221
/*[clinic input]
4222
@permit_long_docstring_body
4223
_imp.acquire_lock
4224
4225
Acquires the interpreter's import lock for the current thread.
4226
4227
This lock should be used by import hooks to ensure thread-safety when importing
4228
modules. On platforms without threads, this function does nothing.
4229
[clinic start generated code]*/
4230
4231
static PyObject *
4232
_imp_acquire_lock_impl(PyObject *module)
4233
/*[clinic end generated code: output=1aff58cb0ee1b026 input=e1a4ef049d34e7dd]*/
4234
14.0k
{
4235
14.0k
    PyInterpreterState *interp = _PyInterpreterState_GET();
4236
14.0k
    _PyImport_AcquireLock(interp);
4237
14.0k
    Py_RETURN_NONE;
4238
14.0k
}
4239
4240
/*[clinic input]
4241
_imp.release_lock
4242
4243
Release the interpreter's import lock.
4244
4245
On platforms without threads, this function does nothing.
4246
[clinic start generated code]*/
4247
4248
static PyObject *
4249
_imp_release_lock_impl(PyObject *module)
4250
/*[clinic end generated code: output=7faab6d0be178b0a input=934fb11516dd778b]*/
4251
14.0k
{
4252
14.0k
    PyInterpreterState *interp = _PyInterpreterState_GET();
4253
14.0k
    if (!_PyRecursiveMutex_IsLockedByCurrentThread(&IMPORT_LOCK(interp))) {
4254
0
        PyErr_SetString(PyExc_RuntimeError,
4255
0
                        "not holding the import lock");
4256
0
        return NULL;
4257
0
    }
4258
14.0k
    _PyImport_ReleaseLock(interp);
4259
14.0k
    Py_RETURN_NONE;
4260
14.0k
}
4261
4262
4263
/*[clinic input]
4264
_imp._fix_co_filename
4265
4266
    code: object(type="PyCodeObject *", subclass_of="&PyCode_Type")
4267
        Code object to change.
4268
4269
    path: unicode
4270
        File path to use.
4271
    /
4272
4273
Changes code.co_filename to specify the passed-in file path.
4274
[clinic start generated code]*/
4275
4276
static PyObject *
4277
_imp__fix_co_filename_impl(PyObject *module, PyCodeObject *code,
4278
                           PyObject *path)
4279
/*[clinic end generated code: output=1d002f100235587d input=895ba50e78b82f05]*/
4280
4281
545
{
4282
545
    update_compiled_module(code, path);
4283
4284
545
    Py_RETURN_NONE;
4285
545
}
4286
4287
4288
/*[clinic input]
4289
_imp.create_builtin
4290
4291
    spec: object
4292
    /
4293
4294
Create an extension module.
4295
[clinic start generated code]*/
4296
4297
static PyObject *
4298
_imp_create_builtin(PyObject *module, PyObject *spec)
4299
/*[clinic end generated code: output=ace7ff22271e6f39 input=37f966f890384e47]*/
4300
294
{
4301
294
    PyThreadState *tstate = _PyThreadState_GET();
4302
4303
294
    PyObject *name = PyObject_GetAttrString(spec, "name");
4304
294
    if (name == NULL) {
4305
0
        return NULL;
4306
0
    }
4307
4308
294
    if (!PyUnicode_Check(name)) {
4309
0
        PyErr_Format(PyExc_TypeError,
4310
0
                     "name must be string, not %.200s",
4311
0
                     Py_TYPE(name)->tp_name);
4312
0
        Py_DECREF(name);
4313
0
        return NULL;
4314
0
    }
4315
4316
294
    PyObject *mod = create_builtin(tstate, name, spec);
4317
294
    Py_DECREF(name);
4318
294
    return mod;
4319
294
}
4320
4321
4322
/*[clinic input]
4323
_imp.extension_suffixes
4324
4325
Returns the list of file suffixes used to identify extension modules.
4326
[clinic start generated code]*/
4327
4328
static PyObject *
4329
_imp_extension_suffixes_impl(PyObject *module)
4330
/*[clinic end generated code: output=0bf346e25a8f0cd3 input=ecdeeecfcb6f839e]*/
4331
32
{
4332
32
    PyObject *list;
4333
4334
32
    list = PyList_New(0);
4335
32
    if (list == NULL)
4336
0
        return NULL;
4337
32
#ifdef HAVE_DYNAMIC_LOADING
4338
32
    const char *suffix;
4339
32
    unsigned int index = 0;
4340
4341
128
    while ((suffix = _PyImport_DynLoadFiletab[index])) {
4342
96
        PyObject *item = PyUnicode_FromString(suffix);
4343
96
        if (item == NULL) {
4344
0
            Py_DECREF(list);
4345
0
            return NULL;
4346
0
        }
4347
96
        if (PyList_Append(list, item) < 0) {
4348
0
            Py_DECREF(list);
4349
0
            Py_DECREF(item);
4350
0
            return NULL;
4351
0
        }
4352
96
        Py_DECREF(item);
4353
96
        index += 1;
4354
96
    }
4355
32
#endif
4356
32
    return list;
4357
32
}
4358
4359
/*[clinic input]
4360
_imp.init_frozen
4361
4362
    name: unicode
4363
    /
4364
4365
Initializes a frozen module.
4366
[clinic start generated code]*/
4367
4368
static PyObject *
4369
_imp_init_frozen_impl(PyObject *module, PyObject *name)
4370
/*[clinic end generated code: output=fc0511ed869fd69c input=13019adfc04f3fb3]*/
4371
0
{
4372
0
    PyThreadState *tstate = _PyThreadState_GET();
4373
0
    int ret;
4374
4375
0
    ret = PyImport_ImportFrozenModuleObject(name);
4376
0
    if (ret < 0)
4377
0
        return NULL;
4378
0
    if (ret == 0) {
4379
0
        Py_RETURN_NONE;
4380
0
    }
4381
0
    return import_add_module(tstate, name);
4382
0
}
4383
4384
/*[clinic input]
4385
_imp.find_frozen
4386
4387
    name: unicode
4388
    /
4389
    *
4390
    withdata: bool = False
4391
4392
Return info about the corresponding frozen module (if there is one) or None.
4393
4394
The returned info (a 2-tuple):
4395
4396
 * data         the raw marshalled bytes
4397
 * is_package   whether or not it is a package
4398
 * origname     the originally frozen module's name, or None if not
4399
                a stdlib module (this will usually be the same as
4400
                the module's current name)
4401
[clinic start generated code]*/
4402
4403
static PyObject *
4404
_imp_find_frozen_impl(PyObject *module, PyObject *name, int withdata)
4405
/*[clinic end generated code: output=8c1c3c7f925397a5 input=22a8847c201542fd]*/
4406
2.66k
{
4407
2.66k
    struct frozen_info info;
4408
2.66k
    frozen_status status = find_frozen(name, &info);
4409
2.66k
    if (status == FROZEN_NOT_FOUND || status == FROZEN_DISABLED) {
4410
2.47k
        Py_RETURN_NONE;
4411
2.47k
    }
4412
190
    else if (status == FROZEN_BAD_NAME) {
4413
0
        Py_RETURN_NONE;
4414
0
    }
4415
190
    else if (status != FROZEN_OKAY) {
4416
0
        set_frozen_error(status, name);
4417
0
        return NULL;
4418
0
    }
4419
4420
190
    PyObject *data = NULL;
4421
190
    if (withdata) {
4422
0
        data = PyMemoryView_FromMemory((char *)info.data, info.size, PyBUF_READ);
4423
0
        if (data == NULL) {
4424
0
            return NULL;
4425
0
        }
4426
0
    }
4427
4428
190
    PyObject *origname = NULL;
4429
190
    if (info.origname != NULL && info.origname[0] != '\0') {
4430
190
        origname = PyUnicode_FromString(info.origname);
4431
190
        if (origname == NULL) {
4432
0
            Py_XDECREF(data);
4433
0
            return NULL;
4434
0
        }
4435
190
    }
4436
4437
190
    PyObject *result = PyTuple_Pack(3, data ? data : Py_None,
4438
190
                                    info.is_package ? Py_True : Py_False,
4439
190
                                    origname ? origname : Py_None);
4440
190
    Py_XDECREF(origname);
4441
190
    Py_XDECREF(data);
4442
190
    return result;
4443
190
}
4444
4445
/*[clinic input]
4446
_imp.get_frozen_object
4447
4448
    name: unicode
4449
    data as dataobj: object = None
4450
    /
4451
4452
Create a code object for a frozen module.
4453
[clinic start generated code]*/
4454
4455
static PyObject *
4456
_imp_get_frozen_object_impl(PyObject *module, PyObject *name,
4457
                            PyObject *dataobj)
4458
/*[clinic end generated code: output=54368a673a35e745 input=034bdb88f6460b7b]*/
4459
190
{
4460
190
    struct frozen_info info = {0};
4461
190
    Py_buffer buf = {0};
4462
190
    if (PyObject_CheckBuffer(dataobj)) {
4463
0
        if (PyObject_GetBuffer(dataobj, &buf, PyBUF_SIMPLE) != 0) {
4464
0
            return NULL;
4465
0
        }
4466
0
        info.data = (const char *)buf.buf;
4467
0
        info.size = buf.len;
4468
0
    }
4469
190
    else if (dataobj != Py_None) {
4470
0
        _PyArg_BadArgument("get_frozen_object", "argument 2", "bytes", dataobj);
4471
0
        return NULL;
4472
0
    }
4473
190
    else {
4474
190
        frozen_status status = find_frozen(name, &info);
4475
190
        if (status != FROZEN_OKAY) {
4476
0
            set_frozen_error(status, name);
4477
0
            return NULL;
4478
0
        }
4479
190
    }
4480
4481
190
    if (info.nameobj == NULL) {
4482
0
        info.nameobj = name;
4483
0
    }
4484
190
    if (info.size == 0) {
4485
        /* Does not contain executable code. */
4486
0
        set_frozen_error(FROZEN_INVALID, name);
4487
0
        return NULL;
4488
0
    }
4489
4490
190
    PyInterpreterState *interp = _PyInterpreterState_GET();
4491
190
    PyObject *codeobj = unmarshal_frozen_code(interp, &info);
4492
190
    if (dataobj != Py_None) {
4493
0
        PyBuffer_Release(&buf);
4494
0
    }
4495
190
    return codeobj;
4496
190
}
4497
4498
/*[clinic input]
4499
_imp.is_frozen_package
4500
4501
    name: unicode
4502
    /
4503
4504
Returns True if the module name is of a frozen package.
4505
[clinic start generated code]*/
4506
4507
static PyObject *
4508
_imp_is_frozen_package_impl(PyObject *module, PyObject *name)
4509
/*[clinic end generated code: output=e70cbdb45784a1c9 input=81b6cdecd080fbb8]*/
4510
16
{
4511
16
    struct frozen_info info;
4512
16
    frozen_status status = find_frozen(name, &info);
4513
16
    if (status != FROZEN_OKAY && status != FROZEN_EXCLUDED) {
4514
0
        set_frozen_error(status, name);
4515
0
        return NULL;
4516
0
    }
4517
16
    return PyBool_FromLong(info.is_package);
4518
16
}
4519
4520
/*[clinic input]
4521
_imp.is_builtin
4522
4523
    name: unicode
4524
    /
4525
4526
Returns True if the module name corresponds to a built-in module.
4527
[clinic start generated code]*/
4528
4529
static PyObject *
4530
_imp_is_builtin_impl(PyObject *module, PyObject *name)
4531
/*[clinic end generated code: output=3bfd1162e2d3be82 input=86befdac021dd1c7]*/
4532
2.95k
{
4533
2.95k
    return PyLong_FromLong(is_builtin(name));
4534
2.95k
}
4535
4536
/*[clinic input]
4537
_imp.is_frozen
4538
4539
    name: unicode
4540
    /
4541
4542
Returns True if the module name corresponds to a frozen module.
4543
[clinic start generated code]*/
4544
4545
static PyObject *
4546
_imp_is_frozen_impl(PyObject *module, PyObject *name)
4547
/*[clinic end generated code: output=01f408f5ec0f2577 input=7301dbca1897d66b]*/
4548
16
{
4549
16
    struct frozen_info info;
4550
16
    frozen_status status = find_frozen(name, &info);
4551
16
    if (status != FROZEN_OKAY) {
4552
0
        Py_RETURN_FALSE;
4553
0
    }
4554
16
    Py_RETURN_TRUE;
4555
16
}
4556
4557
/*[clinic input]
4558
_imp._frozen_module_names
4559
4560
Returns the list of available frozen modules.
4561
[clinic start generated code]*/
4562
4563
static PyObject *
4564
_imp__frozen_module_names_impl(PyObject *module)
4565
/*[clinic end generated code: output=80609ef6256310a8 input=76237fbfa94460d2]*/
4566
0
{
4567
0
    return list_frozen_module_names();
4568
0
}
4569
4570
/*[clinic input]
4571
_imp._override_frozen_modules_for_tests
4572
4573
    override: int
4574
    /
4575
4576
(internal-only) Override PyConfig.use_frozen_modules.
4577
4578
(-1: "off", 1: "on", 0: no override)
4579
See frozen_modules() in Lib/test/support/import_helper.py.
4580
[clinic start generated code]*/
4581
4582
static PyObject *
4583
_imp__override_frozen_modules_for_tests_impl(PyObject *module, int override)
4584
/*[clinic end generated code: output=36d5cb1594160811 input=8f1f95a3ef21aec3]*/
4585
0
{
4586
0
    PyInterpreterState *interp = _PyInterpreterState_GET();
4587
0
    OVERRIDE_FROZEN_MODULES(interp) = override;
4588
0
    Py_RETURN_NONE;
4589
0
}
4590
4591
/*[clinic input]
4592
_imp._override_multi_interp_extensions_check
4593
4594
    override: int
4595
    /
4596
4597
(internal-only) Override PyInterpreterConfig.check_multi_interp_extensions.
4598
4599
(-1: "never", 1: "always", 0: no override)
4600
[clinic start generated code]*/
4601
4602
static PyObject *
4603
_imp__override_multi_interp_extensions_check_impl(PyObject *module,
4604
                                                  int override)
4605
/*[clinic end generated code: output=3ff043af52bbf280 input=e086a2ea181f92ae]*/
4606
0
{
4607
0
    PyInterpreterState *interp = _PyInterpreterState_GET();
4608
0
    if (_Py_IsMainInterpreter(interp)) {
4609
0
        PyErr_SetString(PyExc_RuntimeError,
4610
0
                        "_imp._override_multi_interp_extensions_check() "
4611
0
                        "cannot be used in the main interpreter");
4612
0
        return NULL;
4613
0
    }
4614
#ifdef Py_GIL_DISABLED
4615
    PyErr_SetString(PyExc_RuntimeError,
4616
                    "_imp._override_multi_interp_extensions_check() "
4617
                    "cannot be used in the free-threaded build");
4618
    return NULL;
4619
#else
4620
0
    int oldvalue = OVERRIDE_MULTI_INTERP_EXTENSIONS_CHECK(interp);
4621
0
    OVERRIDE_MULTI_INTERP_EXTENSIONS_CHECK(interp) = override;
4622
0
    return PyLong_FromLong(oldvalue);
4623
0
#endif
4624
0
}
4625
4626
#ifdef HAVE_DYNAMIC_LOADING
4627
4628
/*[clinic input]
4629
_imp.create_dynamic
4630
4631
    spec: object
4632
    file: object = NULL
4633
    /
4634
4635
Create an extension module.
4636
[clinic start generated code]*/
4637
4638
static PyObject *
4639
_imp_create_dynamic_impl(PyObject *module, PyObject *spec, PyObject *file)
4640
/*[clinic end generated code: output=83249b827a4fde77 input=c31b954f4cf4e09d]*/
4641
79
{
4642
79
    PyObject *mod = NULL;
4643
79
    PyThreadState *tstate = _PyThreadState_GET();
4644
4645
79
    struct _Py_ext_module_loader_info info;
4646
79
    if (_Py_ext_module_loader_info_init_from_spec(&info, spec) < 0) {
4647
0
        return NULL;
4648
0
    }
4649
4650
79
    struct extensions_cache_value *cached = NULL;
4651
79
    mod = import_find_extension(tstate, &info, &cached);
4652
79
    if (mod != NULL) {
4653
0
        assert(!_PyErr_Occurred(tstate));
4654
0
        assert(cached != NULL);
4655
        /* The module might not have md_def set in certain reload cases. */
4656
0
        assert(_PyModule_GetDef(mod) == NULL
4657
0
                || cached->def == _PyModule_GetDef(mod));
4658
0
        assert_singlephase(cached);
4659
0
        goto finally;
4660
0
    }
4661
79
    else if (_PyErr_Occurred(tstate)) {
4662
0
        goto finally;
4663
0
    }
4664
    /* Otherwise it must be multi-phase init or the first time it's loaded. */
4665
4666
    /* If the module was added to the global cache
4667
     * but def->m_base.m_copy was cleared (e.g. subinterp fini)
4668
     * then we have to do a little dance here. */
4669
79
    if (cached != NULL) {
4670
0
        assert(cached->def->m_base.m_copy == NULL);
4671
        /* For now we clear the cache and move on. */
4672
0
        _extensions_cache_delete(info.path, info.name);
4673
0
    }
4674
4675
79
    if (PySys_Audit("import", "OOOOO", info.name, info.filename,
4676
79
                    Py_None, Py_None, Py_None) < 0)
4677
0
    {
4678
0
        goto finally;
4679
0
    }
4680
4681
    /* We would move this (and the fclose() below) into
4682
     * _PyImport_GetModInitFunc(), but it isn't clear if the intervening
4683
     * code relies on fp still being open. */
4684
79
    FILE *fp;
4685
79
    if (file != NULL) {
4686
0
        fp = Py_fopen(info.filename, "r");
4687
0
        if (fp == NULL) {
4688
0
            goto finally;
4689
0
        }
4690
0
    }
4691
79
    else {
4692
79
        fp = NULL;
4693
79
    }
4694
4695
79
    PyModInitFunction p0 = _PyImport_GetModInitFunc(&info, fp);
4696
79
    if (p0 == NULL) {
4697
0
        goto finally;
4698
0
    }
4699
4700
#ifdef Py_GIL_DISABLED
4701
    // This call (and the corresponding call to _PyImport_CheckGILForModule())
4702
    // would ideally be inside import_run_extension(). They are kept in the
4703
    // callers for now because that would complicate the control flow inside
4704
    // import_run_extension(). It should be possible to restructure
4705
    // import_run_extension() to address this.
4706
    _PyEval_EnableGILTransient(tstate);
4707
#endif
4708
79
    mod = import_run_extension(
4709
79
                    tstate, p0, &info, spec, get_modules_dict(tstate, true));
4710
#ifdef Py_GIL_DISABLED
4711
    if (_PyImport_CheckGILForModule(mod, info.name) < 0) {
4712
        Py_CLEAR(mod);
4713
        goto finally;
4714
    }
4715
#endif
4716
4717
    // XXX Shouldn't this happen in the error cases too (i.e. in "finally")?
4718
79
    if (fp) {
4719
0
        fclose(fp);
4720
0
    }
4721
4722
79
finally:
4723
79
    _Py_ext_module_loader_info_clear(&info);
4724
79
    return mod;
4725
79
}
4726
4727
/*[clinic input]
4728
_imp.exec_dynamic -> int
4729
4730
    mod: object
4731
    /
4732
4733
Initialize an extension module.
4734
[clinic start generated code]*/
4735
4736
static int
4737
_imp_exec_dynamic_impl(PyObject *module, PyObject *mod)
4738
/*[clinic end generated code: output=f5720ac7b465877d input=9fdbfcb250280d3a]*/
4739
79
{
4740
79
    return exec_builtin_or_dynamic(mod);
4741
79
}
4742
4743
4744
#endif /* HAVE_DYNAMIC_LOADING */
4745
4746
/*[clinic input]
4747
_imp.exec_builtin -> int
4748
4749
    mod: object
4750
    /
4751
4752
Initialize a built-in module.
4753
[clinic start generated code]*/
4754
4755
static int
4756
_imp_exec_builtin_impl(PyObject *module, PyObject *mod)
4757
/*[clinic end generated code: output=0262447b240c038e input=7beed5a2f12a60ca]*/
4758
294
{
4759
294
    return exec_builtin_or_dynamic(mod);
4760
294
}
4761
4762
/*[clinic input]
4763
_imp.source_hash
4764
4765
    key: long
4766
    source: Py_buffer
4767
[clinic start generated code]*/
4768
4769
static PyObject *
4770
_imp_source_hash_impl(PyObject *module, long key, Py_buffer *source)
4771
/*[clinic end generated code: output=edb292448cf399ea input=9aaad1e590089789]*/
4772
0
{
4773
0
    union {
4774
0
        uint64_t x;
4775
0
        char data[sizeof(uint64_t)];
4776
0
    } hash;
4777
0
    hash.x = _Py_KeyedHash((uint64_t)key, source->buf, source->len);
4778
#if !PY_LITTLE_ENDIAN
4779
    // Force to little-endian. There really ought to be a succinct standard way
4780
    // to do this.
4781
    for (size_t i = 0; i < sizeof(hash.data)/2; i++) {
4782
        char tmp = hash.data[i];
4783
        hash.data[i] = hash.data[sizeof(hash.data) - i - 1];
4784
        hash.data[sizeof(hash.data) - i - 1] = tmp;
4785
    }
4786
#endif
4787
0
    return PyBytes_FromStringAndSize(hash.data, sizeof(hash.data));
4788
0
}
4789
4790
4791
PyDoc_STRVAR(doc_imp,
4792
"(Extremely) low-level import machinery bits as used by importlib.");
4793
4794
static PyMethodDef imp_methods[] = {
4795
    _IMP_EXTENSION_SUFFIXES_METHODDEF
4796
    _IMP_LOCK_HELD_METHODDEF
4797
    _IMP_ACQUIRE_LOCK_METHODDEF
4798
    _IMP_RELEASE_LOCK_METHODDEF
4799
    _IMP_FIND_FROZEN_METHODDEF
4800
    _IMP_GET_FROZEN_OBJECT_METHODDEF
4801
    _IMP_IS_FROZEN_PACKAGE_METHODDEF
4802
    _IMP_CREATE_BUILTIN_METHODDEF
4803
    _IMP_INIT_FROZEN_METHODDEF
4804
    _IMP_IS_BUILTIN_METHODDEF
4805
    _IMP_IS_FROZEN_METHODDEF
4806
    _IMP__FROZEN_MODULE_NAMES_METHODDEF
4807
    _IMP__OVERRIDE_FROZEN_MODULES_FOR_TESTS_METHODDEF
4808
    _IMP__OVERRIDE_MULTI_INTERP_EXTENSIONS_CHECK_METHODDEF
4809
    _IMP_CREATE_DYNAMIC_METHODDEF
4810
    _IMP_EXEC_DYNAMIC_METHODDEF
4811
    _IMP_EXEC_BUILTIN_METHODDEF
4812
    _IMP__FIX_CO_FILENAME_METHODDEF
4813
    _IMP_SOURCE_HASH_METHODDEF
4814
    {NULL, NULL}  /* sentinel */
4815
};
4816
4817
4818
static int
4819
imp_module_exec(PyObject *module)
4820
16
{
4821
16
    const wchar_t *mode = _Py_GetConfig()->check_hash_pycs_mode;
4822
16
    PyObject *pyc_mode = PyUnicode_FromWideChar(mode, -1);
4823
16
    if (PyModule_Add(module, "check_hash_based_pycs", pyc_mode) < 0) {
4824
0
        return -1;
4825
0
    }
4826
4827
16
    if (PyModule_AddIntConstant(
4828
16
            module, "pyc_magic_number_token", PYC_MAGIC_NUMBER_TOKEN) < 0)
4829
0
    {
4830
0
        return -1;
4831
0
    }
4832
4833
16
    return 0;
4834
16
}
4835
4836
4837
static PyModuleDef_Slot imp_slots[] = {
4838
    {Py_mod_exec, imp_module_exec},
4839
    {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
4840
    {Py_mod_gil, Py_MOD_GIL_NOT_USED},
4841
    {0, NULL}
4842
};
4843
4844
static struct PyModuleDef imp_module = {
4845
    PyModuleDef_HEAD_INIT,
4846
    .m_name = "_imp",
4847
    .m_doc = doc_imp,
4848
    .m_size = 0,
4849
    .m_methods = imp_methods,
4850
    .m_slots = imp_slots,
4851
};
4852
4853
PyMODINIT_FUNC
4854
PyInit__imp(void)
4855
16
{
4856
16
    return PyModuleDef_Init(&imp_module);
4857
16
}