bpo-28411: Isolate PyInterpreterState.modules (#3575) · python/cpython@d393c1b (original) (raw)
`@@ -296,6 +296,17 @@ PyImport_GetModuleDict(void)
`
296
296
`return interp->modules;
`
297
297
`}
`
298
298
``
``
299
`+
/* In some corner cases it is important to be sure that the import
`
``
300
`+
machinery has been initialized (or not cleaned up yet). For
`
``
301
`+
example, see issue #4236 and PyModule_Create2(). */
`
``
302
+
``
303
`+
int
`
``
304
`+
_PyImport_IsInitialized(PyInterpreterState *interp)
`
``
305
`+
{
`
``
306
`+
if (interp->modules == NULL)
`
``
307
`+
return 0;
`
``
308
`+
return 1;
`
``
309
`+
}
`
299
310
``
300
311
`/* List of names to clear in sys */
`
301
312
`static const char * const sys_deletes[] = {
`
`@@ -323,7 +334,7 @@ PyImport_Cleanup(void)
`
323
334
`Py_ssize_t pos;
`
324
335
`PyObject *key, *value, *dict;
`
325
336
`PyInterpreterState *interp = PyThreadState_GET()->interp;
`
326
``
`-
PyObject *modules = interp->modules;
`
``
337
`+
PyObject *modules = PyImport_GetModuleDict();
`
327
338
`PyObject *weaklist = NULL;
`
328
339
`const char * const *p;
`
329
340
``
`@@ -511,9 +522,9 @@ PyImport_GetMagicTag(void)
`
511
522
``
512
523
`int
`
513
524
`_PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
`
514
``
`-
PyObject *filename)
`
``
525
`+
PyObject *filename, PyObject *modules)
`
515
526
`{
`
516
``
`-
PyObject *modules, *dict, *key;
`
``
527
`+
PyObject *dict, *key;
`
517
528
`struct PyModuleDef *def;
`
518
529
`int res;
`
519
530
`if (extensions == NULL) {
`
`@@ -530,7 +541,6 @@ _PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
`
530
541
`PyErr_BadInternalCall();
`
531
542
`return -1;
`
532
543
` }
`
533
``
`-
modules = PyImport_GetModuleDict();
`
534
544
`if (PyDict_SetItem(modules, name, mod) < 0)
`
535
545
`return -1;
`
536
546
`if (_PyState_AddModule(mod, def) < 0) {
`
`@@ -562,20 +572,28 @@ _PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
`
562
572
`}
`
563
573
``
564
574
`int
`
565
``
`-
_PyImport_FixupBuiltin(PyObject *mod, const char *name)
`
``
575
`+
_PyImport_FixupBuiltin(PyObject *mod, const char *name, PyObject *modules)
`
566
576
`{
`
567
577
`int res;
`
568
578
`PyObject *nameobj;
`
569
579
`nameobj = PyUnicode_InternFromString(name);
`
570
580
`if (nameobj == NULL)
`
571
581
`return -1;
`
572
``
`-
res = _PyImport_FixupExtensionObject(mod, nameobj, nameobj);
`
``
582
`+
res = _PyImport_FixupExtensionObject(mod, nameobj, nameobj, modules);
`
573
583
`Py_DECREF(nameobj);
`
574
584
`return res;
`
575
585
`}
`
576
586
``
577
587
`PyObject *
`
578
588
`_PyImport_FindExtensionObject(PyObject *name, PyObject *filename)
`
``
589
`+
{
`
``
590
`+
PyObject *modules = PyImport_GetModuleDict();
`
``
591
`+
return _PyImport_FindExtensionObjectEx(name, filename, modules);
`
``
592
`+
}
`
``
593
+
``
594
`+
PyObject *
`
``
595
`+
_PyImport_FindExtensionObjectEx(PyObject *name, PyObject *filename,
`
``
596
`+
PyObject *modules)
`
579
597
`{
`
580
598
`PyObject *mod, *mdict, *key;
`
581
599
`PyModuleDef* def;
`
`@@ -592,7 +610,7 @@ _PyImport_FindExtensionObject(PyObject *name, PyObject *filename)
`
592
610
`/* Module does not support repeated initialization */
`
593
611
`if (def->m_base.m_copy == NULL)
`
594
612
`return NULL;
`
595
``
`-
mod = PyImport_AddModuleObject(name);
`
``
613
`+
mod = _PyImport_AddModuleObject(name, modules);
`
596
614
`if (mod == NULL)
`
597
615
`return NULL;
`
598
616
`mdict = PyModule_GetDict(mod);
`
`@@ -607,14 +625,14 @@ _PyImport_FindExtensionObject(PyObject *name, PyObject *filename)
`
607
625
`mod = def->m_base.m_init();
`
608
626
`if (mod == NULL)
`
609
627
`return NULL;
`
610
``
`-
if (PyDict_SetItem(PyImport_GetModuleDict(), name, mod) == -1) {
`
``
628
`+
if (PyDict_SetItem(modules, name, mod) == -1) {
`
611
629
`Py_DECREF(mod);
`
612
630
`return NULL;
`
613
631
` }
`
614
632
`Py_DECREF(mod);
`
615
633
` }
`
616
634
`if (_PyState_AddModule(mod, def) < 0) {
`
617
``
`-
PyDict_DelItem(PyImport_GetModuleDict(), name);
`
``
635
`+
PyDict_DelItem(modules, name);
`
618
636
`Py_DECREF(mod);
`
619
637
`return NULL;
`
620
638
` }
`
`@@ -626,13 +644,13 @@ _PyImport_FindExtensionObject(PyObject *name, PyObject *filename)
`
626
644
`}
`
627
645
``
628
646
`PyObject *
`
629
``
`-
_PyImport_FindBuiltin(const char *name)
`
``
647
`+
_PyImport_FindBuiltin(const char *name, PyObject *modules)
`
630
648
`{
`
631
649
`PyObject *res, *nameobj;
`
632
650
`nameobj = PyUnicode_InternFromString(name);
`
633
651
`if (nameobj == NULL)
`
634
652
`return NULL;
`
635
``
`-
res = _PyImport_FindExtensionObject(nameobj, nameobj);
`
``
653
`+
res = _PyImport_FindExtensionObjectEx(nameobj, nameobj, modules);
`
636
654
`Py_DECREF(nameobj);
`
637
655
`return res;
`
638
656
`}
`
`@@ -647,6 +665,12 @@ PyObject *
`
647
665
`PyImport_AddModuleObject(PyObject *name)
`
648
666
`{
`
649
667
`PyObject *modules = PyImport_GetModuleDict();
`
``
668
`+
return _PyImport_AddModuleObject(name, modules);
`
``
669
`+
}
`
``
670
+
``
671
`+
PyObject *
`
``
672
`+
_PyImport_AddModuleObject(PyObject *name, PyObject *modules)
`
``
673
`+
{
`
650
674
`PyObject *m;
`
651
675
``
652
676
`if ((m = PyDict_GetItemWithError(modules, name)) != NULL &&
`
`@@ -1042,6 +1066,7 @@ _imp_create_builtin(PyObject *module, PyObject *spec)
`
1042
1066
`return NULL;
`
1043
1067
` }
`
1044
1068
``
``
1069
`+
PyObject *modules = NULL;
`
1045
1070
`for (p = PyImport_Inittab; p->name != NULL; p++) {
`
1046
1071
`PyModuleDef *def;
`
1047
1072
`if (_PyUnicode_EqualToASCIIString(name, p->name)) {
`
`@@ -1067,7 +1092,11 @@ _imp_create_builtin(PyObject *module, PyObject *spec)
`
1067
1092
`return NULL;
`
1068
1093
` }
`
1069
1094
`def->m_base.m_init = p->initfunc;
`
1070
``
`-
if (_PyImport_FixupExtensionObject(mod, name, name) < 0) {
`
``
1095
`+
if (modules == NULL) {
`
``
1096
`+
modules = PyImport_GetModuleDict();
`
``
1097
`+
}
`
``
1098
`+
if (_PyImport_FixupExtensionObject(mod, name, name,
`
``
1099
`+
modules) < 0) {
`
1071
1100
`Py_DECREF(name);
`
1072
1101
`return NULL;
`
1073
1102
` }
`
`@@ -1511,7 +1540,8 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
`
1511
1540
`Py_INCREF(abs_name);
`
1512
1541
` }
`
1513
1542
``
1514
``
`-
mod = PyDict_GetItem(interp->modules, abs_name);
`
``
1543
`+
PyObject *modules = PyImport_GetModuleDict();
`
``
1544
`+
mod = PyDict_GetItem(modules, abs_name);
`
1515
1545
`if (mod != NULL && mod != Py_None) {
`
1516
1546
`_Py_IDENTIFIER(spec);
`
1517
1547
`_Py_IDENTIFIER(_initializing);
`
`@@ -1598,7 +1628,8 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
`
1598
1628
` goto error;
`
1599
1629
` }
`
1600
1630
``
1601
``
`-
final_mod = PyDict_GetItem(interp->modules, to_return);
`
``
1631
`+
PyObject *modules = PyImport_GetModuleDict();
`
``
1632
`+
final_mod = PyDict_GetItem(modules, to_return);
`
1602
1633
`Py_DECREF(to_return);
`
1603
1634
`if (final_mod == NULL) {
`
1604
1635
`PyErr_Format(PyExc_KeyError,
`