Jelmer Vernooij 10 лет назад
Родитель
Сommit
f0fb132090
6 измененных файлов с 31 добавлено и 91 удалено
  1. 0 49
      dulwich/_py3_compat.py
  2. 10 12
      dulwich/diff_tree.py
  3. 1 5
      dulwich/lru_cache.py
  4. 15 14
      dulwich/objects.py
  5. 1 4
      dulwich/refs.py
  6. 4 7
      dulwich/tests/test_objects.py

+ 0 - 49
dulwich/_py3_compat.py

@@ -1,49 +0,0 @@
-# _py3_compat.py -- for dealing with python3 compatibility
-# Copyright (C) 2012-2014 Jelmer Vernooij and others.
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License
-# as published by the Free Software Foundation; version 2
-# of the License or (at your option) a later version of the License.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-# MA  02110-1301, USA.
-
-
-import sys
-import operator
-
-PY2 = sys.version_info[0] == 2
-PY3 = sys.version_info[0] == 3
-
-if PY2:
-    text_type = unicode
-else:
-    text_type = str
-
-
-if PY2:
-    def byte2int(bs):
-        return ord(bs[0])
-    def indexbytes(buf, i):
-        return ord(buf[i])
-    def iterbytes(buf):
-        return (ord(byte) for byte in buf)
-else:
-    byte2int = operator.itemgetter(0)
-    indexbytes = operator.getitem
-    iterbytes = iter
-
-
-if PY2:
-    items = lambda d: d.iteritems()
-else:
-    items = lambda d: d.items()
-

+ 10 - 12
dulwich/diff_tree.py

@@ -27,21 +27,19 @@ from io import BytesIO
 from itertools import chain
 import stat
 
-try:
-    from itertools import izip
-except ImportError:
-    # Python3
+if sys.version_info[0] == 3:
+    xrange = range
     izip = zip
+    iteritems = lambda d: d.items()
+else:
+    from itertools import izip
+    iteritems = lambda d: d.iteritems()
+
 from dulwich.objects import (
     S_ISGITLINK,
     TreeEntry,
     )
-from dulwich._py3_compat import (
-    items,
-    )
 
-if sys.version_info[0] == 3:
-    xrange = range
 
 # TreeChange type constants.
 CHANGE_ADD = 'add'
