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 missing __file__
  • Loading branch information
hauntsaninja committed Dec 9, 2024
commit 9a3dbbfadc188ce4213e07a32f1848dc1a4e69de
33 changes: 22 additions & 11 deletions Lib/test/test_import/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -816,22 +816,33 @@ 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("""

scripts = [
"""
import os
os.__spec__.has_location = False
os.__file__ = 123
from os import this_will_never_exist
""",
"""
import os
os.__spec__.has_location = False
del os.__file__
from os import this_will_never_exist
""")
"""
]
for script in scripts:
with self.subTest(script=script), os_helper.temp_dir() as tmp:
with open(os.path.join(tmp, "main.py"), "w", encoding='utf-8') as f:
f.write(script)

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)
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