FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-158402: Clear error if PyImport_GetModule raises in lazy import by encukou · Pull Request #158403 · python/cpython · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
26 changes: 26 additions & 0 deletions Lib/test/test_lazy_import/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL