Django

Code

Ticket #5445: iterables3.patch

File iterables3.patch, 4.6 kB (added by leosoto <leo.soto@gmail.com>, 1 year ago)

Uses is_iterable on django.template too. Also changes the order of the typical check for the common case: now we first check for non-strings, next for iterables.

  • django/http/__init__.py

    old new  
    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

    old new  
    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 not isinstance(value, basestring) and is_iterable(value) 
     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             ]) 
  • django/core/management/validation.py

    old new  
    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

    old new  
    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 
  • ../django/template/__init__.py

    old new  
    5858from inspect import getargspec 
    5959from django.conf import settings 
    6060from django.template.context import Context, RequestContext, ContextPopException 
     61from django.utils.itercompat import is_iterable 
    6162from django.utils.functional import curry, Promise 
    6263from django.utils.text import smart_split 
    6364from django.utils.encoding import smart_unicode, force_unicode 
    6465from django.utils.translation import ugettext as _