Skip to content

Commit a777aca

Browse files
committed
Fix not raising on non-module import
1 parent 30ac23e commit a777aca

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
@@ -678,17 +678,31 @@ def test_lazy_modules_tracks_lazy_imports(self):
678678
class ErrorHandlingTests(LazyImportTestCase):
679679
"""Tests for error handling during lazy import reification."""
680680

681-
def test_missing_lazy_submodule_raises_attribute_error(self):
681+
def test_missing_lazy_submodule_raises_module_not_found_error(self):
682682
"""Accessing a nonexistent lazy submodule via parent attr raises AttributeError."""
683683
code = textwrap.dedent("""
684684
lazy import test.test_lazy_import.data.nonexistent_module
685685
686686
try:
687687
_ = test.test_lazy_import.data.nonexistent_module
688-
except AttributeError:
688+
except ModuleNotFoundError:
689689
pass
690690
else:
691-
raise AssertionError("AttributeError was not raised")
691+
raise AssertionError("ModuleNotFoundError was not raised")
692+
""")
693+
assert_python_ok("-c", code)
694+
695+
def test_non_package_lazily_imported(self):
696+
"""Accessing a nonexistent lazy submodule via parent attr raises AttributeError."""
697+
code = textwrap.dedent("""
698+
lazy import math.pi
699+
700+
try:
701+
_ = math.pi
702+
except ModuleNotFoundError:
703+
pass
704+
else:
705+
raise AssertionError("ModuleNotFoundError was not raised")
692706
""")
693707
assert_python_ok("-c", code)
694708

Python/import.c

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3940,19 +3940,6 @@ _PyImport_LoadLazyImportTstate(PyThreadState *tstate, PyObject *lazy_import)
39403940
goto error;
39413941
}
39423942

3943-
Py_ssize_t dot = -1;
3944-
int full = 0;
3945-
if (lz->lz_attr != NULL) {
3946-
full = 1;
3947-
}
3948-
if (!full) {
3949-
dot = PyUnicode_FindChar(lz->lz_from, '.', 0,
3950-
PyUnicode_GET_LENGTH(lz->lz_from), 1);
3951-
}
3952-
if (dot < 0) {
3953-
full = 1;
3954-
}
3955-
39563943
if (lz->lz_attr != NULL) {
39573944
if (PyUnicode_Check(lz->lz_attr)) {
39583945
fromlist = PyTuple_New(1);
@@ -3978,23 +3965,10 @@ _PyImport_LoadLazyImportTstate(PyThreadState *tstate, PyObject *lazy_import)
39783965
PyErr_SetString(PyExc_ImportError, "__import__ not found");
39793966
goto error;
39803967
}
3981-
if (full) {
3982-
obj = _PyEval_ImportNameWithImport(
3983-
tstate, import_func, globals, globals,
3984-
lz->lz_from, fromlist, _PyLong_GetZero()
3985-
);
3986-
}
3987-
else {
3988-
PyObject *name = PyUnicode_Substring(lz->lz_from, 0, dot);
3989-
if (name == NULL) {
3990-
goto error;
3991-
}
3992-
obj = _PyEval_ImportNameWithImport(
3993-
tstate, import_func, globals, globals,
3994-
name, fromlist, _PyLong_GetZero()
3995-
);
3996-
Py_DECREF(name);
3997-
}
3968+
obj = _PyEval_ImportNameWithImport(
3969+
tstate, import_func, globals, globals,
3970+
lz->lz_from, fromlist, _PyLong_GetZero()
3971+
);
39983972
if (obj == NULL) {
39993973
goto error;
40003974
}

0 commit comments

Comments
 (0)