Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#8462 closed (fixed)

length and length_is filters don't fail silently

Reported by: magneto Owned by: Gary Wilson
Component: Template system Version: master
Severity: Keywords: length
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

django/template/defaultfilters.py", line 469, in length

if the input object has no len defined, this throws a nasty exception .. given the very large number of time i imagine this is used

would it be proper to simply 'return 0' for non list like things?

in particular the 'with' command is problematic (if 'my_thing' is unresolved, it blows up)

{% with my_thing|length as my_length %}

{% endwith %}
Index: django/template/defaultfilters.py
===================================================================
--- django/template/defaultfilters.py   (revision 8455)
+++ django/template/defaultfilters.py   (working copy)
@@ -466,7 +466,10 @@
 
 def length(value):
     """Returns the length of the value - useful for lists."""
-    return len(value)
+    try:
+        return len(value)
+    except:
+        return 0
 length.is_safe = True
 
 def length_is(value, arg):

Attachments (4)

length_invalid_usage_8462.2.diff (757 bytes) - added by marcelor 8 years ago.
More specific exception catching.
length_invalid_usage_8462.diff (761 bytes) - added by marcelor 8 years ago.
Fixed bad syntax.
add_template_length_filters_tests.diff (2.2 KB) - added by rob 8 years ago.
keeping the filter diffs and test diffs together
add_template_length_filters_tests_2.diff (2.3 KB) - added by Chris Beaven 8 years ago.
PEP8 pedantry and one more test

Download all attachments as: .zip

Change History (14)

comment:1 Changed 8 years ago by Chris Beaven

Needs tests: set
Patch needs improvement: set
Summary: template tag Length and the 'error'length and length_is filters don't fail silently
Triage Stage: UnreviewedAccepted

This is also an issue for length_is.

I think returning '' would make more sense for the length filter (it's invalid, not 0)

comment:2 Changed 8 years ago by Malcolm Tredinnick

I agree with Chris: if you're applying length or length_is to something that isn't measurable, it's an invalid usage, so returning an empty string is probably better.

comment:3 Changed 8 years ago by marcelor

Owner: changed from nobody to marcelor
Status: newassigned

Submitted patch which makes both filters to return when their arguments are not measurable.

comment:4 Changed 8 years ago by Chris Beaven

Marcelor, if you wouldn't mind extending the template filter test suite to encompass this change then that'll speed up the checkin process.

Oh, and it'd be better style to just catch ValueError and TypeError rather than a raw except.

Changed 8 years ago by marcelor

More specific exception catching.

Changed 8 years ago by marcelor

Fixed bad syntax.

Changed 8 years ago by rob

keeping the filter diffs and test diffs together

comment:5 Changed 8 years ago by rob

Has patch: set
Needs tests: unset
Patch needs improvement: unset

i think that takes care of needing tests

Changed 8 years ago by Chris Beaven

PEP8 pedantry and one more test

comment:6 Changed 8 years ago by Chris Beaven

Triage Stage: AcceptedReady for checkin

Thanks marcelor and rob. Marking for checkin

comment:7 Changed 8 years ago by Gary Wilson

Owner: changed from marcelor to Gary Wilson
Status: assignednew

comment:8 Changed 8 years ago by Gary Wilson

Status: newassigned

comment:9 Changed 8 years ago by Gary Wilson

Resolution: fixed
Status: assignedclosed

(In [10193]) Fixed #8462 -- Made length and length_is template filters fail silently when given a value that has undefined length and added tests for both filters. Based on patch from marcelor, rob, and SmileyChris.

comment:10 Changed 8 years ago by Gary Wilson

(In [10194]) [1.0.X]: Fixed #8462 -- Made length and length_is template filters fail silently when given a value that has undefined length and added tests for both filters. Based on patch from marcelor, rob, and SmileyChris.

Backport of r10193 from trunk.

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