diff --git a/Lib/test/test_lazy_import/__init__.py b/Lib/test/test_lazy_import/__init__.py index f1169a2aa206bf..31006f804fd6d5 100644 --- a/Lib/test/test_lazy_import/__init__.py +++ b/Lib/test/test_lazy_import/__init__.py @@ -308,6 +308,19 @@ def test_lazy_import_type_attribute_error_message(self): """) assert_python_ok("-c", code) + @support.run_with_limited_c_stack(depth=10_000, size=256 * 1024) + def test_deeply_dotted_name(self): + """A long dotted name must not overflow the C stack.""" + m = types.SimpleNamespace() + # Every lookup in m.m.m... succeeds, so resolve() only fails if the + # C stack runs out. + m.m = m + ns = {"__builtins__": {"__lazy_import__": __lazy_import__, + "__import__": lambda *args: m}} + exec("lazy import " + ".".join(["m"] * 10_000) + " as x", ns) + with contextlib.suppress(RecursionError): + ns["x"].resolve() + class SyntaxRestrictionTests(LazyImportTestCase): """Tests for syntax restrictions on lazy imports.""" diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-13-04-03-48.gh-issue-159288.vhqBQG.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-13-04-03-48.gh-issue-159288.vhqBQG.rst new file mode 100644 index 00000000000000..2d969e8cbac8c4 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-13-04-03-48.gh-issue-159288.vhqBQG.rst @@ -0,0 +1,3 @@ +Fix a crash when walking a very long chain of lazy import placeholders, as +created by an aliased dotted import such as ``lazy import a.b.c as d``. +:exc:`RecursionError` is now raised instead. diff --git a/Objects/lazyimportobject.c b/Objects/lazyimportobject.c index bb35ddcdae1755..aafd2ce764a1cb 100644 --- a/Objects/lazyimportobject.c +++ b/Objects/lazyimportobject.c @@ -204,7 +204,11 @@ static PyObject * lazy_import_path(PyLazyImportObject *m) { if (PyLazyImport_CheckExact(m->lz_from)) { + if (_Py_EnterRecursiveCall(" while getting the name of a lazy import")) { + return NULL; + } PyObject *base = lazy_import_path((PyLazyImportObject *)m->lz_from); + _Py_LeaveRecursiveCall(); if (base == NULL) { return NULL; } @@ -277,8 +281,12 @@ lazy_import_replay_from(PyThreadState *tstate, PyObject *mod, if (!PyLazyImport_CheckExact(lz->lz_from)) { return Py_NewRef(mod); } + if (_Py_EnterRecursiveCallTstate(tstate, " while resolving a lazy import")) { + return NULL; + } PyObject *from = lazy_import_replay_from( tstate, mod, (PyLazyImportObject *)lz->lz_from); + _Py_LeaveRecursiveCallTstate(tstate); if (from == NULL) { return NULL; }