https://github.com/python/cpython/commit/2be2dd5fc219a5c252d72f351c85db14314bfca5
commit: 2be2dd5fc219a5c252d72f351c85db14314bfca5
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2026-02-21T22:06:59+01:00
summary:

gh-145076: Check globals type in __lazy_import__() (#145086)

files:
M Lib/test/test_import/test_lazy_imports.py
M Python/bltinmodule.c

diff --git a/Lib/test/test_import/test_lazy_imports.py 
b/Lib/test/test_import/test_lazy_imports.py
index dc185c070acc62..39d37f68e0b47b 100644
--- a/Lib/test/test_import/test_lazy_imports.py
+++ b/Lib/test/test_import/test_lazy_imports.py
@@ -401,6 +401,8 @@ def test_dunder_lazy_import_invalid_arguments(self):
 
         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__."""
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 493a6e0413d8eb..301125051f3b0e 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -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;
     }

_______________________________________________
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