[Django] #33036: Custom tags with missing context param and no other params throw an unhelpful IndexError

24 views
Skip to first unread message

Django

unread,
Aug 18, 2021, 5:30:54 PM8/18/21
to django-...@googlegroups.com
#33036: Custom tags with missing context param and no other params throw an
unhelpful IndexError
-------------------------------------------+------------------------
Reporter: Matt Westcott | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: dev
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------------+------------------------
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)

--
Ticket URL: <https://code.djangoproject.com/ticket/33036>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 18, 2021, 5:33:18 PM8/18/21
to django-...@googlegroups.com
#33036: Custom tags with missing context param and no other params throw an
unhelpful IndexError
---------------------------------+--------------------------------------

Reporter: Matt Westcott | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Changes (by Matt Westcott):

* has_patch: 0 => 1


Comment:

PR: https://github.com/django/django/pull/14779

--
Ticket URL: <https://code.djangoproject.com/ticket/33036#comment:1>

Django

unread,
Aug 19, 2021, 12:54:56 AM8/19/21
to django-...@googlegroups.com
#33036: Custom tags with missing context param and no other params throw an
unhelpful IndexError
-------------------------------------+-------------------------------------
Reporter: Matt Westcott | Owner: Matt
Type: | Westcott
Cleanup/optimization | Status: assigned

Component: Template system | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* owner: nobody => Matt Westcott
* status: new => assigned
* type: Bug => Cleanup/optimization
* stage: Unreviewed => Accepted


Comment:

Thanks for the report.

--
Ticket URL: <https://code.djangoproject.com/ticket/33036#comment:2>

Django

unread,
Aug 19, 2021, 1:36:48 AM8/19/21
to django-...@googlegroups.com
#33036: Custom tags with missing context param and no other params throw an
unhelpful IndexError
-------------------------------------+-------------------------------------
Reporter: Matt Westcott | Owner: Matt
Type: | Westcott
Cleanup/optimization | Status: assigned
Component: Template system | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/33036#comment:3>

Django

unread,
Aug 19, 2021, 2:21:31 AM8/19/21
to django-...@googlegroups.com
#33036: Custom tags with missing context param and no other params throw an
unhelpful IndexError
-------------------------------------+-------------------------------------
Reporter: Matt Westcott | Owner: Matt
Type: | Westcott
Cleanup/optimization | Status: closed

Component: Template system | Version: dev
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"5092f7247de8d3aa2fdb762e50f5ac2aa4e6f6e0" 5092f72]:
{{{
#!CommitTicketReference repository=""
revision="5092f7247de8d3aa2fdb762e50f5ac2aa4e6f6e0"
Fixed #33036 -- Made simple_tag()/inclusion_tag() with takes_context raise
TemplateSyntaxError when function has no parameters.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33036#comment:4>

Reply all
Reply to author
Forward
0 new messages