Django

Code

Ticket #6513 (closed: fixed)

Opened 9 months ago

Last modified 7 months ago

floatformat templatetag not robust

Reported by: Soeren Sonnenburg <bugreports@nn7.de> Assigned to: PhiR
Milestone: Component: Template system
Version: SVN Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

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

Attachments

floatformat_catch_overflow.diff (508 bytes) - added by Karen Tracey <kmtracey@gmail.com> on 03/18/08 11:49:41.
Catch overflow error on attempt to floatformat infinity.
6513_w_tests.diff (1.0 kB) - added by PhiR on 03/19/08 14:01:26.
added regression tests

Change History

03/17/08 14:12:59 changed by jefurii

  • status changed from new to closed.
  • needs_better_patch changed.
  • component changed from Uncategorized to Template system.
  • needs_tests changed.
  • needs_docs changed.
  • resolution set to worksforme.
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.

(follow-up: ↓ 3 ) 03/17/08 22:14:33 changed by Soeren Sonnenburg <bugreports@nn7.de>

  • status changed from closed to reopened.
  • resolution deleted.

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

(in reply to: ↑ 2 ) 03/18/08 11:47:43 changed by Karen Tracey <kmtracey@gmail.com>

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.

03/18/08 11:49:41 changed by Karen Tracey <kmtracey@gmail.com>

  • attachment floatformat_catch_overflow.diff added.

Catch overflow error on attempt to floatformat infinity.

03/19/08 14:01:26 changed by PhiR

  • attachment 6513_w_tests.diff added.

added regression tests

03/19/08 14:02:38 changed by PhiR

  • has_patch set to 1.
  • stage changed from Unreviewed to Ready for checkin.

03/19/08 14:02:51 changed by PhiR

  • owner changed from nobody to PhiR.
  • status changed from reopened to new.

03/19/08 14:28:53 changed by Karen Tracey <kmtracey@gmail.com>

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.

03/19/08 15:36:04 changed by PhiR

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!

03/20/08 02:36:35 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

(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.


Add/Change #6513 (floatformat templatetag not robust)




Change Properties
Action