[Lldb-commits] [lldb] r116137 - /lldb/trunk/test/lldbutil.py

Johnny Chen johnny.chen at apple.com
Fri Oct 8 18:31:09 PDT 2010


Author: johnny
Date: Fri Oct  8 20:31:09 2010
New Revision: 116137

URL: http://llvm.org/viewvc/llvm-project?rev=116137&view=rev
Log:
Add a generator adaptor class named 'Iterator' which turns lldb aggregate data
structures into an iterable Python object.

Example:

    def disassemble_instructions (insts):
        from lldbutil import Iterator
        for i in Iterator(insts, 'GetSize', 'GetInstructionAtIndex'):
            print i

Modified:
    lldb/trunk/test/lldbutil.py

Modified: lldb/trunk/test/lldbutil.py
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/test/lldbutil.py?rev=116137&r1=116136&r2=116137&view=diff
==============================================================================
--- lldb/trunk/test/lldbutil.py (original)
+++ lldb/trunk/test/lldbutil.py Fri Oct  8 20:31:09 2010
@@ -6,6 +6,30 @@
 import sys
 import StringIO
 
+class Iterator(object):
+    """
+    A generator adaptor for lldb aggregate data structures.
+
+    API clients pass in the aggregate object, and the names of the methods to
+    get the size of the object and its individual element.
+
+    Example usage:
+
+    def disassemble_instructions (insts):
+        from lldbutil import Iterator
+        for i in Iterator(insts, 'GetSize', 'GetInstructionAtIndex'):
+            print i
+    """
+    def __init__(self, obj, getsize, getelem):
+        self.obj = obj
+        self.getsize = getattr(obj, getsize)
+        self.getelem = getattr(obj, getelem)
+
+    def __iter__(self):
+        for i in range(self.getsize()):
+            yield self.getelem(i)
+
+
 ########################################################
 #                                                      #
 # Convert some enum value to its string's counterpart. #





More information about the lldb-commits mailing list