From f9f3609704c1f5cdcdc8ef04dfdf5234217d6686 Mon Sep 17 00:00:00 2001 From: Adrian McCarthy Date: Thu, 7 Apr 2016 22:52:12 +0000 Subject: [PATCH] Fix TestImport for Windows by ensuring backslashes in the directory paths are properly escaped in Python. The Python import works by ensuring the directory of the module or package is in sys.path, and then it does a Python `import foo`. The original code was not escaping the backslashes in the directory path, so this wasn't working. Differential Revision: http://reviews.llvm.org/D18873 llvm-svn: 265738 --- .../ScriptInterpreter/Python/ScriptInterpreterPython.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp index 4ca7cd0..0af1fd5 100644 --- a/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp +++ b/lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp @@ -2548,7 +2548,7 @@ ScriptInterpreterPython::LoadScriptingModule(const char *pathname, bool can_relo StreamString command_stream; - // Before executing Pyton code, lock the GIL. + // Before executing Python code, lock the GIL. Locker py_lock (this, Locker::AcquireLock | (init_session ? Locker::InitSession : 0) | Locker::NoSTDIN, Locker::FreeAcquiredLock | (init_session ? Locker::TearDownSession : 0)); @@ -2569,9 +2569,10 @@ ScriptInterpreterPython::LoadScriptingModule(const char *pathname, bool can_relo target_file.GetFileType() == FileSpec::eFileTypeRegular || target_file.GetFileType() == FileSpec::eFileTypeSymbolicLink) { - std::string directory(target_file.GetDirectory().GetCString()); - replace_all(directory,"'","\\'"); - + std::string directory = target_file.GetDirectory().GetCString(); + replace_all(directory, "\\", "\\\\"); + replace_all(directory, "'", "\\'"); + // now make sure that Python has "directory" in the search path StreamString command_stream; command_stream.Printf("if not (sys.path.__contains__('%s')):\n sys.path.insert(1,'%s');\n\n", @@ -2583,7 +2584,7 @@ ScriptInterpreterPython::LoadScriptingModule(const char *pathname, bool can_relo error.SetErrorString("Python sys.path handling failed"); return false; } - + // strip .py or .pyc extension ConstString extension = target_file.GetFileNameExtension(); if (extension) @@ -2634,8 +2635,8 @@ ScriptInterpreterPython::LoadScriptingModule(const char *pathname, bool can_relo command_stream.Printf("reload_module(%s)",basename.c_str()); } else - command_stream.Printf("import %s",basename.c_str()); - + command_stream.Printf("import %s", basename.c_str()); + error = ExecuteMultipleLines(command_stream.GetData(), ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false).SetSetLLDBGlobals(false)); if (error.Fail()) return false; -- 2.7.4