瀏覽代碼

Raise KeyError rather than TypeError when passing in 20 or 40-length
unicode object to Repo.__getitem__.

Jelmer Vernooij 11 年之前
父節點
當前提交
d14c55c6b8
共有 3 個文件被更改,包括 13 次插入1 次删除
  1. 6 0
      NEWS
  2. 1 1
      dulwich/repo.py
  3. 6 0
      dulwich/tests/test_repository.py

+ 6 - 0
NEWS

@@ -14,6 +14,12 @@
 
  * Remove long deprecated ``Tree.entries``. (Jelmer Vernooij)
 
+ BUG FIXES
+
+ * Raise KeyError rather than TypeError when passing in
+   unicode object of length 20 or 40 to Repo.__getitem__.
+   (Jelmer Vernooij)
+
 0.9.4	2013-11-30
 
  IMPROVEMENTS

+ 1 - 1
dulwich/repo.py

@@ -415,7 +415,7 @@ class BaseRepo(object):
         :return: A `ShaFile` object, such as a Commit or Blob
         :raise KeyError: when the specified ref or object does not exist
         """
-        if len(name) in (20, 40):
+        if len(name) in (20, 40) and isinstance(name, str):
             try:
                 return self.object_store[name]
             except (KeyError, ValueError):

+ 6 - 0
dulwich/tests/test_repository.py

@@ -108,6 +108,12 @@ class RepositoryTests(TestCase):
         self.assertEqual('a90fa2d900a17e99b433217e988c4eb4a2e9a097',
                           r["refs/tags/foo"].id)
 
+    def test_getitem_notfound_unicode(self):
+        r = self._repo = open_repo('a.git')
+        # In the future, this might raise a TypeError since we don't
+        # handle unicode strings properly (what encoding?) for refs.
+        self.assertRaises(KeyError, r.__getitem__, u"11" * 19 + "--")
+
     def test_delitem(self):
         r = self._repo = open_repo('a.git')