Skip to content

Commit 4653751

Browse files
Kronuzfacebook-github-bot
authored andcommitted
Properly handle errors in PyImport_IsLazyImportsEnabled
Summary: We were just doing nothing in case of errors. This handles the errors in `PyImport_IsLazyImportsEnabled`. This also makes the code simpler to follow and more performant because it does nothing if there is no "excluding" filter. Reviewed By: czardoz Differential Revision: D66359781 fbshipit-source-id: 203951de70bf2356b41ba232e3b8e7fdb1828f2c
1 parent c788d9e commit 4653751

File tree

1 file changed

+41
-26
lines changed

1 file changed

+41
-26
lines changed

Python/import.c

+41-26
Original file line numberDiff line numberDiff line change
@@ -4592,39 +4592,54 @@ _imp__set_lazy_imports_in_module_impl(PyObject *module, PyObject *enabled)
45924592
}
45934593

45944594
static int
4595-
is_lazy_imports_active(PyInterpreterState *interp, _PyInterpreterFrame *frame)
4596-
{
4597-
int lazy_imports = interp->lazy_imports;
4598-
if (lazy_imports) {
4599-
if (PyDict_CheckExact(frame->f_globals)) {
4600-
PyObject *lazy_imports_enabled = PyDict_GetItem(frame->f_globals, &_Py_ID(__lazy_imports_enabled__));
4601-
if (lazy_imports_enabled != NULL) {
4602-
if (PyObject_IsTrue(lazy_imports_enabled) != 1) {
4603-
lazy_imports = 0;
4604-
}
4605-
} else {
4606-
PyObject *modname = PyDict_GetItem(frame->f_globals, &_Py_ID(__name__));
4607-
if (modname != NULL && modname != Py_None) {
4608-
PyObject *filter = interp->excluding_modules;
4609-
if (filter != NULL && PySequence_Contains(filter, modname)) {
4610-
lazy_imports = 0; /* Module explicitly excluded from lazy importing */
4611-
}
4612-
PyDict_SetItem(frame->f_globals, &_Py_ID(__lazy_imports_enabled__), lazy_imports ? Py_True : Py_False);
4613-
} else {
4614-
lazy_imports = 0;
4615-
}
4616-
}
4617-
} else {
4618-
lazy_imports = 0;
4595+
is_lazy_imports_active(PyThreadState *tstate, _PyInterpreterFrame *frame)
4596+
{
4597+
int lazy_imports = tstate->interp->lazy_imports;
4598+
assert(lazy_imports != -1);
4599+
if (!lazy_imports) {
4600+
return 0;
4601+
}
4602+
4603+
PyObject *filter = tstate->interp->excluding_modules;
4604+
if (filter == NULL) {
4605+
return 1;
4606+
}
4607+
4608+
if (!PyDict_CheckExact(frame->f_globals)) {
4609+
return 0;
4610+
}
4611+
4612+
PyObject *modname = PyDict_GetItem(frame->f_globals, &_Py_ID(__name__));
4613+
if (modname == NULL || Py_IsNone(modname)) {
4614+
return 0;
4615+
}
4616+
4617+
PyObject *lazy_imports_enabled = PyDict_GetItem(frame->f_globals, &_Py_ID(__lazy_imports_enabled__));
4618+
if (lazy_imports_enabled == NULL) {
4619+
int eager = PySequence_Contains(filter, modname);
4620+
if (eager == -1) {
4621+
_PyErr_Clear(tstate);
4622+
return 0;
4623+
}
4624+
lazy_imports = eager ? 0 : 1;
4625+
if (PyDict_SetItem(frame->f_globals, &_Py_ID(__lazy_imports_enabled__), lazy_imports ? Py_True : Py_False) == -1) {
4626+
_PyErr_Clear(tstate);
4627+
}
4628+
} else {
4629+
lazy_imports = PyObject_IsTrue(lazy_imports_enabled);
4630+
if (lazy_imports == -1) {
4631+
_PyErr_Clear(tstate);
4632+
return 0;
46194633
}
46204634
}
4635+
46214636
return lazy_imports;
46224637
}
46234638

46244639
int
46254640
_PyImport_IsLazyImportsActive(PyThreadState *tstate)
46264641
{
4627-
return is_lazy_imports_active(tstate->interp, tstate->cframe->current_frame);
4642+
return is_lazy_imports_active(tstate, tstate->cframe->current_frame);
46284643
}
46294644

46304645
int
@@ -4640,7 +4655,7 @@ PyImport_IsLazyImportsEnabled(void)
46404655
assert(0);
46414656
return 0;
46424657
}
4643-
return is_lazy_imports_active(tstate->interp, frame);
4658+
return is_lazy_imports_active(tstate, frame);
46444659
}
46454660

46464661
/*[clinic input]

0 commit comments

Comments
 (0)