]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-145076: Check globals type in __lazy_import__() (#145086)
authorVictor Stinner <vstinner@python.org>
Sat, 21 Feb 2026 21:06:59 +0000 (22:06 +0100)
committerGitHub <noreply@github.com>
Sat, 21 Feb 2026 21:06:59 +0000 (22:06 +0100)
Lib/test/test_import/test_lazy_imports.py
Python/bltinmodule.c

index dc185c070acc624413fbd4489b21ec5ffb2c406d..39d37f68e0b47baf9460db1940fbc2b459df94bb 100644 (file)
@@ -401,6 +401,8 @@ class DunderLazyImportTests(unittest.TestCase):
 
         with self.assertRaises(ValueError):
             __lazy_import__("sys", level=-1)
+        with self.assertRaises(TypeError):
+            __lazy_import__("sys", globals=1)
 
     def test_dunder_lazy_import_builtins(self):
         """__lazy_import__ should use module's __builtins__ for __import__."""
index 493a6e0413d8eb2997a91499dc418b9c74062f63..301125051f3b0e8b4115857e97d6de2f06b3f602 100644 (file)
@@ -318,6 +318,12 @@ builtin___lazy_import___impl(PyObject *module, PyObject *name,
         locals = globals;
     }
 
+    if (!PyDict_Check(globals)) {
+        PyErr_Format(PyExc_TypeError,
+                     "expect dict for globals, got %T", globals);
+        return NULL;
+    }
+
     if (PyDict_GetItemRef(globals, &_Py_ID(__builtins__), &builtins) < 0) {
         return NULL;
     }