[Lldb-commits] [PATCH] D96370: Pass enviroment variables to python scripts.

Rumeet Dhindsa via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Tue Feb 9 14:05:35 PST 2021


rdhindsa created this revision.
rdhindsa added a reviewer: labath.
rdhindsa requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

It enables environment variables set for lldb targets to be passed to python scripts. This allows the user to put Python scripts at different locations, but being able to import them by setting pythonpath.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D96370

Files:
  lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
  lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h
  lldb/test/Shell/ScriptInterpreter/Python/Inputs/environ.py
  lldb/test/Shell/ScriptInterpreter/Python/pass_environment.test


Index: lldb/test/Shell/ScriptInterpreter/Python/pass_environment.test
===================================================================
--- /dev/null
+++ lldb/test/Shell/ScriptInterpreter/Python/pass_environment.test
@@ -0,0 +1,10 @@
+# REQUIRES: python
+
+# RUN: %lldb --batch \
+# RUN:    --script-language python \
+# RUN:    -O 'settings append target.env-vars PYTHONPATH=/tmp' \
+# RUN:    -O 'command script import %S/Inputs/environ.py' \
+# RUN:    -O 'test' 2>&1  | FileCheck %s
+
+# CHECK: :/tmp
+# CHECK: '/tmp'
Index: lldb/test/Shell/ScriptInterpreter/Python/Inputs/environ.py
===================================================================
--- /dev/null
+++ lldb/test/Shell/ScriptInterpreter/Python/Inputs/environ.py
@@ -0,0 +1,10 @@
+import os
+import sys
+
+def test(debugger, command, result, internal_dict):
+  print(os.environ["PYTHONPATH"])
+  print(sys.path)
+
+def __lldb_init_module(debugger, internal_dict):
+  debugger.HandleCommand('command script add -f environ.test test')
+
Index: lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h
===================================================================
--- lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h
+++ lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h
@@ -341,6 +341,8 @@
                                          lldb::user_id_t watch_id);
   static void InitializePrivate();
 
+  static void SetPythonEnvironment(Debugger& debugger);
+
   class SynchronicityHandler {
   private:
     lldb::DebuggerSP m_debugger_sp;
Index: lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
===================================================================
--- lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
+++ lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
@@ -537,6 +537,8 @@
   PyRun_SimpleString(run_string.GetData());
   run_string.Clear();
 
+  SetPythonEnvironment(m_debugger);
+
   run_string.Printf("run_one_line (%s, 'import lldb.embedded_interpreter; from "
                     "lldb.embedded_interpreter import run_python_interpreter; "
                     "from lldb.embedded_interpreter import run_one_line')",
@@ -3269,6 +3271,26 @@
                      "from lldb.embedded_interpreter import run_one_line");
 }
 
+void ScriptInterpreterPythonImpl::SetPythonEnvironment(Debugger &debugger) {
+  StreamString run_string;
+  lldb::TargetSP target_get = debugger.GetTargetList().GetSelectedTarget();
+  const Environment &env = target_get->GetGlobalProperties()->GetEnvironment();
+
+  PyRun_SimpleString("import os");
+  for (const auto &KV : env) {
+    if (strcmp(KV.getKey().data(), "PYTHONPATH") == 0 ||
+        strcmp(KV.getKey().data(), "PATH") == 0) {
+      run_string.Clear();
+      run_string.Printf(
+          "os.environ[\"%s\"] = os.environ.get(\"%s\",\"\")+ os.pathsep +"
+          "\"%s\"",
+          KV.getKey().data(), KV.getKey().data(), KV.getValue().data());
+      PyRun_SimpleString(run_string.GetData());
+      AddToSysPath(AddLocation::End, KV.getValue().data());
+    }
+  }
+}
+
 void ScriptInterpreterPythonImpl::AddToSysPath(AddLocation location,
                                                std::string path) {
   std::string path_copy;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D96370.322495.patch
Type: text/x-patch
Size: 3317 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20210209/6e577399/attachment-0001.bin>


More information about the lldb-commits mailing list