From 3ccba387258ed9aea68d8b6a2714c954ed43319c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20David=20Ib=C3=A1=C3=B1ez?= Date: Mon, 28 Jul 2014 18:02:24 +0200 Subject: [PATCH] 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. --- pygit2/index.py | 4 ++-- pygit2/py2.py | 10 ++++++++++ pygit2/py3.py | 10 ++++++++++ pygit2/utils.py | 4 ++-- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/pygit2/index.py b/pygit2/index.py index f1b851c..5213c22 100644 --- a/pygit2/index.py +++ b/pygit2/index.py @@ -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'))[:])) diff --git a/pygit2/py2.py b/pygit2/py2.py index af8888b..3c57fba 100644 --- a/pygit2/py2.py +++ b/pygit2/py2.py @@ -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) diff --git a/pygit2/py3.py b/pygit2/py3.py index fa46af1..0b6c79a 100644 --- a/pygit2/py3.py +++ b/pygit2/py3.py @@ -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)) diff --git a/pygit2/utils.py b/pygit2/utils.py index 3a4a01a..3c5fc88 100644 --- a/pygit2/utils.py +++ b/pygit2/utils.py @@ -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