[Lldb-commits] [lldb] r180975 - <rdar://problem/11558812>

Enrico Granata egranata at apple.com
Thu May 2 16:57:33 PDT 2013


Author: enrico
Date: Thu May  2 18:57:33 2013
New Revision: 180975

URL: http://llvm.org/viewvc/llvm-project?rev=180975&view=rev
Log:
<rdar://problem/11558812>

Allow command script import to load packages.

e.g.:
egranata$ ./lldb
(lldb) command script import lldb.macosx.crashlog
"crashlog" and "save_crashlog" command installed, use the "--help" option for detailed help
"malloc_info", "ptr_refs", "cstr_refs", and "objc_refs" commands have been installed, use the "--help" options on these commands for detailed help.
The "unwind-diagnose" command has been installed, type "help unwind-diagnose" for detailed help.
(lldb) 

./lldb
(lldb) command script import theFoo
I am happy
(lldb) fbc
àèìòù
(lldb)

egranata$ ls theFoo/
__init__.py theBar.py

egranata$ cat theFoo/__init__.py 
import lldb
import theBar

def __lldb_init_module(debugger, internal_dict):
	print "I am happy"
	debugger.HandleCommand("command script add -f theFoo.theBar.theCommand fbc")
	return None

egranata$ cat theFoo/theBar.py 
#encoding=utf-8

def theCommand(debugger, command, result, internal_dict):
	result.PutCString(u"àèìòù")
	return None


Modified:
    lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp

Modified: lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp?rev=180975&r1=180974&r2=180975&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp (original)
+++ lldb/trunk/source/Interpreter/ScriptInterpreterPython.cpp Thu May  2 18:57:33 2013
@@ -2594,42 +2594,47 @@ ScriptInterpreterPython::LoadScriptingMo
     {
         FileSpec target_file(pathname, true);
         
-        // TODO: would we want to reject any other value?
-        if (target_file.GetFileType() == FileSpec::eFileTypeInvalid ||
-            target_file.GetFileType() == FileSpec::eFileTypeUnknown)
-        {
-            error.SetErrorString("invalid pathname");
-            return false;
-        }
-        
-        const char* directory = target_file.GetDirectory().GetCString();
-        std::string basename(target_file.GetFilename().GetCString());
+        std::string basename;
+        StreamString command_stream;
 
         // Before executing Pyton code, lock the GIL.
         Locker py_lock (this,
                         Locker::AcquireLock      | (init_session ? Locker::InitSession     : 0),
                         Locker::FreeAcquiredLock | (init_session ? Locker::TearDownSession : 0));
         
-        // 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",
-                              directory,
-                              directory);
-        bool syspath_retval = ExecuteMultipleLines(command_stream.GetData(), ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false).SetSetLLDBGlobals(false));
-        if (!syspath_retval)
+        if (target_file.GetFileType() == FileSpec::eFileTypeInvalid ||
+            target_file.GetFileType() == FileSpec::eFileTypeUnknown ||
+            target_file.GetFileType() == FileSpec::eFileTypeDirectory )
         {
-            error.SetErrorString("Python sys.path handling failed");
-            return false;
+            // if not a filename, try to just plain import
+            basename = pathname;
         }
-        
-        // strip .py or .pyc extension
-        ConstString extension = target_file.GetFileNameExtension();
-        if (extension)
+        else
         {
-            if (::strcmp(extension.GetCString(), "py") == 0)
-                basename.resize(basename.length()-3);
-            else if(::strcmp(extension.GetCString(), "pyc") == 0)
-                basename.resize(basename.length()-4);
+            const char* directory = target_file.GetDirectory().GetCString();
+            std::string basename(target_file.GetFilename().GetCString());
+            
+            // 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",
+                                  directory,
+                                  directory);
+            bool syspath_retval = ExecuteMultipleLines(command_stream.GetData(), ScriptInterpreter::ExecuteScriptOptions().SetEnableIO(false).SetSetLLDBGlobals(false));
+            if (!syspath_retval)
+            {
+                error.SetErrorString("Python sys.path handling failed");
+                return false;
+            }
+            
+            // strip .py or .pyc extension
+            ConstString extension = target_file.GetFileNameExtension();
+            if (extension)
+            {
+                if (::strcmp(extension.GetCString(), "py") == 0)
+                    basename.resize(basename.length()-3);
+                else if(::strcmp(extension.GetCString(), "pyc") == 0)
+                    basename.resize(basename.length()-4);
+            }
         }
         
         // check if the module is already import-ed





More information about the lldb-commits mailing list