Skip to content

Commit

Permalink
[3.11] pythonGH-115979: update test_importlib to work under WASI SDK …
Browse files Browse the repository at this point in the history
…21 (pythonGH-116754) (pythonGH-116762)

(cherry picked from commit 61733a2)
  • Loading branch information
brettcannon authored Mar 13, 2024
1 parent 039fd9e commit 3cc24f1
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 27 deletions.
3 changes: 2 additions & 1 deletion Lib/test/test_importlib/extension/test_case_sensitivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
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):

Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_importlib/extension/test_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_importlib/extension/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_importlib/extension/test_path_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
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."""
Expand Down
9 changes: 6 additions & 3 deletions Lib/test/test_importlib/test_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,8 @@ def test_spec_from_loader_is_package_true_with_fileloader(self):
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)

Expand Down Expand Up @@ -744,7 +745,8 @@ def test_spec_from_file_location_smsl_empty(self):
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)

Expand All @@ -769,7 +771,8 @@ def test_spec_from_file_location_smsl_default(self):
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)

Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_importlib/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ def test_cache_from_source_respects_pycache_prefix_relative(self):
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')
Expand Down
48 changes: 29 additions & 19 deletions Lib/test/test_importlib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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
Expand All @@ -22,25 +23,34 @@
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()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update test_importlib so that it passes under WASI SDK 21.

0 comments on commit 3cc24f1

Please sign in to comment.