Author: mtredinnick
Date: 2009-03-07 21:32:16 -0600 (Sat, 07 Mar 2009)
New Revision: 9986

Modified:
   django/trunk/django/db/models/sql/where.py
   django/trunk/tests/regressiontests/queries/models.py
Log:
Fixed #10432 -- Handle all kinds of iterators in queryset filters.

Only consumes the iterators once and works with Python 2.3.

Modified: django/trunk/django/db/models/sql/where.py
===================================================================
--- django/trunk/django/db/models/sql/where.py  2009-03-07 23:02:48 UTC (rev 
9985)
+++ django/trunk/django/db/models/sql/where.py  2009-03-08 03:32:16 UTC (rev 
9986)
@@ -47,6 +47,10 @@
             return
 
         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"):
             try:
                 obj, params = obj.process(lookup_type, value)

Modified: django/trunk/tests/regressiontests/queries/models.py
===================================================================
--- django/trunk/tests/regressiontests/queries/models.py        2009-03-07 
23:02:48 UTC (rev 9985)
+++ django/trunk/tests/regressiontests/queries/models.py        2009-03-08 
03:32:16 UTC (rev 9986)
@@ -1090,6 +1090,19 @@
 >>> 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__
@@ -1140,3 +1153,13 @@
 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 ()))
+[]
+
+"""


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to