8000 gh-127651: Use __file__ in diagnostics if origin is missing by hauntsaninja · Pull Request #127660 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-127651: Use __file__ in diagnostics if origin is missing #127660

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

Merged
merged 15 commits into from
Dec 10, 2024
Merged
Prev Previous commit
Next Next commit
add test for error path
  • Loading branch information
hauntsaninja committed Dec 9, 2024
commit 7c30a5d760a167d62dbcd8b708db551f4ecadad0
17 changes: 17 additions & 0 deletions Lib/test/test_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,23 @@ def test_frozen_module_from_import_error(self):
f"cannot import name 'this_will_never_exist' from 'os' \\({re.escape(os.__file__)}\\)"
)

with os_helper.temp_dir() as tmp:
with open(os.path.join(tmp, "main.py"), "w", encoding='utf-8') as f:
f.write("""
import os
os.__file__ = 123
os.__spec__.has_location = False
from os import this_will_never_exist
""")

expected_error = (
b"cannot import name 'this_will_never_exist' "
b"from 'os' \\(unknown location\\)"
)
popen = script_helper.spawn_python(os.path.join(tmp, "main.py"), cwd=tmp)
stdout, stderr = popen.communicate()
self.assertRegex(stdout, expected_error)

def test_script_shadowing_stdlib(self):
script_errors = [
(
Expand Down
Loading
0