[Lldb-commits] [lldb] r177877 - - Masking out SBCommandReturnObject::Printf() from the Python layer because SWIG and varargs do not get along well.

Enrico Granata egranata at apple.com
Mon Mar 25 10:37:39 PDT 2013


Author: enrico
Date: Mon Mar 25 12:37:39 2013
New Revision: 177877

URL: http://llvm.org/viewvc/llvm-project?rev=177877&view=rev
Log:
- Masking out SBCommandReturnObject::Printf() from the Python layer because SWIG and varargs do not get along well.
It is replaced by a Print("str") call which is equivalent to Printf("%s","str")
- Providing file-like behavior for SBStream with appropriate extension write() and flush() calls, plus documenting that these are only meant and only exist for Python
Documenting the file-like behavior on our website


Modified:
    lldb/trunk/scripts/Python/interface/SBCommandReturnObject.i
    lldb/trunk/scripts/Python/python-extensions.swig
    lldb/trunk/www/python-reference.html

Modified: lldb/trunk/scripts/Python/interface/SBCommandReturnObject.i
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/Python/interface/SBCommandReturnObject.i?rev=177877&r1=177876&r2=177877&view=diff
==============================================================================
--- lldb/trunk/scripts/Python/interface/SBCommandReturnObject.i (original)
+++ lldb/trunk/scripts/Python/interface/SBCommandReturnObject.i Mon Mar 25 12:37:39 2013
@@ -86,8 +86,14 @@ public:
 	void
 	PutCString(const char* string, int len = -1);
 
-	size_t
-	Printf(const char* format, ...);
+    // wrapping the variadic Printf() with a plain Print()
+    // because it is hard to support varargs in SWIG bridgings
+    %extend {
+        void Print (const char* str)
+        {
+            self->Printf("%s", str);
+        }
+    }
 
 };
 

Modified: lldb/trunk/scripts/Python/python-extensions.swig
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/Python/python-extensions.swig?rev=177877&r1=177876&r2=177877&view=diff
==============================================================================
--- lldb/trunk/scripts/Python/python-extensions.swig (original)
+++ lldb/trunk/scripts/Python/python-extensions.swig Mon Mar 25 12:37:39 2013
@@ -68,6 +68,12 @@
                 else
                     return PyString_FromString("");
         }
+        
+        /* the write() and flush() calls are not part of the SB API proper, and are solely for Python usage
+        they are meant to make an SBCommandReturnObject into a file-like object so that instructions of the sort
+        print >>sb_command_return_object, "something"
+        will work correctly */
+
         void lldb::SBCommandReturnObject::write (const char* str)
         {
             if (str)
@@ -272,6 +278,20 @@
                     return PyString_FromString("");
         }
 }
+%extend lldb::SBStream {
+        /* the write() and flush() calls are not part of the SB API proper, and are solely for Python usage
+        they are meant to make an SBStream into a file-like object so that instructions of the sort
+        print >>sb_stream, "something"
+        will work correctly */
+
+        void lldb::SBStream::write (const char* str)
+        {
+            if (str)
+                $self->Printf("%s",str);
+        }
+        void lldb::SBStream::flush ()
+        {}
+}
 %extend lldb::SBSymbol {
         PyObject *lldb::SBSymbol::__str__ (){
                 lldb::SBStream description;

Modified: lldb/trunk/www/python-reference.html
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/www/python-reference.html?rev=177877&r1=177876&r2=177877&view=diff
==============================================================================
--- lldb/trunk/www/python-reference.html (original)
+++ lldb/trunk/www/python-reference.html Mon Mar 25 12:37:39 2013
@@ -368,10 +368,9 @@ Enter your Python command(s). Type 'DONE
                 <b>lldb.SBCommandReturnObject</b>
             </td>
             <td class="content">
-                A return object where you can indicate the success or failure of your command. You can also
-                provide information for the command result by printing data into it. You can also just print
-                data as you normally would in a python script and the output will show up; this is useful for
-                logging, but the real output for your command should go in the result object.
+                A return object which encapsulates success/failure information for the command and output text
+                that needs to be printed as a result of the command. The plain Python "print" command also works but
+                text won't go in the result by default (it is useful as a temporary logging facility).
             </td>
         </tr>
         <tr>
@@ -387,6 +386,9 @@ Enter your Python command(s). Type 'DONE
             </td>
         </tr>
         </table>
+        <p>As a convenience, you can treat the result object as a Python file object, and say
+        print >>result, "my command does lots of cool stuff". SBCommandReturnObject and SBStream
+        both support this file-like behavior by providing write() and flush() calls at the Python layer.</p>
         <p>One other handy convenience when defining lldb command-line commands is the command
           <b>command script import</b> which will import a module specified by file path - so you
           don't have to change your PYTHONPATH for temporary scripts.  It also has another convenience





More information about the lldb-commits mailing list