Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace uses of deprecated imp module #41

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion codepy/cuda.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def compile(self, host_toolchain, nvcc_toolchain,
module_path = os.path.join(destination_base, mod_name
+ host_toolchain.so_ext)
try:
from imp import load_dynamic
from codepy.tools import load_dynamic
return load_dynamic(mod_name, module_path)
except Exception:
return link_extension(host_toolchain,
Expand Down
4 changes: 2 additions & 2 deletions codepy/jit.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def extension_from_string(toolchain, name, source_string,
cache_dir, debug, wait_on_error, debug_recompile,
False, sleep_delay=sleep_delay)
# try loading it
from imp import load_dynamic
from codepy.tools import load_dynamic
return load_dynamic(mod_name, ext_file)


Expand Down Expand Up @@ -484,7 +484,7 @@ def link_extension(toolchain, objects, mod_name, cache_dir=None,
raise

# try loading it
from imp import load_dynamic
from codepy.tools import load_dynamic
return load_dynamic(mod_name, destination)


Expand Down
6 changes: 3 additions & 3 deletions codepy/libraries.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ def add_boost_numeric_bindings(toolchain):

def add_numpy(toolchain):
def get_numpy_incpath():
from imp import find_module
file, pathname, descr = find_module("numpy")
from importlib.util import find_spec
spec = find_spec("numpy")
from os.path import join
return join(pathname, "core", "include")
return join(spec.submodule_search_locations[0], "core", "include")

toolchain.add_library("numpy", [get_numpy_incpath()], [], [])

Expand Down
21 changes: 21 additions & 0 deletions codepy/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,24 @@ def join_continued_lines(lines):
warn("line continuation at end of file")

return result


def load_dynamic(name: str, path: str):
"""Implementation of ``imp.load_dynamic`` based on :mod:`importlib`."""
# https://github.com/python/cpython/pull/105951

from importlib.machinery import ExtensionFileLoader

loader = ExtensionFileLoader(name, path)

from importlib.util import module_from_spec, spec_from_loader

spec = spec_from_loader(name, loader)
module = module_from_spec(spec)

# The module is always executed and not cached in sys.modules.
# Uncomment the following line to cache the module.
# sys.modules[module.__name__] = module

loader.exec_module(module)
return module
Loading