Ticket #13956: patch_r16322.diff

File patch_r16322.diff, 10.9 KB (added by Stephen Burrows, 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):  
    799799            return ''
    800800        return _render_value_in_context(output, context)
    801801
    802 def generic_tag_compiler(params, defaults, name, node_class, parser, token):
     802def generic_tag_compiler(params, defaults, name, node_class, parser, token, varargs=None):
    803803    "Returns a template.Node subclass."
    804804    bits = token.split_contents()[1:]
    805805    bmax = len(params)
    806806    def_len = defaults and len(defaults) or 0
    807807    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)
    814819    return node_class(bits)
    815820
    816821class Library(object):
    class Library(object):  
    868873
    869874    def simple_tag(self, func=None, takes_context=None):
    870875        def dec(func):
    871             params, xx, xxx, defaults = getargspec(func)
     876            params, varargs, xxx, defaults = getargspec(func)
    872877            if takes_context:
    873878                if params[0] == 'context':
    874879                    params = params[1:]
    class Library(object):  
    887892                        func_args = resolved_vars
    888893                    return func(*func_args)
    889894
    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)
    891896            compile_func.__doc__ = func.__doc__
    892897            self.tag(getattr(func, "_decorated_function", func).__name__, compile_func)
    893898            return func
    class Library(object):  
    963968
    964969    def inclusion_tag(self, file_name, context_class=Context, takes_context=False):
    965970        def dec(func):
    966             params, xx, xxx, defaults = getargspec(func)
     971            params, varargs, xxx, defaults = getargspec(func)
    967972            if takes_context:
    968973                if params[0] == 'context':
    969974                    params = params[1:]
    class Library(object):  
    10031008                        new_context['csrf_token'] = csrf_token
    10041009                    return self.nodelist.render(new_context)
    10051010
    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)
    10071012            compile_func.__doc__ = func.__doc__
    10081013            self.tag(getattr(func, "_decorated_function", func).__name__, compile_func)
    10091014            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):  
    3939    return "params_and_context - Expected result (context value: %s): %s" % (context['value'], arg)
    4040params_and_context.anything = "Expected params_and_context __dict__"
    4141
     42@register.simple_tag
     43def simple_tag_with_three_arguments(one, two, three):
     44    return ', '.join((one, two, three))
     45
     46@register.simple_tag
     47def simple_tag_with_two_default_arguments(one, two='2', three='3'):
     48    return ', '.join((one, two, three))
     49
     50@register.simple_tag
     51def simple_tag_with_unlimited_arguments(one, two='2', *args):
     52    return ', '.join([one, two] + list(args))
     53
     54@register.simple_tag
     55def simple_tag_with_only_unlimited_arguments(*args):
     56    return ', '.join(args)
     57
    4258@register.inclusion_tag('inclusion.html')
    4359def inclusion_no_params():
    4460    """Expected inclusion_no_params __doc__"""
    def inclusion_params_and_context(context, arg):  
    6985    return {"result" : "inclusion_params_and_context - Expected result (context value: %s): %s" % (context['value'], arg)}
    7086inclusion_params_and_context.anything = "Expected inclusion_params_and_context __dict__"
    7187
     88@register.inclusion_tag('basic-syntax03')
     89def inclusion_tag_with_two_arguments(one, two):
     90    return {
     91        'first': one,
     92        'second': two
     93    }
     94
     95@register.inclusion_tag('basic-syntax03')
     96def 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')
     103def 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')
     109def inclusion_tag_with_only_unlimited_arguments(*args):
     110    return {
     111        'values': args
     112    }
     113
    72114@register.simple_tag(takes_context=True)
    73115def current_app(context):
    74116    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):  
    15991599            'static-prefixtag02': ('{% load static %}{% get_static_prefix as static_prefix %}{{ static_prefix }}', {}, settings.STATIC_URL),
    16001600            'static-prefixtag03': ('{% load static %}{% get_media_prefix %}', {}, settings.MEDIA_URL),
    16011601            '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),
    16021652        }
    16031653
    16041654class TemplateTagLoading(unittest.TestCase):
Back to Top