Opened 4 years ago
Closed 4 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:1 by , 4 years ago
| Has patch: | set |
|---|
comment:2 by , 4 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
| Triage Stage: | Unreviewed → Accepted |
| Type: | Bug → Cleanup/optimization |
Thanks for the report.
comment:3 by , 4 years ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
Note:
See TracTickets
for help on using tickets.
PR: https://github.com/django/django/pull/14779