Przeglądaj źródła

Raise TypeError if unicode string is used as key

Fixes #144.
Jonas Haag 11 lat temu
rodzic
commit
2431093d75
2 zmienionych plików z 5 dodań i 2 usunięć
  1. 3 1
      dulwich/repo.py
  2. 2 1
      dulwich/tests/test_repository.py

+ 3 - 1
dulwich/repo.py

@@ -439,7 +439,9 @@ 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) and isinstance(name, str):
+        if not isinstance(name, str):
+            raise TypeError("'name' must be bytestring, not %.80s" % type(name).__name__)
+        if len(name) in (20, 40):
             try:
                 return self.object_store[name]
             except (KeyError, ValueError):

+ 2 - 1
dulwich/tests/test_repository.py

@@ -112,7 +112,8 @@ class RepositoryTests(TestCase):
         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 + "--")
+        self.assertRaisesRegexp(TypeError, "'name' must be bytestring, not unicode",
+                                r.__getitem__, u"11" * 19 + "--")
 
     def test_delitem(self):
         r = self._repo = open_repo('a.git')