Skip to content

Commit 0cc5047

Browse files
[3.12] GH-115979: update test_importlib to work under WASI SDK 21 (GH-116754) (GH-116759)
GH-115979: update test_importlib to work under WASI SDK 21 (GH-116754) (cherry picked from commit 61733a2) Co-authored-by: Brett Cannon <[email protected]>
1 parent 592c0e2 commit 0cc5047

File tree

8 files changed

+45
-28
lines changed

8 files changed

+45
-28
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, f'{util.EXTENSIONS.name} 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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class LoaderTests:
1919
"""Test ExtensionFileLoader."""
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(
@@ -101,7 +101,7 @@ class SinglePhaseExtensionModuleTests(abc.LoaderTests):
101101
# Test loading extension modules without multi-phase initialization.
102102

103103
def setUp(self):
104-
if not self.machinery.EXTENSION_SUFFIXES:
104+
if not self.machinery.EXTENSION_SUFFIXES or not util.EXTENSIONS:
105105
raise unittest.SkipTest("Requires dynamic loading support.")
106106
self.name = '_testsinglephase'
107107
if self.name in sys.builtin_module_names:
@@ -182,7 +182,7 @@ class MultiPhaseExtensionModuleTests(abc.LoaderTests):
182182
# Test loading extension modules with multi-phase initialization (PEP 489).
183183

184184
def setUp(self):
185-
if not self.machinery.EXTENSION_SUFFIXES:
185+
if not self.machinery.EXTENSION_SUFFIXES or not util.EXTENSIONS:
186186
raise unittest.SkipTest("Requires dynamic loading support.")
187187
self.name = '_testmultiphase'
188188
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
@@ -502,7 +502,8 @@ def test_spec_from_loader_is_package_true_with_fileloader(self):
502502
self.assertEqual(spec.loader, self.fileloader)
503503
self.assertEqual(spec.origin, self.path)
504504
self.assertIs(spec.loader_state, None)
505-
self.assertEqual(spec.submodule_search_locations, [os.getcwd()])
505+
location = cwd if (cwd := os.getcwd()) != '/' else ''
506+
self.assertEqual(spec.submodule_search_locations, [location])
506507
self.assertEqual(spec.cached, self.cached)
507508
self.assertTrue(spec.has_location)
508509

@@ -601,7 +602,8 @@ def test_spec_from_file_location_smsl_empty(self):
601602
self.assertEqual(spec.loader, self.fileloader)
602603
self.assertEqual(spec.origin, self.path)
603604
self.assertIs(spec.loader_state, None)
604-
self.assertEqual(spec.submodule_search_locations, [os.getcwd()])
605+
location = cwd if (cwd := os.getcwd()) != '/' else ''
606+
self.assertEqual(spec.submodule_search_locations, [location])
605607
self.assertEqual(spec.cached, self.cached)
606608
self.assertTrue(spec.has_location)
607609

@@ -626,7 +628,8 @@ def test_spec_from_file_location_smsl_default(self):
626628
self.assertEqual(spec.loader, self.pkgloader)
627629
self.assertEqual(spec.origin, self.path)
628630
self.assertIs(spec.loader_state, None)
629-
self.assertEqual(spec.submodule_search_locations, [os.getcwd()])
631+
location = cwd if (cwd := os.getcwd()) != '/' else ''
632+
self.assertEqual(spec.submodule_search_locations, [location])
630633
self.assertEqual(spec.cached, self.cached)
631634
self.assertTrue(spec.has_location)
632635

Lib/test/test_importlib/test_util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ def test_cache_from_source_respects_pycache_prefix_relative(self):
577577
with util.temporary_pycache_prefix(pycache_prefix):
578578
self.assertEqual(
579579
self.util.cache_from_source(path, optimization=''),
580-
expect)
580+
os.path.normpath(expect))
581581

582582
@unittest.skipIf(sys.implementation.cache_tag is None,
583583
'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 = '_testsinglephase'
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 = '_testsinglephase'
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)