Ticket #4287: inf-bug.2.patch

File inf-bug.2.patch, 3.3 KB (added by calmez, 13 years ago)
  • django/db/models/fields/__init__.py

     
    66import math
    77from itertools import tee
    88
    9 from django.db import connection
     9from django.db import connection, utils
    1010from django.db.models.query_utils import QueryWrapper
    1111from django.conf import settings
    1212from django import forms
     
    848848    empty_strings_allowed = False
    849849    default_error_messages = {
    850850        'invalid': _("This value must be a float."),
     851        'no-infinity': _("This field has no infinity enabled"),
    851852    }
    852853    description = _("Floating point number")
    853854
     855    def __init__(self, allow_infinity=False, *args, **kwargs):
     856        if allow_infinity and not(connection.features.supports_infinity_floats):
     857            raise utils.DatabaseError("Your database does not support storing infinity values.")
     858        self.allow_infinity = allow_infinity
     859        super(FloatField, self).__init__(*args, **kwargs)
     860
    854861    def get_prep_value(self, value):
    855862        if value is None:
    856863            return None
     
    863870        if value is None:
    864871            return value
    865872        try:
    866             return float(value)
     873            float_value = float(value)
    867874        except (TypeError, ValueError):
    868875            raise exceptions.ValidationError(self.error_messages['invalid'])
     876        if not(self.allow_infinity) and float_value in (float('inf'), float('-inf')):
     877            raise exceptions.ValidationError(self.error_messages['no-infinity'])
     878        else:
     879            return float_value
    869880
    870881    def formfield(self, **kwargs):
    871882        defaults = {'form_class': forms.FloatField}
  • django/db/backends/sqlite3/base.py

     
    5757    supports_unspecified_pk = True
    5858    supports_1000_query_parameters = False
    5959    supports_mixed_date_datetime_comparisons = False
     60    supports_infinity_floats = True
    6061
    6162    def _supports_stddev(self):
    6263        """Confirm support for STDDEV and related stats functions
  • django/db/backends/__init__.py

     
    335335    # date_interval_sql can properly handle mixed Date/DateTime fields and timedeltas
    336336    supports_mixed_date_datetime_comparisons = True
    337337
     338    # Can an float store infinity?
     339    supports_infinity_floats = False
     340
    338341    # Features that need to be confirmed at runtime
    339342    # Cache whether the confirmation has been performed.
    340343    _confirmed = False
  • django/forms/fields.py

     
    235235        'invalid': _(u'Enter a number.'),
    236236    }
    237237
     238    def __init__(self, store_infinity=False, *args, **kwargs):
     239        self.store_infinity = store_infinity
     240        super(IntegerField, self).__init__(*args, **kwargs)
     241
    238242    def to_python(self, value):
    239243        """
    240244        Validates that float() can be called on the input. Returns the result
Back to Top