Get back iter(Blame)

Was lost in commit cd0842592
This commit is contained in:
J. David Ibáñez 2015-02-12 13:02:04 +01:00
parent 94be744ba6
commit 7130df3a5e
6 changed files with 34 additions and 50 deletions

@ -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``

@ -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)

@ -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')

@ -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):

@ -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]

@ -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)