Opened 3 years ago

Closed 3 years ago

#33036 closed Cleanup/optimization (fixed)

Custom tags with missing context param and no other params throw an unhelpful IndexError

Reported by: Matt Westcott Owned by: Matt Westcott
Component: Template system Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Given a simple_tag or inclusion_tag with takes_context=True, where the context param has been forgotten:

@register.simple_tag(takes_context=True)
def simple_tag_without_context_parameter(arg):
    return "Expected result"

the parse_bits function checks for this case and throws an informative TemplateSyntaxError. However, in the case that the tag takes no other parameters:

@register.simple_tag(takes_context=True)
def simple_tag_no_params_without_context_parameter():
    return "Expected result"

the checking code fails at the point where it looks at params[0], throwing an opaque IndexError instead.

Traceback (most recent call last):
  File "/Users/matthew/Development/tbx/wagtail/devscript/libs/django/tests/template_tests/test_custom.py", line 179, in test_simple_tag_no_params_missing_context
    self.engine.from_string('{% load custom %}{% simple_tag_no_params_without_context_parameter %}')
  File "/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/template/engine.py", line 156, in from_string
    return Template(template_code, engine=self)
  File "/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/template/base.py", line 155, in __init__
    self.nodelist = self.compile_nodelist()
  File "/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/template/base.py", line 199, in compile_nodelist
    return parser.parse()
  File "/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/template/base.py", line 502, in parse
    raise self.error(token, e)
  File "/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/template/base.py", line 500, in parse
    compiled_result = compile_func(self, token)
  File "/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/template/library.py", line 119, in compile_func
    args, kwargs = parse_bits(
  File "/Users/matthew/Development/tbx/wagtail/devscript/libs/django/django/template/library.py", line 246, in parse_bits
    if params[0] == 'context':
IndexError: list index out of range

(PR to follow)

Change History (4)

comment:2 by Mariusz Felisiak, 3 years ago

Owner: changed from nobody to Matt Westcott
Status: newassigned
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

Thanks for the report.

comment:3 by Mariusz Felisiak, 3 years ago

Triage Stage: AcceptedReady for checkin

comment:4 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

Resolution: fixed
Status: assignedclosed

In 5092f72:

Fixed #33036 -- Made simple_tag()/inclusion_tag() with takes_context raise TemplateSyntaxError when function has no parameters.

Note: See TracTickets for help on using tickets.
Back to Top