Django

Code

Ticket #3670 (closed: fixed)

Opened 2 years ago

Last modified 2 years ago

ifequal and ifnotequal failing with negative numbers

Reported by: bram.dejong+django@gmail.com Assigned to: adrian
Milestone: Component: Template system
Version: SVN Keywords: ifequal ifnotequal negative
Cc: Triage Stage: Ready for checkin
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

{% ifequal -1 -20 %}
hello
{% endifequal %}

prints "hello"

{% ifnotequal -1 -1 %}
hello
{% endifnotequal %}

doesn't print anything

this because in resolve_variable( ) in django.template.init uses:

if path[0].isdigit():

which could be:

if path[0].isdigit() or path[0] == "-":

Attachments

templateif_patch.patch (436 bytes) - added by bram.dejong+django@gmail.com on 03/06/07 18:24:07.
numeric.patch (2.8 kB) - added by SmileyChris on 03/06/07 21:16:24.
numeric_with_negative.patch (3.1 kB) - added by SmileyChris on 03/06/07 21:19:30.

Change History

03/06/07 18:24:07 changed by bram.dejong+django@gmail.com

  • attachment templateif_patch.patch added.

03/06/07 18:25:46 changed by bram.dejong+django@gmail.com

that should have been:

{% ifnotequal -1 -5 %}
hello
{% endifnotequal %}

prints nothing

03/06/07 18:50:56 changed by mtredinnick

  • needs_better_patch set to 1.
  • needs_tests set to 1.

This patch doesn't handle the case of {% ifequal foo -elephant %} sensibly. It will try to process "-elephant" as if it were a number. I realise that is an error in any case, but we shouldn't confuse things further.

03/06/07 18:51:10 changed by mtredinnick

  • stage changed from Unreviewed to Accepted.

03/06/07 21:16:24 changed by SmileyChris

  • attachment numeric.patch added.

03/06/07 21:19:30 changed by SmileyChris

  • attachment numeric_with_negative.patch added.

03/06/07 21:20:28 changed by SmileyChris

  • needs_better_patch deleted.
  • needs_tests deleted.
  • stage changed from Accepted to Ready for checkin.

Straightforward enough.

03/08/07 23:41:24 changed by mtredinnick

We run this piece of code a *lot* (for every argument in every template tag), so speed is important. I'm going to hold off committing whilst I do some timing tests on different approaches. No need to rewrite the patch in any way -- it's correct on a functional level. I just want to try out a few options whilst we're playing in this area.

03/09/07 00:07:01 changed by mtredinnick

The reg-exp seems to be fastest here. I tried a couple of ways of removing the double scan over the string in the case of numbers (we do a reg-exp pass and then a pass for '.'), but it added more code than speed-ups and wasn't worth it.

03/09/07 00:12:16 changed by mtredinnick

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

(In [4690]) Fixed #3670 -- Fixed template argument parsing so that it understands negative floats and integers as numeric types. Patch from SmileyChris?.


Add/Change #3670 (ifequal and ifnotequal failing with negative numbers)




Change Properties
Action