Coverage Report

Created: 2025-11-11 06:44

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