Fix regression in v0.21.1, IndexEntry.path returns str

And str in Python 2 means bytes. Recall, we are supposed to give a
native experience to both Python 2 and 3 users.
This commit is contained in:
J. David Ibáñez
2014-07-28 18:02:24 +02:00
parent be807a1dfc
commit 3ccba38725
4 changed files with 24 additions and 4 deletions

View File

@@ -32,7 +32,7 @@ from __future__ import absolute_import, unicode_literals
from _pygit2 import Oid, Tree, Diff
from .errors import check_error
from .ffi import ffi, C
from .utils import to_bytes, is_string, strings_to_strarray
from .utils import is_string, strings_to_strarray, to_bytes, to_str
class Index(object):
@@ -357,7 +357,7 @@ class IndexEntry(object):
return None
entry = cls.__new__(cls)
entry.path = ffi.string(centry.path).decode()
entry.path = to_str(ffi.string(centry.path))
entry.mode = centry.mode
entry.id = Oid(raw=bytes(ffi.buffer(ffi.addressof(centry, 'id'))[:]))

View File

@@ -45,3 +45,13 @@ def to_bytes(s, encoding='utf-8', errors='strict'):
def is_string(s):
return isinstance(s, basestring)
def to_str(s):
if type(s) is str:
return s
if type(s) is unicode:
return s.encode()
raise TypeError, 'unexpected type "%s"' % repr(s)

View File

@@ -44,3 +44,13 @@ def to_bytes(s, encoding='utf-8', errors='strict'):
def is_string(s):
return isinstance(s, str)
def to_str(s):
if type(s) is str:
return s
if type(s) is bytes:
return s.decode()
raise TypeError('unexpected type "%s"' % repr(s))

View File

@@ -36,9 +36,9 @@ from .ffi import ffi
if version_info[0] < 3:
from .py2 import to_bytes, is_string
from .py2 import is_string, to_bytes, to_str
else:
from .py3 import to_bytes, is_string
from .py3 import is_string, to_bytes, to_str