From bc424e342f65dd5b6ca8a624320066dd8f8958de Mon Sep 17 00:00:00 2001 From: Thom Wiggers Date: Sat, 27 Feb 2016 23:43:44 +0100 Subject: [PATCH 1/2] Add unit tests for bytes and unicode Repositories Add unit test for bytes repository paths Add a unicode path test for Repositories --- test/test_repository.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test/test_repository.py b/test/test_repository.py index e4cc912f7..5590ea2eb 100644 --- a/test/test_repository.py +++ b/test/test_repository.py @@ -39,6 +39,8 @@ from os.path import join, realpath import sys +import six + # Import from pygit2 from pygit2 import GIT_OBJ_ANY, GIT_OBJ_BLOB, GIT_OBJ_COMMIT from pygit2 import init_repository, clone_repository, discover_repository @@ -482,12 +484,17 @@ def test_head(self): self.assertFalse(self.repo.head_is_detached) -class BytesStringRepositoryTest(utils.NoRepoTestCase): +class StringTypesRepositoryTest(utils.NoRepoTestCase): def test_bytes_string(self): repo_path = b'./test/data/testrepo.git/' pygit2.Repository(repo_path) + def test_unicode_string(self): + # String is unicode because of unicode_literals + repo_path = './test/data/testrepo.git/' + pygit2.Repository(repo_path) + class CloneRepositoryTest(utils.NoRepoTestCase): From 735510f14d09c2c3bcb2d6aeb1e0f1b8aeb5fe36 Mon Sep 17 00:00:00 2001 From: Thom Wiggers Date: Sun, 28 Feb 2016 12:32:51 +0100 Subject: [PATCH 2/2] Fix repository crash if path passed is not a str Tries to decode any non-string objects (such as bytes) Introduces `six` as a dependency Closes #588 --- pygit2/repository.py | 8 ++++++-- setup.py | 5 +++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/pygit2/repository.py b/pygit2/repository.py index d7ca58e49..e9bbdc66e 100644 --- a/pygit2/repository.py +++ b/pygit2/repository.py @@ -37,6 +37,8 @@ else: from io import BytesIO as StringIO +import six + # Import from pygit2 from _pygit2 import Repository as _Repository from _pygit2 import Oid, GIT_OID_HEXSZ, GIT_OID_MINPREFIXLEN @@ -56,8 +58,10 @@ class Repository(_Repository): - def __init__(self, *args, **kwargs): - super(Repository, self).__init__(*args, **kwargs) + def __init__(self, path, *args, **kwargs): + if not isinstance(path, six.string_types): + path = path.decode('utf-8') + super(Repository, self).__init__(path, *args, **kwargs) self._common_init() @classmethod diff --git a/setup.py b/setup.py index 54bbb0caf..1fb947511 100644 --- a/setup.py +++ b/setup.py @@ -63,6 +63,7 @@ # Python 2 support # See https://github.com/libgit2/pygit2/pull/180 for a discussion about this. +# Using six isn't an option here yet, we don't necessarily have six installed if sys.version_info[0] == 2: u = lambda s: unicode(s, 'utf-8') else: @@ -186,7 +187,7 @@ def run(self): if cffi_major_version == 0: extra_args['ext_modules'].append(ffi.verifier.get_extension()) else: - extra_args['cffi_modules']=['pygit2/_run.py:ffi'] + extra_args['cffi_modules'] = ['pygit2/_run.py:ffi'] setup(name='pygit2', @@ -202,7 +203,7 @@ def run(self): packages=['pygit2'], package_data={'pygit2': ['decl.h']}, setup_requires=['cffi'], - install_requires=['cffi'], + install_requires=['cffi', 'six'], zip_safe=False, cmdclass=cmdclass, **extra_args)