Opened 16 years ago

Closed 16 years ago

Last modified 16 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: dev
Severity: Keywords: length
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

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 Marcelo Ramos 16 years ago.
More specific exception catching.
length_invalid_usage_8462.diff (761 bytes ) - added by Marcelo Ramos 16 years ago.
Fixed bad syntax.
add_template_length_filters_tests.diff (2.2 KB ) - added by rob 16 years ago.
keeping the filter diffs and test diffs together
add_template_length_filters_tests_2.diff (2.3 KB ) - added by Chris Beaven 16 years ago.
PEP8 pedantry and one more test

Download all attachments as: .zip

Change History (14)

comment:1 by Chris Beaven, 16 years ago

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 by Malcolm Tredinnick, 16 years ago

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 by Marcelo Ramos, 16 years ago

Owner: changed from nobody to Marcelo Ramos
Status: newassigned

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

comment:4 by Chris Beaven, 16 years ago

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.

by Marcelo Ramos, 16 years ago

More specific exception catching.

by Marcelo Ramos, 16 years ago

Fixed bad syntax.

by rob, 16 years ago

keeping the filter diffs and test diffs together

comment:5 by rob, 16 years ago

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

i think that takes care of needing tests

by Chris Beaven, 16 years ago

PEP8 pedantry and one more test

comment:6 by Chris Beaven, 16 years ago

Triage Stage: AcceptedReady for checkin

Thanks marcelor and rob. Marking for checkin

comment:7 by Gary Wilson, 16 years ago

Owner: changed from Marcelo Ramos to Gary Wilson
Status: assignednew

comment:8 by Gary Wilson, 16 years ago

Status: newassigned

comment:9 by Gary Wilson, 16 years ago

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 by Gary Wilson, 16 years ago

(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