Ticket #5445: iterables2.patch

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

another patch following malcom's suggestions

  • django/http/__init__.py

     
    304289    content = property(_get_content, _set_content)
    305290
    306291    def __iter__(self):
    307         self._iterator = self._container.__iter__()
     292        self._iterator = iter(self._container)
    308293        return self
    309294
    310295    def next(self):
  • django/test/client.py

     
    1616from django.utils.functional import curry
    1717from django.utils.encoding import smart_str
    1818from django.utils.http import urlencode
     19from django.utils.itercompat import is_iterable
    1920
    2021BOUNDARY = 'BoUnDaRyStRiNg'
    2122MULTIPART_CONTENT = 'multipart/form-data; boundary=%s' % BOUNDARY
     
    7475                '',
    7576                value.read()
    7677            ])
    77         elif hasattr(value, '__iter__'):
    78             for item in value:
     78        else:
     79            if is_iterable(value) and not isinstance(value, basestring):
     80                for item in value:
     81                    lines.extend([
     82                        '--' + boundary,
     83                        'Content-Disposition: form-data; name="%s"' % to_str(key),
     84                        '',
     85                        to_str(item)
     86                    ])
     87            else:
    7988                lines.extend([
    8089                    '--' + boundary,
    8190                    'Content-Disposition: form-data; name="%s"' % to_str(key),
    8291                    '',
    83                     to_str(item)
     92                    to_str(value)
    8493                ])
    85         else:
    86             lines.extend([
    87                 '--' + boundary,
    88                 'Content-Disposition: form-data; name="%s"' % to_str(key),
    89                 '',
    90                 to_str(value)
    91             ])
    9294
    9395    lines.extend([
    9496        '--' + boundary + '--',
  • django/core/management/validation.py

     
    11import sys
    22from django.core.management.color import color_style
     3from django.utils.itercompat import is_iterable
    34
    45class ModelErrorCollection:
    56    def __init__(self, outfile=sys.stdout):
     
    5152            if f.prepopulate_from is not None and type(f.prepopulate_from) not in (list, tuple):
    5253                e.add(opts, '"%s": prepopulate_from should be a list or tuple.' % f.name)
    5354            if f.choices:
    54                 if not hasattr(f.choices, '__iter__'):
     55                if isinstance(f.choices, basestring) or \
     56                       not is_iterable(f.choices):
    5557                    e.add(opts, '"%s": "choices" should be iterable (e.g., a tuple or list).' % f.name)
    5658                else:
    5759                    for c in f.choices:
  • django/utils/itercompat.py

     
    5757    tee = compat_tee
    5858if hasattr(itertools, 'groupby'):
    5959    groupby = itertools.groupby
     60
     61def is_iterable(x):
     62    "A implementation independent way of checking for iterables"
     63    try:
     64        iter(x)
     65    except TypeError:
     66        return False
     67    else:
     68        return True
     69
Back to Top