Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.5.x] Fixed #20094 - Be more careful when checking for Iterator

Python 2.6 has some different behaviour when checking
isinstance(foo, collections.Iterator).
Backport of 829dc3c from master.
  • Loading branch information...
commit dd897e4eeb20cab9ac708c46f34c5f09758205c8 1 parent 7206330
Marc Tamlyn authored March 20, 2013 claudep committed March 22, 2013
4  django/db/models/fields/__init__.py
... ...
@@ -1,6 +1,5 @@
1 1
 from __future__ import unicode_literals
2 2
 
3  
-import collections
4 3
 import copy
5 4
 import datetime
6 5
 import decimal
@@ -16,6 +15,7 @@
16 15
 from django.utils.datastructures import DictWrapper
17 16
 from django.utils.dateparse import parse_date, parse_datetime, parse_time
18 17
 from django.utils.functional import curry, total_ordering
  18
+from django.utils.itercompat import is_iterator
19 19
 from django.utils.text import capfirst
20 20
 from django.utils import timezone
21 21
 from django.utils.translation import ugettext_lazy as _
@@ -444,7 +444,7 @@ def bind(self, fieldmapping, original, bound_field_class):
444 444
         return bound_field_class(self, fieldmapping, original)
445 445
 
446 446
     def _get_choices(self):
447  
-        if isinstance(self._choices, collections.Iterator):
  447
+        if is_iterator(self._choices):
448 448
             choices, self._choices = tee(self._choices)
449 449
             return choices
450 450
         else:
4  django/db/models/sql/where.py
@@ -4,7 +4,6 @@
4 4
 
5 5
 from __future__ import absolute_import
6 6
 
7  
-import collections
8 7
 import datetime
9 8
 from itertools import repeat
10 9
 
@@ -12,6 +11,7 @@
12 11
 from django.db.models.fields import Field
13 12
 from django.db.models.sql.datastructures import EmptyResultSet
14 13
 from django.db.models.sql.aggregates import Aggregate
  14
+from django.utils.itercompat import is_iterator
15 15
 from django.utils.six.moves import xrange
16 16
 
17 17
 # Connection types
@@ -51,7 +51,7 @@ def add(self, data, connector):
51 51
             return
52 52
 
53 53
         obj, lookup_type, value = data
54  
-        if isinstance(value, collections.Iterator):
  54
+        if is_iterator(value):
55 55
             # Consume any generators immediately, so that we can determine
56 56
             # emptiness and transform any non-empty values correctly.
57 57
             value = list(value)
15  django/utils/itercompat.py
@@ -4,10 +4,12 @@
4 4
 these implementations if necessary.
5 5
 """
6 6
 
7  
-from django.utils.six.moves import builtins
  7
+import collections
8 8
 import itertools
  9
+import sys
9 10
 import warnings
10 11
 
  12
+
11 13
 def is_iterable(x):
12 14
     "A implementation independent way of checking for iterables"
13 15
     try:
@@ -17,6 +19,17 @@ def is_iterable(x):
17 19
     else:
18 20
         return True
19 21
 
  22
+def is_iterator(x):
  23
+    """An implementation independent way of checking for iterators
  24
+
  25
+    Python 2.6 has a different implementation of collections.Iterator which
  26
+    accepts anything with a `next` method. 2.7+ requires and `__iter__` method
  27
+    as well.
  28
+    """
  29
+    if sys.version_info >= (2, 7):
  30
+        return isinstance(x, collections.Iterator)
  31
+    return isinstance(x, collections.Iterator) and hasattr(x, '__iter__')
  32
+
20 33
 def product(*args, **kwds):
21 34
     warnings.warn("django.utils.itercompat.product is deprecated; use the native version instead",
22 35
                   PendingDeprecationWarning)
11  tests/regressiontests/utils/itercompat.py
... ...
@@ -0,0 +1,11 @@
  1
+from django.test import TestCase
  2
+
  3
+from .models import Category, Thing
  4
+
  5
+
  6
+class TestIsIterator(TestCase):
  7
+    def test_regression(self):
  8
+        """This failed on Django 1.5/Py2.6 because category has a next method."""
  9
+        category = Category.objects.create(name='category')
  10
+        Thing.objects.create(category=category)
  11
+        Thing.objects.filter(category=category)
14  tests/regressiontests/utils/models.py
... ...
@@ -1 +1,13 @@
1  
-# Test runner needs a models.py file.
  1
+from django.db import models
  2
+
  3
+
  4
+class Category(models.Model):
  5
+    name = models.CharField(max_length=100)
  6
+
  7
+    def next(self):
  8
+        return self
  9
+
  10
+
  11
+class Thing(models.Model):
  12
+    name = models.CharField(max_length=100)
  13
+    category = models.ForeignKey(Category)
1  tests/regressiontests/utils/tests.py
@@ -19,6 +19,7 @@
19 19
 from .html import TestUtilsHtml
20 20
 from .http import TestUtilsHttp, ETagProcessingTests, HttpDateProcessingTests
21 21
 from .ipv6 import TestUtilsIPv6
  22
+from .itercompat import TestIsIterator
22 23
 from .jslex import JsToCForGettextTest, JsTokensTest
23 24
 from .module_loading import CustomLoader, DefaultLoader, EggLoader
24 25
 from .numberformat import TestNumberFormat

0 notes on commit dd897e4

Please sign in to comment.
Something went wrong with that request. Please try again.