diff options
| author | Stephen Tozer <stephen.tozer@sony.com> | 2024-10-10 14:38:07 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-10 14:38:07 +0100 |
| commit | 6779376ee917279b16e256839d236cfdf8fd9ab9 (patch) | |
| tree | beb694bbf7e9ade2135a779d6b793cf75983b7d5 /cross-project-tests | |
| parent | 480e7f0667794822f7f3a065bed73d9a2ecc2d58 (diff) | |
[Dexter] Remove outdated imp dependency (#111833)
Fixes: https://github.com/llvm/llvm-project/issues/111815
This patch replaces usage of the python `imp` library, which is
deprecated since python3.4 and removed in python3.12, with the
`importlib` library. As part of this update the repeated
find_module+load_module pattern is moved into a utility function, since
the importlib equivalent is much more verbose.
Diffstat (limited to 'cross-project-tests')
5 files changed, 23 insertions, 17 deletions
diff --git a/cross-project-tests/debuginfo-tests/dexter/dex/debugger/lldb/LLDB.py b/cross-project-tests/debuginfo-tests/dexter/dex/debugger/lldb/LLDB.py index 2307550aca04..e8bc65cd3fbe 100644 --- a/cross-project-tests/debuginfo-tests/dexter/dex/debugger/lldb/LLDB.py +++ b/cross-project-tests/debuginfo-tests/dexter/dex/debugger/lldb/LLDB.py @@ -7,7 +7,6 @@ """Interface for communicating with the LLDB debugger via its python interface. """ -import imp import os import shlex from subprocess import CalledProcessError, check_output, STDOUT @@ -18,6 +17,7 @@ from dex.dextIR import FrameIR, LocIR, StepIR, StopReason, ValueIR from dex.dextIR import StackFrame, SourceLocation, ProgramState from dex.utils.Exceptions import DebuggerException, LoadDebuggerException from dex.utils.ReturnCode import ReturnCode +from dex.utils.Imports import load_module class LLDB(DebuggerBase): @@ -82,8 +82,7 @@ class LLDB(DebuggerBase): ) try: - module_info = imp.find_module("lldb", [pythonpath]) - return imp.load_module("lldb", *module_info) + return load_module("lldb", pythonpath) except ImportError as e: msg = str(e) if msg.endswith("not a valid Win32 application."): diff --git a/cross-project-tests/debuginfo-tests/dexter/dex/debugger/visualstudio/VisualStudio.py b/cross-project-tests/debuginfo-tests/dexter/dex/debugger/visualstudio/VisualStudio.py index 17587b3f3e18..7cb56ec0c25a 100644 --- a/cross-project-tests/debuginfo-tests/dexter/dex/debugger/visualstudio/VisualStudio.py +++ b/cross-project-tests/debuginfo-tests/dexter/dex/debugger/visualstudio/VisualStudio.py @@ -7,7 +7,6 @@ """Interface for communicating with the Visual Studio debugger via DTE.""" import abc -import imp import os import sys from enum import IntEnum @@ -19,15 +18,14 @@ from dex.debugger.DebuggerBase import DebuggerBase, watch_is_active from dex.dextIR import FrameIR, LocIR, StepIR, StopReason, ValueIR from dex.dextIR import StackFrame, SourceLocation, ProgramState from dex.utils.Exceptions import Error, LoadDebuggerException +from dex.utils.Imports import load_module from dex.utils.ReturnCode import ReturnCode - def _load_com_module(): try: - module_info = imp.find_module( - "ComInterface", [os.path.join(os.path.dirname(__file__), "windows")] + return load_module( + "ComInterface", os.path.join(os.path.dirname(__file__), "windows") ) - return imp.load_module("ComInterface", *module_info) except ImportError as e: raise LoadDebuggerException(e, sys.exc_info()) diff --git a/cross-project-tests/debuginfo-tests/dexter/dex/tools/Main.py b/cross-project-tests/debuginfo-tests/dexter/dex/tools/Main.py index b6c146ad7840..512958d20f4b 100644 --- a/cross-project-tests/debuginfo-tests/dexter/dex/tools/Main.py +++ b/cross-project-tests/debuginfo-tests/dexter/dex/tools/Main.py @@ -10,7 +10,6 @@ parsing and running the unit-testing harnesses, before calling the reequested subtool. """ -import imp import os import sys @@ -18,6 +17,7 @@ from dex.utils import PrettyOutput, Timer from dex.utils import ExtArgParse as argparse from dex.utils import get_root_directory from dex.utils.Exceptions import Error, ToolArgumentError +from dex.utils.Imports import load_module from dex.utils.Logging import Logger from dex.utils.UnitTests import unit_tests_ok from dex.utils.Version import version @@ -135,9 +135,7 @@ def _import_tool_module(tool_name): tool_name = tool_name.replace("-", "_") tools_directory = get_tools_directory() - module_info = imp.find_module(tool_name, [tools_directory]) - - return imp.load_module(tool_name, *module_info) + return load_module(tool_name, tools_directory) def tool_main(context, tool, args): diff --git a/cross-project-tests/debuginfo-tests/dexter/dex/tools/help/Tool.py b/cross-project-tests/debuginfo-tests/dexter/dex/tools/help/Tool.py index 520bf9f59917..44e0a0e65c4b 100644 --- a/cross-project-tests/debuginfo-tests/dexter/dex/tools/help/Tool.py +++ b/cross-project-tests/debuginfo-tests/dexter/dex/tools/help/Tool.py @@ -6,10 +6,10 @@ # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception """Help tool.""" -import imp import textwrap from dex.tools import ToolBase, get_tool_names, get_tools_directory, tool_main +from dex.utils.Imports import load_module from dex.utils.ReturnCode import ReturnCode @@ -39,8 +39,7 @@ class Tool(ToolBase): tools_directory = get_tools_directory() for tool_name in sorted(self._visible_tool_names): internal_name = tool_name.replace("-", "_") - module_info = imp.find_module(internal_name, [tools_directory]) - tool_doc = imp.load_module(internal_name, *module_info).Tool.__doc__ + tool_doc = load_module(internal_name, tools_directory).Tool.__doc__ tool_doc = tool_doc.strip() if tool_doc else "" tool_doc = textwrap.fill(" ".join(tool_doc.split()), 80) s += "<g>{}</>\n{}\n\n".format(tool_name, tool_doc) @@ -53,6 +52,5 @@ class Tool(ToolBase): tool_name = self.context.options.tool.replace("-", "_") tools_directory = get_tools_directory() - module_info = imp.find_module(tool_name, [tools_directory]) - module = imp.load_module(tool_name, *module_info) + module = load_module(tool_name, tools_directory) return tool_main(self.context, module.Tool(self.context), ["--help"]) diff --git a/cross-project-tests/debuginfo-tests/dexter/dex/utils/Imports.py b/cross-project-tests/debuginfo-tests/dexter/dex/utils/Imports.py new file mode 100644 index 000000000000..ea052c21a184 --- /dev/null +++ b/cross-project-tests/debuginfo-tests/dexter/dex/utils/Imports.py @@ -0,0 +1,13 @@ +import importlib +import os +import sys + + +def load_module(name, path): + spec = importlib.util.spec_from_file_location( + name, os.path.join(path, name, "__init__.py") + ) + module = importlib.util.module_from_spec(spec) + sys.modules[name] = module + spec.loader.exec_module(module) + return module |
