[Lldb-commits] [lldb] r131494 - in /lldb/trunk: scripts/Python/modify-python-lldb.py test/python_api/lldbutil/frame/TestFrameUtils.py test/python_api/lldbutil/iter/TestLLDBIterator.py test/python_api/lldbutil/iter/TestRegistersIterator.py test/python_api/lldbutil/process/TestPrintStackTraces.py

Johnny Chen johnny.chen at apple.com
Tue May 17 15:14:39 PDT 2011


Author: johnny
Date: Tue May 17 17:14:39 2011
New Revision: 131494

URL: http://llvm.org/viewvc/llvm-project?rev=131494&view=rev
Log:
Add truth value testing to those lldb Python objects with the IsValid() method definitions.
object.__nonzero__(self) is called to implement truth value testing and the built-in operation bool(),
via a simple delegation to self.IsValid().

Change tests under python_api/lldbutil to utilize this mechanism.

Modified:
    lldb/trunk/scripts/Python/modify-python-lldb.py
    lldb/trunk/test/python_api/lldbutil/frame/TestFrameUtils.py
    lldb/trunk/test/python_api/lldbutil/iter/TestLLDBIterator.py
    lldb/trunk/test/python_api/lldbutil/iter/TestRegistersIterator.py
    lldb/trunk/test/python_api/lldbutil/process/TestPrintStackTraces.py

