浏览代码

Fixed #21959 -- Handled Inf/NaN in widthratio tag.

Thanks rmoe for the report and the patch.
Aymeric Augustin 11 年之前
父节点
当前提交
07ae47f7f8
共有 2 个文件被更改,包括 4 次插入2 次删除
  1. 2 2
      django/template/defaulttags.py
  2. 2 0
      tests/template_tests/tests.py

+ 2 - 2
django/template/defaulttags.py

@@ -498,11 +498,11 @@ class WidthRatioNode(Node):
             value = float(value)
             max_value = float(max_value)
             ratio = (value / max_value) * max_width
+            result = str(int(round(ratio)))
         except ZeroDivisionError:
             return '0'
-        except (ValueError, TypeError):
+        except (ValueError, TypeError, OverflowError):
             return ''
-        result = str(int(round(ratio)))
 
         if self.asvar:
             context[self.asvar] = result

+ 2 - 0
tests/template_tests/tests.py

@@ -1668,6 +1668,8 @@ class TemplateTests(TestCase):
 
             'widthratio18': ('{% widthratio a b 100 as %}', {}, template.TemplateSyntaxError),
             'widthratio19': ('{% widthratio a b 100 not_as variable %}', {}, template.TemplateSyntaxError),
+            'widthratio20': ('{% widthratio a b 100 %}', {'a': float('inf'), 'b': float('inf')}, ''),
+            'widthratio21': ('{% widthratio a b 100 %}', {'a': float('inf'), 'b': 2}, ''),
 
             ### WITH TAG ########################################################
             'with01': ('{% with key=dict.key %}{{ key }}{% endwith %}', {'dict': {'key': 50}}, '50'),