[Lldb-commits] [lldb] r265738 - Fix TestImport for Windows by ensuring backslashes in the directory paths are properly escaped in Python.

Adrian McCarthy via lldb-commits lldb-commits at lists.llvm.org
Thu Apr 7 15:52:13 PDT 2016


Author: amccarth
Date: Thu Apr  7 17:52:12 2016
New Revision: 265738

URL: http://llvm.org/viewvc/llvm-project?rev=265738&view=rev
Log:
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

Modified:
    lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp

Modified: lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp?rev=265738&r1=265737&r2=265738&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp (original)
+++ lldb/trunk/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp Thu Apr  7 17:52:12 2016
@@ -2548,7 +2548,7 @@ ScriptInterpreterPython::LoadScriptingMo
         
         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::LoadScriptingMo
                  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::LoadScriptingMo
                 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::LoadScriptingMo
                 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;




More information about the lldb-commits mailing list