Code

Opened 6 years ago

Closed 5 years ago

Last modified 5 years ago

#8462 closed (fixed)

length and length_is filters don't fail silently

Reported by: magneto Owned by: gwilson
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 5 years ago.
More specific exception catching.
length_invalid_usage_8462.diff (761 bytes) - added by marcelor 5 years ago.
Fixed bad syntax.
add_template_length_filters_tests.diff (2.2 KB) - added by rob 5 years ago.
keeping the filter diffs and test diffs together
add_template_length_filters_tests_2.diff (2.3 KB) - added by SmileyChris 5 years ago.
PEP8 pedantry and one more test

Download all attachments as: .zip

Change History (14)

comment:1 Changed 6 years ago by SmileyChris

  • Needs documentation unset
  • Needs tests set
  • Patch needs improvement set
  • Summary changed from template tag Length and the 'error' to length and length_is filters don't fail silently
  • Triage Stage changed from Unreviewed to Accepted

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 6 years ago by mtredinnick

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 5 years ago by marcelor

  • Owner changed from nobody to marcelor
  • Status changed from new to assigned

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

comment:4 Changed 5 years ago by SmileyChris

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 5 years ago by marcelor

More specific exception catching.

Changed 5 years ago by marcelor

Fixed bad syntax.

Changed 5 years ago by rob

keeping the filter diffs and test diffs together

comment:5 Changed 5 years ago by rob

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

i think that takes care of needing tests

Changed 5 years ago by SmileyChris

PEP8 pedantry and one more test

comment:6 Changed 5 years ago by SmileyChris

  • Triage Stage changed from Accepted to Ready for checkin

Thanks marcelor and rob. Marking for checkin

comment:7 Changed 5 years ago by gwilson

  • Owner changed from marcelor to gwilson
  • Status changed from assigned to new

comment:8 Changed 5 years ago by gwilson

  • Status changed from new to assigned

comment:9 Changed 5 years ago by gwilson

  • Resolution set to fixed
  • Status changed from assigned to closed

(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 5 years ago by gwilson

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.