Ticket #5445: iterables.patch

File iterables.patch, 3.4 KB (added by leosoto <leo.soto@…>, 17 years ago)

Replaced iter attribute lookup by trial an error with iter()

  • django/core/management/validation.py

    diff -r a729ba90ca04 django/core/management/validation.py
    a b import sys  
    11import sys
     2import types
    23from django.core.management.color import color_style
    34
    45class ModelErrorCollection:
    class ModelErrorCollection:  
    1112        self.errors.append((context, error))
    1213        self.outfile.write(self.style.ERROR("%s: %s\n" % (context, error)))
    1314
     15def _is_iterable(x):
     16    try:
     17        iter(x)
     18    except TypeError:
     19        return False
     20    else:
     21        return True
    1422def get_validation_errors(outfile, app=None):
    1523    """
    1624    Validates all models that are part of the specified app. If no app name is provided,
    def get_validation_errors(outfile, app=N  
    5159            if f.prepopulate_from is not None and type(f.prepopulate_from) not in (list, tuple):
    5260                e.add(opts, '"%s": prepopulate_from should be a list or tuple.' % f.name)
    5361            if f.choices:
    54                 if not hasattr(f.choices, '__iter__'):
     62                if isinstance(f.choices, types.StringTypes) or \
     63                       not _is_iterable(f.choices):
    5564                    e.add(opts, '"%s": "choices" should be iterable (e.g., a tuple or list).' % f.name)
    5665                else:
    5766                    for c in f.choices:
  • django/http/__init__.py

    a b class HttpResponse(object):  
    289289    content = property(_get_content, _set_content)
    290290
    291291    def __iter__(self):
    292         self._iterator = self._container.__iter__()
     292        self._iterator = iter(self._container)
    293293        return self
    294294
    295295    def next(self):
  • django/test/client.py

    diff -r a729ba90ca04 django/test/client.py
    a b import datetime  
    11import datetime
    22import sys
     3import types
    34from cStringIO import StringIO
    45from urlparse import urlparse
    56from django.conf import settings
    def encode_multipart(boundary, data):  
    7475                '',
    7576                value.read()
    7677            ])
    77         elif hasattr(value, '__iter__'):
    78             for item in value:
     78        else:
     79            try:
     80                iter(value)
     81            except:
     82                iterable = False
     83            else:
     84                iterable = True
     85            if iterable and not isinstance(value, types.StringTypes):
     86                for item in value:
     87                    lines.extend([
     88                        '--' + boundary,
     89                        'Content-Disposition: form-data; name="%s"' % to_str(key),
     90                        '',
     91                        to_str(item)
     92                    ])
     93            else:
    7994                lines.extend([
    8095                    '--' + boundary,
    8196                    'Content-Disposition: form-data; name="%s"' % to_str(key),
    8297                    '',
    83                     to_str(item)
     98                    to_str(value)
    8499                ])
    85         else:
    86             lines.extend([
    87                 '--' + boundary,
    88                 'Content-Disposition: form-data; name="%s"' % to_str(key),
    89                 '',
    90                 to_str(value)
    91             ])
    92100
    93101    lines.extend([
    94102        '--' + boundary + '--',
Back to Top