https://github.com/python/cpython/commit/5fe139cc39fb8110b3d6cbed6224d7c8f5d91987
commit: 5fe139cc39fb8110b3d6cbed6224d7c8f5d91987
branch: main
author: Pablo Galindo Salgado <[email protected]>
committer: pablogsal <[email protected]>
date: 2026-02-15T18:36:47Z
summary:

gh-144727: Add test for circular lazy import crash (#144727) (#144838)

Add a regression test ensuring that circular lazy imports raise a proper
error (ImportCycleError) instead of crashing with a segfault. The crash
was fixed in gh-144733 but no test was added at the time.

files:
M Lib/test/test_import/test_lazy_imports.py

diff --git a/Lib/test/test_import/test_lazy_imports.py 
b/Lib/test/test_import/test_lazy_imports.py
index 1193af9589034b..a4c9c14ae2b5f8 100644
--- a/Lib/test/test_import/test_lazy_imports.py
+++ b/Lib/test/test_import/test_lazy_imports.py
@@ -8,6 +8,8 @@
 import threading
 import types
 import unittest
+import tempfile
+import os
 
 try:
     import _testcapi
@@ -598,6 +600,39 @@ def test_error_during_module_execution_propagates(self):
         self.assertEqual(result.returncode, 0, f"stdout: {result.stdout}, 
stderr: {result.stderr}")
         self.assertIn("OK", result.stdout)
 
+    def test_circular_lazy_import_does_not_crash_for_gh_144727(self):
+        with tempfile.TemporaryDirectory() as tmpdir:
+            a_path = os.path.join(tmpdir, "a.py")
+            b_path = os.path.join(tmpdir, "b.py")
+
+            with open(a_path, "w") as f:
+                f.write(textwrap.dedent("""\
+                    lazy import b
+
+                    def something():
+                        b.hello()
+
+                    something()
+                """))
+
+            with open(b_path, "w") as f:
+                f.write(textwrap.dedent("""\
+                    lazy import a
+
+                    def hello():
+                        print(a)
+                """))
+
+            result = subprocess.run(
+                [sys.executable, a_path],
+                capture_output=True,
+                text=True,
+                cwd=tmpdir,
+            )
+            # Should get a proper Python error, not a crash
+            self.assertEqual(result.returncode, 1)
+            self.assertIn("Error", result.stderr)
+
 
 class GlobalsAndDictTests(unittest.TestCase):
     """Tests for globals() and __dict__ behavior with lazy imports.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to