From 7fec5a2240835af7c7f3accd64d9d894d4f92782 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Thu, 19 Sep 2013 09:27:19 -0400 Subject: [PATCH] Fixed #7557 -- Added type checking to Variable initialization. Thanks tobias for the suggestion and boblefrag and saz for work on the patch. --- django/template/base.py | 3 +++ tests/template_tests/test_parser.py | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/django/template/base.py b/django/template/base.py index c6ab7790a0..39b3bcae5c 100644 --- a/django/template/base.py +++ b/django/template/base.py @@ -675,6 +675,9 @@ class Variable(object): 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. # diff --git a/tests/template_tests/test_parser.py b/tests/template_tests/test_parser.py index 159a240530..8deb11da89 100644 --- a/tests/template_tests/test_parser.py +++ b/tests/template_tests/test_parser.py @@ -87,6 +87,10 @@ class ParserTests(TestCase): 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