Skip to content

Commit 69e82f2

Browse files
committed
Fix not raising on non-module import
1 parent eea1a49 commit 69e82f2

2 files changed

Lines changed: 21 additions & 33 deletions

File tree

Lib/test/test_lazy_import/__init__.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -686,17 +686,31 @@ def test_lazy_modules_tracks_lazy_imports(self):
686686
class ErrorHandlingTests(LazyImportTestCase):
687687
"""Tests for error handling during lazy import reification."""
688688

689-
def test_missing_lazy_submodule_raises_attribute_error(self):
689+
def test_missing_lazy_submodule_raises_module_not_found_error(self):
690690
"""Accessing a nonexistent lazy submodule via parent attr raises AttributeError."""
691691
code = textwrap.dedent("""
692692
lazy import test.test_lazy_import.data.nonexistent_module
693693
694694
try:
695695
_ = test.test_lazy_import.data.nonexistent_module
696-
except AttributeError:
696+
except ModuleNotFoundError:
697697
pass
698698
else:
699-
raise AssertionError("AttributeError was not raised")
699+
raise AssertionError("ModuleNotFoundError was not raised")
700+
""")
701+
assert_python_ok("-c", code)
702+
703+
def test_non_package_lazily_imported(self):
704+
"""Accessing a nonexistent lazy submodule via parent attr raises AttributeError."""
705+
code = textwrap.dedent("""
706+
lazy import math.pi
707+
708+
try:
709+
_ = math.pi
710+
except ModuleNotFoundError:
711+
pass
712+
else:
713+
raise AssertionError("ModuleNotFoundError was not raised")
700714
""")
701715
assert_python_ok("-c", code)
702716

Python/import.c

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3937,19 +3937,6 @@ _PyImport_LoadLazyImportTstate(PyThreadState *tstate, PyObject *lazy_import)
39373937
goto error;
39383938
}
39393939

3940-
Py_ssize_t dot = -1;
3941-
int full = 0;
3942-
if (lz->lz_attr != NULL) {
3943-
full = 1;
3944-
}
3945-
if (!full) {
3946-
dot = PyUnicode_FindChar(lz->lz_from, '.', 0,
3947-
PyUnicode_GET_LENGTH(lz->lz_from), 1);
3948-
}
3949-
if (dot < 0) {
3950-
full = 1;
3951-
}
3952-
39533940
if (lz->lz_attr != NULL) {
39543941
if (PyUnicode_Check(lz->lz_attr)) {
39553942
fromlist = PyTuple_New(1);
@@ -3975,23 +3962,10 @@ _PyImport_LoadLazyImportTstate(PyThreadState *tstate, PyObject *lazy_import)
39753962
PyErr_SetString(PyExc_ImportError, "__import__ not found");
39763963
goto error;
39773964
}
3978-
if (full) {
3979-
obj = _PyEval_ImportNameWithImport(
3980-
tstate, import_func, globals, globals,
3981-
lz->lz_from, fromlist, _PyLong_GetZero()
3982-
);
3983-
}
3984-
else {
3985-
PyObject *name = PyUnicode_Substring(lz->lz_from, 0, dot);
3986-
if (name == NULL) {
3987-
goto error;
3988-
}
3989-
obj = _PyEval_ImportNameWithImport(
3990-
tstate, import_func, globals, globals,
3991-
name, fromlist, _PyLong_GetZero()
3992-
);
3993-
Py_DECREF(name);
3994-
}
3965+
obj = _PyEval_ImportNameWithImport(
3966+
tstate, import_func, globals, globals,
3967+
lz->lz_from, fromlist, _PyLong_GetZero()
3968+
);
39953969
if (obj == NULL) {
39963970
goto error;
39973971
}

0 commit comments

Comments
 (0)