Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.0.X]: Fixed #8462 -- Made `length` and `length_is` template filter…

…s 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.


git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10194 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 54d6fe97e9c90c92c562ecd3278c14f177b1e5b4 1 parent 4a5a21d
Gary Wilson Jr. authored March 30, 2009
10  django/template/defaultfilters.py
@@ -507,12 +507,18 @@ def last(value):
507 507
 
508 508
 def length(value):
509 509
     """Returns the length of the value - useful for lists."""
510  
-    return len(value)
  510
+    try:
  511
+        return len(value)
  512
+    except (ValueError, TypeError):
  513
+        return ''
511 514
 length.is_safe = True
512 515
 
513 516
 def length_is(value, arg):
514 517
     """Returns a boolean of whether the value's length is the argument."""
515  
-    return len(value) == int(arg)
  518
+    try:
  519
+        return len(value) == int(arg)
  520
+    except (ValueError, TypeError):
  521
+        return ''
516 522
 length_is.is_safe = False
517 523
 
518 524
 def random(value):
26  tests/regressiontests/templates/filters.py
@@ -278,13 +278,33 @@ def get_filter_tests():
278 278
         'escapejs01': (r'{{ a|escapejs }}', {'a': 'testing\r\njavascript \'string" <b>escaping</b>'}, 'testing\\x0D\\x0Ajavascript \\x27string\\x22 \\x3Cb\\x3Eescaping\\x3C/b\\x3E'),
279 279
         'escapejs02': (r'{% autoescape off %}{{ a|escapejs }}{% endautoescape %}', {'a': 'testing\r\njavascript \'string" <b>escaping</b>'}, 'testing\\x0D\\x0Ajavascript \\x27string\\x22 \\x3Cb\\x3Eescaping\\x3C/b\\x3E'),
280 280
 
  281
+
  282
+        # length filter.
  283
+        'length01': ('{{ list|length }}', {'list': ['4', None, True, {}]}, '4'),
  284
+        'length02': ('{{ list|length }}', {'list': []}, '0'),
  285
+        'length03': ('{{ string|length }}', {'string': ''}, '0'),
  286
+        'length04': ('{{ string|length }}', {'string': 'django'}, '6'),
  287
+        # Invalid uses that should fail silently.
  288
+        'length05': ('{{ int|length }}', {'int': 7}, ''),
  289
+        'length06': ('{{ None|length }}', {'None': None}, ''),
  290
+
  291
+        # length_is filter.
  292
+        'length_is01': ('{% if some_list|length_is:"4" %}Four{% endif %}', {'some_list': ['4', None, True, {}]}, 'Four'),
  293
+        'length_is02': ('{% if some_list|length_is:"4" %}Four{% else %}Not Four{% endif %}', {'some_list': ['4', None, True, {}, 17]}, 'Not Four'),
  294
+        'length_is03': ('{% if mystring|length_is:"4" %}Four{% endif %}', {'mystring': 'word'}, 'Four'),
  295
+        'length_is04': ('{% if mystring|length_is:"4" %}Four{% else %}Not Four{% endif %}', {'mystring': 'Python'}, 'Not Four'),
  296
+        'length_is05': ('{% if mystring|length_is:"4" %}Four{% else %}Not Four{% endif %}', {'mystring': ''}, 'Not Four'),
  297
+        'length_is06': ('{% with var|length as my_length %}{{ my_length }}{% endwith %}', {'var': 'django'}, '6'),
281 298
         # Boolean return value from length_is should not be coerced to a string
282  
-        'lengthis01': (r'{% if "X"|length_is:0 %}Length is 0{% else %}Length not 0{% endif %}', {}, 'Length not 0'),
283  
-        'lengthis02': (r'{% if "X"|length_is:1 %}Length is 1{% else %}Length not 1{% endif %}', {}, 'Length is 1'),
  299
+        'length_is07': (r'{% if "X"|length_is:0 %}Length is 0{% else %}Length not 0{% endif %}', {}, 'Length not 0'),
  300
+        'length_is08': (r'{% if "X"|length_is:1 %}Length is 1{% else %}Length not 1{% endif %}', {}, 'Length is 1'),
  301
+        # Invalid uses that should fail silently.
  302
+        'length_is09': ('{{ var|length_is:"fish" }}', {'var': 'django'}, ''),
  303
+        'length_is10': ('{{ int|length_is:"1" }}', {'int': 7}, ''),
  304
+        'length_is11': ('{{ none|length_is:"1" }}', {'none': None}, ''),
284 305
 
285 306
         'join01': (r'{{ a|join:", " }}', {'a': ['alpha', 'beta & me']}, 'alpha, beta &amp; me'),
286 307
         'join02': (r'{% autoescape off %}{{ a|join:", " }}{% endautoescape %}', {'a': ['alpha', 'beta & me']}, 'alpha, beta & me'),
287 308
         'join03': (r'{{ a|join:" &amp; " }}', {'a': ['alpha', 'beta & me']}, 'alpha &amp; beta &amp; me'),
288 309
         'join04': (r'{% autoescape off %}{{ a|join:" &amp; " }}{% endautoescape %}', {'a': ['alpha', 'beta & me']}, 'alpha &amp; beta & me'),
289 310
     }
290  
-

0 notes on commit 54d6fe9

Please sign in to comment.
Something went wrong with that request. Please try again.