[Lldb-commits] [lldb] r234516 - [Python] Fix issue configuring sys.path during startup.

Zachary Turner zturner at google.com
Thu Apr 9 11:08:51 PDT 2015


Author: zturner
Date: Thu Apr  9 13:08:50 2015
New Revision: 234516

URL: http://llvm.org/viewvc/llvm-project?rev=234516&view=rev
Log:
[Python] Fix issue configuring sys.path during startup.

Previously, users on Windows had to manually specify PYTHONPATH
to point to the site-packages directory before running LLDB.
The reason for this was because sys.path was being initialized
with a path containing unescaped backslashes, causing escape
sequences to end up in the paths.

Modified:
    lldb/trunk/include/lldb/Host/FileSpec.h
    lldb/trunk/include/lldb/Interpreter/ScriptInterpreterPython.h
    lldb/trunk/source/Host/common/FileSpec.cpp
    lldb/trunk/source/Host/windows/HostInfoWindows.cpp
    lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp

Modified: lldb/trunk/include/lldb/Host/FileSpec.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/FileSpec.h?rev=234516&r1=234515&r2=234516&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/FileSpec.h (original)
+++ lldb/trunk/include/lldb/Host/FileSpec.h Thu Apr  9 13:08:50 2015
@@ -409,6 +409,17 @@ public:
     GetPath (bool denormalize = true) const;
 
     //------------------------------------------------------------------
+    /// Extract the full path to the file.
+    ///
+    /// Extract the directory and path into an llvm::SmallVectorImpl<>
+    ///
+    /// @return
+    ///     Returns a std::string with the directory and filename
+    ///     concatenated.
+    //------------------------------------------------------------------
+    void GetPath(llvm::SmallVectorImpl<char> &path, bool denormalize = true) const;
+
+    //------------------------------------------------------------------
     /// Extract the extension of the file.
     ///
     /// Returns a ConstString that represents the extension of the filename

Modified: lldb/trunk/include/lldb/Interpreter/ScriptInterpreterPython.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Interpreter/ScriptInterpreterPython.h?rev=234516&r1=234515&r2=234516&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Interpreter/ScriptInterpreterPython.h (original)
+++ lldb/trunk/include/lldb/Interpreter/ScriptInterpreterPython.h Thu Apr  9 13:08:50 2015
@@ -490,6 +490,13 @@ public:
         PyGILState_STATE         m_GILState;
 	};
 protected:
+    enum class AddLocation
+    {
+        Beginning,
+        End
+    };
+
+    static void AddToSysPath(AddLocation location, std::string path);
 
     uint32_t
     IsExecutingPython () const

Modified: lldb/trunk/source/Host/common/FileSpec.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/FileSpec.cpp?rev=234516&r1=234515&r2=234516&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/FileSpec.cpp (original)
+++ lldb/trunk/source/Host/common/FileSpec.cpp Thu Apr  9 13:08:50 2015
@@ -798,17 +798,23 @@ FileSpec::GetPath(char *path, size_t pat
 }
 
 std::string
-FileSpec::GetPath (bool denormalize) const
+FileSpec::GetPath(bool denormalize) const
 {
     llvm::SmallString<64> result;
+    GetPath(result, denormalize);
+    return std::string(result.begin(), result.end());
+}
+
+void
+FileSpec::GetPath(llvm::SmallVectorImpl<char> &path, bool denormalize) const
+{
     if (m_directory)
-        result.append(m_directory.GetCString());
+        path.append(m_directory.GetCString(), m_directory.GetCString() + m_directory.GetLength());
     if (m_filename)
-        llvm::sys::path::append(result, m_filename.GetCString());
-    if (denormalize && !result.empty())
-        DeNormalize(result, m_syntax);
-
-    return std::string(result.begin(), result.end());
+        llvm::sys::path::append(path, m_filename.GetCString());
+    Normalize(path, m_syntax);
+    if (denormalize && !path.empty())
+        DeNormalize(path, m_syntax);
 }
 
 ConstString

Modified: lldb/trunk/source/Host/windows/HostInfoWindows.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/windows/HostInfoWindows.cpp?rev=234516&r1=234515&r2=234516&view=diff
==============================================================================
--- lldb/trunk/source/Host/windows/HostInfoWindows.cpp (original)
+++ lldb/trunk/source/Host/windows/HostInfoWindows.cpp Thu Apr  9 13:08:50 2015
@@ -13,7 +13,9 @@
 
 #include "lldb/Host/windows/HostInfoWindows.h"
 #include "llvm/ADT/SmallString.h"
