From f1796e1ac5e56cd1cd5ffbc21bdc1b1d3f85540a Mon Sep 17 00:00:00 2001 From: Harold Cooper Date: Wed, 10 Feb 2016 00:28:57 -0500 Subject: [PATCH] Fix race condition in ResultSet.__iter__. Fixes #13, hopefully for good this time. --- records.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/records.py b/records.py index 70bb5fd..f579185 100644 --- a/records.py +++ b/records.py @@ -112,17 +112,14 @@ class ResultSet(object): return r def __iter__(self): - """Starts by returning the cached items and then consumes the - generator in case it is not fully consumed. - """ - if self._all_rows: - for row in self._all_rows: - yield row - try: - while True: - yield self.__next__() - except StopIteration: - pass + """Iterate over all rows, consuming the underlying generator only when necessary.""" + i = 0 + while True: + # Other code may have iterated between yields, so always check the cache. + if i < len(self._all_rows): yield self._all_rows[i] + else: yield next(self) # Throws StopIteration when done. + i += 1 + def next(self): return self.__next__()