-
-
Notifications
You must be signed in to change notification settings - Fork 35.2k
[3.15] gh-155194: Fix not raising on non-module import #155188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -686,17 +686,45 @@ def test_lazy_modules_tracks_lazy_imports(self): | |
| class ErrorHandlingTests(LazyImportTestCase): | ||
| """Tests for error handling during lazy import reification.""" | ||
|
|
||
| def test_missing_lazy_submodule_raises_attribute_error(self): | ||
| def test_missing_lazy_submodule_raises_module_not_found_error(self): | ||
| """Accessing a nonexistent lazy submodule via parent attr raises AttributeError.""" | ||
|
DinoV marked this conversation as resolved.
|
||
| code = textwrap.dedent(""" | ||
| lazy import test.test_lazy_import.data.nonexistent_module | ||
|
|
||
| try: | ||
| _ = test.test_lazy_import.data.nonexistent_module | ||
| except AttributeError: | ||
| except ModuleNotFoundError: | ||
| pass | ||
| else: | ||
| raise AssertionError("AttributeError was not raised") | ||
| raise AssertionError("ModuleNotFoundError was not raised") | ||
| """) | ||
| assert_python_ok("-c", code) | ||
|
|
||
| def test_non_package_lazily_imported(self): | ||
| """Accessing a nonexistent lazy submodule via parent attr raises ModuleNotFoundError.""" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pablogsal I've fixed up these in the main PR (#155189), I'm going to close this one per the discussion above. |
||
| code = textwrap.dedent(""" | ||
| lazy import math.pi | ||
|
|
||
| try: | ||
| _ = math.pi | ||
| except ModuleNotFoundError: | ||
| pass | ||
| else: | ||
| raise AssertionError("ModuleNotFoundError was not raised") | ||
| """) | ||
| assert_python_ok("-c", code) | ||
|
|
||
| def test_missing_attribute_raises_import_error(self): | ||
| """Accessing a nonexistent lazy submodule via from import raises ImportError.""" | ||
|
DinoV marked this conversation as resolved.
|
||
| code = textwrap.dedent(""" | ||
| lazy from sys import doesnotexist | ||
|
|
||
| try: | ||
| _ = doesnotexist | ||
| except ImportError: | ||
| pass | ||
| else: | ||
| raise AssertionError("ImportError was not raised") | ||
| """) | ||
| assert_python_ok("-c", code) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| lazy from . import bar |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import traceback | ||
| traceback.print_stack() | ||
| while True: pass | ||
|
DinoV marked this conversation as resolved.
DinoV marked this conversation as resolved.
|
||
| print("BAR_MODULE_LOADED") | ||
| def f(): pass | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3937,19 +3937,6 @@ _PyImport_LoadLazyImportTstate(PyThreadState *tstate, PyObject *lazy_import) | |
| goto error; | ||
| } | ||
|
|
||
| Py_ssize_t dot = -1; | ||
| int full = 0; | ||
| if (lz->lz_attr != NULL) { | ||
| full = 1; | ||
| } | ||
| if (!full) { | ||
| dot = PyUnicode_FindChar(lz->lz_from, '.', 0, | ||
| PyUnicode_GET_LENGTH(lz->lz_from), 1); | ||
| } | ||
| if (dot < 0) { | ||
| full = 1; | ||
| } | ||
|
|
||
| if (lz->lz_attr != NULL) { | ||
| if (PyUnicode_Check(lz->lz_attr)) { | ||
| fromlist = PyTuple_New(1); | ||
|
|
@@ -3975,23 +3962,10 @@ _PyImport_LoadLazyImportTstate(PyThreadState *tstate, PyObject *lazy_import) | |
| PyErr_SetString(PyExc_ImportError, "__import__ not found"); | ||
| goto error; | ||
| } | ||
| if (full) { | ||
| obj = _PyEval_ImportNameWithImport( | ||
| tstate, import_func, globals, globals, | ||
| lz->lz_from, fromlist, _PyLong_GetZero() | ||
| ); | ||
| } | ||
| else { | ||
| PyObject *name = PyUnicode_Substring(lz->lz_from, 0, dot); | ||
| if (name == NULL) { | ||
| goto error; | ||
| } | ||
| obj = _PyEval_ImportNameWithImport( | ||
| tstate, import_func, globals, globals, | ||
| name, fromlist, _PyLong_GetZero() | ||
| ); | ||
| Py_DECREF(name); | ||
| } | ||
| obj = _PyEval_ImportNameWithImport( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, does this change the error for
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope, that'll be the same. In the |
||
| tstate, import_func, globals, globals, | ||
| lz->lz_from, fromlist, _PyLong_GetZero() | ||
| ); | ||
| if (obj == NULL) { | ||
| goto error; | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.