Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Lib/test/test_lazy_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,32 @@ def hello():
self.assertEqual(result.returncode, 1)
self.assertIn("Error", result.stderr)

def test_raising_spec_initializing(self):
"""Test a raising spec._initializing"""
code = textwrap.dedent("""
import sys
import types

class Spec:
@property
def _initializing(self):
raise RuntimeError("boom")

module = types.ModuleType("cached_spec")
module.__spec__ = Spec()
module.attr = 1
sys.modules["cached_spec"] = module
lazy from cached_spec import attr # this ignores the error
try:
attr # this runs into the error again
except RuntimeError as e:
print('error:', e)
else:
raise AssertionError("exception not raised")
""")
proc = assert_python_ok("-c", code)
self.assertIn(b"error: boom", proc.out)


@support.requires_subprocess()
class GlobalsAndDictTests(LazyImportTestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
During a lazy import, if getting an existing module fails with an exception,
ignore the exception. (Later reification will likely trigger it again.)
5 changes: 5 additions & 0 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -3358,6 +3358,11 @@ _PyEval_LazyImportFrom(PyThreadState *tstate, _PyInterpreterFrame *frame, PyObje
}
Py_DECREF(mod);
}
else if (PyErr_Occurred()) {
// Getting the module failed. Ignore that for now; future
// reification will likely fail again.
PyErr_Clear();
}

return _PyLazyImport_New(frame, d->lz_builtins, v, name);
}
Expand Down
Loading