diff --git a/README.rst b/README.rst index 5da196b..5442a49 100644 --- a/README.rst +++ b/README.rst @@ -34,6 +34,8 @@ Changelog - Make pygit work in a frozen environment `#453 <https://github.com/libgit2/pygit2/pull/453>`_ +- New ``iter(pygit2.Blame)`` + - New ``pygit2.DiffDelta`` and ``pygit2.DiffFile`` - Rename ``pygit2.Hunk`` to ``pygit2.DiffHunk`` diff --git a/pygit2/blame.py b/pygit2/blame.py index 3145ee3..74cb13d 100644 --- a/pygit2/blame.py +++ b/pygit2/blame.py @@ -32,8 +32,10 @@ from __future__ import absolute_import, unicode_literals from .errors import check_error from .ffi import ffi, C from .utils import to_bytes, is_string, to_str +from .utils import GenericIterator from _pygit2 import Signature, Oid + def wrap_signature(csig): if not csig: return None @@ -142,21 +144,5 @@ class Blame(object): return BlameHunk._from_c(self, chunk) - -class BlameIterator(object): - def __init__(self, blame): - self._count = len(blame) - self._index = 0 - self._blame = blame - - def __next__(self): - if self._index >= self._count: - raise StopIteration - - hunk = self._blame[self._blame] - self._index += 1 - - return hunk - - def next(self): - return self.__next__() + def __iter__(self): + return GenericIterator(self) diff --git a/pygit2/config.py b/pygit2/config.py index 9d55fa2..9beee95 100644 --- a/pygit2/config.py +++ b/pygit2/config.py @@ -63,15 +63,12 @@ class ConfigIterator(object): def __next__(self): entry = self._next_entry() - name = ffi.string(entry.name).decode('utf-8') - - return name + return ffi.string(entry.name).decode('utf-8') class ConfigMultivarIterator(ConfigIterator): def __next__(self): entry = self._next_entry() - return ffi.string(entry.value).decode('utf-8') diff --git a/pygit2/index.py b/pygit2/index.py index 47d4cd6..2b86692 100644 --- a/pygit2/index.py +++ b/pygit2/index.py @@ -32,7 +32,8 @@ 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 is_string, to_bytes, to_str, StrArray +from .utils import is_string, to_bytes, to_str +from .utils import GenericIterator, StrArray class Index(object): @@ -92,7 +93,7 @@ class Index(object): return IndexEntry._from_c(centry) def __iter__(self): - return IndexIterator(self) + return GenericIterator(self) def read(self, force=True): """Update the contents the Index @@ -369,26 +370,6 @@ class IndexEntry(object): return entry -class IndexIterator(object): - - def __init__(self, index): - self.index = index - self.n = 0 - self.max = len(index) - - def next(self): - return self.__next__() - - def __next__(self): - if self.n >= self.max: - raise StopIteration - - entry = self.index[self.n] - self.n += 1 - - return entry - - class ConflictCollection(object): def __init__(self, index): diff --git a/pygit2/utils.py b/pygit2/utils.py index 17582a4..3bd548f 100644 --- a/pygit2/utils.py +++ b/pygit2/utils.py @@ -81,3 +81,27 @@ class StrArray(object): def __exit__(self, type, value, traceback): pass + + +class GenericIterator(object): + """Helper to easily implement an iterator. + + The constructor gets a container which must implement __len__ and + __getitem__ + """ + + def __init__(self, container): + self.container = container + self.length = len(container) + self.idx = 0 + + def next(self): + return self.__next__() + + def __next__(self): + idx = self.idx + if idx >= self.length: + raise StopIteration + + self.idx += 1 + return self.container[idx] diff --git a/src/types.h b/src/types.h index 1f78756..6aa8dce 100644 --- a/src/types.h +++ b/src/types.h @@ -156,12 +156,6 @@ typedef struct { git_index_entry entry; } IndexEntry; -typedef struct { - PyObject_HEAD - Index *owner; - int i; -} IndexIter; - /* git_reference, git_reflog */ SIMPLE_TYPE(Walker, git_revwalk, walk)