+#include "llvm/Support/FileSystem.h"
 #include "llvm/Support/raw_ostream.h"
+#include "llvm/Support/Path.h"
 
 using namespace lldb_private;
 
@@ -107,11 +109,11 @@ HostInfoWindows::ComputePythonDirectory(
     FileSpec lldb_file_spec;
     if (!GetLLDBPath(lldb::ePathTypeLLDBShlibDir, lldb_file_spec))
         return false;
-
-    char raw_path[PATH_MAX];
-    lldb_file_spec.AppendPathComponent("../lib/site-packages");
-    lldb_file_spec.GetPath(raw_path, sizeof(raw_path));
-
-    file_spec.GetDirectory().SetCString(raw_path);
+    llvm::SmallString<64> path;
+    lldb_file_spec.GetPath(path);
+    llvm::sys::path::remove_filename(path);
+    llvm::sys::path::append(path, "lib", "site-packages");
+    std::replace(path.begin(), path.end(), '\\', '/');
+    file_spec.GetDirectory().SetString(path.c_str());
     return true;
 }

Modified: lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp?rev=234516&r1=234515&r2=234516&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp (original)
+++ lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp Thu Apr  9 13:08:50 2015
@@ -3044,36 +3044,17 @@ ScriptInterpreterPython::InitializePriva
     // Update the path python uses to search for modules to include the current directory.
 
     PyRun_SimpleString ("import sys");
-    PyRun_SimpleString ("sys.path.append ('.')");
-
-    // Find the module that owns this code and use that path we get to
-    // set the sys.path appropriately.
+    AddToSysPath(AddLocation::End, ".");
 
     FileSpec file_spec;
-    char python_dir_path[PATH_MAX];
+    // Don't denormalize paths when calling file_spec.GetPath().  On platforms that use
+    // a backslash as the path separator, this will result in executing python code containing
+    // paths with unescaped backslashes.  But Python also accepts forward slashes, so to make
+    // life easier we just use that.
     if (HostInfo::GetLLDBPath(ePathTypePythonDir, file_spec))
-    {
-        std::string python_path("sys.path.insert(0,\"");
-        size_t orig_len = python_path.length();
-        if (file_spec.GetPath(python_dir_path, sizeof (python_dir_path)))
-        {
-            python_path.append (python_dir_path);
-            python_path.append ("\")");
-            PyRun_SimpleString (python_path.c_str());
-            python_path.resize (orig_len);
-        }
-
-        if (HostInfo::GetLLDBPath(ePathTypeLLDBShlibDir, file_spec))
-        {
-            if (file_spec.GetPath(python_dir_path, sizeof (python_dir_path)))
-            {
-                python_path.append (python_dir_path);
-                python_path.append ("\")");
-                PyRun_SimpleString (python_path.c_str());
-                python_path.resize (orig_len);
-            }
-        }
-    }
+        AddToSysPath(AddLocation::Beginning, file_spec.GetPath(false));
+    if (HostInfo::GetLLDBPath(ePathTypeLLDBShlibDir, file_spec))
+        AddToSysPath(AddLocation::Beginning, file_spec.GetPath(false));
 
     PyRun_SimpleString ("sys.dont_write_bytecode = 1; import lldb.embedded_interpreter; from lldb.embedded_interpreter import run_python_interpreter; from lldb.embedded_interpreter import run_one_line");
 
@@ -3089,6 +3070,28 @@ ScriptInterpreterPython::InitializePriva
     stdin_tty_state.Restore();
 }
 
+void
+ScriptInterpreterPython::AddToSysPath(AddLocation location, std::string path)
+{
+    std::string path_copy;
+
+    std::string statement;
+    if (location == AddLocation::Beginning)
+    {
+        statement.assign("sys.path.insert(0,\"");
+        statement.append (path);
+        statement.append ("\")");
+    }
+    else
+    {
+        statement.assign("sys.path.append(\"");
+        statement.append(path);
+        statement.append("\")");
+    }
+    PyRun_SimpleString (statement.c_str());
+}
+
+
 //void
 //ScriptInterpreterPython::Terminate ()
 //{





More information about the lldb-commits mailing list