← Back to team overview

dulwich-users team mailing list archive

[PATCH 04/33] pack: Inline PackObjectIterator.

 

From: Dave Borowitz <dborowitz@xxxxxxxxxx>

Change-Id: I9dde6181f7bd9ceed9017720400a8b6fb87ea44f
---
 NEWS            |    3 +++
 dulwich/pack.py |   40 ++++++++++------------------------------
 2 files changed, 13 insertions(+), 30 deletions(-)

diff --git a/NEWS b/NEWS
index a68f0f7..90b8291 100644
--- a/NEWS
+++ b/NEWS
@@ -29,6 +29,9 @@
     crc32 to compute, and each return an additional crc32 element in their
     return values. (Dave Borowitz)
 
+  * PackObjectIterator was removed; its functionality is still exposed by
+    PackData.iterobjects. (Dave Borowitz)
+
  TEST CHANGES
 
   * If setuptools is installed, "python setup.py test" will now run the testsuite.
diff --git a/dulwich/pack.py b/dulwich/pack.py
index 7b04065..a94caf3 100644
--- a/dulwich/pack.py
+++ b/dulwich/pack.py
@@ -742,34 +742,6 @@ class PackStreamReader(object):
             raise ChecksumMismatch(pack_sha, calculated_sha)
 
 
-class PackObjectIterator(object):
-
-    def __init__(self, pack, progress=None):
-        self.i = 0
-        self.offset = pack._header_size
-        self.num = len(pack)
-        self.map = pack._file
-        self._progress = progress
-
-    def __iter__(self):
-        return self
-
-    def __len__(self):
-        return self.num
-
-    def next(self):
-        if self.i == self.num:
-            raise StopIteration
-        self.map.seek(self.offset)  # Back up over unused data.
-        type_num, obj, total_size, crc32, unused = unpack_object(
-          self.map.read, compute_crc32=True)
-        ret = (self.offset, type_num, obj, crc32)
-        self.offset += total_size
-        if self._progress is not None:
-            self._progress(self.i, self.num)
-        self.i+=1
-        return ret
-
 def obj_sha(type, chunks):
     """Compute the SHA for a numeric type and object chunks."""
     sha = make_sha()
@@ -910,8 +882,16 @@ class PackData(object):
             self._offset_cache[offset] = type, chunks
         return type, chunks
 
-    def iterobjects(self, progress=None):
-        return PackObjectIterator(self, progress)
+    def iterobjects(self, progress=None, compute_crc32=True):
+        offset = self._header_size
+        for i in xrange(1, self._num_objects + 1):
+            self._file.seek(offset)  # Back up over unused data.
+            type_num, obj, total_size, crc32, unused = unpack_object(
+              self._file.read, compute_crc32=compute_crc32)
+            if progress is not None:
+                progress(i, self._num_objects)
+            yield offset, type_num, obj, crc32
+            offset += total_size
 
     def iterentries(self, progress=None):
         """Yield entries summarizing the contents of this pack.
-- 
1.7.3.1



References