[Lldb-commits] [lldb] [lldb] Tweak Python interpreter workaround on macOS (PR #95582)

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Fri Jun 14 11:44:11 PDT 2024


https://github.com/JDevlieghere created https://github.com/llvm/llvm-project/pull/95582

Avoid copying the Python interpreter when running in a virtual environment as it will already have its own copy of the Python interpreter. Also leave a breadcrumb that we're running with a different Python interpreter.

>From 76e0dd2cef42105f5112a19a7f5c822939e5fdf1 Mon Sep 17 00:00:00 2001
From: Jonas Devlieghere <jonas at devlieghere.com>
Date: Fri, 14 Jun 2024 11:41:54 -0700
Subject: [PATCH] [lldb] Tweak Python interpreter workaround on macOS

Avoid copying the Python interpreter when running in a virtual
environment as it will already have its own copy of the Python
interpreter. Also leave a breadcrumb that we're running with a different
Python interpreter.
---
 lldb/test/API/lit.cfg.py | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/lldb/test/API/lit.cfg.py b/lldb/test/API/lit.cfg.py
index d934349fe3ca3..1e99c8cb95d16 100644
--- a/lldb/test/API/lit.cfg.py
+++ b/lldb/test/API/lit.cfg.py
@@ -58,6 +58,15 @@ def find_shlibpath_var():
 # enabled, we can't inject libraries into system binaries at all, so we need a
 # copy of the "real" python to work with.
 def find_python_interpreter():
+    # This is only necessary when using DYLD_INSERT_LIBRARIES.
+    if "DYLD_INSERT_LIBRARIES" not in config.environment:
+        return None
+
+    # If we're running in a virtual environment, we already have a copy of the
+    # Python executable.
+    if "VIRTUAL_ENV" in config.environment:
+        return None
+
     # Avoid doing any work if we already copied the binary.
     copied_python = os.path.join(config.lldb_build_directory, "copied-python")
     if os.path.isfile(copied_python):
@@ -84,7 +93,7 @@ def find_python_interpreter():
     # RPATH and cannot be copied.
     try:
         # We don't care about the output, just make sure it runs.
-        subprocess.check_output([copied_python, "-V"], stderr=subprocess.STDOUT)
+        subprocess.check_call([copied_python, "-V"])
     except subprocess.CalledProcessError:
         # The copied Python didn't work. Assume we're dealing with the Python
         # interpreter in Xcode. Given that this is not a system binary SIP
@@ -130,8 +139,13 @@ def delete_module_cache(path):
                 "libclang_rt.tsan_osx_dynamic.dylib"
             )
 
-if "DYLD_INSERT_LIBRARIES" in config.environment and platform.system() == "Darwin":
-    config.python_executable = find_python_interpreter()
+if platform.system() == "Darwin":
+    python_executable = find_python_interpreter()
+    if python_executable:
+        lit_config.note(
+            "Using {} instead of {}".format(python_executable, config.python_executable)
+        )
+        config.python_executable = python_executable
 
 # Shared library build of LLVM may require LD_LIBRARY_PATH or equivalent.
 if is_configured("shared_libs"):



More information about the lldb-commits mailing list