From 4e8ddf53337ba922e85681400c16d51ef43ccd0a Mon Sep 17 00:00:00 2001 From: Zachary Turner Date: Thu, 9 Apr 2015 18:08:50 +0000 Subject: [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. llvm-svn: 234516 --- .../source/Interpreter/ScriptInterpreterPython.cpp | 57 ++++++++++++---------- 1 file changed, 30 insertions(+), 27 deletions(-) (limited to 'lldb/source/Interpreter/ScriptInterpreterPython.cpp') diff --git a/lldb/source/Interpreter/ScriptInterpreterPython.cpp b/lldb/source/Interpreter/ScriptInterpreterPython.cpp index f8532e7e8c09..099cbaa0cedd 100644 --- a/lldb/source/Interpreter/ScriptInterpreterPython.cpp +++ b/lldb/source/Interpreter/ScriptInterpreterPython.cpp @@ -3044,36 +3044,17 @@ ScriptInterpreterPython::InitializePrivate () // 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::InitializePrivate () 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 () //{ -- cgit v1.2.3