Coverage Report

Created: 2025-11-16 06:26

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