Parcourir la source

py3k: Go through all uses of itertools and make them work on py3k

Hannu Valtonen il y a 10 ans
Parent
commit
2b721b6b86

+ 7 - 3
dulwich/diff_tree.py

@@ -24,7 +24,11 @@ from collections import (
     )
 
 from io import BytesIO
-import itertools
+try:
+    from itertools import izip, chain
+except ImportError:
+    from itertools import chain
+    izip = zip
 import stat
 
 from dulwich.objects import (
@@ -290,7 +294,7 @@ def _count_blocks(obj):
     block_truncate = block.truncate
     block_getvalue = block.getvalue
 
-    for c in itertools.chain(*obj.as_raw_chunks()):
+    for c in chain(*obj.as_raw_chunks()):
         block_write(c)
         n += 1
         if c == '\n' or n == _BLOCK_SIZE:
@@ -447,7 +451,7 @@ class RenameDetector(object):
         delete_paths = set()
         for sha, sha_deletes in delete_map.iteritems():
             sha_adds = add_map[sha]
-            for (old, is_delete), new in itertools.izip(sha_deletes, sha_adds):
+            for (old, is_delete), new in izip(sha_deletes, sha_adds):
                 if stat.S_IFMT(old.mode) != stat.S_IFMT(new.mode):
                     continue
                 if is_delete:

+ 2 - 2
dulwich/object_store.py

@@ -23,7 +23,7 @@
 
 from io import BytesIO
 import errno
-import itertools
+from itertools import chain
 import os
 import stat
 import tempfile
@@ -336,7 +336,7 @@ class PackBasedObjectStore(BaseObjectStore):
     def __iter__(self):
         """Iterate over the SHAs that are present in this store."""
         iterables = self.packs + [self._iter_loose_objects()] + [self._iter_alternate_objects()]
-        return itertools.chain(*iterables)
+        return chain(*iterables)
 
     def contains_loose(self, sha):
         """Check if a particular object is present by SHA1 and is loose.

+ 7 - 5
dulwich/pack.py

@@ -38,11 +38,13 @@ from collections import (
     deque,
     )
 import difflib
-from itertools import (
-    chain,
-    imap,
-    izip,
-    )
+
+try:
+    from itertools import chain, imap, izip
+except ImportError: # On Py3k the equivalents are the basic map and zip
+    from itertools import chain
+    imap, izip = map, zip
+
 try:
     import mmap
 except ImportError:

+ 2 - 2
dulwich/tests/compat/test_repository.py

@@ -21,7 +21,7 @@
 
 
 from io import BytesIO
-import itertools
+from itertools import chain
 import os
 
 from dulwich.objects import (
@@ -118,7 +118,7 @@ class ObjectStoreTestCase(CompatTestCase):
     def test_packed_objects(self):
         expected_shas = self._get_all_shas() - self._get_loose_shas()
         self.assertShasMatch(expected_shas,
-                             itertools.chain(*self._repo.object_store.packs))
+                             chain(*self._repo.object_store.packs))
 
     def test_all_objects(self):
         expected_shas = self._get_all_shas()

+ 1 - 4
dulwich/tests/test_diff_tree.py

@@ -18,10 +18,7 @@
 
 """Tests for file and tree diff utilities."""
 
-from itertools import (
-    permutations,
-    )
-
+from itertools import permutations
 from dulwich.diff_tree import (
     CHANGE_MODIFY,
     CHANGE_RENAME,

+ 2 - 2
dulwich/walk.py

@@ -23,7 +23,7 @@ from collections import defaultdict
 
 import collections
 import heapq
-import itertools
+from itertools import chain
 
 from dulwich.diff_tree import (
     RENAME_CHANGE_TYPES,
@@ -100,7 +100,7 @@ class _CommitTimeQueue(object):
         self._extra_commits_left = _MAX_EXTRA_COMMITS
         self._is_finished = False
 
-        for commit_id in itertools.chain(walker.include, walker.excluded):
+        for commit_id in chain(walker.include, walker.excluded):
             self._push(commit_id)
 
     def _push(self, commit_id):