@@ -966,11 +966,10 @@ exec_code_in_module(PyObject *name, PyObject *module_dict, PyObject *code_object
966966 Py_DECREF (v );
967967
968968 m = PyImport_GetModule (name );
969- if (m == NULL ) {
969+ if (m == NULL && ! PyErr_Occurred () ) {
970970 PyErr_Format (PyExc_ImportError ,
971971 "Loaded module %R not found in sys.modules" ,
972972 name );
973- return NULL ;
974973 }
975974
976975 return m ;
@@ -1735,6 +1734,10 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
17351734 }
17361735
17371736 mod = PyImport_GetModule (abs_name );
1737+ if (mod == NULL && PyErr_Occurred ()) {
1738+ goto error ;
1739+ }
1740+
17381741 if (mod != NULL && mod != Py_None ) {
17391742 _Py_IDENTIFIER (__spec__ );
17401743 _Py_IDENTIFIER (_lock_unlock_module );
@@ -1810,9 +1813,11 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
18101813 final_mod = PyImport_GetModule (to_return );
18111814 Py_DECREF (to_return );
18121815 if (final_mod == NULL ) {
1813- PyErr_Format (PyExc_KeyError ,
1814- "%R not in sys.modules as expected" ,
1815- to_return );
1816+ if (!PyErr_Occurred ()) {
1817+ PyErr_Format (PyExc_KeyError ,
1818+ "%R not in sys.modules as expected" ,
1819+ to_return );
1820+ }
18161821 goto error ;
18171822 }
18181823 }
@@ -1875,6 +1880,10 @@ PyImport_ReloadModule(PyObject *m)
18751880 PyObject * reloaded_module = NULL ;
18761881 PyObject * imp = _PyImport_GetModuleId (& PyId_imp );
18771882 if (imp == NULL ) {
1883+ if (PyErr_Occurred ()) {
1884+ return NULL ;
1885+ }
1886+
18781887 imp = PyImport_ImportModule ("imp" );
18791888 if (imp == NULL ) {
18801889 return NULL ;
0 commit comments