From 759ae3c2dafe149485a15d9a3bc97527c1ac1163 Mon Sep 17 00:00:00 2001 From: Karen Tracey Date: Sat, 11 Aug 2012 19:15:50 -0400 Subject: [PATCH] Fixed #18739 -- witdthratio behavior on None args Made behavior given None consistent with how non-numerics were handled. Thanks to ja.geb@me.com for the report. --- django/template/defaulttags.py | 4 ++-- tests/regressiontests/templates/tests.py | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py index 14391f08e1d..cb2ecd26d8f 100644 --- a/django/template/defaulttags.py +++ b/django/template/defaulttags.py @@ -448,7 +448,7 @@ class WidthRatioNode(Node): max_width = int(self.max_width.resolve(context)) except VariableDoesNotExist: return '' - except ValueError: + except (ValueError, TypeError): raise TemplateSyntaxError("widthratio final argument must be an number") try: value = float(value) @@ -456,7 +456,7 @@ class WidthRatioNode(Node): ratio = (value / max_value) * max_width except ZeroDivisionError: return '0' - except ValueError: + except (ValueError, TypeError): return '' return str(int(round(ratio))) diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py index edbb21b6bd8..a21b7ff323d 100644 --- a/tests/regressiontests/templates/tests.py +++ b/tests/regressiontests/templates/tests.py @@ -1466,6 +1466,14 @@ class Templates(unittest.TestCase): # #10043: widthratio should allow max_width to be a variable 'widthratio11': ('{% widthratio a b c %}', {'a':50,'b':100, 'c': 100}, '50'), + # #18739: widthratio should handle None args consistently with non-numerics + 'widthratio12a': ('{% widthratio a b c %}', {'a':'a','b':100,'c':100}, ''), + 'widthratio12b': ('{% widthratio a b c %}', {'a':None,'b':100,'c':100}, ''), + 'widthratio13a': ('{% widthratio a b c %}', {'a':0,'b':'b','c':100}, ''), + 'widthratio13b': ('{% widthratio a b c %}', {'a':0,'b':None,'c':100}, ''), + 'widthratio14a': ('{% widthratio a b c %}', {'a':0,'b':100,'c':'c'}, template.TemplateSyntaxError), + 'widthratio14b': ('{% widthratio a b c %}', {'a':0,'b':100,'c':None}, template.TemplateSyntaxError), + ### WITH TAG ######################################################## 'with01': ('{% with key=dict.key %}{{ key }}{% endwith %}', {'dict': {'key': 50}}, '50'), 'legacywith01': ('{% with dict.key as key %}{{ key }}{% endwith %}', {'dict': {'key': 50}}, '50'),