Code

Ticket #7557: type_check.diff

File type_check.diff, 1.1 KB (added by boblefrag, 12 months ago)

use six.string_type instead of basestring for python3 compatibility. Added a test to check that a non string value raise a TypeError

Line 
1diff --git a/django/template/base.py b/django/template/base.py
2index c5bddaf..fb6b309 100644
3--- a/django/template/base.py
4+++ b/django/template/base.py
5@@ -690,7 +690,9 @@ class Variable(object):
6         self.lookups = None
7         self.translate = False
8         self.message_context = None
9-
10+        if not isinstance(var, six.string_types):
11+            raise TypeError(
12+                "Variable must be a string or number, got %s" % type(var))
13         try:
14             # First try to treat this variable as a number.
15             #
16diff --git a/tests/template_tests/test_parser.py b/tests/template_tests/test_parser.py
17index 9422da8..d5d8ec4 100644
18--- a/tests/template_tests/test_parser.py
19+++ b/tests/template_tests/test_parser.py
20@@ -85,6 +85,8 @@ class ParserTests(TestCase):
21         self.assertRaises(TemplateSyntaxError,
22             Variable, "article._hidden"
23         )
24+        # Variables should raise on non string type
25+        self.assertRaises(TypeError, Variable({}))
26 
27     @override_settings(DEBUG=True, TEMPLATE_DEBUG=True)
28     def test_compile_filter_error(self):