@@ -296,6 +296,17 @@ PyImport_GetModuleDict(void)
296296 return interp -> modules ;
297297}
298298
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+ }
299310
300311/* List of names to clear in sys */
301312static const char * const sys_deletes [] = {
@@ -323,7 +334,7 @@ PyImport_Cleanup(void)
323334 Py_ssize_t pos ;
324335 PyObject * key , * value , * dict ;
325336 PyInterpreterState * interp = PyThreadState_GET ()-> interp ;
326- PyObject * modules = interp -> modules ;
337+ PyObject * modules = PyImport_GetModuleDict () ;
327338 PyObject * weaklist = NULL ;
328339 const char * const * p ;
329340
@@ -511,9 +522,9 @@ PyImport_GetMagicTag(void)
511522
512523int
513524_PyImport_FixupExtensionObject (PyObject * mod , PyObject * name ,
514- PyObject * filename )
525+ PyObject * filename , PyObject * modules )
515526{
516- PyObject * modules , * dict , * key ;
527+ PyObject * dict , * key ;
517528 struct PyModuleDef * def ;
518529 int res ;
519530 if (extensions == NULL ) {
@@ -530,7 +541,6 @@ _PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
530541 PyErr_BadInternalCall ();
531542 return -1 ;
532543 }
533- modules = PyImport_GetModuleDict ();
534544 if (PyDict_SetItem (modules , name , mod ) < 0 )
535545 return -1 ;
536546 if (_PyState_AddModule (mod , def ) < 0 ) {
@@ -562,20 +572,28 @@ _PyImport_FixupExtensionObject(PyObject *mod, PyObject *name,
562572}
563573
564574int
565- _PyImport_FixupBuiltin (PyObject * mod , const char * name )
575+ _PyImport_FixupBuiltin (PyObject * mod , const char * name , PyObject * modules )
566576{
567577 int res ;
568578 PyObject * nameobj ;
569579 nameobj = PyUnicode_InternFromString (name );
570580 if (nameobj == NULL )
571581 return -1 ;
572- res = _PyImport_FixupExtensionObject (mod , nameobj , nameobj );
582+ res = _PyImport_FixupExtensionObject (mod , nameobj , nameobj , modules );
573583 Py_DECREF (nameobj );
574584 return res ;
575585}
576586
577587PyObject *
578588_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 )
579597{
580598 PyObject * mod , * mdict , * key ;
581599 PyModuleDef * def ;
@@ -592,7 +610,7 @@ _PyImport_FindExtensionObject(PyObject *name, PyObject *filename)
592610 /* Module does not support repeated initialization */
593611 if (def -> m_base .m_copy == NULL )
594612 return NULL ;
595- mod = PyImport_AddModuleObject (name );
613+ mod = _PyImport_AddModuleObject (name , modules );
596614 if (mod == NULL )
597615 return NULL ;
598616 mdict = PyModule_GetDict (mod );
@@ -607,14 +625,14 @@ _PyImport_FindExtensionObject(PyObject *name, PyObject *filename)
607625 mod = def -> m_base .m_init ();
608626 if (mod == NULL )
609627 return NULL ;
610- if (PyDict_SetItem (PyImport_GetModuleDict () , name , mod ) == -1 ) {
628+ if (PyDict_SetItem (modules , name , mod ) == -1 ) {
611629 Py_DECREF (mod );
612630 return NULL ;
613631 }
614632 Py_DECREF (mod );
615633 }
616634 if (_PyState_AddModule (mod , def ) < 0 ) {
617- PyDict_DelItem (PyImport_GetModuleDict () , name );
635+ PyDict_DelItem (modules , name );
618636 Py_DECREF (mod );
619637 return NULL ;
620638 }
@@ -626,13 +644,13 @@ _PyImport_FindExtensionObject(PyObject *name, PyObject *filename)
626644}
627645
628646PyObject *
629- _PyImport_FindBuiltin (const char * name )
647+ _PyImport_FindBuiltin (const char * name , PyObject * modules )
630648{
631649 PyObject * res , * nameobj ;
632650 nameobj = PyUnicode_InternFromString (name );
633651 if (nameobj == NULL )
634652 return NULL ;
635- res = _PyImport_FindExtensionObject (nameobj , nameobj );
653+ res = _PyImport_FindExtensionObjectEx (nameobj , nameobj , modules );
636654 Py_DECREF (nameobj );
637655 return res ;
638656}
@@ -647,6 +665,12 @@ PyObject *
647665PyImport_AddModuleObject (PyObject * name )
648666{
649667 PyObject * modules = PyImport_GetModuleDict ();
668+ return _PyImport_AddModuleObject (name , modules );
669+ }
670+
671+ PyObject *
672+ _PyImport_AddModuleObject (PyObject * name , PyObject * modules )
673+ {
650674 PyObject * m ;
651675
652676 if ((m = PyDict_GetItemWithError (modules , name )) != NULL &&
@@ -1042,6 +1066,7 @@ _imp_create_builtin(PyObject *module, PyObject *spec)
10421066 return NULL ;
10431067 }
10441068
1069+ PyObject * modules = NULL ;
10451070 for (p = PyImport_Inittab ; p -> name != NULL ; p ++ ) {
10461071 PyModuleDef * def ;
10471072 if (_PyUnicode_EqualToASCIIString (name , p -> name )) {
@@ -1067,7 +1092,11 @@ _imp_create_builtin(PyObject *module, PyObject *spec)
10671092 return NULL ;
10681093 }
10691094 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 ) {
10711100 Py_DECREF (name );
10721101 return NULL ;
10731102 }
@@ -1511,7 +1540,8 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
15111540 Py_INCREF (abs_name );
15121541 }
15131542
1514- mod = PyDict_GetItem (interp -> modules , abs_name );
1543+ PyObject * modules = PyImport_GetModuleDict ();
1544+ mod = PyDict_GetItem (modules , abs_name );
15151545 if (mod != NULL && mod != Py_None ) {
15161546 _Py_IDENTIFIER (__spec__ );
15171547 _Py_IDENTIFIER (_initializing );
@@ -1598,7 +1628,8 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
15981628 goto error ;
15991629 }
16001630
1601- final_mod = PyDict_GetItem (interp -> modules , to_return );
1631+ PyObject * modules = PyImport_GetModuleDict ();
1632+ final_mod = PyDict_GetItem (modules , to_return );
16021633 Py_DECREF (to_return );
16031634 if (final_mod == NULL ) {
16041635 PyErr_Format (PyExc_KeyError ,
0 commit comments