Skip to content

Commit 3cc24f1

Browse files
authored
[3.11] GH-115979: update test_importlib to work under WASI SDK 21 (GH-116754) (GH-116762)
(cherry picked from commit 61733a2)
1 parent 039fd9e commit 3cc24f1

File tree

8 files changed

+44
-27
lines changed

8 files changed

+44
-27
lines changed

Lib/test/test_importlib/extension/test_case_sensitivity.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
machinery = util.import_importlib('importlib.machinery')
99

1010

11-
@unittest.skipIf(util.EXTENSIONS.filename is None, '_testcapi not available')
11+
@unittest.skipIf(util.EXTENSIONS is None or util.EXTENSIONS.filename is None,
12+
'dynamic loading not supported or test module not available')
1213
@util.case_insensitive_tests
1314
class ExtensionModuleCaseSensitivityTest(util.CASEOKTestBase):
1415

Lib/test/test_importlib/extension/test_finder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class FinderTests(abc.FinderTests):
1111
"""Test the finder for extension modules."""
1212

1313
def setUp(self):
14-
if not self.machinery.EXTENSION_SUFFIXES:
14+
if not self.machinery.EXTENSION_SUFFIXES or not util.EXTENSIONS:
1515
raise unittest.SkipTest("Requires dynamic loading support.")
1616
if util.EXTENSIONS.name in sys.builtin_module_names:
1717
raise unittest.SkipTest(

Lib/test/test_importlib/extension/test_loader.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class LoaderTests(abc.LoaderTests):
1919
"""Test load_module() for extension modules."""
2020

2121
def setUp(self):
22-
if not self.machinery.EXTENSION_SUFFIXES:
22+
if not self.machinery.EXTENSION_SUFFIXES or not util.EXTENSIONS:
2323
raise unittest.SkipTest("Requires dynamic loading support.")
2424
if util.EXTENSIONS.name in sys.builtin_module_names:
2525
raise unittest.SkipTest(
@@ -99,7 +99,7 @@ class MultiPhaseExtensionModuleTests(abc.LoaderTests):
9999
# Test loading extension modules with multi-phase initialization (PEP 489).
100100

101101
def setUp(self):
102-
if not self.machinery.EXTENSION_SUFFIXES:
102+
if not self.machinery.EXTENSION_SUFFIXES or not util.EXTENSIONS:
103103
raise unittest.SkipTest("Requires dynamic loading support.")
104104
self.name = '_testmultiphase'
105105
if self.name in sys.builtin_module_names:

Lib/test/test_importlib/extension/test_path_hook.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import unittest
66

77

8+
@unittest.skipIf(util.EXTENSIONS is None or util.EXTENSIONS.filename is None,
9+
'dynamic loading not supported or test module not available')
810
class PathHookTests:
911

1012
"""Test the path hook for extension modules."""

Lib/test/test_importlib/test_spec.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,8 @@ def test_spec_from_loader_is_package_true_with_fileloader(self):
645645
self.assertEqual(spec.loader, self.fileloader)
646646
self.assertEqual(spec.origin, self.path)
647647
self.assertIs(spec.loader_state, None)
648-
self.assertEqual(spec.submodule_search_locations, [os.getcwd()])
648+
location = cwd if (cwd := os.getcwd()) != '/' else ''
649+
self.assertEqual(spec.submodule_search_locations, [location])
649650
self.assertEqual(spec.cached, self.cached)
650651
self.assertTrue(spec.has_location)
651652

@@ -744,7 +745,8 @@ def test_spec_from_file_location_smsl_empty(self):
744745
self.assertEqual(spec.loader, self.fileloader)
745746
self.assertEqual(spec.origin, self.path)
746747
self.assertIs(spec.loader_state, None)
747-
self.assertEqual(spec.submodule_search_locations, [os.getcwd()])
748+
location = cwd if (cwd := os.getcwd()) != '/' else ''
749+
self.assertEqual(spec.submodule_search_locations, [location])
748750
self.assertEqual(spec.cached, self.cached)
749751
self.assertTrue(spec.has_location)
750752

@@ -769,7 +771,8 @@ def test_spec_from_file_location_smsl_default(self):
769771
self.assertEqual(spec.loader, self.pkgloader)
770772
self.assertEqual(spec.origin, self.path)
771773
self.assertIs(spec.loader_state, None)
772-
self.assertEqual(spec.submodule_search_locations, [os.getcwd()])
774+
location = cwd if (cwd := os.getcwd()) != '/' else ''
775+
self.assertEqual(spec.submodule_search_locations, [location])
773776
self.assertEqual(spec.cached, self.cached)
774777
self.assertTrue(spec.has_location)
775778

Lib/test/test_importlib/test_util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ def test_cache_from_source_respects_pycache_prefix_relative(self):
803803
with util.temporary_pycache_prefix(pycache_prefix):
804804
self.assertEqual(
805805
self.util.cache_from_source(path, optimization=''),
806-
expect)
806+
os.path.normpath(expect))
807807

808808
@unittest.skipIf(sys.implementation.cache_tag is None,
809809
'requires sys.implementation.cache_tag to not be None')

Lib/test/test_importlib/util.py

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import marshal
77
import os
88
import os.path
9+
from test import support
910
from test.support import import_helper
1011
from test.support import os_helper
1112
import unittest
@@ -22,25 +23,34 @@
2223
if 'importlib' not in sys.builtin_module_names:
2324
BUILTINS.bad_name = 'importlib'
2425

25-
EXTENSIONS = types.SimpleNamespace()
26-
EXTENSIONS.path = None
27-
EXTENSIONS.ext = None
28-
EXTENSIONS.filename = None
29-
EXTENSIONS.file_path = None
30-
EXTENSIONS.name = '_testcapi'
31-
32-
def _extension_details():
33-
global EXTENSIONS
34-
for path in sys.path:
35-
for ext in machinery.EXTENSION_SUFFIXES:
36-
filename = EXTENSIONS.name + ext
37-
file_path = os.path.join(path, filename)
38-
if os.path.exists(file_path):
39-
EXTENSIONS.path = path
40-
EXTENSIONS.ext = ext
41-
EXTENSIONS.filename = filename
42-
EXTENSIONS.file_path = file_path
43-
return
26+
if support.is_wasi:
27+
# dlopen() is a shim for WASI as of WASI SDK which fails by default.
28+
# We don't provide an implementation, so tests will fail.
29+
# But we also don't want to turn off dynamic loading for those that provide
30+
# a working implementation.
31+
def _extension_details():
32+
global EXTENSIONS
33+
EXTENSIONS = None
34+
else:
35+
EXTENSIONS = types.SimpleNamespace()
36+
EXTENSIONS.path = None
37+
EXTENSIONS.ext = None
38+
EXTENSIONS.filename = None
39+
EXTENSIONS.file_path = None
40+
EXTENSIONS.name = '_testcapi'
41+
42+
def _extension_details():
43+
global EXTENSIONS
44+
for path in sys.path:
45+
for ext in machinery.EXTENSION_SUFFIXES:
46+
filename = EXTENSIONS.name + ext
47+
file_path = os.path.join(path, filename)
48+
if os.path.exists(file_path):
49+
EXTENSIONS.path = path
50+
EXTENSIONS.ext = ext
51+
EXTENSIONS.filename = filename
52+
EXTENSIONS.file_path = file_path
53+
return
4454

4555
_extension_details()
4656

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Update test_importlib so that it passes under WASI SDK 21.

0 commit comments

Comments
 (0)