[llvm] [Dexter] Remove outdated imp dependency (PR #111833)
Stephen Tozer via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 10 06:09:02 PDT 2024
https://github.com/SLTozer created https://github.com/llvm/llvm-project/pull/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.
>From 49614ba46047aee6e0a60ac0921e90607fbe89f4 Mon Sep 17 00:00:00 2001
From: Stephen Tozer <stephen.tozer at sony.com>
Date: Thu, 10 Oct 2024 14:05:46 +0100
Subject: [PATCH] [Dexter] Remove outdated imp dependency
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.
---
.../debuginfo-tests/dexter/dex/debugger/lldb/LLDB.py | 5 ++---
.../dexter/dex/debugger/visualstudio/VisualStudio.py | 8 +++-----
.../debuginfo-tests/dexter/dex/tools/Main.py | 6 ++----
.../debuginfo-tests/dexter/dex/tools/help/Tool.py | 8 +++-----
.../debuginfo-tests/dexter/dex/utils/Imports.py | 10 ++++++++++
5 files changed, 20 insertions(+), 17 deletions(-)
create mode 100644 cross-project-tests/debuginfo-tests/dexter/dex/utils/Imports.py
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 2307550aca047b..e8bc65cd3fbe88 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 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 @@ def _load_interface(self):
)
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 17587b3f3e18d6..7cb56ec0c25a76 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.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 b6c146ad784062..512958d20f4bbc 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 @@
subtool.
"""
-import imp
import os
import sys
@@ -18,6 +17,7 @@
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 520bf9f59917af..44e0a0e65c4bac 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 @@ def _default_text(self):
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 @@ def go(self) -> ReturnCode:
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 00000000000000..511e753606bde9
--- /dev/null
+++ b/cross-project-tests/debuginfo-tests/dexter/dex/utils/Imports.py
@@ -0,0 +1,10 @@
+import importlib
+import sys
+
+
+def load_module(name, path):
+ spec = importlib.util.spec_from_file_location(name, f"{path}/{name}/__init__.py")
+ module = importlib.util.module_from_spec(spec)
+ sys.modules[name] = module
+ spec.loader.exec_module(module)
+ return module
More information about the llvm-commits
mailing list