[Zodb-checkins] SVN: ZODB/branches/gocept-iteration/src/ZODB/ - removed Iterator class, made iterators compatible with Python iterators

Thomas Lotze tl at gocept.com
Thu Feb 14 04:33:59 EST 2008


Log message for revision 83820:
  - removed Iterator class, made iterators compatible with Python iterators
  - made MappingStorage implement no-op for iterator()
  

Changed:
  U   ZODB/branches/gocept-iteration/src/ZODB/FileStorage/FileStorage.py
  U   ZODB/branches/gocept-iteration/src/ZODB/MappingStorage.py
  U   ZODB/branches/gocept-iteration/src/ZODB/tests/IteratorStorage.py
  U   ZODB/branches/gocept-iteration/src/ZODB/tests/PackableStorage.py
  U   ZODB/branches/gocept-iteration/src/ZODB/tests/TransactionalUndoStorage.py

-=-
Modified: ZODB/branches/gocept-iteration/src/ZODB/FileStorage/FileStorage.py
===================================================================
--- ZODB/branches/gocept-iteration/src/ZODB/FileStorage/FileStorage.py	2008-02-14 09:33:06 UTC (rev 83819)
+++ ZODB/branches/gocept-iteration/src/ZODB/FileStorage/FileStorage.py	2008-02-14 09:33:58 UTC (rev 83820)
@@ -1516,26 +1516,8 @@
     file.seek(pos)
     file.truncate()
 
-class Iterator:
-    """A General simple iterator that uses the Python for-loop index protocol
-    """
-    __index=-1
-    __current=None
 
-    def __getitem__(self, i):
-        __index=self.__index
-        while i > __index:
-            __index=__index+1
-            try:
-                self.__current=self.next()
-            except StopIteration:
-                raise IndexError(i)
-
-        self.__index=__index
-        return self.__current
-
-
-class FileIterator(Iterator, FileStorageFormatter):
+class FileIterator(FileStorageFormatter):
     """Iterate over the transactions in a FileStorage file.
     """
     _ltid = z64
@@ -1571,6 +1553,9 @@
     def iterator(self):
         return self
 
+    def __iter__(self):
+        return self
+
     def close(self):
         file = self._file
         if file is not None:
@@ -1699,9 +1684,9 @@
         raise StopIteration
 
 
-class RecordIterator(Iterator, BaseStorage.TransactionRecord,
-                     FileStorageFormatter):
+class RecordIterator(BaseStorage.TransactionRecord, FileStorageFormatter):
     """Iterate over the transactions in a FileStorage file."""
+
     def __init__(self, tid, status, user, desc, ext, pos, tend, file, tpos):
         BaseStorage.TransactionRecord.__init__(
             self, tid, status, user, desc, ext)
@@ -1711,7 +1696,10 @@
         self._file = file
         self._tpos = tpos
 
-    def next(self, index=0):
+    def __iter__(self):
+        return self
+
+    def next(self):
         pos = self._pos
         while pos < self._tend:
             # Read the data records for this transaction
@@ -1743,7 +1731,7 @@
             r = Record(h.oid, h.tid, data, prev_txn, pos)
             return r
 
-        raise IndexError(index)
+        raise StopIteration
 
 
 class Record(BaseStorage.DataRecord):

Modified: ZODB/branches/gocept-iteration/src/ZODB/MappingStorage.py
===================================================================
--- ZODB/branches/gocept-iteration/src/ZODB/MappingStorage.py	2008-02-14 09:33:06 UTC (rev 83819)
+++ ZODB/branches/gocept-iteration/src/ZODB/MappingStorage.py	2008-02-14 09:33:58 UTC (rev 83820)
@@ -140,3 +140,6 @@
 
     def close(self):
         pass
+
+    def iterator(self, start=None, stop=None):
+        return iter(())

Modified: ZODB/branches/gocept-iteration/src/ZODB/tests/IteratorStorage.py
===================================================================
--- ZODB/branches/gocept-iteration/src/ZODB/tests/IteratorStorage.py	2008-02-14 09:33:06 UTC (rev 83819)
+++ ZODB/branches/gocept-iteration/src/ZODB/tests/IteratorStorage.py	2008-02-14 09:33:58 UTC (rev 83820)
@@ -56,7 +56,7 @@
         revid1 = self._dostore(oid, data=MinPO(11))
         txniter = self._storage.iterator()
         txniter.close()
-        self.assertRaises(IOError, txniter.__getitem__, 0)
+        self.assertRaises(IOError, txniter.next)
 
     def checkUndoZombie(self):
         oid = self._storage.new_oid()
@@ -190,11 +190,11 @@
                 eq(rec1.data,    rec2.data)
             # Make sure there are no more records left in rec1 and rec2,
             # meaning they were the same length.
