You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I believe that the QuerySetNoCache object violates the iterator protocol because iter is not idempotent. I discovered this with code like the following:
from itertools import *
from mongoengine import *
class Foo(Document):
pass
def chunks(iterator, n):
it = iter(iterator)
while True:
r = tuple(islice(it, n))
if not r:
return
yield r
connect('test')
Foo.objects.delete()
Foo().save()
qs = Foo.objects.no_cache()
for c in chunks(qs, 10):
print c
This is an infinite loop because islice calls iter on its argument, but calling iter on the QuerySetNoCache rewinds the cursor. This can be repaired by changing the body of the __iter__ method to just return self, but that's not backwards compatible as it no longer clones the query set when iter is called on the original object multiple times. Perhaps the solution is something along the lines of
def __iter__(self):
qs = self.clone()
qs.rewind()
while 1:
yield qs.next()
This would fix the issue because the __iter__ method now never returns self, and the generator properly does. On the other hand, this looks odd to me, and I wouldn't submit it in a PR.
The text was updated successfully, but these errors were encountered:
I believe that the
QuerySetNoCache
object violates the iterator protocol becauseiter
is not idempotent. I discovered this with code like the following:This is an infinite loop because
islice
callsiter
on its argument, but callingiter
on theQuerySetNoCache
rewinds the cursor. This can be repaired by changing the body of the__iter__
method to justreturn self
, but that's not backwards compatible as it no longer clones the query set wheniter
is called on the original object multiple times. Perhaps the solution is something along the lines ofThis would fix the issue because the
__iter__
method now never returnsself
, and the generator properly does. On the other hand, this looks odd to me, and I wouldn't submit it in a PR.The text was updated successfully, but these errors were encountered: