Skip to content

Commit

Permalink
Do not use loal_module in backend selection code (#132)
Browse files Browse the repository at this point in the history
* Do not use load_module
* fixes #131
  • Loading branch information
itziakos authored Jun 15, 2024
1 parent 83be1e3 commit 4f3e2ef
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
14 changes: 8 additions & 6 deletions win32ctypes/core/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# (C) Copyright 2014-2023 Enthought, Inc., Austin, TX
# (C) Copyright 2014-2024 Enthought, Inc., Austin, TX
# All right reserved.
#
# This file is open source software distributed according to the terms in
Expand All @@ -26,10 +26,11 @@ class BackendLoader(Loader):
def __init__(self, redirect_module):
self.redirect_module = redirect_module

def load_module(self, fullname):
module = importlib.import_module(self.redirect_module)
sys.modules[fullname] = module
return module
def create_module(self, spec):
return importlib.import_module(self.redirect_module)

def exec_module(self, module):
pass


class BackendFinder(MetaPathFinder):
Expand All @@ -47,7 +48,8 @@ def find_spec(self, fullname, path, target=None):
else:
redirected = f'win32ctypes.core.cffi.{module_name}'
loader = BackendLoader(redirected)
return importlib.machinery.ModuleSpec(module_name, loader)
return importlib.machinery.ModuleSpec(
f'win32ctypes.core.{module_name}', loader)
else:
return None

Expand Down
4 changes: 4 additions & 0 deletions win32ctypes/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ def test_backend_cffi_load(self):
# when/then
for name in _modules:
module = importlib.import_module(f'win32ctypes.core.{name}')
self.assertEqual(
module.__spec__.name, f'win32ctypes.core.{name}')
self.assertTrue(module.__file__.endswith(f'cffi\\{name}.py'))

@unittest.skipIf(_backend != 'ctypes', 'ctypes backend not enabled')
def test_backend_ctypes_load(self):
# when/then
for name in _modules:
module = importlib.import_module(f'win32ctypes.core.{name}')
self.assertEqual(
module.__spec__.name, f'win32ctypes.core.{name}')
self.assertTrue(module.__file__.endswith(f'ctypes\\{name}.py'))

0 comments on commit 4f3e2ef

Please sign in to comment.