From: Bartosz Sławecki Date: Mon, 2 Mar 2026 11:01:32 +0000 (+0100) Subject: gh-144851: Fix `__lazy_import__` crash with user-defined filters (#144852) X-Git-Tag: v3.15.0a7~110 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=8bcb3eaa67959ec32bc4d683a79a0a67885925db;p=thirdparty%2FPython%2Fcpython.git gh-144851: Fix `__lazy_import__` crash with user-defined filters (#144852) --- diff --git a/Lib/test/test_lazy_import/__init__.py b/Lib/test/test_lazy_import/__init__.py index df19af05246d..5d30ec229978 100644 --- a/Lib/test/test_lazy_import/__init__.py +++ b/Lib/test/test_lazy_import/__init__.py @@ -391,6 +391,11 @@ class DunderLazyImportTests(unittest.TestCase): 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 4c234a4a7043..dfc4d5707bfd 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);