diff options
author | Shantanu <12621235+hauntsaninja@users.noreply.github.com> | 2024-12-20 08:22:26 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-12-20 08:22:26 (GMT) |
commit | 45e6dd63b88a782f2ec96ab1da54eb5a074d8f4c (patch) | |
tree | fad1d18683f0a9bd13d538c3626bedc0988653d3 /Lib | |
parent | daa260ebb1c1b20321e7f26df7c9dbd35d4edcbf (diff) | |
download | cpython-45e6dd63b88a782f2ec96ab1da54eb5a074d8f4c.zip cpython-45e6dd63b88a782f2ec96ab1da54eb5a074d8f4c.tar.gz cpython-45e6dd63b88a782f2ec96ab1da54eb5a074d8f4c.tar.bz2 |
gh-128030: Avoid error from PyModule_GetFilenameObject for non-module (#128047)
I missed the extra `PyModule_Check` in #127660 because I was looking at
3.12 as the base implementation for import from. This meant that I
missed the `PyModuleCheck` introduced in #112661.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_import/__init__.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py index 83efbc1..c2cec64 100644 --- a/Lib/test/test_import/__init__.py +++ b/Lib/test/test_import/__init__.py @@ -851,6 +851,29 @@ from os import this_will_never_exist stdout, stderr = popen.communicate() self.assertIn(expected_error, stdout) + def test_non_module_from_import_error(self): + prefix = """ +import sys +class NotAModule: ... +nm = NotAModule() +nm.symbol = 123 +sys.modules["not_a_module"] = nm +from not_a_module import symbol +""" + scripts = [ + prefix + "from not_a_module import missing_symbol", + prefix + "nm.__spec__ = []\nfrom not_a_module import missing_symbol", + ] + for script in scripts: + with self.subTest(script=script): + expected_error = ( + b"ImportError: cannot import name 'missing_symbol' from " + b"'<unknown module name>' (unknown location)" + ) + popen = script_helper.spawn_python("-c", script) + stdout, stderr = popen.communicate() + self.assertIn(expected_error, stdout) + def test_script_shadowing_stdlib(self): script_errors = [ ( |