Ticket #10806: forms-typeerror.patch

File forms-typeerror.patch, 2.0 KB (added by Adi J. Sieker, 16 years ago)
  • django/forms/forms.py

    diff --git a/django/forms/forms.py b/django/forms/forms.py
    index ef9d657..775480f 100644
    a b Form classes  
    44
    55from copy import deepcopy
    66
    7 from django.utils.datastructures import SortedDict
     7from django.utils.datastructures import SortedDict, MergeDict
    88from django.utils.html import conditional_escape
    99from django.utils.encoding import StrAndUnicode, smart_unicode, force_unicode
    1010from django.utils.safestring import mark_safe
    class BaseForm(StrAndUnicode):  
    7272                 initial=None, error_class=ErrorList, label_suffix=':',
    7373                 empty_permitted=False):
    7474        self.is_bound = data is not None or files is not None
     75       
     76        if data <> None and not isinstance(data, (dict, MergeDict)):
     77            raise TypeError('data parameter must be of type dict not of type %s' % type(data))
     78       
     79        if files <> None and not isinstance(files, (dict, MergeDict)):
     80            raise TypeError('files parameter must be of type dict not of type %s' % type(files))
     81       
    7582        self.data = data or {}
    7683        self.files = files or {}
    7784        self.auto_id = auto_id
  • tests/regressiontests/forms/forms.py

    diff --git a/tests/regressiontests/forms/forms.py b/tests/regressiontests/forms/forms.py
    index 642cd20..876cdca 100644
    a b u'Lennon'  
    189189>>> p.cleaned_data['birthday']
    190190datetime.date(1940, 10, 9)
    191191
     192passing something other than a dict into __init__ will raise a TypeError
     193>>> p = Person('a string')
     194Traceback (most recent call last):
     195...
     196TypeError: data parameter must be of type dict not of type <type 'str'>
     197
     198this is also true for the files parameter of __init__
     199>>> p = Person(data, files='another string')
     200Traceback (most recent call last):
     201...
     202TypeError: files parameter must be of type dict not of type <type 'str'>
    192203
    193204cleaned_data will include a key and value for *all* fields defined in the Form,
    194205even if the Form's data didn't include a value for fields that are not
Back to Top