Ver código fonte

pack: Inline PackObjectIterator.

Change-Id: I9dde6181f7bd9ceed9017720400a8b6fb87ea44f
Dave Borowitz 13 anos atrás
pai
commit
a2804ff7fe
2 arquivos alterados com 13 adições e 30 exclusões
  1. 3 0
      NEWS
  2. 10 30
      dulwich/pack.py

+ 3 - 0
NEWS

@@ -29,6 +29,9 @@
     crc32 to compute, and each return an additional crc32 element in their
     crc32 to compute, and each return an additional crc32 element in their
     return values. (Dave Borowitz)
     return values. (Dave Borowitz)
 
 
+  * PackObjectIterator was removed; its functionality is still exposed by
+    PackData.iterobjects. (Dave Borowitz)
+
  TEST CHANGES
  TEST CHANGES
 
 
   * If setuptools is installed, "python setup.py test" will now run the testsuite.
   * If setuptools is installed, "python setup.py test" will now run the testsuite.

+ 10 - 30
dulwich/pack.py

@@ -742,34 +742,6 @@ class PackStreamReader(object):
             raise ChecksumMismatch(pack_sha, calculated_sha)
             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):
 def obj_sha(type, chunks):
     """Compute the SHA for a numeric type and object chunks."""
     """Compute the SHA for a numeric type and object chunks."""
     sha = make_sha()
     sha = make_sha()
@@ -910,8 +882,16 @@ class PackData(object):
             self._offset_cache[offset] = type, chunks
             self._offset_cache[offset] = type, chunks
         return 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):
     def iterentries(self, progress=None):
         """Yield entries summarizing the contents of this pack.
         """Yield entries summarizing the contents of this pack.