Modified: lldb/trunk/scripts/Python/modify-python-lldb.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/scripts/Python/modify-python-lldb.py?rev=131494&r1=131493&r2=131494&view=diff
==============================================================================
--- lldb/trunk/scripts/Python/modify-python-lldb.py (original)
+++ lldb/trunk/scripts/Python/modify-python-lldb.py Tue May 17 17:14:39 2011
@@ -34,22 +34,24 @@
 
 '''
 
-#
 # This supports the iteration protocol.
-#
 iter_def = "    def __iter__(self): return lldb_iter(self, '%s', '%s')"
 module_iter = "    def module_iter(self): return lldb_iter(self, '%s', '%s')"
 breakpoint_iter = "    def breakpoint_iter(self): return lldb_iter(self, '%s', '%s')"
-#
+
 # Called to implement the built-in function len().
 # Eligible objects are those containers with unambiguous iteration support.
-#
 len_def = "    def __len__(self): return self.%s()"
-#
+
 # This supports the rich comparison methods of __eq__ and __ne__.
 eq_def = "    def __eq__(self, other): return isinstance(other, %s) and %s"
 ne_def = "    def __ne__(self, other): return not self.__eq__(other)"
 
+# Called to implement truth value testing and the built-in operation bool();
+# should return False or True, or their integer equivalents 0 or 1.
+# Delegate to self.IsValid() if it is defined for the current lldb object.
+nonzero_def = "    def __nonzero__(self): return self.IsValid()"
+
 #
 # The dictionary defines a mapping from classname to (getsize, getelem) tuple.
 #
@@ -109,6 +111,9 @@
 # The pattern for recognizing the beginning of the __init__ method definition.
 init_pattern = re.compile("^    def __init__\(self, \*args\):")
 
+# The pattern for recognizing the beginning of the IsValid method definition.
+isvalid_pattern = re.compile("^    def IsValid\(\*args\):")
+
 # These define the states of our finite state machine.
 NORMAL = 0
 DEFINING_ITERATOR = 1
@@ -117,13 +122,32 @@
 # The lldb_iter_def only needs to be inserted once.
 lldb_iter_defined = False;
 
+# Our FSM begins its life in the NORMAL state, and transitions to the
+# DEFINING_ITERATOR and/or DEFINING_EQUALITY state whenever it encounters the
+# beginning of certain class definitions, see dictionaries 'd' and 'e' above.
+#
+# Note that the two states DEFINING_ITERATOR and DEFINING_EQUALITY are
+# orthogonal in that our FSM can be in one, the other, or both states at the
+# same time.  During such time, the FSM is eagerly searching for the __init__
+# method definition in order to insert the appropriate method(s) into the lldb
+# module.
+#
+# Assuming that SWIG puts out the __init__ method definition before other method
+# definitions, the FSM, while in NORMAL state, also checks the current input for
+# IsValid() definition, and inserts a __nonzero__() method definition to
+# implement truth value testing and the built-in operation bool().
 state = NORMAL
 for line in content.splitlines():
     if state == NORMAL:
         match = class_pattern.search(line)
+        # Inserts the lldb_iter() definition before the first class definition.
         if not lldb_iter_defined and match:
             print >> new_content, lldb_iter_def
             lldb_iter_defined = True
+
+        # If we are at the beginning of the class definitions, prepare to
+        # transition to the DEFINING_ITERATOR/DEFINING_EQUALITY state for the
+        # right class names.
         if match:
             cls = match.group(1)
             if cls in d:
@@ -132,6 +156,10 @@
             if cls in e:
                 # Adding support for eq and ne for the matched SB class.
                 state = (state | DEFINING_EQUALITY)
+        # Look for 'def IsValid(*args):', and once located, add implementation
+        # of truth value testing for objects by delegation.
+        elif isvalid_pattern.search(line):
+            print >> new_content, nonzero_def
     elif state > NORMAL:
         match = init_pattern.search(line)
         if match:

Modified: lldb/trunk/test/python_api/lldbutil/frame/TestFrameUtils.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/python_api/lldbutil/frame/TestFrameUtils.py?rev=131494&r1=131493&r2=131494&view=diff
==============================================================================
--- lldb/trunk/test/python_api/lldbutil/frame/TestFrameUtils.py (original)
+++ lldb/trunk/test/python_api/lldbutil/frame/TestFrameUtils.py Tue May 17 17:14:39 2011
@@ -27,15 +27,15 @@
         exe = os.path.join(os.getcwd(), "a.out")
 
         target = self.dbg.CreateTarget(exe)
-        self.assertTrue(target.IsValid(), VALID_TARGET)
+        self.assertTrue(target, VALID_TARGET)
 
         breakpoint = target.BreakpointCreateByLocation("main.c", self.line)
-        self.assertTrue(breakpoint.IsValid(), VALID_BREAKPOINT)
+        self.assertTrue(breakpoint, VALID_BREAKPOINT)
 
         # Now launch the process, and do not stop at entry point.
         self.process = target.LaunchSimple(None, None, os.getcwd())
 
-        if not self.process.IsValid():
+        if not self.process:
             self.fail("SBTarget.LaunchProcess() failed")
         self.assertTrue(self.process.GetState() == lldb.eStateStopped,
                         PROCESS_STOPPED)
@@ -45,7 +45,7 @@
         frame0 = thread.GetFrameAtIndex(0)
         frame1 = thread.GetFrameAtIndex(1)
         parent = lldbutil.get_parent_frame(frame0)
-        self.assertTrue(parent.IsValid() and parent.GetFrameID() == frame1.GetFrameID())
+        self.assertTrue(parent and parent.GetFrameID() == frame1.GetFrameID())
         frame0_args = lldbutil.get_args_as_string(frame0)
         parent_args = lldbutil.get_args_as_string(parent)
         self.assertTrue(frame0_args and parent_args)

Modified: lldb/trunk/test/python_api/lldbutil/iter/TestLLDBIterator.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/python_api/lldbutil/iter/TestLLDBIterator.py?rev=131494&r1=131493&r2=131494&view=diff
==============================================================================
--- lldb/trunk/test/python_api/lldbutil/iter/TestLLDBIterator.py (original)
+++ lldb/trunk/test/python_api/lldbutil/iter/TestLLDBIterator.py Tue May 17 17:14:39 2011
@@ -38,16 +38,16 @@
         exe = os.path.join(os.getcwd(), "a.out")
 
         target = self.dbg.CreateTarget(exe)
-        self.assertTrue(target.IsValid(), VALID_TARGET)
+        self.assertTrue(target, VALID_TARGET)
 
         breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line1)
-        self.assertTrue(breakpoint.IsValid(), VALID_BREAKPOINT)
+        self.assertTrue(breakpoint, VALID_BREAKPOINT)
 
         # Now launch the process, and do not stop at entry point.
         rc = lldb.SBError()
         self.process = target.Launch (self.dbg.GetListener(), None, None, os.ctermid(), os.ctermid(), os.ctermid(), None, 0, False, rc)
 
-        if not rc.Success() or not self.process.IsValid():
+        if not rc.Success() or not self.process:
             self.fail("SBTarget.LaunchProcess() failed")
 
         from lldbutil import get_description
@@ -70,12 +70,12 @@
         exe = os.path.join(os.getcwd(), "a.out")
 
         target = self.dbg.CreateTarget(exe)
-        self.assertTrue(target.IsValid(), VALID_TARGET)
+        self.assertTrue(target, VALID_TARGET)
 
         breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line1)
-        self.assertTrue(breakpoint.IsValid(), VALID_BREAKPOINT)
+        self.assertTrue(breakpoint, VALID_BREAKPOINT)
         breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line2)
-        self.assertTrue(breakpoint.IsValid(), VALID_BREAKPOINT)
+        self.assertTrue(breakpoint, VALID_BREAKPOINT)
 
         self.assertTrue(target.GetNumBreakpoints() == 2)
 
@@ -99,16 +99,16 @@
         exe = os.path.join(os.getcwd(), "a.out")
 
         target = self.dbg.CreateTarget(exe)
-        self.assertTrue(target.IsValid(), VALID_TARGET)
+        self.assertTrue(target, VALID_TARGET)
 
         breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line1)
-        self.assertTrue(breakpoint.IsValid(), VALID_BREAKPOINT)
+        self.assertTrue(breakpoint, VALID_BREAKPOINT)
 
         # Now launch the process, and do not stop at entry point.
         rc = lldb.SBError()
         self.process = target.Launch (self.dbg.GetListener(), None, None, os.ctermid(), os.ctermid(), os.ctermid(), None, 0, False, rc)
 
-        if not rc.Success() or not self.process.IsValid():
+        if not rc.Success() or not self.process:
             self.fail("SBTarget.LaunchProcess() failed")
 
         from lldbutil import print_stacktrace

Modified: lldb/trunk/test/python_api/lldbutil/iter/TestRegistersIterator.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/python_api/lldbutil/iter/TestRegistersIterator.py?rev=131494&r1=131493&r2=131494&view=diff
==============================================================================
--- lldb/trunk/test/python_api/lldbutil/iter/TestRegistersIterator.py (original)
+++ lldb/trunk/test/python_api/lldbutil/iter/TestRegistersIterator.py Tue May 17 17:14:39 2011
@@ -27,16 +27,16 @@
         exe = os.path.join(os.getcwd(), "a.out")
 
         target = self.dbg.CreateTarget(exe)
-        self.assertTrue(target.IsValid(), VALID_TARGET)
+        self.assertTrue(target, VALID_TARGET)
 
         breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line1)
-        self.assertTrue(breakpoint.IsValid(), VALID_BREAKPOINT)
+        self.assertTrue(breakpoint, VALID_BREAKPOINT)
 
         # Now launch the process, and do not stop at entry point.
         rc = lldb.SBError()
         self.process = target.Launch (self.dbg.GetListener(), None, None, os.ctermid(), os.ctermid(), os.ctermid(), None, 0, False, rc)
 
-        if not rc.Success() or not self.process.IsValid():
+        if not rc.Success() or not self.process:
             self.fail("SBTarget.LaunchProcess() failed")
 
         import lldbutil
@@ -52,7 +52,7 @@
                     if self.TraceOn():
                         print "\nNumber of general purpose registers: %d" % num
                     for reg in REGs:
-                        self.assertTrue(reg.IsValid())
+                        self.assertTrue(reg)
                         if self.TraceOn():
                             print "%s => %s" % (reg.GetName(), reg.GetValue(frame))
 
@@ -61,7 +61,7 @@
                     if self.TraceOn():
                         print "\nNumber of floating point registers: %d" % num
                     for reg in REGs:
-                        self.assertTrue(reg.IsValid())
+                        self.assertTrue(reg)
                         if self.TraceOn():
                             print "%s => %s" % (reg.GetName(), reg.GetValue(frame))
 
@@ -70,7 +70,7 @@
                     if self.TraceOn():
                         print "\nNumber of exception state registers: %d" % num
                     for reg in REGs:
-                        self.assertTrue(reg.IsValid())
+                        self.assertTrue(reg)
                         if self.TraceOn():
                             print "%s => %s" % (reg.GetName(), reg.GetValue(frame))
 
@@ -79,7 +79,7 @@
                                  "Floating Point Registers",
                                  "Exception State Registers"]:
                         REGs = lldbutil.get_registers(frame, kind)
-                        self.assertTrue(REGs.IsValid())
+                        self.assertTrue(REGs)
 
                     # We've finished dumping the registers for frame #0.
                     break

Modified: lldb/trunk/test/python_api/lldbutil/process/TestPrintStackTraces.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/python_api/lldbutil/process/TestPrintStackTraces.py?rev=131494&r1=131493&r2=131494&view=diff
==============================================================================
--- lldb/trunk/test/python_api/lldbutil/process/TestPrintStackTraces.py (original)
+++ lldb/trunk/test/python_api/lldbutil/process/TestPrintStackTraces.py Tue May 17 17:14:39 2011
@@ -28,16 +28,16 @@
         exe = os.path.join(os.getcwd(), "a.out")
 
         target = self.dbg.CreateTarget(exe)
-        self.assertTrue(target.IsValid(), VALID_TARGET)
+        self.assertTrue(target, VALID_TARGET)
 
         breakpoint = target.BreakpointCreateByLocation("main.cpp", self.line)
-        self.assertTrue(breakpoint.IsValid(), VALID_BREAKPOINT)
+        self.assertTrue(breakpoint, VALID_BREAKPOINT)
 
         # Now launch the process, and do not stop at entry point.
         rc = lldb.SBError()
         self.process = target.Launch (self.dbg.GetListener(), None, None, os.ctermid(), os.ctermid(), os.ctermid(), None, 0, False, rc)
 
-        if not rc.Success() or not self.process.IsValid():
+        if not rc.Success() or not self.process:
             self.fail("SBTarget.LaunchProcess() failed")
 
         import lldbutil





More information about the lldb-commits mailing list