#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)
Change History (14)
comment:1 by , 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: | Unreviewed → Accepted |
comment:2 by , 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 , 16 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
Submitted patch which makes both filters to return when their arguments are not measurable.
comment:4 by , 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 , 16 years ago
Attachment: | length_invalid_usage_8462.2.diff added |
---|
More specific exception catching.
by , 16 years ago
Attachment: | add_template_length_filters_tests.diff added |
---|
keeping the filter diffs and test diffs together
comment:5 by , 16 years ago
Has patch: | set |
---|---|
Needs tests: | unset |
Patch needs improvement: | unset |
i think that takes care of needing tests
by , 16 years ago
Attachment: | add_template_length_filters_tests_2.diff added |
---|
PEP8 pedantry and one more test
comment:6 by , 16 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
Thanks marcelor and rob. Marking for checkin
comment:7 by , 16 years ago
Owner: | changed from | to
---|---|
Status: | assigned → new |
comment:8 by , 16 years ago
Status: | new → assigned |
---|
comment:9 by , 16 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
This is also an issue for
length_is
.I think returning
''
would make more sense for the length filter (it's invalid, not0
)