Skip to content

Commit 2be2dd5

Browse files
authored
gh-145076: Check globals type in __lazy_import__() (#145086)
1 parent fbd3b25 commit 2be2dd5

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

Lib/test/test_import/test_lazy_imports.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,8 @@ def test_dunder_lazy_import_invalid_arguments(self):
401401

402402
with self.assertRaises(ValueError):
403403
__lazy_import__("sys", level=-1)
404+
with self.assertRaises(TypeError):
405+
__lazy_import__("sys", globals=1)
404406

405407
def test_dunder_lazy_import_builtins(self):
406408
"""__lazy_import__ should use module's __builtins__ for __import__."""

Python/bltinmodule.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,12 @@ builtin___lazy_import___impl(PyObject *module, PyObject *name,
318318
locals = globals;
319319
}
320320

321+
if (!PyDict_Check(globals)) {
322+
PyErr_Format(PyExc_TypeError,
323+
"expect dict for globals, got %T", globals);
324+
return NULL;
325+
}
326+
321327
if (PyDict_GetItemRef(globals, &_Py_ID(__builtins__), &builtins) < 0) {
322328
return NULL;
323329
}

0 commit comments

Comments
 (0)