Ticket #13956: indefinite_args_patch_with_complete_tests.diff
File indefinite_args_patch_with_complete_tests.diff, 10.2 KB (added by , 14 years ago) |
---|
-
django/template/__init__.py
848 848 return '' 849 849 return _render_value_in_context(output, context) 850 850 851 def generic_tag_compiler(params, defaults, name, node_class, parser, token ):851 def generic_tag_compiler(params, defaults, name, node_class, parser, token, varargs=None): 852 852 "Returns a template.Node subclass." 853 853 bits = token.split_contents()[1:] 854 854 bmax = len(params) 855 855 def_len = defaults and len(defaults) or 0 856 856 bmin = bmax - def_len 857 if(len(bits) < bmin or len(bits) > bmax): 858 if bmin == bmax: 859 message = "%s takes %s arguments" % (name, bmin) 860 else: 861 message = "%s takes between %s and %s arguments" % (name, bmin, bmax) 862 raise TemplateSyntaxError(message) 857 if varargs is None: 858 if(len(bits) < bmin or len(bits) > bmax): 859 if bmin == bmax: 860 message = "%s takes %s arguments" % (name, bmin) 861 else: 862 message = "%s takes between %s and %s arguments" % (name, bmin, bmax) 863 raise TemplateSyntaxError(message) 864 else: 865 if(len(bits) < bmin): 866 message = "%s takes at least %s arguments" % (name, bmin) 867 raise TemplateSyntaxError(message) 863 868 return node_class(bits) 864 869 865 870 class Library(object): … … 916 921 return func 917 922 918 923 def simple_tag(self,func): 919 params, xx, xxx, defaults = getargspec(func)924 params, varargs, xx, defaults = getargspec(func) 920 925 921 926 class SimpleNode(Node): 922 927 def __init__(self, vars_to_resolve): … … 926 931 resolved_vars = [var.resolve(context) for var in self.vars_to_resolve] 927 932 return func(*resolved_vars) 928 933 929 compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, SimpleNode )934 compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, SimpleNode, varargs=varargs) 930 935 compile_func.__doc__ = func.__doc__ 931 936 self.tag(getattr(func, "_decorated_function", func).__name__, compile_func) 932 937 return func 933 938 934 939 def inclusion_tag(self, file_name, context_class=Context, takes_context=False): 935 940 def dec(func): 936 params, xx, xxx, defaults = getargspec(func)941 params, varargs, xx, defaults = getargspec(func) 937 942 if takes_context: 938 943 if params[0] == 'context': 939 944 params = params[1:] … … 968 973 if csrf_token is not None: 969 974 new_context['csrf_token'] = csrf_token 970 975 return self.nodelist.render(new_context) 971 972 compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, InclusionNode) 976 compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, InclusionNode, varargs=varargs) 973 977 compile_func.__doc__ = func.__doc__ 974 978 self.tag(getattr(func, "_decorated_function", func).__name__, compile_func) 975 979 return func -
tests/regressiontests/templates/tests.py
1350 1350 # Escape requirement survives lookup. 1351 1351 'autoescape-lookup01': ('{{ var.key }}', { "var": {"key": "this & that" }}, "this & that" ), 1352 1352 1353 1354 ### SIMPLE TAGS ###################################################### 1355 'simpletags01': ('{% load custom %}{% simple_tag_with_three_arguments "one" "two" "three" %}', {}, 'one, two, three'), 1356 'simpletags02': ('{% load custom %}{% simple_tag_with_three_arguments one two three %}', {'one': '1', 'two': '2', 'three': '3'}, '1, 2, 3'), 1357 1358 # Default arguments 1359 'simpletags-default01': ('{% load custom %}{% simple_tag_with_two_default_arguments "one" "two" "three" %}', {}, 'one, two, three'), 1360 'simpletags-default02': ('{% load custom %}{% simple_tag_with_two_default_arguments one three %}', {'one': '1', 'two': '2', 'three': '3'}, '1, 3, 3'), 1361 'simpletags-default03': ('{% load custom %}{% simple_tag_with_two_default_arguments one %}', {'one': 'first'}, 'first, 2, 3'), 1362 1363 # Unlimited arguments 1364 'simpletags-unlimited01': ('{% load custom %}{% simple_tag_with_unlimited_arguments "one" "two" "three" %}', {}, 'one, two, three'), 1365 'simpletags-unlimited02': ('{% load custom %}{% simple_tag_with_unlimited_arguments "one" %}', {}, 'one, 2'), 1366 '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'), 1367 'simpletags-unlimited04': ('{% load custom %}{% simple_tag_with_unlimited_arguments one %}', {'one': 'first'}, 'first, 2'), 1368 'simpletags-unlimited05': ('{% load custom %}{% simple_tag_with_only_unlimited_arguments one %}', {'one': 'first'}, 'first'), 1369 'simpletags-unlimited06': ('{% load custom %}{% simple_tag_with_only_unlimited_arguments %}', {}, ''), 1370 'simpletags-unlimited07': ('{% load custom %}{% simple_tag_with_only_unlimited_arguments "one" "2" "3" %}', {}, 'one, 2, 3'), 1371 1372 # Failures 1373 'simpletags-fail01': ('{% load custom %}{% simple_tag_with_three_arguments "one" "three" %}', {}, template.TemplateSyntaxError), 1374 'simpletags-fail02': ('{% load custom %}{% simple_tag_with_three_arguments "one" "two" "three" "four" %}', {}, template.TemplateSyntaxError), 1375 'simpletags-fail03': ('{% load custom %}{% simple_tag_with_two_default_arguments %}', {}, template.TemplateSyntaxError), 1376 'simpletags-fail04': ('{% load custom %}{% simple_tag_with_two_default_arguments "one" "two" "three" "four" %}', {}, template.TemplateSyntaxError), 1377 'simpletags-fail05': ('{% load custom %}{% simple_tag_with_unlimited_arguments %}', {}, template.TemplateSyntaxError), 1378 1379 1380 1381 ### INCLUSION TAGS ###################################################### 1382 'inclusiontags01': ('{% load custom %}{% inclusion_tag_with_two_arguments "1" "2" %}', {}, "1 --- 2"), 1383 'inclusiontags02': ('{% load custom %}{% inclusion_tag_with_two_arguments "1" two %}', {'two': '2'}, "1 --- 2"), 1384 1385 # Default arguments 1386 'inclusiontags-default01': ('{% load custom %}{% inclusion_tag_with_one_default_argument "1" "2" %}', {}, "1 --- 2"), 1387 'inclusiontags-default02': ('{% load custom %}{% inclusion_tag_with_one_default_argument one %}', {'one': "1"}, "1 --- hi"), 1388 1389 # Unlimited arguments 1390 'inclusiontags-unlimited01': ('{% load custom %}{% inclusion_tag_with_unlimited_arguments "1" "2" %}', {}, "12"), 1391 'inclusiontags-unlimited02': ('{% load custom %}{% inclusion_tag_with_unlimited_arguments one %}', {'one': 'first'}, "firsthi"), 1392 'inclusiontags-unlimited03': ('{% load custom %}{% inclusion_tag_with_unlimited_arguments "1" "2" "3" "5" "7" %}', {}, "12357"), 1393 'inclusiontags-unlimited04': ('{% load custom %}{% inclusion_tag_with_only_unlimited_arguments %}', {}, ""), 1394 'inclusiontags-unlimited05': ('{% load custom %}{% inclusion_tag_with_only_unlimited_arguments "1" "2" "3" "5" hello %}', {'hello': 'hello'}, "1235hello"), 1395 1396 # Failures 1397 'inclusiontags-fail01': ('{% load custom %}{% inclusion_tag_with_two_arguments "1" %}', {}, template.TemplateSyntaxError), 1398 'inclusiontags-fail02': ('{% load custom %}{% inclusion_tag_with_two_arguments "1" two "3" %}', {'two': '2'}, template.TemplateSyntaxError), 1399 'inclusiontags-fail03': ('{% load custom %}{% inclusion_tag_with_one_default_argument %}', {}, template.TemplateSyntaxError), 1400 'inclusiontags-fail04': ('{% load custom %}{% inclusion_tag_with_one_default_argument "1" two "3" %}', {'two': '2'}, template.TemplateSyntaxError), 1401 'inclusiontags-fail05': ('{% load custom %}{% inclusion_tag_with_unlimited_arguments %}', {}, template.TemplateSyntaxError), 1402 1353 1403 } 1354 1404 1355 1405 -
tests/regressiontests/templates/templatetags/custom.py
9 9 10 10 register.filter(trim) 11 11 12 13 def simple_tag_with_three_arguments(one, two, three): 14 return ', '.join((one, two, three)) 15 16 register.simple_tag(simple_tag_with_three_arguments) 17 18 19 def simple_tag_with_two_default_arguments(one, two='2', three='3'): 20 return ', '.join((one, two, three)) 21 22 register.simple_tag(simple_tag_with_two_default_arguments) 23 24 25 def simple_tag_with_unlimited_arguments(one, two='2', *args): 26 return ', '.join([one, two] + list(args)) 27 28 register.simple_tag(simple_tag_with_unlimited_arguments) 29 30 31 def simple_tag_with_only_unlimited_arguments(*args): 32 return ', '.join(args) 33 34 register.simple_tag(simple_tag_with_only_unlimited_arguments) 35 36 37 def inclusion_tag_with_two_arguments(one, two): 38 return { 39 'first': one, 40 'second': two 41 } 42 43 register.inclusion_tag('basic-syntax03')(inclusion_tag_with_two_arguments) 44 45 46 def inclusion_tag_with_one_default_argument(one, two='hi'): 47 return { 48 'first': one, 49 'second': two 50 } 51 52 register.inclusion_tag('basic-syntax03')(inclusion_tag_with_one_default_argument) 53 54 55 def inclusion_tag_with_unlimited_arguments(one, two='hi', *args): 56 return { 57 'values': [one, two] + list(args) 58 } 59 60 register.inclusion_tag('for-tag01')(inclusion_tag_with_unlimited_arguments) 61 62 63 def inclusion_tag_with_only_unlimited_arguments(*args): 64 return { 65 'values': args 66 } 67 68 register.inclusion_tag('for-tag01')(inclusion_tag_with_only_unlimited_arguments) 69 No newline at end of file