Opened 9 years ago

Closed 9 years ago

#6513 closed (fixed)

floatformat templatetag not robust

Reported by: Soeren Sonnenburg <bugreports@…> Owned by: Philippe Raoult
Component: Template system Version: master
Severity: Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

it fails when the input is None or inf for example.

Attachments (2)

floatformat_catch_overflow.diff (508 bytes) - added by Karen Tracey <kmtracey@…> 9 years ago.
Catch overflow error on attempt to floatformat infinity.
6513_w_tests.diff (1005 bytes) - added by Philippe Raoult 9 years ago.
added regression tests

Download all attachments as: .zip

Change History (10)

comment:1 Changed 9 years ago by jefurii

Component: UncategorizedTemplate system
Resolution: worksforme
Status: newclosed
it fails when the input is None or inf for example.

I think the poster means "int". They don't define "fails" either.

According to the docs (http://www.djangoproject.com/documentation/templates_python/#writing-custom-template-filters), filters should fail silently or return the original input. floatformat looks like it's working correctly:

This code

Float: {{ 34.12345|floatformat }}<br/>
None: {{ None|floatformat }}<br/>
Int: {{ 34|floatformat }}<br/>

produces this output,

Float: 34.1
None: 
Int: 34

which is correct according to the guidelines for filters.

comment:2 Changed 9 years ago by Soeren Sonnenburg <bugreports@…>

Resolution: worksforme
Status: closedreopened

no I meant inf (infinity), nan (not a number) etc.

comment:3 in reply to:  2 Changed 9 years ago by Karen Tracey <kmtracey@…>

Replying to Soeren Sonnenburg <bugreports@nn7.de>:

no I meant inf (infinity), nan (not a number) etc.

OK, I see a problem with infinity (OverflowError) but not NaN. What problem do you see with NaN? On Linux w/ Python 2.5.1 it displays as 'nan'. On Windows w/ Python 2.5.1 it displays as '-1.$'; this seems to be due to underlying Python behavior and I'm not sure what Django could do about it. (On Linux with a fix for the Overflow error infinity display as 'inf', on Windows it's '1.#INF'.) I'll attach a fix for the OverflowError in case that is useful.

Not sure what else to test to cover the 'etc' mentioned in the comment. I did also try negative infinity and that too works OK once the overflow error is fixed.

Changed 9 years ago by Karen Tracey <kmtracey@…>

Catch overflow error on attempt to floatformat infinity.

Changed 9 years ago by Philippe Raoult

Attachment: 6513_w_tests.diff added

added regression tests

comment:4 Changed 9 years ago by Philippe Raoult

Has patch: set
Triage Stage: UnreviewedReady for checkin

comment:5 Changed 9 years ago by Philippe Raoult

Owner: changed from nobody to Philippe Raoult
Status: reopenednew

comment:6 Changed 9 years ago by Karen Tracey <kmtracey@…>

FYI the added tests aren't going to work on Windows. Constructing special values there isn't so straightforward and they don't display the same when they are turned into strings:

D:\tmp>python
Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> x1 = float('infinity')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for float(): infinity
>>> x2 = float('nan')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for float(): nan
>>> x1 = float(1e30000)
>>> x1
1.#INF
>>> x2 = x1/x1
>>> x2
-1.#IND
>>>

http://www.python.org/dev/peps/pep-0754/ is what I used to get some idea of Python's cross-platform handling of special float values. Maybe there is a better reference but it was the best I found via a quick Google search. Net that I got from that PEP (which is rejected) is that constructing these values in a cross-platform-independent fashion is possible but roundabout, and their string representations differ across platforms. Makes it hard to construct a cross-platform-independent test for floatformat.

comment:7 Changed 9 years ago by Philippe Raoult

I've read that and some other stuff and it looks like there's nothing we can do right now because this is an issue with python. The tests won't work but the tags will, and I guess that'll be good enough for now.

Thanks for the heads-up!

comment:8 Changed 9 years ago by Malcolm Tredinnick

Resolution: fixed
Status: newclosed

(In [7336]) FIxed #6513 -- Handle overflows better in the floatformat filter. It's not
possible to test this automatically everywhere due to differing representations
on different platforms. Manual testing confirms it works, though.

Thanks, Karen Tracey.

Note: See TracTickets for help on using tickets.
Back to Top