Author: kmtracey
Date: 2008-11-07 19:44:46 -0600 (Fri, 07 Nov 2008)
New Revision: 9369

Modified:
   django/trunk/django/template/defaultfilters.py
   django/trunk/tests/regressiontests/defaultfilters/tests.py
Log:
Fixed #5748 -- Made floatformat filter round properly on all platforms and 
handle NaN input correctly on Windows.  Also added tests for these cases.  
Thanks for the report and initial patch to SmileyChris and PJCrosier.


Modified: django/trunk/django/template/defaultfilters.py
===================================================================
--- django/trunk/django/template/defaultfilters.py      2008-11-06 21:37:08 UTC 
(rev 9368)
+++ django/trunk/django/template/defaultfilters.py      2008-11-08 01:44:46 UTC 
(rev 9369)
@@ -1,6 +1,12 @@
 """Default variable filters."""
 
 import re
+
+try:
+    from decimal import Decimal, InvalidOperation, ROUND_HALF_UP
+except ImportError:
+    from django.utils._decimal import Decimal, InvalidOperation, ROUND_HALF_UP
+
 import random as random_module
 try:
     from functools import wraps
@@ -45,7 +51,6 @@
 # STRINGS         #
 ###################
 
-
 def addslashes(value):
     """
     Adds slashes before quotes. Useful for escaping strings in CSV, for
@@ -92,6 +97,18 @@
 fix_ampersands.is_safe=True
 fix_ampersands = stringfilter(fix_ampersands)
 
+# 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 completley 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)]
+
 def floatformat(text, arg=-1):
     """
     Displays a float to a specified number of decimal places.
@@ -119,24 +136,42 @@
     * {{ num1|floatformat:"-3" }} displays "34.232"
     * {{ num2|floatformat:"-3" }} displays "34"
     * {{ num3|floatformat:"-3" }} displays "34.260"
+    
+    If the input float is infinity or NaN, the (platform-dependent) string
+    representation of that value will be displayed.
     """
+        
     try:
-        f = float(text)
-    except (ValueError, TypeError):
+        input_val = force_unicode(text)
+        d = Decimal(input_val)
+    except UnicodeEncodeError:
         return u''
+    except InvalidOperation:
+        if input_val in special_floats:
+            return input_val
+        else:
+            return u''
     try:
-        d = int(arg)
+        p = int(arg)
     except ValueError:
-        return force_unicode(f)
+        return input_val
+    
     try:
-        m = f - int(f)
-    except OverflowError:
-        return force_unicode(f)
-    if not m and d < 0:
-        return mark_safe(u'%d' % int(f))
+        m = int(d) - d
+    except (OverflowError, InvalidOperation):
+        return input_val
+    
+    if not m and p < 0:
+        return mark_safe(u'%d' % (int(d)))
+    
+    if p == 0:
+        exp = Decimal(1)
     else:
-        formatstr = u'%%.%df' % abs(d)
-        return mark_safe(formatstr % f)
+        exp = Decimal('1.0') / (Decimal(10) ** abs(p))
+    try:
+        return mark_safe(u'%s' % str(d.quantize(exp, ROUND_HALF_UP)))
+    except InvalidOperation:
+        return input_val
 floatformat.is_safe = True
 
 def iriencode(value):

Modified: django/trunk/tests/regressiontests/defaultfilters/tests.py
===================================================================
--- django/trunk/tests/regressiontests/defaultfilters/tests.py  2008-11-06 
21:37:08 UTC (rev 9368)
+++ django/trunk/tests/regressiontests/defaultfilters/tests.py  2008-11-08 
01:44:46 UTC (rev 9369)
@@ -13,15 +13,15 @@
 u'0.0'
 >>> floatformat(0.0)
 u'0'
->>> floatformat(7.7,3)
+>>> floatformat(7.7, 3)
 u'7.700'
->>> floatformat(6.000000,3)
+>>> floatformat(6.000000, 3)
 u'6.000'
 >>> floatformat(6.200000, 3)
 u'6.200'
 >>> floatformat(6.200000, -3)
 u'6.200'
->>> floatformat(13.1031,-3)
+>>> floatformat(13.1031, -3)
 u'13.103'
 >>> floatformat(11.1197, -2)
 u'11.12'
@@ -35,10 +35,23 @@
 u''
 >>> floatformat(13.1031, u'bar')
 u'13.1031'
+>>> floatformat(18.125, 2) 
+u'18.13' 
 >>> floatformat(u'foo', u'bar')
 u''
+>>> floatformat(u'¿Cómo esta usted?')
+u''
 >>> floatformat(None)
 u''
+>>> pos_inf = float(1e30000)
+>>> floatformat(pos_inf) == unicode(pos_inf)
+True
+>>> neg_inf = float(-1e30000)
+>>> floatformat(neg_inf) == unicode(neg_inf)
+True
+>>> nan = pos_inf / pos_inf
+>>> floatformat(nan) == unicode(nan)
+True
 
 >>> addslashes(u'"double quotes" and \'single quotes\'')
 u'\\"double quotes\\" and \\\'single quotes\\\''


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to