summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_importlib
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2022-10-06 22:40:22 (GMT)
committerGitHub <noreply@github.com>2022-10-06 22:40:22 (GMT)
commite1c4d56fdde28728c37de855edbb463fa0d7f95d (patch)
treeb4c3a715e975f6349a8b07d856965fffb911f26d /Lib/test/test_importlib
parentf8edc6ff531bb98858185857513371f14519ed1d (diff)
downloadcpython-e1c4d56fdde28728c37de855edbb463fa0d7f95d.zip
cpython-e1c4d56fdde28728c37de855edbb463fa0d7f95d.tar.gz
cpython-e1c4d56fdde28728c37de855edbb463fa0d7f95d.tar.bz2
gh-65961: Do not rely solely on `__cached__` (GH-97990)
Make sure `__spec__.cached` (at minimum) can be used.
Diffstat (limited to 'Lib/test/test_importlib')
-rw-r--r--Lib/test/test_importlib/import_/test_helpers.py71
1 files changed, 71 insertions, 0 deletions
diff --git a/Lib/test/test_importlib/import_/test_helpers.py b/Lib/test/test_importlib/import_/test_helpers.py
new file mode 100644
index 0000000..90df56f
--- /dev/null
+++ b/Lib/test/test_importlib/import_/test_helpers.py
@@ -0,0 +1,71 @@
+"""Tests for helper functions used by import.c ."""
+
+from importlib import _bootstrap_external, machinery
+import os.path
+import unittest
+
+from .. import util
+
+
+class FixUpModuleTests:
+
+ def test_no_loader_but_spec(self):
+ loader = object()
+ name = "hello"
+ path = "hello.py"
+ spec = machinery.ModuleSpec(name, loader)
+ ns = {"__spec__": spec}
+ _bootstrap_external._fix_up_module(ns, name, path)
+
+ expected = {"__spec__": spec, "__loader__": loader, "__file__": path,
+ "__cached__": None}
+ self.assertEqual(ns, expected)
+
+ def test_no_loader_no_spec_but_sourceless(self):
+ name = "hello"
+ path = "hello.py"
+ ns = {}
+ _bootstrap_external._fix_up_module(ns, name, path, path)
+
+ expected = {"__file__": path, "__cached__": path}
+
+ for key, val in expected.items():
+ with self.subTest(f"{key}: {val}"):
+ self.assertEqual(ns[key], val)
+
+ spec = ns["__spec__"]
+ self.assertIsInstance(spec, machinery.ModuleSpec)
+ self.assertEqual(spec.name, name)
+ self.assertEqual(spec.origin, os.path.abspath(path))
+ self.assertEqual(spec.cached, os.path.abspath(path))
+ self.assertIsInstance(spec.loader, machinery.SourcelessFileLoader)
+ self.assertEqual(spec.loader.name, name)
+ self.assertEqual(spec.loader.path, path)
+ self.assertEqual(spec.loader, ns["__loader__"])
+
+ def test_no_loader_no_spec_but_source(self):
+ name = "hello"
+ path = "hello.py"
+ ns = {}
+ _bootstrap_external._fix_up_module(ns, name, path)
+
+ expected = {"__file__": path, "__cached__": None}
+
+ for key, val in expected.items():
+ with self.subTest(f"{key}: {val}"):
+ self.assertEqual(ns[key], val)
+
+ spec = ns["__spec__"]
+ self.assertIsInstance(spec, machinery.ModuleSpec)
+ self.assertEqual(spec.name, name)
+ self.assertEqual(spec.origin, os.path.abspath(path))
+ self.assertIsInstance(spec.loader, machinery.SourceFileLoader)
+ self.assertEqual(spec.loader.name, name)
+ self.assertEqual(spec.loader.path, path)
+ self.assertEqual(spec.loader, ns["__loader__"])
+
+
+FrozenFixUpModuleTests, SourceFixUpModuleTests = util.test_both(FixUpModuleTests)
+
+if __name__ == "__main__":
+ unittest.main()