[Lldb-commits] [lldb] [lldb] Replace the usage of module imp with module importlib (PR #70443)

via lldb-commits lldb-commits at lists.llvm.org
Fri Oct 27 04:58:54 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-lldb

Author: Tulio Magno Quites Machado Filho (tuliom)

<details>
<summary>Changes</summary>

imp got removed in Python 3.12 [1] and the community recommends using importlib in newer Python versions.

[1] https://docs.python.org/3.12/whatsnew/3.12.html#imp

---
Full diff: https://github.com/llvm/llvm-project/pull/70443.diff


2 Files Affected:

- (modified) lldb/scripts/use_lldb_suite.py (+22-8) 
- (modified) lldb/test/API/use_lldb_suite.py (+21-8) 


``````````diff
diff --git a/lldb/scripts/use_lldb_suite.py b/lldb/scripts/use_lldb_suite.py
index 6388d87b181ce03..4cedfa532cf972d 100644
--- a/lldb/scripts/use_lldb_suite.py
+++ b/lldb/scripts/use_lldb_suite.py
@@ -17,11 +17,25 @@ def find_lldb_root():
 
 
 lldb_root = find_lldb_root()
-import imp
-
-fp, pathname, desc = imp.find_module("use_lldb_suite_root", [lldb_root])
-try:
-    imp.load_module("use_lldb_suite_root", fp, pathname, desc)
-finally:
-    if fp:
-        fp.close()
+
+# Module imp got removed in Python 3.12.
+if (
+    sys.version_info.major == 3 and sys.version_info.minor >= 12
+) or sys.version_info.major > 3:
+    import importlib.machinery
+    import importlib.util
+
+    path = os.path.join(lldb_root, "use_lldb_suite_root.py")
+    loader = importlib.machinery.SourceFileLoader("use_lldb_suite_root", path)
+    spec = importlib.util.spec_from_loader("use_lldb_suite_root", loader=loader)
+    module = importlib.util.module_from_spec(spec)
+    loader.exec_module(module)
+else:
+    import imp
+
+    fp, pathname, desc = imp.find_module("use_lldb_suite_root", [lldb_root])
+    try:
+        imp.load_module("use_lldb_suite_root", fp, pathname, desc)
+    finally:
+        if fp:
+            fp.close()
diff --git a/lldb/test/API/use_lldb_suite.py b/lldb/test/API/use_lldb_suite.py
index e237dd4b8a5607c..c9332d9921b4eb3 100644
--- a/lldb/test/API/use_lldb_suite.py
+++ b/lldb/test/API/use_lldb_suite.py
@@ -20,11 +20,24 @@ def find_lldb_root():
 
 lldb_root = find_lldb_root()
 
-import imp
-
-fp, pathname, desc = imp.find_module("use_lldb_suite_root", [lldb_root])
-try:
-    imp.load_module("use_lldb_suite_root", fp, pathname, desc)
-finally:
-    if fp:
-        fp.close()
+# Module imp got removed in Python 3.12.
+if (
+    sys.version_info.major == 3 and sys.version_info.minor >= 12
+) or sys.version_info.major > 3:
+    import importlib.machinery
+    import importlib.util
+
+    path = os.path.join(lldb_root, "use_lldb_suite_root.py")
+    loader = importlib.machinery.SourceFileLoader("use_lldb_suite_root", path)
+    spec = importlib.util.spec_from_loader("use_lldb_suite_root", loader=loader)
+    module = importlib.util.module_from_spec(spec)
+    loader.exec_module(module)
+else:
+    import imp
+
+    fp, pathname, desc = imp.find_module("use_lldb_suite_root", [lldb_root])
+    try:
+        imp.load_module("use_lldb_suite_root", fp, pathname, desc)
+    finally:
+        if fp:
+            fp.close()

``````````

</details>


https://github.com/llvm/llvm-project/pull/70443


More information about the lldb-commits mailing list