Skip to content

Commit 9a3dbbf

Browse files
committed
add test for missing __file__
1 parent 7c30a5d commit 9a3dbbf

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

Lib/test/test_import/__init__.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -816,22 +816,33 @@ def test_frozen_module_from_import_error(self):
816816
f"cannot import name 'this_will_never_exist' from 'os' \\({re.escape(os.__file__)}\\)"
817817
)
818818

819-
with os_helper.temp_dir() as tmp:
820-
with open(os.path.join(tmp, "main.py"), "w", encoding='utf-8') as f:
821-
f.write("""
819+
820+
scripts = [
821+
"""
822822
import os
823+
os.__spec__.has_location = False
823824
os.__file__ = 123
825+
from os import this_will_never_exist
826+
""",
827+
"""
828+
import os
824829
os.__spec__.has_location = False
830+
del os.__file__
825831
from os import this_will_never_exist
826-
""")
832+
"""
833+
]
834+
for script in scripts:
835+
with self.subTest(script=script), os_helper.temp_dir() as tmp:
836+
with open(os.path.join(tmp, "main.py"), "w", encoding='utf-8') as f:
837+
f.write(script)
827838

828-
expected_error = (
829-
b"cannot import name 'this_will_never_exist' "
830-
b"from 'os' \\(unknown location\\)"
831-
)
832-
popen = script_helper.spawn_python(os.path.join(tmp, "main.py"), cwd=tmp)
833-
stdout, stderr = popen.communicate()
834-
self.assertRegex(stdout, expected_error)
839+
expected_error = (
840+
b"cannot import name 'this_will_never_exist' "
841+
b"from 'os' \\(unknown location\\)"
842+
)
843+
popen = script_helper.spawn_python(os.path.join(tmp, "main.py"), cwd=tmp)
844+
stdout, stderr = popen.communicate()
845+
self.assertRegex(stdout, expected_error)
835846

836847
def test_script_shadowing_stdlib(self):
837848
script_errors = [

0 commit comments

Comments
 (0)