Opened 17 years ago
Closed 17 years ago
#6513 closed (fixed)
floatformat templatetag not robust
Reported by: | Owned by: | Philippe Raoult | |
---|---|---|---|
Component: | Template system | Version: | dev |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
it fails when the input is None or inf for example.
Attachments (2)
Change History (10)
comment:1 by , 17 years ago
Component: | Uncategorized → Template system |
---|---|
Resolution: | → worksforme |
Status: | new → closed |
follow-up: 3 comment:2 by , 17 years ago
Resolution: | worksforme |
---|---|
Status: | closed → reopened |
no I meant inf (infinity), nan (not a number) etc.
comment:3 by , 17 years ago
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.
by , 17 years ago
Attachment: | floatformat_catch_overflow.diff added |
---|
Catch overflow error on attempt to floatformat infinity.
comment:4 by , 17 years ago
Has patch: | set |
---|---|
Triage Stage: | Unreviewed → Ready for checkin |
comment:5 by , 17 years ago
Owner: | changed from | to
---|---|
Status: | reopened → new |
comment:6 by , 17 years ago
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 by , 17 years ago
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 by , 17 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
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
produces this output,
which is correct according to the guidelines for filters.