Skip to content

Commit

Permalink
replace uses of deprecated imp module
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfikl committed Mar 1, 2024
1 parent 05ee666 commit 8c28483
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
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

0 comments on commit 8c28483

Please sign in to comment.