summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorShantanu <12621235+hauntsaninja@users.noreply.github.com>2024-12-10 04:55:20 (GMT)
committerGitHub <noreply@github.com>2024-12-10 04:55:20 (GMT)
commit3983527c3a6b389e373a233e514919555853ccb3 (patch)
tree7a9bbb49fc4b6e8b6996f25635db330978bde525 /Lib/test
parent58c753827ac7aa3d7f1495ac206c28bf2f6c67e8 (diff)
downloadcpython-3983527c3a6b389e373a233e514919555853ccb3.zip
cpython-3983527c3a6b389e373a233e514919555853ccb3.tar.gz
cpython-3983527c3a6b389e373a233e514919555853ccb3.tar.bz2
gh-127651: Use __file__ in diagnostics if origin is missing (#127660)
See the left hand side in https://github.com/python/cpython/pull/123929/files#diff-c22186367cbe20233e843261998dc027ae5f1f8c0d2e778abfa454ae74cc59deL2840-L2849 --------- Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_import/__init__.py48
1 files changed, 46 insertions, 2 deletions
diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py
index c52b7f3..83efbc1 100644
--- a/Lib/test/test_import/__init__.py
+++ b/Lib/test/test_import/__init__.py
@@ -807,6 +807,50 @@ class ImportTests(unittest.TestCase):
self.assertIn("Frozen object named 'x' is invalid",
str(cm.exception))
+ def test_frozen_module_from_import_error(self):
+ with self.assertRaises(ImportError) as cm:
+ from os import this_will_never_exist
+ self.assertIn(
+ f"cannot import name 'this_will_never_exist' from 'os' ({os.__file__})",
+ str(cm.exception),
+ )
+ with self.assertRaises(ImportError) as cm:
+ from sys import this_will_never_exist
+ self.assertIn(
+ "cannot import name 'this_will_never_exist' from 'sys' (unknown location)",
+ str(cm.exception),
+ )
+
+ scripts = [
+ """
+import os
+os.__spec__.has_location = False
+os.__file__ = []
+from os import this_will_never_exist
+""",
+ """
+import os
+os.__spec__.has_location = False
+del os.__file__
+from os import this_will_never_exist
+""",
+ """
+import os
+os.__spec__.origin = []
+os.__file__ = []
+from os import this_will_never_exist
+"""
+ ]
+ for script in scripts:
+ with self.subTest(script=script):
+ expected_error = (
+ b"cannot import name 'this_will_never_exist' "
+ b"from 'os' (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 = [
(
@@ -1068,7 +1112,7 @@ try:
except AttributeError as e:
print(str(e))
-fractions.__spec__.origin = 0
+fractions.__spec__.origin = []
try:
fractions.Fraction
except AttributeError as e:
@@ -1092,7 +1136,7 @@ try:
except ImportError as e:
print(str(e))
-fractions.__spec__.origin = 0
+fractions.__spec__.origin = []
try:
from fractions import Fraction
except ImportError as e: