mirror of https://github.com/django/django.git
Refs #5748 -- Removed obsolete Python/Windows workarounds in floatformat filter.
This commit is contained in:
parent
9e6e32bf5d
commit
990ce6386c
|
@ -83,19 +83,6 @@ def escapejs_filter(value):
|
||||||
return escapejs(value)
|
return escapejs(value)
|
||||||
|
|
||||||
|
|
||||||
# Values for testing floatformat input against infinity and NaN representations,
|
|
||||||
# which differ across platforms and Python versions. Some (i.e. old Windows
|
|
||||||
# ones) are not recognized by Decimal but we want to return them unchanged vs.
|
|
||||||
# returning an empty string as we do for completely invalid input. Note these
|
|
||||||
# need to be built up from values that are not inf/nan, since inf/nan values do
|
|
||||||
# not reload properly from .pyc files on Windows prior to some level of Python 2.5
|
|
||||||
# (see Python Issue757815 and Issue1080440).
|
|
||||||
pos_inf = 1e200 * 1e200
|
|
||||||
neg_inf = -1e200 * 1e200
|
|
||||||
nan = (1e200 * 1e200) // (1e200 * 1e200)
|
|
||||||
special_floats = [str(pos_inf), str(neg_inf), str(nan)]
|
|
||||||
|
|
||||||
|
|
||||||
@register.filter(is_safe=True)
|
@register.filter(is_safe=True)
|
||||||
def floatformat(text, arg=-1):
|
def floatformat(text, arg=-1):
|
||||||
"""
|
"""
|
||||||
|
@ -132,14 +119,10 @@ def floatformat(text, arg=-1):
|
||||||
try:
|
try:
|
||||||
input_val = repr(text)
|
input_val = repr(text)
|
||||||
d = Decimal(input_val)
|
d = Decimal(input_val)
|
||||||
except UnicodeEncodeError:
|
|
||||||
return ''
|
|
||||||
except InvalidOperation:
|
except InvalidOperation:
|
||||||
if input_val in special_floats:
|
|
||||||
return input_val
|
|
||||||
try:
|
try:
|
||||||
d = Decimal(force_text(float(text)))
|
d = Decimal(force_text(float(text)))
|
||||||
except (ValueError, InvalidOperation, TypeError, UnicodeEncodeError):
|
except (ValueError, InvalidOperation, TypeError):
|
||||||
return ''
|
return ''
|
||||||
try:
|
try:
|
||||||
p = int(arg)
|
p = int(arg)
|
||||||
|
@ -158,15 +141,14 @@ def floatformat(text, arg=-1):
|
||||||
exp = Decimal(1)
|
exp = Decimal(1)
|
||||||
else:
|
else:
|
||||||
exp = Decimal('1.0') / (Decimal(10) ** abs(p))
|
exp = Decimal('1.0') / (Decimal(10) ** abs(p))
|
||||||
try:
|
# Set the precision high enough to avoid an exception (#15789).
|
||||||
# Set the precision high enough to avoid an exception, see #15789.
|
|
||||||
tupl = d.as_tuple()
|
tupl = d.as_tuple()
|
||||||
units = len(tupl[1])
|
units = len(tupl[1])
|
||||||
units += -tupl[2] if m else tupl[2]
|
units += -tupl[2] if m else tupl[2]
|
||||||
prec = abs(p) + units + 1
|
prec = abs(p) + units + 1
|
||||||
|
|
||||||
# Avoid conversion to scientific notation by accessing `sign`, `digits`
|
# Avoid conversion to scientific notation by accessing `sign`, `digits`,
|
||||||
# and `exponent` from `Decimal.as_tuple()` directly.
|
# and `exponent` from Decimal.as_tuple() directly.
|
||||||
sign, digits, exponent = d.quantize(exp, ROUND_HALF_UP, Context(prec=prec)).as_tuple()
|
sign, digits, exponent = d.quantize(exp, ROUND_HALF_UP, Context(prec=prec)).as_tuple()
|
||||||
digits = [str(digit) for digit in reversed(digits)]
|
digits = [str(digit) for digit in reversed(digits)]
|
||||||
while len(digits) <= abs(exponent):
|
while len(digits) <= abs(exponent):
|
||||||
|
@ -176,8 +158,6 @@ def floatformat(text, arg=-1):
|
||||||
digits.append('-')
|
digits.append('-')
|
||||||
number = ''.join(reversed(digits))
|
number = ''.join(reversed(digits))
|
||||||
return mark_safe(formats.number_format(number, abs(p)))
|
return mark_safe(formats.number_format(number, abs(p)))
|
||||||
except InvalidOperation:
|
|
||||||
return input_val
|
|
||||||
|
|
||||||
|
|
||||||
@register.filter(is_safe=True)
|
@register.filter(is_safe=True)
|
||||||
|
|
Loading…
Reference in New Issue