Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[1.0.X] Fixed #10432 -- Handle all kinds of iterators in queryset fil…

…ters.

Only consumes the iterators once and works with Python 2.3.

Backport of r9986 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@9987 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit fd5d0cdebbc4e6dd620809c21b97bbb822d60b6d 1 parent 84b890b
@malcolmt malcolmt authored
View
4 django/db/models/sql/where.py
@@ -43,6 +43,10 @@ def add(self, data, connector):
return
alias, col, field, 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)
try:
if field:
params = field.get_db_prep_lookup(lookup_type, value)
View
24 tests/regressiontests/queries/models.py
@@ -1068,6 +1068,19 @@ def __unicode__(self):
>>> Tag.objects.filter(name__in=()).update(name="foo")
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__
@@ -1101,3 +1114,14 @@ def __unicode__(self):
[]
"""
+
+# 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 ()))
+[]
+
+"""
+
Please sign in to comment.
Something went wrong with that request. Please try again.