@@ -3830,12 +3830,21 @@ _PyImport_LoadLazyImportTstate(PyThreadState *tstate, PyObject *lazy_import)
38303830 return NULL ;
38313831 } else if (is_loading == 1 ) {
38323832 PyObject * name = _PyLazyImport_GetName (lazy_import );
3833- PyObject * errmsg = PyUnicode_FromFormat ("cannot import name %R "
3834- "(most likely due to a circular import)" ,
3835- name );
3833+ if (name == NULL ) {
3834+ _PyImport_ReleaseLock (interp );
3835+ return NULL ;
3836+ }
3837+ PyObject * errmsg = PyUnicode_FromFormat (
3838+ "cannot import name %R (most likely due to a circular import)" ,
3839+ name );
3840+ if (errmsg == NULL ) {
3841+ Py_DECREF (name );
3842+ _PyImport_ReleaseLock (interp );
3843+ return NULL ;
3844+ }
38363845 PyErr_SetImportErrorSubclass (PyExc_ImportCycleError , errmsg , lz -> lz_from , NULL );
3837- Py_XDECREF (errmsg );
3838- Py_XDECREF (name );
3846+ Py_DECREF (errmsg );
3847+ Py_DECREF (name );
38393848 _PyImport_ReleaseLock (interp );
38403849 return NULL ;
38413850 } else if (PySet_Add (importing , lazy_import ) < 0 ) {
@@ -3918,8 +3927,7 @@ _PyImport_LoadLazyImportTstate(PyThreadState *tstate, PyObject *lazy_import)
39183927 goto ok ;
39193928
39203929error :
3921- Py_XDECREF (obj );
3922- obj = NULL ;
3930+ Py_CLEAR (obj );
39233931
39243932 /* If an error occurred and we have frame information, add it to the exception */
39253933 if (PyErr_Occurred () && lz -> lz_code != NULL && lz -> lz_instr_offset >= 0 ) {
@@ -3992,8 +4000,7 @@ _PyImport_LoadLazyImportTstate(PyThreadState *tstate, PyObject *lazy_import)
39924000
39934001ok :
39944002 if (PySet_Discard (importing , lazy_import ) < 0 ) {
3995- Py_DECREF (obj );
3996- obj = NULL ;
4003+ Py_CLEAR (obj );
39974004 }
39984005
39994006 // Release the global import lock
0 commit comments