https://github.com/python/cpython/commit/8bcb3eaa67959ec32bc4d683a79a0a67885925db
commit: 8bcb3eaa67959ec32bc4d683a79a0a67885925db
branch: main
author: Bartosz SÅ‚awecki <[email protected]>
committer: Yhg1s <[email protected]>
date: 2026-03-02T12:01:32+01:00
summary:

gh-144851: Fix `__lazy_import__` crash with user-defined filters (#144852)

files:
M Lib/test/test_lazy_import/__init__.py
M Python/import.c

diff --git a/Lib/test/test_lazy_import/__init__.py 
b/Lib/test/test_lazy_import/__init__.py
index df19af05246dcd..5d30ec2299789b 100644
--- a/Lib/test/test_lazy_import/__init__.py
+++ b/Lib/test/test_lazy_import/__init__.py
@@ -391,6 +391,11 @@ def test_dunder_lazy_import(self):
         import test.test_lazy_import.data.dunder_lazy_import
         self.assertNotIn("test.test_lazy_import.data.basic2", sys.modules)
 
+    def test_dunder_lazy_import_with_custom_filter(self):
+        sys.set_lazy_imports_filter(lambda importer, imported, fromlist: False)
+        import test.test_lazy_import.data.dunder_lazy_import
+        self.assertIn("test.test_lazy_import.data.basic2", sys.modules)
+
     def test_dunder_lazy_import_used(self):
         """Using __lazy_import__ result should trigger module load."""
         import test.test_lazy_import.data.dunder_lazy_import_used
diff --git a/Python/import.c b/Python/import.c
index 4c234a4a70437c..dfc4d5707bfdba 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -4512,6 +4512,10 @@ _PyImport_LazyImportModuleLevelObject(PyThreadState 
*tstate,
             assert(!PyErr_Occurred());
             modname = Py_NewRef(Py_None);
         }
+        if (fromlist == NULL) {
+            assert(!PyErr_Occurred());
+            fromlist = Py_NewRef(Py_None);
+        }
         PyObject *args[] = {modname, name, fromlist};
         PyObject *res = PyObject_Vectorcall(filter, args, 3, NULL);
 

_______________________________________________
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