Jelajahi Sumber

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.
Karen Tracey 12 tahun lalu
induk
melakukan
759ae3c2da
2 mengubah file dengan 10 tambahan dan 2 penghapusan
  1. 2 2
      django/template/defaulttags.py
  2. 8 0
      tests/regressiontests/templates/tests.py

+ 2 - 2
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)))
 

+ 8 - 0
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'),