-            self.assertRaises(IndexError, txn1.next)
-            self.assertRaises(IndexError, txn2.next)
+            self.assertRaises(StopIteration, txn1.next)
+            self.assertRaises(StopIteration, txn2.next)
         # Make sure ther are no more records left in txn1 and txn2, meaning
         # they were the same length
-        self.assertRaises(IndexError, iter1.next)
-        self.assertRaises(IndexError, iter2.next)
+        self.assertRaises(StopIteration, iter1.next)
+        self.assertRaises(StopIteration, iter2.next)
         iter1.close()
         iter2.close()

Modified: ZODB/branches/gocept-iteration/src/ZODB/tests/PackableStorage.py
===================================================================
--- ZODB/branches/gocept-iteration/src/ZODB/tests/PackableStorage.py	2008-02-14 09:33:06 UTC (rev 83819)
+++ ZODB/branches/gocept-iteration/src/ZODB/tests/PackableStorage.py	2008-02-14 09:33:58 UTC (rev 83820)
@@ -147,6 +147,18 @@
             self._storage.tpc_vote(t)
             self._storage.tpc_finish(t)
 
+    def _sanity_check(self):
+        # Iterate over the storage to make sure it's sane.
+        if not hasattr(self._storage, "iterator"):
+            return
+        it = self._storage.iterator()
+        for txn in it:
+            for data in txn:
+                pass
+        # XXX see bug #191573
+        if hasattr(it, "close"):
+            it.close()
+
 class PackableStorage(PackableStorageBase):
 
     def checkPackEmptyStorage(self):
@@ -251,17 +263,8 @@
 
             self.fail('a thread is still alive')
 
-        # Iterate over the storage to make sure it's sane, but not every
-        # storage supports iterators.
-        if not hasattr(self._storage, "iterator"):
-            return
+        self._sanity_check()
 
-        it = self._storage.iterator()
-        for txn in it:
-            for data in txn:
-                pass
-        it.close()
-
     def checkPackWhileWriting(self):
         self._PackWhileWriting(pack_now=False)
 
@@ -302,18 +305,8 @@
             packt = time.time()
         thread.join()
 
-        # Iterate over the storage to make sure it's sane.
-        if not hasattr(self._storage, "iterator"):
-            return
-        it = self._storage.iterator()
-        for txn in it:
-            for data in txn:
-                pass
-        # XXX see bug #191573
-        if hasattr(it, "close"):
-            it.close()
+        self._sanity_check()
 
-
 class PackableUndoStorage(PackableStorageBase):
 
     def checkPackAllRevisions(self):

Modified: ZODB/branches/gocept-iteration/src/ZODB/tests/TransactionalUndoStorage.py
===================================================================
--- ZODB/branches/gocept-iteration/src/ZODB/tests/TransactionalUndoStorage.py	2008-02-14 09:33:06 UTC (rev 83819)
+++ ZODB/branches/gocept-iteration/src/ZODB/tests/TransactionalUndoStorage.py	2008-02-14 09:33:58 UTC (rev 83820)
@@ -636,11 +636,13 @@
             for j in range(OBJECTS):
                 oid = s.new_oid()
                 obj = MinPO(i * OBJECTS + j)
-                revid = s.store(oid, None, zodb_pickle(obj), '', t)
-                orig.append((tid, oid, revid))
+                s.store(oid, None, zodb_pickle(obj), '', t)
+                orig.append((tid, oid))
             s.tpc_vote(t)
             s.tpc_finish(t)
 
+        orig = [(tid, oid, s.getTid(oid)) for tid, oid in orig]
+
         i = 0
         for tid, oid, revid in orig:
             self._dostore(oid, revid=revid, data=MinPO(revid),
@@ -669,13 +671,10 @@
         #     BATCHES undos
 
         iter = s.iterator()
-        offset = 0
-
         eq = self.assertEqual
 
         for i in range(BATCHES):
-            txn = iter[offset]
-            offset += 1
+            txn = iter.next()
 
             tid = p64(i + 1)
             eq(txn.tid, tid)
@@ -687,13 +686,11 @@
             eq(L1, L2)
 
         for i in range(BATCHES * OBJECTS):
-            txn = iter[offset]
-            offset += 1
+            txn = iter.next()
             eq(len([rec for rec in txn if rec.data_txn is None]), 1)
 
         for i in range(BATCHES):
-            txn = iter[offset]
-            offset += 1
+            txn = iter.next()
 
             # The undos are performed in reverse order.
             otid = p64(BATCHES - i)
@@ -704,7 +701,7 @@
             L2.sort()
             eq(L1, L2)
 
-        self.assertRaises(IndexError, iter.__getitem__, offset)
+        self.assertRaises(StopIteration, iter.next)
 
     def checkUndoLogMetadata(self):
         # test that the metadata is correct in the undo log



More information about the Zodb-checkins mailing list