]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] GH-115979: update test_importlib to work under WASI SDK 21 (GH-116754) (GH...
authorBrett Cannon <brett@python.org>
Wed, 13 Mar 2024 21:55:29 +0000 (14:55 -0700)
committerGitHub <noreply@github.com>
Wed, 13 Mar 2024 21:55:29 +0000 (21:55 +0000)
(cherry picked from commit 61733a2fb9dc36d2246d922146a3462a2248832d)

Lib/test/test_importlib/extension/test_case_sensitivity.py
Lib/test/test_importlib/extension/test_finder.py
Lib/test/test_importlib/extension/test_loader.py
Lib/test/test_importlib/extension/test_path_hook.py
Lib/test/test_importlib/test_spec.py
Lib/test/test_importlib/test_util.py
Lib/test/test_importlib/util.py
Misc/NEWS.d/next/Tests/2024-03-13-12-06-49.gh-issue-115979.zsNpQD.rst [new file with mode: 0644]

index 366e565cf4b7aa46faaab9a7fd3028048a689b82..40311627a144e821b9076bb8dec5b439c1b72ef3 100644 (file)
@@ -8,7 +8,8 @@ importlib = util.import_importlib('importlib')
 machinery = util.import_importlib('importlib.machinery')
 
 
-@unittest.skipIf(util.EXTENSIONS.filename is None, '_testcapi not available')
+@unittest.skipIf(util.EXTENSIONS is None or util.EXTENSIONS.filename is None,
+                 'dynamic loading not supported or test module not available')
 @util.case_insensitive_tests
 class ExtensionModuleCaseSensitivityTest(util.CASEOKTestBase):
 
index 1d5b6e7a5de94b7f95c3df2133c238018b541eea..3de120958fd27db67aeee8f0ebea48a3bca60846 100644 (file)
@@ -11,7 +11,7 @@ class FinderTests(abc.FinderTests):
     """Test the finder for extension modules."""
 
     def setUp(self):
-        if not self.machinery.EXTENSION_SUFFIXES:
+        if not self.machinery.EXTENSION_SUFFIXES or not util.EXTENSIONS:
             raise unittest.SkipTest("Requires dynamic loading support.")
         if util.EXTENSIONS.name in sys.builtin_module_names:
             raise unittest.SkipTest(
index 270c565fd5a6249c696149d588df8a60c28b0384..2519149c0710c0b1f4ee685e6fbabedc85b4b4e3 100644 (file)
@@ -19,7 +19,7 @@ class LoaderTests(abc.LoaderTests):
     """Test load_module() for extension modules."""
 
     def setUp(self):
-        if not self.machinery.EXTENSION_SUFFIXES:
+        if not self.machinery.EXTENSION_SUFFIXES or not util.EXTENSIONS:
             raise unittest.SkipTest("Requires dynamic loading support.")
         if util.EXTENSIONS.name in sys.builtin_module_names:
             raise unittest.SkipTest(
@@ -99,7 +99,7 @@ class MultiPhaseExtensionModuleTests(abc.LoaderTests):
     # Test loading extension modules with multi-phase initialization (PEP 489).
 
     def setUp(self):
-        if not self.machinery.EXTENSION_SUFFIXES:
+        if not self.machinery.EXTENSION_SUFFIXES or not util.EXTENSIONS:
             raise unittest.SkipTest("Requires dynamic loading support.")
         self.name = '_testmultiphase'
         if self.name in sys.builtin_module_names:
index a0adc70ad1ec4d3c564b0aafe9f9883d9180dcfb..2a8b2ccb01e44e5220d7974fe6ba99a557086126 100644 (file)
@@ -5,6 +5,8 @@ machinery = util.import_importlib('importlib.machinery')
 import unittest
 
 
+@unittest.skipIf(util.EXTENSIONS is None or util.EXTENSIONS.filename is None,
+                 'dynamic loading not supported or test module not available')
 class PathHookTests:
 
     """Test the path hook for extension modules."""
index 21e2c02094f22e39cf9e7a90abec755f232e0749..06a97ec8fab191225cf9210e19b77c0381d56feb 100644 (file)
@@ -645,7 +645,8 @@ class FactoryTests:
         self.assertEqual(spec.loader, self.fileloader)
         self.assertEqual(spec.origin, self.path)
         self.assertIs(spec.loader_state, None)
-        self.assertEqual(spec.submodule_search_locations, [os.getcwd()])
+        location = cwd if (cwd := os.getcwd()) != '/' else ''
+        self.assertEqual(spec.submodule_search_locations, [location])
         self.assertEqual(spec.cached, self.cached)
         self.assertTrue(spec.has_location)
 
@@ -744,7 +745,8 @@ class FactoryTests:
         self.assertEqual(spec.loader, self.fileloader)
         self.assertEqual(spec.origin, self.path)
         self.assertIs(spec.loader_state, None)
-        self.assertEqual(spec.submodule_search_locations, [os.getcwd()])
+        location = cwd if (cwd := os.getcwd()) != '/' else ''
+        self.assertEqual(spec.submodule_search_locations, [location])
         self.assertEqual(spec.cached, self.cached)
         self.assertTrue(spec.has_location)
 
@@ -769,7 +771,8 @@ class FactoryTests:
         self.assertEqual(spec.loader, self.pkgloader)
         self.assertEqual(spec.origin, self.path)
         self.assertIs(spec.loader_state, None)
-        self.assertEqual(spec.submodule_search_locations, [os.getcwd()])
+        location = cwd if (cwd := os.getcwd()) != '/' else ''
+        self.assertEqual(spec.submodule_search_locations, [location])
         self.assertEqual(spec.cached, self.cached)
         self.assertTrue(spec.has_location)
 
index a62d68fcd8b333d281e96feb24a1506c8d46829e..f63ea6307b0f9532979e4bc4115f4ae2a824ac7c 100644 (file)
@@ -803,7 +803,7 @@ class PEP3147Tests:
         with util.temporary_pycache_prefix(pycache_prefix):
             self.assertEqual(
                 self.util.cache_from_source(path, optimization=''),
-                expect)
+                os.path.normpath(expect))
 
     @unittest.skipIf(sys.implementation.cache_tag is None,
                      'requires sys.implementation.cache_tag to not be None')
index 0b6dcc5eaf03d20cd4870700e93a5bd94a21f864..4f1b190f4ccc5a235d8a149f361cbc982b24666d 100644 (file)
@@ -6,6 +6,7 @@ from importlib import machinery, util, invalidate_caches
 import marshal
 import os
 import os.path
+from test import support
 from test.support import import_helper
 from test.support import os_helper
 import unittest
@@ -22,25 +23,34 @@ if 'errno' in sys.builtin_module_names:
 if 'importlib' not in sys.builtin_module_names:
     BUILTINS.bad_name = 'importlib'
 
-EXTENSIONS = types.SimpleNamespace()
-EXTENSIONS.path = None
-EXTENSIONS.ext = None
-EXTENSIONS.filename = None
-EXTENSIONS.file_path = None
-EXTENSIONS.name = '_testcapi'
-
-def _extension_details():
-    global EXTENSIONS
-    for path in sys.path:
-        for ext in machinery.EXTENSION_SUFFIXES:
-            filename = EXTENSIONS.name + ext
-            file_path = os.path.join(path, filename)
-            if os.path.exists(file_path):
-                EXTENSIONS.path = path
-                EXTENSIONS.ext = ext
-                EXTENSIONS.filename = filename
-                EXTENSIONS.file_path = file_path
-                return
+if support.is_wasi:
+    # dlopen() is a shim for WASI as of WASI SDK which fails by default.
+    # We don't provide an implementation, so tests will fail.
+    # But we also don't want to turn off dynamic loading for those that provide
+    # a working implementation.
+    def _extension_details():
+        global EXTENSIONS
+        EXTENSIONS = None
+else:
+    EXTENSIONS = types.SimpleNamespace()
+    EXTENSIONS.path = None
+    EXTENSIONS.ext = None
+    EXTENSIONS.filename = None
+    EXTENSIONS.file_path = None
+    EXTENSIONS.name = '_testcapi'
+
+    def _extension_details():
+        global EXTENSIONS
+        for path in sys.path:
+            for ext in machinery.EXTENSION_SUFFIXES:
+                filename = EXTENSIONS.name + ext
+                file_path = os.path.join(path, filename)
+                if os.path.exists(file_path):
+                    EXTENSIONS.path = path
+                    EXTENSIONS.ext = ext
+                    EXTENSIONS.filename = filename
+                    EXTENSIONS.file_path = file_path
+                    return
 
 _extension_details()
 
diff --git a/Misc/NEWS.d/next/Tests/2024-03-13-12-06-49.gh-issue-115979.zsNpQD.rst b/Misc/NEWS.d/next/Tests/2024-03-13-12-06-49.gh-issue-115979.zsNpQD.rst
new file mode 100644 (file)
index 0000000..02bc2b8
--- /dev/null
@@ -0,0 +1 @@
+Update test_importlib so that it passes under WASI SDK 21.