@@ -2999,13 +2999,21 @@ _PyEval_ImportName(PyThreadState *tstate, PyObject *builtins, PyObject *globals,
29992999 return NULL ;
30003000 }
30013001
3002+ PyObject * res = _PyEval_ImportNameWithImport (tstate , import_func , globals , locals , name , fromlist , level );
3003+ Py_DECREF (import_func );
3004+ return res ;
3005+ }
3006+
3007+ PyObject *
3008+ _PyEval_ImportNameWithImport (PyThreadState * tstate , PyObject * import_func , PyObject * globals , PyObject * locals ,
3009+ PyObject * name , PyObject * fromlist , PyObject * level )
3010+ {
30023011 if (locals == NULL ) {
30033012 locals = Py_None ;
30043013 }
30053014
30063015 /* Fast path for not overloaded __import__. */
30073016 if (_PyImport_IsDefaultImportFunc (tstate -> interp , import_func )) {
3008- Py_DECREF (import_func );
30093017 int ilevel = PyLong_AsInt (level );
30103018 if (ilevel == -1 && _PyErr_Occurred (tstate )) {
30113019 return NULL ;
@@ -3020,7 +3028,6 @@ _PyEval_ImportName(PyThreadState *tstate, PyObject *builtins, PyObject *globals,
30203028
30213029 PyObject * args [5 ] = {name , globals , locals , fromlist , level };
30223030 PyObject * res = PyObject_Vectorcall (import_func , args , 5 , NULL );
3023- Py_DECREF (import_func );
30243031 return res ;
30253032}
30263033
@@ -3029,6 +3036,7 @@ PyObject *
30293036_PyEval_LazyImportName (PyThreadState * tstate , PyObject * builtins , PyObject * globals ,
30303037 PyObject * locals , PyObject * name , PyObject * fromlist , PyObject * level , int lazy )
30313038{
3039+ PyObject * res = NULL ;
30323040 // Check if global policy overrides the local syntax
30333041 switch (PyImport_LazyImportsEnabled ()) {
30343042 case PyLazyImportsMode_ForcedOff :
@@ -3047,34 +3055,42 @@ _PyEval_LazyImportName(PyThreadState *tstate, PyObject *builtins, PyObject *glob
30473055 }
30483056
30493057 PyObject * import_func ;
3050- if (PyMapping_GetOptionalItem (builtins , & _Py_ID (__lazy_import__ ), & import_func ) < 0 ) {
3051- return NULL ;
3058+ if (PyMapping_GetOptionalItem (builtins , & _Py_ID (__import__ ), & import_func ) < 0 ) {
3059+ goto error ;
3060+ } else if (import_func == NULL ) {
3061+ _PyErr_SetString (tstate , PyExc_ImportError , "__import__ not found" );
3062+ goto error ;
30523063 }
3053- if (import_func == NULL ) {
3064+
3065+ PyObject * lazy_import_func ;
3066+ if (PyMapping_GetOptionalItem (builtins , & _Py_ID (__lazy_import__ ), & lazy_import_func ) < 0 ) {
3067+ goto error ;
3068+ } else if (lazy_import_func == NULL ) {
30543069 _PyErr_SetString (tstate , PyExc_ImportError , "__lazy_import__ not found" );
3055- return NULL ;
3070+ goto error ;
30563071 }
30573072
30583073 if (locals == NULL ) {
30593074 locals = Py_None ;
30603075 }
30613076
3062- if (_PyImport_IsDefaultLazyImportFunc (tstate -> interp , import_func )) {
3063- Py_DECREF (import_func );
3064-
3077+ if (_PyImport_IsDefaultLazyImportFunc (tstate -> interp , lazy_import_func )) {
30653078 int ilevel = PyLong_AsInt (level );
30663079 if (ilevel == -1 && PyErr_Occurred ()) {
3067- return NULL ;
3080+ goto error ;
30683081 }
30693082
3070- return _PyImport_LazyImportModuleLevelObject (
3071- tstate , name , builtins , globals , locals , fromlist , ilevel
3083+ res = _PyImport_LazyImportModuleLevelObject (
3084+ tstate , name , import_func , globals , locals , fromlist , ilevel
30723085 );
3086+ goto error ;
30733087 }
30743088
3075- PyObject * args [5 ] = {name , globals , locals , fromlist , level };
3076- PyObject * res = PyObject_Vectorcall (import_func , args , 5 , NULL );
3077- Py_DECREF (import_func );
3089+ PyObject * args [6 ] = {name , globals , locals , fromlist , level , import_func };
3090+ res = PyObject_Vectorcall (lazy_import_func , args , 6 , NULL );
3091+ error :
3092+ Py_XDECREF (lazy_import_func );
3093+ Py_XDECREF (import_func );
30783094 return res ;
30793095}
30803096
@@ -3256,20 +3272,20 @@ _PyEval_LazyImportFrom(PyThreadState *tstate, PyObject *v, PyObject *name)
32563272 if (d -> lz_attr != NULL ) {
32573273 if (PyUnicode_Check (d -> lz_attr )) {
32583274 PyObject * from = PyUnicode_FromFormat ("%U.%U" , d -> lz_from , d -> lz_attr );
3259- ret = _PyLazyImport_New (d -> lz_builtins , from , name );
3275+ ret = _PyLazyImport_New (d -> lz_import_func , from , name );
32603276 Py_DECREF (from );
32613277 return ret ;
32623278 }
32633279 } else {
32643280 Py_ssize_t dot = PyUnicode_FindChar (d -> lz_from , '.' , 0 , PyUnicode_GET_LENGTH (d -> lz_from ), 1 );
32653281 if (dot >= 0 ) {
32663282 PyObject * from = PyUnicode_Substring (d -> lz_from , 0 , dot );
3267- ret = _PyLazyImport_New (d -> lz_builtins , from , name );
3283+ ret = _PyLazyImport_New (d -> lz_import_func , from , name );
32683284 Py_DECREF (from );
32693285 return ret ;
32703286 }
32713287 }
3272- ret = _PyLazyImport_New (d -> lz_builtins , d -> lz_from , name );
3288+ ret = _PyLazyImport_New (d -> lz_import_func , d -> lz_from , name );
32733289 return ret ;
32743290}
32753291
0 commit comments