Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[py3] Fixed 'iterable but non string' detection

In Python 3, the str type has an __iter__ attribute. Therefore, the
presence of an __iter__ attribute is not sufficient to distinguish
'standard' iterables (list, tuple) from strings.
  • Loading branch information...
commit db729266d6feb3b4b4519bd789c4693b9aac00d1 1 parent 0955d16
@claudep claudep authored
View
2  django/contrib/admin/helpers.py
@@ -94,7 +94,7 @@ def __iter__(self):
class Fieldline(object):
def __init__(self, form, field, readonly_fields=None, model_admin=None):
self.form = form # A django.forms.Form instance
- if not hasattr(field, "__iter__"):
+ if not hasattr(field, "__iter__") or isinstance(field, six.text_type):
self.fields = [field]
else:
self.fields = field
View
4 django/core/serializers/python.py
@@ -98,7 +98,7 @@ def Deserializer(object_list, **options):
if field.rel and isinstance(field.rel, models.ManyToManyRel):
if hasattr(field.rel.to._default_manager, 'get_by_natural_key'):
def m2m_convert(value):
- if hasattr(value, '__iter__'):
+ if hasattr(value, '__iter__') and not isinstance(value, six.text_type):
return field.rel.to._default_manager.db_manager(db).get_by_natural_key(*value).pk
else:
return smart_text(field.rel.to._meta.pk.to_python(value))
@@ -110,7 +110,7 @@ def m2m_convert(value):
elif field.rel and isinstance(field.rel, models.ManyToOneRel):
if field_value is not None:
if hasattr(field.rel.to._default_manager, 'get_by_natural_key'):
- if hasattr(field_value, '__iter__'):
+ if hasattr(field_value, '__iter__') and not isinstance(field_value, six.text_type):
obj = field.rel.to._default_manager.db_manager(db).get_by_natural_key(*field_value)
value = getattr(obj, field.rel.field_name)
# If this is a natural foreign key to an object that
View
2  django/forms/models.py
@@ -1035,6 +1035,6 @@ def clean(self, value):
return qs
def prepare_value(self, value):
- if hasattr(value, '__iter__'):
+ if hasattr(value, '__iter__') and not isinstance(value, six.text_type):
return [super(ModelMultipleChoiceField, self).prepare_value(v) for v in value]
return super(ModelMultipleChoiceField, self).prepare_value(value)
View
2  django/http/__init__.py
@@ -656,7 +656,7 @@ def _get_content(self):
return b''.join([smart_bytes(e, self._charset) for e in self._container])
def _set_content(self, value):
- if hasattr(value, '__iter__'):
+ if hasattr(value, '__iter__') and not isinstance(value, (bytes, six.text_type)):
self._container = value
self._base_content_is_iter = True
else:
Please sign in to comment.
Something went wrong with that request. Please try again.