Browse files

Fixed #7557 -- Added type checking to Variable initialization.

Thanks tobias for the suggestion and boblefrag and saz for work on the
patch.
  • Loading branch information...
1 parent 55b9bff commit 7fec5a2240835af7c7f3accd64d9d894d4f92782 @timgraham timgraham committed Sep 19, 2013
Showing with 7 additions and 0 deletions.
  1. +3 −0 django/template/base.py
  2. +4 −0 tests/template_tests/test_parser.py
View
3 django/template/base.py
@@ -675,6 +675,9 @@ def __init__(self, var):
self.translate = False
self.message_context = None
+ if not isinstance(var, six.string_types):
+ raise TypeError(
+ "Variable must be a string or number, got %s" % type(var))
try:
# First try to treat this variable as a number.
#
View
4 tests/template_tests/test_parser.py
@@ -87,6 +87,10 @@ def test_variable_parsing(self):
Variable, "article._hidden"
)
+ # Variables should raise on non string type
+ with six.assertRaisesRegex(self, TypeError, "Variable must be a string or number, got <(class|type) 'dict'>"):
+ Variable({})
+
@override_settings(DEBUG=True, TEMPLATE_DEBUG=True)
def test_compile_filter_error(self):
# regression test for #19819

0 comments on commit 7fec5a2

Please sign in to comment.