Skip to content

Commit

Permalink
Fixed #10432 -- Handle all kinds of iterators in queryset filters.
Browse files Browse the repository at this point in the history
Only consumes the iterators once and works with Python 2.3.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9986 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
malcolmt committed Mar 8, 2009
1 parent f1ada99 commit cd99c62
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions django/db/models/sql/where.py
Expand Up @@ -47,6 +47,10 @@ def add(self, data, connector):
return return


obj, lookup_type, value = data obj, lookup_type, value = data
if hasattr(value, '__iter__') and hasattr(value, 'next'):
# Consume any generators immediately, so that we can determine
# emptiness and transform any non-empty values correctly.
value = list(value)
if hasattr(obj, "process"): if hasattr(obj, "process"):
try: try:
obj, params = obj.process(lookup_type, value) obj, params = obj.process(lookup_type, value)
Expand Down
23 changes: 23 additions & 0 deletions tests/regressiontests/queries/models.py
Expand Up @@ -1090,6 +1090,19 @@ def __unicode__(self):
>>> Tag.objects.filter(name__in=()).update(name="foo") >>> Tag.objects.filter(name__in=()).update(name="foo")
0 0
Bug #10432 (see also the Python 2.4+ tests for this, below). Testing an empty
"__in" filter with a generator as the value.
>>> def f():
... return iter([])
>>> n_obj = Note.objects.all()[0]
>>> def g():
... for i in [n_obj.pk]:
... yield i
>>> Note.objects.filter(pk__in=f())
[]
>>> list(Note.objects.filter(pk__in=g())) == [n_obj]
True
"""} """}


# In Python 2.3 and the Python 2.6 beta releases, exceptions raised in __len__ # In Python 2.3 and the Python 2.6 beta releases, exceptions raised in __len__
Expand Down Expand Up @@ -1140,3 +1153,13 @@ def __unicode__(self):
True True
""" """

# Generator expressions are only in Python 2.4 and later.
if sys.version_info >= (2, 4):
__test__["API_TESTS"] += """
Using an empty generator expression as the rvalue for an "__in" lookup is legal
(regression for #10432).
>>> Note.objects.filter(pk__in=(x for x in ()))
[]
"""

0 comments on commit cd99c62

Please sign in to comment.