@@ -3874,6 +3874,10 @@ _PyImport_LoadLazyImportTstate(PyThreadState *tstate, PyObject *lazy_import)
38743874 if (PyMapping_GetOptionalItem (lz -> lz_builtins , & _Py_ID (__import__ ), & import_func ) < 0 ) {
38753875 goto error ;
38763876 }
3877+ if (import_func == NULL ) {
3878+ PyErr_SetString (PyExc_ImportError , "__import__ not found" );
3879+ goto error ;
3880+ }
38773881 if (full ) {
38783882 obj = _PyEval_ImportNameWithImport (tstate ,
38793883 import_func ,
@@ -4305,16 +4309,22 @@ register_lazy_on_parent(PyThreadState *tstate, PyObject *name, PyObject *builtin
43054309 if (parent_dict == NULL ) {
43064310 goto done ;
43074311 }
4308- if (PyDict_CheckExact (parent_dict ) && ! PyDict_Contains ( parent_dict , child ) ) {
4309- PyObject * lazy_module_attr = _PyLazyImport_New ( builtins , parent , child );
4310- if (lazy_module_attr == NULL ) {
4312+ if (PyDict_CheckExact (parent_dict )) {
4313+ int contains = PyDict_Contains ( parent_dict , child );
4314+ if (contains < 0 ) {
43114315 goto done ;
43124316 }
4313- if (PyDict_SetItem (parent_dict , child , lazy_module_attr ) < 0 ) {
4317+ if (!contains ) {
4318+ PyObject * lazy_module_attr = _PyLazyImport_New (builtins , parent , child );
4319+ if (lazy_module_attr == NULL ) {
4320+ goto done ;
4321+ }
4322+ if (PyDict_SetItem (parent_dict , child , lazy_module_attr ) < 0 ) {
4323+ Py_DECREF (lazy_module_attr );
4324+ goto done ;
4325+ }
43144326 Py_DECREF (lazy_module_attr );
4315- goto done ;
43164327 }
4317- Py_DECREF (lazy_module_attr );
43184328 }
43194329 }
43204330
@@ -4344,7 +4354,8 @@ _PyImport_LazyImportModuleLevelObject(PyThreadState *tstate,
43444354 }
43454355
43464356 PyInterpreterState * interp = tstate -> interp ;
4347- assert (_PyEval_GetFrame ()-> f_globals == _PyEval_GetFrame ()-> f_locals ); // should only be called in global scope
4357+ _PyInterpreterFrame * frame = _PyEval_GetFrame ();
4358+ assert (frame != NULL && frame -> f_globals == frame -> f_locals ); // should only be called in global scope
43484359
43494360 // Check if the filter disables the lazy import
43504361 PyObject * filter = FT_ATOMIC_LOAD_PTR_RELAXED (LAZY_IMPORTS_FILTER (interp ));
@@ -4374,6 +4385,10 @@ _PyImport_LazyImportModuleLevelObject(PyThreadState *tstate,
43744385 int is_true = PyObject_IsTrue (res );
43754386 Py_DECREF (res );
43764387
4388+ if (is_true < 0 ) {
4389+ Py_DECREF (abs_name );
4390+ return NULL ;
4391+ }
43774392 if (!is_true ) {
43784393 Py_DECREF (abs_name );
43794394 return PyImport_ImportModuleLevelObject (
0 commit comments