|
|
@ -25,28 +25,31 @@ class CachedIterator():
|
|
|
|
# If we're more than 1 beyond the end, something has gone horribly wrong.
|
|
|
|
# If we're more than 1 beyond the end, something has gone horribly wrong.
|
|
|
|
# We should've already lengthened it last iteration
|
|
|
|
# We should've already lengthened it last iteration
|
|
|
|
assert len(self.items) == i, "CachedIterator logic error: {} != {}".format(len(self.items), i)
|
|
|
|
assert len(self.items) == i, "CachedIterator logic error: {} != {}".format(len(self.items), i)
|
|
|
|
# Check if the iterator is still active. If not, we've reached the end or an error.
|
|
|
|
|
|
|
|
if self.iterator is None:
|
|
|
|
|
|
|
|
if self.error is not None:
|
|
|
|
|
|
|
|
raise self.error
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
# Note we don't need the lock up until now because we're only trying to be gevent-safe,
|
|
|
|
# Note we don't need the lock up until now because we're only trying to be gevent-safe,
|
|
|
|
# not thread-safe. Simple operations like checking lengths can't be interrupted.
|
|
|
|
# not thread-safe. Simple operations like checking lengths can't be interrupted.
|
|
|
|
# However calling next on the iterator may cause a switch.
|
|
|
|
# However calling next on the iterator may cause a switch.
|
|
|
|
with self.lock:
|
|
|
|
with self.lock:
|
|
|
|
try:
|
|
|
|
# Taking the lock may have also caused a switch, so we need to re-check
|
|
|
|
item = next(self.iterator)
|
|
|
|
# our conditions. Someone else may have already added the item we need.
|
|
|
|
except StopIteration:
|
|
|
|
if len(self.items) <= i:
|
|
|
|
# We've reached the end. Discard the iterator (in theory an iterator that
|
|
|
|
# Check if the iterator is still active. If not, we've reached the end or an error.
|
|
|
|
# has raised StopIteration once will keep raising it every time thereafter,
|
|
|
|
if self.iterator is None:
|
|
|
|
# but best not to rely on that).
|
|
|
|
if self.error is not None:
|
|
|
|
self.iterator = None
|
|
|
|
raise self.error
|
|
|
|
# And we're done.
|
|
|
|
return
|
|
|
|
return
|
|
|
|
try:
|
|
|
|
except Exception as ex:
|
|
|
|
item = next(self.iterator)
|
|
|
|
# Discard the iterator and save the error to be re-served
|
|
|
|
except StopIteration:
|
|
|
|
self.iterator = None
|
|
|
|
# We've reached the end. Discard the iterator (in theory an iterator that
|
|
|
|
self.error = ex
|
|
|
|
# has raised StopIteration once will keep raising it every time thereafter,
|
|
|
|
raise
|
|
|
|
# but best not to rely on that).
|
|
|
|
self.items.append(item)
|
|
|
|
self.iterator = None
|
|
|
|
|
|
|
|
# And we're done.
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
except Exception as ex:
|
|
|
|
|
|
|
|
# Discard the iterator and save the error to be re-served
|
|
|
|
|
|
|
|
self.iterator = None
|
|
|
|
|
|
|
|
self.error = ex
|
|
|
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
self.items.append(item)
|
|
|
|
yield self.items[i]
|
|
|
|
yield self.items[i]
|
|
|
|