summaryrefslogtreecommitdiff
path: root/django/utils/itercompat.py
diff options
context:
space:
mode:
authorMarc Tamlyn <marc.tamlyn@gmail.com>2013-03-20 10:47:56 +0000
committerClaude Paroz <claude@2xlibre.net>2013-03-22 17:31:29 +0100
commit829dc3c5a64d3fa203b8cc0055e83cf23addfee3 (patch)
tree227c1b5aba3e785744863b269262c3c2c0c90ca8 /django/utils/itercompat.py
parentf7795e968dd07323aa5bd16f9da0e60d97a75d0f (diff)
Fixed #20094 - Be more careful when checking for Iterator
Python 2.6 has some different behaviour when checking isinstance(foo, collections.Iterator).
Diffstat (limited to 'django/utils/itercompat.py')
-rw-r--r--django/utils/itercompat.py15
1 files changed, 14 insertions, 1 deletions
diff --git a/django/utils/itercompat.py b/django/utils/itercompat.py
index e7ea326536..c50dcfb779 100644
--- a/django/utils/itercompat.py
+++ b/django/utils/itercompat.py
@@ -4,10 +4,12 @@ Where possible, we try to use the system-native version and only fall back to
these implementations if necessary.
"""
-from django.utils.six.moves import builtins
+import collections
import itertools
+import sys
import warnings
+
def is_iterable(x):
"A implementation independent way of checking for iterables"
try:
@@ -17,6 +19,17 @@ def is_iterable(x):
else:
return True
+def is_iterator(x):
+ """An implementation independent way of checking for iterators
+
+ Python 2.6 has a different implementation of collections.Iterator which
+ accepts anything with a `next` method. 2.7+ requires and `__iter__` method
+ as well.
+ """
+ if sys.version_info >= (2, 7):
+ return isinstance(x, collections.Iterator)
+ return isinstance(x, collections.Iterator) and hasattr(x, '__iter__')
+
def product(*args, **kwds):
warnings.warn("django.utils.itercompat.product is deprecated; use the native version instead",
DeprecationWarning, stacklevel=2)