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

This commit is contained in:
Victor Stinner 2026-02-21 22:06:59 +01:00 committed by GitHub
parent fbd3b25e00
commit 2be2dd5fc2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8 additions and 0 deletions

View file

@ -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__."""

View 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;
}