Skip to content

Commit 75f0f14

Browse files
SLTozerDanielCChen
authored andcommitted
[Dexter] Remove outdated imp dependency (llvm#111833)
Fixes: llvm#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.
1 parent a64209f commit 75f0f14

File tree

5 files changed

+23
-17
lines changed

5 files changed

+23
-17
lines changed

cross-project-tests/debuginfo-tests/dexter/dex/debugger/lldb/LLDB.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"""Interface for communicating with the LLDB debugger via its python interface.
88
"""
99

10-
import imp
1110
import os
1211
import shlex
1312
from subprocess import CalledProcessError, check_output, STDOUT
@@ -18,6 +17,7 @@
1817
from dex.dextIR import StackFrame, SourceLocation, ProgramState
1918
from dex.utils.Exceptions import DebuggerException, LoadDebuggerException
2019
from dex.utils.ReturnCode import ReturnCode
20+
from dex.utils.Imports import load_module
2121

2222

2323
class LLDB(DebuggerBase):
@@ -82,8 +82,7 @@ def _load_interface(self):
8282
)
8383

8484
try:
85-
module_info = imp.find_module("lldb", [pythonpath])
86-
return imp.load_module("lldb", *module_info)
85+
return load_module("lldb", pythonpath)
8786
except ImportError as e:
8887
msg = str(e)
8988
if msg.endswith("not a valid Win32 application."):

cross-project-tests/debuginfo-tests/dexter/dex/debugger/visualstudio/VisualStudio.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"""Interface for communicating with the Visual Studio debugger via DTE."""
88

99
import abc
10-
import imp
1110
import os
1211
import sys
1312
from enum import IntEnum
@@ -19,15 +18,14 @@
1918
from dex.dextIR import FrameIR, LocIR, StepIR, StopReason, ValueIR
2019
from dex.dextIR import StackFrame, SourceLocation, ProgramState
2120
from dex.utils.Exceptions import Error, LoadDebuggerException
21+
from dex.utils.Imports import load_module
2222
from dex.utils.ReturnCode import ReturnCode
2323

24-
2524
def _load_com_module():
2625
try:
27-
module_info = imp.find_module(
28-
"ComInterface", [os.path.join(os.path.dirname(__file__), "windows")]
26+
return load_module(
27+
"ComInterface", os.path.join(os.path.dirname(__file__), "windows")
2928
)
30-
return imp.load_module("ComInterface", *module_info)
3129
except ImportError as e:
3230
raise LoadDebuggerException(e, sys.exc_info())
3331

cross-project-tests/debuginfo-tests/dexter/dex/tools/Main.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
subtool.
1111
"""
1212

13-
import imp
1413
import os
1514
import sys
1615

1716
from dex.utils import PrettyOutput, Timer
1817
from dex.utils import ExtArgParse as argparse
1918
from dex.utils import get_root_directory
2019
from dex.utils.Exceptions import Error, ToolArgumentError
20+
from dex.utils.Imports import load_module
2121
from dex.utils.Logging import Logger
2222
from dex.utils.UnitTests import unit_tests_ok
2323
from dex.utils.Version import version
@@ -135,9 +135,7 @@ def _import_tool_module(tool_name):
135135
tool_name = tool_name.replace("-", "_")
136136

137137
tools_directory = get_tools_directory()
138-
module_info = imp.find_module(tool_name, [tools_directory])
139-
140-
return imp.load_module(tool_name, *module_info)
138+
return load_module(tool_name, tools_directory)
141139

142140

143141
def tool_main(context, tool, args):

cross-project-tests/debuginfo-tests/dexter/dex/tools/help/Tool.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
77
"""Help tool."""
88

9-
import imp
109
import textwrap
1110

1211
from dex.tools import ToolBase, get_tool_names, get_tools_directory, tool_main
12+
from dex.utils.Imports import load_module
1313
from dex.utils.ReturnCode import ReturnCode
1414

1515

@@ -39,8 +39,7 @@ def _default_text(self):
3939
tools_directory = get_tools_directory()
4040
for tool_name in sorted(self._visible_tool_names):
4141
internal_name = tool_name.replace("-", "_")
42-
module_info = imp.find_module(internal_name, [tools_directory])
43-
tool_doc = imp.load_module(internal_name, *module_info).Tool.__doc__
42+
tool_doc = load_module(internal_name, tools_directory).Tool.__doc__
4443
tool_doc = tool_doc.strip() if tool_doc else ""
4544
tool_doc = textwrap.fill(" ".join(tool_doc.split()), 80)
4645
s += "<g>{}</>\n{}\n\n".format(tool_name, tool_doc)
@@ -53,6 +52,5 @@ def go(self) -> ReturnCode:
5352

5453
tool_name = self.context.options.tool.replace("-", "_")
5554
tools_directory = get_tools_directory()
56-
module_info = imp.find_module(tool_name, [tools_directory])
57-
module = imp.load_module(tool_name, *module_info)
55+
module = load_module(tool_name, tools_directory)
5856
return tool_main(self.context, module.Tool(self.context), ["--help"])
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import importlib
2+
import os
3+
import sys
4+
5+
6+
def load_module(name, path):
7+
spec = importlib.util.spec_from_file_location(
8+
name, os.path.join(path, name, "__init__.py")
9+
)
10+
module = importlib.util.module_from_spec(spec)
11+
sys.modules[name] = module
12+
spec.loader.exec_module(module)
13+
return module

0 commit comments

Comments
 (0)