Simplify the generator adaptor to a Python function instead of a class definition,
with the function name 'lldb_iter'. Example:
def disassemble_instructions (insts):
from lldbutil import lldb_iter
for i in lldb_iter(insts, 'GetSize', 'GetInstructionAtIndex'):
print i
git-svn-id: https://llvm.org/svn/llvm-project/llvdb/trunk@116171 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/test/lldbutil.py b/test/lldbutil.py
index c97cb52..90a0b40 100644
--- a/test/lldbutil.py
+++ b/test/lldbutil.py
@@ -6,28 +6,25 @@
import sys
import StringIO
-class Iterator(object):
+def lldb_iter(obj, getsize, getelem):
"""
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.
+ API clients pass in the aggregate object, the name of the method to get the
+ size of the object, and the name of the method to get the element given an
+ index.
Example usage:
def disassemble_instructions (insts):
- from lldbutil import Iterator
- for i in Iterator(insts, 'GetSize', 'GetInstructionAtIndex'):
+ from lldbutil import lldb_iter
+ for i in lldb_iter(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)
+ size = getattr(obj, getsize)
+ elem = getattr(obj, getelem)
+ for i in range(size()):
+ yield elem(i)
########################################################