Ticket #4167: django-0.91-formfield-list-mutability-fix.patch

File django-0.91-formfield-list-mutability-fix.patch, 12.6 KB (added by Tom Tobin <korpios@…>, 18 years ago)

FormField list-mutability fix for 0.91-bugfixes

  • django/core/formfields.py

     
    325325
    326326class TextField(FormField):
    327327    input_type = "text"
    328     def __init__(self, field_name, length=30, maxlength=None, is_required=False, validator_list=[], member_name=None):
     328    def __init__(self, field_name, length=30, maxlength=None, is_required=False, validator_list=None, member_name=None):
     329        if validator_list is None: validator_list = []
    329330        self.field_name = field_name
    330331        self.length, self.maxlength = length, maxlength
    331332        self.is_required = is_required
     
    362363    input_type = "password"
    363364
    364365class LargeTextField(TextField):
    365     def __init__(self, field_name, rows=10, cols=40, is_required=False, validator_list=[], maxlength=None):
     366    def __init__(self, field_name, rows=10, cols=40, is_required=False, validator_list=None, maxlength=None):
     367        if validator_list is None: validator_list = []
    366368        self.field_name = field_name
    367369        self.rows, self.cols, self.is_required = rows, cols, is_required
    368370        self.validator_list = validator_list[:]
     
    380382            self.field_name, self.rows, self.cols, escape(data))
    381383
    382384class HiddenField(FormField):
    383     def __init__(self, field_name, is_required=False, validator_list=[]):
     385    def __init__(self, field_name, is_required=False, validator_list=None):
     386        if validator_list is None: validator_list = []
    384387        self.field_name, self.is_required = field_name, is_required
    385388        self.validator_list = validator_list[:]
    386389
     
    410413    html2python = staticmethod(html2python)
    411414
    412415class SelectField(FormField):
    413     def __init__(self, field_name, choices=[], size=1, is_required=False, validator_list=[], member_name=None):
     416    def __init__(self, field_name, choices=[], size=1, is_required=False, validator_list=None, member_name=None):
     417        if validator_list is None: validator_list = []
    414418        self.field_name = field_name
    415419        # choices is a list of (value, human-readable key) tuples because order matters
    416420        self.choices, self.size, self.is_required = choices, size, is_required
     
    446450    html2python = staticmethod(html2python)
    447451
    448452class RadioSelectField(FormField):
    449     def __init__(self, field_name, choices=[], ul_class='', is_required=False, validator_list=[], member_name=None):
     453    def __init__(self, field_name, choices=[], ul_class='', is_required=False, validator_list=None, member_name=None):
     454        if validator_list is None: validator_list = []
    450455        self.field_name = field_name
    451456        # choices is a list of (value, human-readable key) tuples because order matters
    452457        self.choices, self.is_required = choices, is_required
     
    510515
    511516class NullBooleanField(SelectField):
    512517    "This SelectField provides 'Yes', 'No' and 'Unknown', mapping results to True, False or None"
    513     def __init__(self, field_name, is_required=False, validator_list=[]):
     518    def __init__(self, field_name, is_required=False, validator_list=None):
     519        if validator_list is None: validator_list = []
    514520        SelectField.__init__(self, field_name, choices=[('1', 'Unknown'), ('2', 'Yes'), ('3', 'No')],
    515521            is_required=is_required, validator_list=validator_list)
    516522
     
    563569    back into the single list that validators, renderers and save() expect.
    564570    """
    565571    requires_data_list = True
    566     def __init__(self, field_name, choices=[], validator_list=[]):
     572    def __init__(self, field_name, choices=[], validator_list=None):
     573        if validator_list is None: validator_list = []
    567574        SelectMultipleField.__init__(self, field_name, choices, size=1, is_required=False, validator_list=validator_list)
    568575
    569576    def prepare(self, new_data):
     
    594601####################
    595602
    596603class FileUploadField(FormField):
    597     def __init__(self, field_name, is_required=False, validator_list=[]):
     604    def __init__(self, field_name, is_required=False, validator_list=None):
     605        if validator_list is None: validator_list = []
    598606        self.field_name, self.is_required = field_name, is_required
    599607        self.validator_list = [self.isNonEmptyFile] + validator_list
    600608
     
    629637####################
    630638
    631639class IntegerField(TextField):
    632     def __init__(self, field_name, length=10, maxlength=None, is_required=False, validator_list=[], member_name=None):
     640    def __init__(self, field_name, length=10, maxlength=None, is_required=False, validator_list=None, member_name=None):
     641        if validator_list is None: validator_list = []
    633642        validator_list = [self.isInteger] + validator_list
    634643        if member_name is not None:
    635644            self.member_name = member_name
     
    648657    html2python = staticmethod(html2python)
    649658
    650659class SmallIntegerField(IntegerField):
    651     def __init__(self, field_name, length=5, maxlength=5, is_required=False, validator_list=[]):
     660    def __init__(self, field_name, length=5, maxlength=5, is_required=False, validator_list=None):
     661        if validator_list is None: validator_list = []
    652662        validator_list = [self.isSmallInteger] + validator_list
    653663        IntegerField.__init__(self, field_name, length, maxlength, is_required, validator_list)
    654664
     
    657667            raise validators.CriticalValidationError, _("Enter a whole number between -32,768 and 32,767.")
    658668
    659669class PositiveIntegerField(IntegerField):
    660     def __init__(self, field_name, length=10, maxlength=None, is_required=False, validator_list=[]):
     670    def __init__(self, field_name, length=10, maxlength=None, is_required=False, validator_list=None):
     671        if validator_list is None: validator_list = []
    661672        validator_list = [self.isPositive] + validator_list
    662673        IntegerField.__init__(self, field_name, length, maxlength, is_required, validator_list)
    663674
     
    666677            raise validators.CriticalValidationError, _("Enter a positive number.")
    667678
    668679class PositiveSmallIntegerField(IntegerField):
    669     def __init__(self, field_name, length=5, maxlength=None, is_required=False, validator_list=[]):
     680    def __init__(self, field_name, length=5, maxlength=None, is_required=False, validator_list=None):
     681        if validator_list is None: validator_list = []
    670682        validator_list = [self.isPositiveSmall] + validator_list
    671683        IntegerField.__init__(self, field_name, length, maxlength, is_required, validator_list)
    672684
     
    675687            raise validators.CriticalValidationError, _("Enter a whole number between 0 and 32,767.")
    676688
    677689class FloatField(TextField):
    678     def __init__(self, field_name, max_digits, decimal_places, is_required=False, validator_list=[]):
     690    def __init__(self, field_name, max_digits, decimal_places, is_required=False, validator_list=None):
     691        if validator_list is None: validator_list = []
    679692        self.max_digits, self.decimal_places = max_digits, decimal_places
    680693        validator_list = [self.isValidFloat] + validator_list
    681694        TextField.__init__(self, field_name, max_digits+1, max_digits+1, is_required, validator_list)
     
    700713class DatetimeField(TextField):
    701714    """A FormField that automatically converts its data to a datetime.datetime object.
    702715    The data should be in the format YYYY-MM-DD HH:MM:SS."""
    703     def __init__(self, field_name, length=30, maxlength=None, is_required=False, validator_list=[]):
     716    def __init__(self, field_name, length=30, maxlength=None, is_required=False, validator_list=None):
     717        if validator_list is None: validator_list = []
    704718        self.field_name = field_name
    705719        self.length, self.maxlength = length, maxlength
    706720        self.is_required = is_required
     
    723737class DateField(TextField):
    724738    """A FormField that automatically converts its data to a datetime.date object.
    725739    The data should be in the format YYYY-MM-DD."""
    726     def __init__(self, field_name, is_required=False, validator_list=[]):
     740    def __init__(self, field_name, is_required=False, validator_list=None):
     741        if validator_list is None: validator_list = []
    727742        validator_list = [self.isValidDate] + validator_list
    728743        TextField.__init__(self, field_name, length=10, maxlength=10,
    729744            is_required=is_required, validator_list=validator_list)
     
    747762class TimeField(TextField):
    748763    """A FormField that automatically converts its data to a datetime.time object.
    749764    The data should be in the format HH:MM:SS or HH:MM:SS.mmmmmm."""
    750     def __init__(self, field_name, is_required=False, validator_list=[]):
     765    def __init__(self, field_name, is_required=False, validator_list=None):
     766        if validator_list is None: validator_list = []
    751767        validator_list = [self.isValidTime] + validator_list
    752768        TextField.__init__(self, field_name, length=8, maxlength=8,
    753769            is_required=is_required, validator_list=validator_list)
     
    781797
    782798class EmailField(TextField):
    783799    "A convenience FormField for validating e-mail addresses"
    784     def __init__(self, field_name, length=50, maxlength=75, is_required=False, validator_list=[]):
     800    def __init__(self, field_name, length=50, maxlength=75, is_required=False, validator_list=None):
     801        if validator_list is None: validator_list = []
    785802        validator_list = [self.isValidEmail] + validator_list
    786803        TextField.__init__(self, field_name, length, maxlength=maxlength,
    787804            is_required=is_required, validator_list=validator_list)
     
    794811
    795812class URLField(TextField):
    796813    "A convenience FormField for validating URLs"
    797     def __init__(self, field_name, length=50, is_required=False, validator_list=[]):
     814    def __init__(self, field_name, length=50, is_required=False, validator_list=None):
     815        if validator_list is None: validator_list = []
    798816        validator_list = [self.isValidURL] + validator_list
    799817        TextField.__init__(self, field_name, length=length, maxlength=200,
    800818            is_required=is_required, validator_list=validator_list)
     
    806824            raise validators.CriticalValidationError, e.messages
    807825
    808826class IPAddressField(TextField):
    809     def __init__(self, field_name, length=15, maxlength=15, is_required=False, validator_list=[]):
     827    def __init__(self, field_name, length=15, maxlength=15, is_required=False, validator_list=None):
     828        if validator_list is None: validator_list = []
    810829        validator_list = [self.isValidIPAddress] + validator_list
    811830        TextField.__init__(self, field_name, length=length, maxlength=maxlength,
    812831            is_required=is_required, validator_list=validator_list)
     
    827846
    828847class FilePathField(SelectField):
    829848    "A SelectField whose choices are the files in a given directory."
    830     def __init__(self, field_name, path, match=None, recursive=False, is_required=False, validator_list=[]):
     849    def __init__(self, field_name, path, match=None, recursive=False, is_required=False, validator_list=None):
     850        if validator_list is None: validator_list = []
    831851        import os
    832852        if match is not None:
    833853            import re
     
    850870
    851871class PhoneNumberField(TextField):
    852872    "A convenience FormField for validating phone numbers (e.g. '630-555-1234')"
    853     def __init__(self, field_name, is_required=False, validator_list=[]):
     873    def __init__(self, field_name, is_required=False, validator_list=None):
     874        if validator_list is None: validator_list = []
    854875        validator_list = [self.isValidPhone] + validator_list
    855876        TextField.__init__(self, field_name, length=12, maxlength=12,
    856877            is_required=is_required, validator_list=validator_list)
     
    863884
    864885class USStateField(TextField):
    865886    "A convenience FormField for validating U.S. states (e.g. 'IL')"
    866     def __init__(self, field_name, is_required=False, validator_list=[]):
     887    def __init__(self, field_name, is_required=False, validator_list=None):
     888        if validator_list is None: validator_list = []
    867889        validator_list = [self.isValidUSState] + validator_list
    868890        TextField.__init__(self, field_name, length=2, maxlength=2,
    869891            is_required=is_required, validator_list=validator_list)
     
    883905
    884906class CommaSeparatedIntegerField(TextField):
    885907    "A convenience FormField for validating comma-separated integer fields"
    886     def __init__(self, field_name, maxlength=None, is_required=False, validator_list=[]):
     908    def __init__(self, field_name, maxlength=None, is_required=False, validator_list=None):
     909        if validator_list is None: validator_list = []
    887910        validator_list = [self.isCommaSeparatedIntegerList] + validator_list
    888911        TextField.__init__(self, field_name, length=20, maxlength=maxlength,
    889912            is_required=is_required, validator_list=validator_list)
Back to Top