Ticket #13956: patch_r16322.diff
File patch_r16322.diff, 10.9 KB (added by , 13 years ago) |
---|
-
django/template/base.py
diff --git a/django/template/base.py b/django/template/base.py index eacc29c..cbe5cb3 100644
a b class VariableNode(Node): 799 799 return '' 800 800 return _render_value_in_context(output, context) 801 801 802 def generic_tag_compiler(params, defaults, name, node_class, parser, token ):802 def generic_tag_compiler(params, defaults, name, node_class, parser, token, varargs=None): 803 803 "Returns a template.Node subclass." 804 804 bits = token.split_contents()[1:] 805 805 bmax = len(params) 806 806 def_len = defaults and len(defaults) or 0 807 807 bmin = bmax - def_len 808 if(len(bits) < bmin or len(bits) > bmax): 809 if bmin == bmax: 810 message = "%s takes %s arguments" % (name, bmin) 811 else: 812 message = "%s takes between %s and %s arguments" % (name, bmin, bmax) 813 raise TemplateSyntaxError(message) 808 if varargs is None: 809 if(len(bits) < bmin or len(bits) > bmax): 810 if bmin == bmax: 811 message = "%s takes %s arguments" % (name, bmin) 812 else: 813 message = "%s takes between %s and %s arguments" % (name, bmin, bmax) 814 raise TemplateSyntaxError(message) 815 else: 816 if(len(bits) < bmin): 817 message = "%s takes at least %s arguments" % (name, bmin) 818 raise TemplateSyntaxError(message) 814 819 return node_class(bits) 815 820 816 821 class Library(object): … … class Library(object): 868 873 869 874 def simple_tag(self, func=None, takes_context=None): 870 875 def dec(func): 871 params, xx, xxx, defaults = getargspec(func)876 params, varargs, xxx, defaults = getargspec(func) 872 877 if takes_context: 873 878 if params[0] == 'context': 874 879 params = params[1:] … … class Library(object): 887 892 func_args = resolved_vars 888 893 return func(*func_args) 889 894 890 compile_func = partial(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, SimpleNode )895 compile_func = partial(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, SimpleNode, varargs=varargs) 891 896 compile_func.__doc__ = func.__doc__ 892 897 self.tag(getattr(func, "_decorated_function", func).__name__, compile_func) 893 898 return func … … class Library(object): 963 968 964 969 def inclusion_tag(self, file_name, context_class=Context, takes_context=False): 965 970 def dec(func): 966 params, xx, xxx, defaults = getargspec(func)971 params, varargs, xxx, defaults = getargspec(func) 967 972 if takes_context: 968 973 if params[0] == 'context': 969 974 params = params[1:] … … class Library(object): 1003 1008 new_context['csrf_token'] = csrf_token 1004 1009 return self.nodelist.render(new_context) 1005 1010 1006 compile_func = partial(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, InclusionNode )1011 compile_func = partial(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, InclusionNode, varargs=varargs) 1007 1012 compile_func.__doc__ = func.__doc__ 1008 1013 self.tag(getattr(func, "_decorated_function", func).__name__, compile_func) 1009 1014 return func -
tests/regressiontests/templates/templatetags/custom.py
diff --git a/tests/regressiontests/templates/templatetags/custom.py b/tests/regressiontests/templates/templatetags/custom.py index 2e281f7..51c0cb9 100644
a b def params_and_context(context, arg): 39 39 return "params_and_context - Expected result (context value: %s): %s" % (context['value'], arg) 40 40 params_and_context.anything = "Expected params_and_context __dict__" 41 41 42 @register.simple_tag 43 def simple_tag_with_three_arguments(one, two, three): 44 return ', '.join((one, two, three)) 45 46 @register.simple_tag 47 def simple_tag_with_two_default_arguments(one, two='2', three='3'): 48 return ', '.join((one, two, three)) 49 50 @register.simple_tag 51 def simple_tag_with_unlimited_arguments(one, two='2', *args): 52 return ', '.join([one, two] + list(args)) 53 54 @register.simple_tag 55 def simple_tag_with_only_unlimited_arguments(*args): 56 return ', '.join(args) 57 42 58 @register.inclusion_tag('inclusion.html') 43 59 def inclusion_no_params(): 44 60 """Expected inclusion_no_params __doc__""" … … def inclusion_params_and_context(context, arg): 69 85 return {"result" : "inclusion_params_and_context - Expected result (context value: %s): %s" % (context['value'], arg)} 70 86 inclusion_params_and_context.anything = "Expected inclusion_params_and_context __dict__" 71 87 88 @register.inclusion_tag('basic-syntax03') 89 def inclusion_tag_with_two_arguments(one, two): 90 return { 91 'first': one, 92 'second': two 93 } 94 95 @register.inclusion_tag('basic-syntax03') 96 def inclusion_tag_with_one_default_argument(one, two='hi'): 97 return { 98 'first': one, 99 'second': two 100 } 101 102 @register.inclusion_tag('for-tag01') 103 def inclusion_tag_with_unlimited_arguments(one, two='hi', *args): 104 return { 105 'values': [one, two] + list(args) 106 } 107 108 @register.inclusion_tag('for-tag01') 109 def inclusion_tag_with_only_unlimited_arguments(*args): 110 return { 111 'values': args 112 } 113 72 114 @register.simple_tag(takes_context=True) 73 115 def current_app(context): 74 116 return "%s" % context.current_app -
tests/regressiontests/templates/tests.py
diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py index 5237bb6..f819006 100644
a b class Templates(unittest.TestCase): 1599 1599 'static-prefixtag02': ('{% load static %}{% get_static_prefix as static_prefix %}{{ static_prefix }}', {}, settings.STATIC_URL), 1600 1600 'static-prefixtag03': ('{% load static %}{% get_media_prefix %}', {}, settings.MEDIA_URL), 1601 1601 'static-prefixtag04': ('{% load static %}{% get_media_prefix as media_prefix %}{{ media_prefix }}', {}, settings.MEDIA_URL), 1602 1603 1604 ### SIMPLE TAGS ###################################################### 1605 'simpletags01': ('{% load custom %}{% simple_tag_with_three_arguments "one" "two" "three" %}', {}, 'one, two, three'), 1606 'simpletags02': ('{% load custom %}{% simple_tag_with_three_arguments one two three %}', {'one': '1', 'two': '2', 'three': '3'}, '1, 2, 3'), 1607 1608 # Default arguments 1609 'simpletags-default01': ('{% load custom %}{% simple_tag_with_two_default_arguments "one" "two" "three" %}', {}, 'one, two, three'), 1610 'simpletags-default02': ('{% load custom %}{% simple_tag_with_two_default_arguments one three %}', {'one': '1', 'two': '2', 'three': '3'}, '1, 3, 3'), 1611 'simpletags-default03': ('{% load custom %}{% simple_tag_with_two_default_arguments one %}', {'one': 'first'}, 'first, 2, 3'), 1612 1613 # Unlimited arguments 1614 'simpletags-unlimited01': ('{% load custom %}{% simple_tag_with_unlimited_arguments "one" "two" "three" %}', {}, 'one, two, three'), 1615 'simpletags-unlimited02': ('{% load custom %}{% simple_tag_with_unlimited_arguments "one" %}', {}, 'one, 2'), 1616 'simpletags-unlimited03': ('{% load custom %}{% simple_tag_with_unlimited_arguments one two three four five %}', {'one': '1', 'two': '2', 'three': '3', 'four': '4', 'five': '5'}, '1, 2, 3, 4, 5'), 1617 'simpletags-unlimited04': ('{% load custom %}{% simple_tag_with_unlimited_arguments one %}', {'one': 'first'}, 'first, 2'), 1618 'simpletags-unlimited05': ('{% load custom %}{% simple_tag_with_only_unlimited_arguments one %}', {'one': 'first'}, 'first'), 1619 'simpletags-unlimited06': ('{% load custom %}{% simple_tag_with_only_unlimited_arguments %}', {}, ''), 1620 'simpletags-unlimited07': ('{% load custom %}{% simple_tag_with_only_unlimited_arguments "one" "2" "3" %}', {}, 'one, 2, 3'), 1621 1622 # Failures 1623 'simpletags-fail01': ('{% load custom %}{% simple_tag_with_three_arguments "one" "three" %}', {}, template.TemplateSyntaxError), 1624 'simpletags-fail02': ('{% load custom %}{% simple_tag_with_three_arguments "one" "two" "three" "four" %}', {}, template.TemplateSyntaxError), 1625 'simpletags-fail03': ('{% load custom %}{% simple_tag_with_two_default_arguments %}', {}, template.TemplateSyntaxError), 1626 'simpletags-fail04': ('{% load custom %}{% simple_tag_with_two_default_arguments "one" "two" "three" "four" %}', {}, template.TemplateSyntaxError), 1627 'simpletags-fail05': ('{% load custom %}{% simple_tag_with_unlimited_arguments %}', {}, template.TemplateSyntaxError), 1628 1629 1630 1631 ### INCLUSION TAGS ###################################################### 1632 'inclusiontags01': ('{% load custom %}{% inclusion_tag_with_two_arguments "1" "2" %}', {}, "1 --- 2"), 1633 'inclusiontags02': ('{% load custom %}{% inclusion_tag_with_two_arguments "1" two %}', {'two': '2'}, "1 --- 2"), 1634 1635 # Default arguments 1636 'inclusiontags-default01': ('{% load custom %}{% inclusion_tag_with_one_default_argument "1" "2" %}', {}, "1 --- 2"), 1637 'inclusiontags-default02': ('{% load custom %}{% inclusion_tag_with_one_default_argument one %}', {'one': "1"}, "1 --- hi"), 1638 1639 # Unlimited arguments 1640 'inclusiontags-unlimited01': ('{% load custom %}{% inclusion_tag_with_unlimited_arguments "1" "2" %}', {}, "12"), 1641 'inclusiontags-unlimited02': ('{% load custom %}{% inclusion_tag_with_unlimited_arguments one %}', {'one': 'first'}, "firsthi"), 1642 'inclusiontags-unlimited03': ('{% load custom %}{% inclusion_tag_with_unlimited_arguments "1" "2" "3" "5" "7" %}', {}, "12357"), 1643 'inclusiontags-unlimited04': ('{% load custom %}{% inclusion_tag_with_only_unlimited_arguments %}', {}, ""), 1644 'inclusiontags-unlimited05': ('{% load custom %}{% inclusion_tag_with_only_unlimited_arguments "1" "2" "3" "5" hello %}', {'hello': 'hello'}, "1235hello"), 1645 1646 # Failures 1647 'inclusiontags-fail01': ('{% load custom %}{% inclusion_tag_with_two_arguments "1" %}', {}, template.TemplateSyntaxError), 1648 'inclusiontags-fail02': ('{% load custom %}{% inclusion_tag_with_two_arguments "1" two "3" %}', {'two': '2'}, template.TemplateSyntaxError), 1649 'inclusiontags-fail03': ('{% load custom %}{% inclusion_tag_with_one_default_argument %}', {}, template.TemplateSyntaxError), 1650 'inclusiontags-fail04': ('{% load custom %}{% inclusion_tag_with_one_default_argument "1" two "3" %}', {'two': '2'}, template.TemplateSyntaxError), 1651 'inclusiontags-fail05': ('{% load custom %}{% inclusion_tag_with_unlimited_arguments %}', {}, template.TemplateSyntaxError), 1602 1652 } 1603 1653 1604 1654 class TemplateTagLoading(unittest.TestCase):