@@ -267,7 +265,7 @@ def tree_changes_for_merge(store, parent_tree_ids, tree_id,
     change_type = lambda c: c.type
 
     # Yield only conflicting changes.
-    for _, changes in sorted(items(changes_by_path)):
+    for _, changes in sorted(iteritems(changes_by_path)):
         assert len(changes) == num_parents
         have = [c for c in changes if c is not None]
         if _all_eq(have, change_type, CHANGE_DELETE):
@@ -332,7 +330,7 @@ def _common_bytes(blocks1, blocks2):
     if len(blocks1) > len(blocks2):
         blocks1, blocks2 = blocks2, blocks1
     score = 0
-    for block, count1 in items(blocks1):
+    for block, count1 in iteritems(blocks1):
         count2 = blocks2.get(block)
         if count2:
             score += min(count1, count2)
@@ -460,7 +458,7 @@ class RenameDetector(object):
 
         add_paths = set()
         delete_paths = set()
-        for sha, sha_deletes in items(delete_map):
+        for sha, sha_deletes in iteritems(delete_map):
             sha_adds = add_map[sha]
             for (old, is_delete), new in izip(sha_deletes, sha_adds):
                 if stat.S_IFMT(old.mode) != stat.S_IFMT(new.mode):

+ 1 - 5
dulwich/lru_cache.py

@@ -17,10 +17,6 @@
 
 """A simple least-recently-used (LRU) cache."""
 
-from dulwich._py3_compat import (
-    items,
-)
-
 _null_key = object()
 
 
@@ -186,7 +182,7 @@ class LRUCache(object):
 
     def items(self):
         """Get the key:value pairs as a dict."""
-        return dict((k, n.value) for k, n in items(self._cache))
+        return dict((k, n.value) for k, n in self._cache.items())
 
     def cleanup(self):
         """Clear the cache until it shrinks to the requested size.

+ 15 - 14
dulwich/objects.py

@@ -25,6 +25,7 @@ from collections import namedtuple
 import os
 import posixpath
 import stat
+import sys
 import warnings
 import zlib
 from hashlib import sha1
@@ -38,13 +39,12 @@ from dulwich.errors import (
     ObjectFormatException,
     )
 from dulwich.file import GitFile
-from dulwich._py3_compat import (
-    byte2int,
-    indexbytes,
-    iterbytes,
-    items,
-    text_type,
-    )
+
+if sys.version_info[0] == 2:
+    iteritems = lambda d: d.iteritems()
+else:
+    iteritems = lambda d: d.items()
+
 
 ZERO_SHA = b'0' * 40
 
@@ -106,7 +106,7 @@ def hex_to_filename(path, hex):
     # os.path.join accepts bytes or unicode, but all args must be of the same
     # type. Make sure that hex which is expected to be bytes, is the same type
     # as path.
-    if isinstance(path, text_type):
+    if getattr(path, 'encode', None) is not None:
         hex = hex.decode('ascii')
     dir = hex[:2]
     file = hex[2:]
@@ -196,7 +196,7 @@ class FixedSha(object):
     __slots__ = ('_hexsha', '_sha')
 
     def __init__(self, hexsha):
-        if isinstance(hexsha, text_type):
+        if getattr(hexsha, 'encode', None) is not None:
             hexsha = hexsha.encode('ascii')
         if not isinstance(hexsha, bytes):
             raise TypeError('Expected bytes for hexsha, got %r' % hexsha)
@@ -330,7 +330,7 @@ class ShaFile(object):
     @staticmethod
     def _parse_object_header(magic, f):
         """Parse a new style object, creating it but not reading the file."""
-        num_type = (byte2int(magic) >> 4) & 7
+        num_type = (ord(magic[0:1]) >> 4) & 7
         obj_class = object_class(num_type)
         if not obj_class:
             raise ObjectFormatException("Not a known type %d" % num_type)
@@ -342,17 +342,18 @@ class ShaFile(object):
         """Parse a new style object, setting self._text."""
         # skip type and size; type must have already been determined, and
         # we trust zlib to fail if it's otherwise corrupted
-        byte = byte2int(map)
+        byte = ord(map[0:1])
         used = 1
         while (byte & 0x80) != 0:
-            byte = indexbytes(map, used)
+            byte = ord(map[used:used+1])
             used += 1
         raw = map[used:]
         self.set_raw_string(_decompress(raw))
 
     @classmethod
     def _is_legacy_object(cls, magic):
-        b0, b1 = iterbytes(magic)
+        b0 = ord(magic[0:1])
+        b1 = ord(magic[1:2])
         word = (b0 << 8) + b1
         return (b0 & 0x8F) == 0x08 and (word % 31) == 0
 
@@ -817,7 +818,7 @@ def sorted_tree_items(entries, name_order):
     :return: Iterator over (name, mode, hexsha)
     """
     key_func = name_order and key_entry_name_order or key_entry
-    for name, entry in sorted(items(entries), key=key_func):
+    for name, entry in sorted(iteritems(entries), key=key_func):
         mode, hexsha = entry
         # Stricter type checks than normal to mirror checks in the C version.
         mode = int(mode)

+ 1 - 4
dulwich/refs.py

@@ -36,9 +36,6 @@ from dulwich.file import (
     GitFile,
     ensure_dir_exists,
     )
-from dulwich._py3_compat import (
-    items,
-    )
 
 
 SYMREF = b'ref: '
@@ -110,7 +107,7 @@ class RefsContainer(object):
         return None
 
     def import_refs(self, base, other):
-        for name, value in items(other):
+        for name, value in other.items():
             self[b'/'.join((base, name))] = value
 
     def allkeys(self):

+ 4 - 7
dulwich/tests/test_objects.py

@@ -63,9 +63,6 @@ from dulwich.tests.utils import (
     functest_builder,
     ext_functest_builder,
     )
-from dulwich._py3_compat import (
-    items,
-    )
 
 a_sha = b'6f670c0fb53f9463760b7295fbb814e965fb20c8'
 b_sha = b'2969be3e8ee1c0222396a5611407e4769f14e54b'
@@ -500,7 +497,7 @@ class CommitParseTests(ShaFileCheckTests):
         if encoding is not None:
             lines.append(b'encoding ' + encoding)
         if extra is not None:
-            for name, value in sorted(items(extra)):
+            for name, value in sorted(extra.items()):
                 lines.append(name + b' ' + value)
         lines.append(b'')
         if message is not None:
@@ -678,13 +675,13 @@ class TreeTests(ShaFileCheckTests):
 
     def test_tree_iteritems_dir_sort(self):
         x = Tree()
-        for name, item in items(_TREE_ITEMS):
+        for name, item in _TREE_ITEMS.items():
             x[name] = item
-        self.assertEqual(_SORTED_TREE_ITEMS, list(items(x)))
+        self.assertEqual(_SORTED_TREE_ITEMS, x.items())
 
     def test_tree_items_dir_sort(self):
         x = Tree()
-        for name, item in items(_TREE_ITEMS):
+        for name, item in _TREE_ITEMS.items():
             x[name] = item
         self.assertEqual(_SORTED_TREE_ITEMS, x.items())