diff --git a/AUTHORS b/AUTHORS index a5306196b1..e1966c5af5 100644 --- a/AUTHORS +++ b/AUTHORS @@ -263,6 +263,7 @@ answer newbie questions, and generally made Django that much better: SmileyChris smurf@smurf.noris.de sopel + Leo Soto Wiliam Alves de Souza Georgi Stanojevski Vasiliy Stavenko diff --git a/django/core/management/validation.py b/django/core/management/validation.py index aa4ef47c2f..6528a34f29 100644 --- a/django/core/management/validation.py +++ b/django/core/management/validation.py @@ -1,5 +1,6 @@ import sys from django.core.management.color import color_style +from django.utils.itercompat import is_iterable class ModelErrorCollection: def __init__(self, outfile=sys.stdout): @@ -51,7 +52,8 @@ def get_validation_errors(outfile, app=None): if f.prepopulate_from is not None and type(f.prepopulate_from) not in (list, tuple): e.add(opts, '"%s": prepopulate_from should be a list or tuple.' % f.name) if f.choices: - if not hasattr(f.choices, '__iter__'): + if isinstance(f.choices, basestring) or \ + not is_iterable(f.choices): e.add(opts, '"%s": "choices" should be iterable (e.g., a tuple or list).' % f.name) else: for c in f.choices: diff --git a/django/http/__init__.py b/django/http/__init__.py index 2b68a6243a..102094b95d 100644 --- a/django/http/__init__.py +++ b/django/http/__init__.py @@ -304,7 +304,7 @@ class HttpResponse(object): content = property(_get_content, _set_content) def __iter__(self): - self._iterator = self._container.__iter__() + self._iterator = iter(self._container) return self def next(self): diff --git a/django/template/__init__.py b/django/template/__init__.py index 6880fd5997..de8591ac5c 100644 --- a/django/template/__init__.py +++ b/django/template/__init__.py @@ -58,6 +58,7 @@ import re from inspect import getargspec from django.conf import settings from django.template.context import Context, RequestContext, ContextPopException +from django.utils.itercompat import is_iterable from django.utils.functional import curry, Promise from django.utils.text import smart_split from django.utils.encoding import smart_unicode, force_unicode @@ -900,7 +901,7 @@ class Library(object): if not getattr(self, 'nodelist', False): from django.template.loader import get_template, select_template - if hasattr(file_name, '__iter__'): + if not isinstance(file_name, basestring) and is_iterable(file_name): t = select_template(file_name) else: t = get_template(file_name) diff --git a/django/test/client.py b/django/test/client.py index c3e221554f..faacc5bf9e 100644 --- a/django/test/client.py +++ b/django/test/client.py @@ -16,6 +16,7 @@ from django.test import signals from django.utils.functional import curry from django.utils.encoding import smart_str from django.utils.http import urlencode +from django.utils.itercompat import is_iterable BOUNDARY = 'BoUnDaRyStRiNg' MULTIPART_CONTENT = 'multipart/form-data; boundary=%s' % BOUNDARY @@ -74,21 +75,22 @@ def encode_multipart(boundary, data): '', value.read() ]) - elif hasattr(value, '__iter__'): - for item in value: + else: + if not isinstance(value, basestring) and is_iterable(value): + for item in value: + lines.extend([ + '--' + boundary, + 'Content-Disposition: form-data; name="%s"' % to_str(key), + '', + to_str(item) + ]) + else: lines.extend([ '--' + boundary, 'Content-Disposition: form-data; name="%s"' % to_str(key), '', - to_str(item) + to_str(value) ]) - else: - lines.extend([ - '--' + boundary, - 'Content-Disposition: form-data; name="%s"' % to_str(key), - '', - to_str(value) - ]) lines.extend([ '--' + boundary + '--', diff --git a/django/utils/itercompat.py b/django/utils/itercompat.py index 0de1b6cbe2..3742d6c5d8 100644 --- a/django/utils/itercompat.py +++ b/django/utils/itercompat.py @@ -57,3 +57,13 @@ else: tee = compat_tee if hasattr(itertools, 'groupby'): groupby = itertools.groupby + +def is_iterable(x): + "A implementation independent way of checking for iterables" + try: + iter(x) + except TypeError: + return False + else: + return True +