[Django] #30425: Jinja: get_exception_info: source might be None

10 views
Skip to first unread message

Django

unread,
Apr 29, 2019, 3:50:43 PM4/29/19
to django-...@googlegroups.com
#30425: Jinja: get_exception_info: source might be None
-------------------------------------------+------------------------
Reporter: Daniel Hahler | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: 2.2
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 |
-------------------------------------------+------------------------
The "source" attribute might be None with syntax error exceptions with
Jinja2 templates.

The current code
(https://github.com/django/django/blob/673fe2e3ec63614259e86e7a370b9d1e91fcc1e1/django/template/backends/jinja2.py#L84-L108)
does not handle it, but could read the source then.

See https://github.com/niwinz/django-jinja/pull/233 for a fix with more
details.

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

Django

unread,
Apr 30, 2019, 2:51:43 AM4/30/19
to django-...@googlegroups.com
#30425: Handle `jinja2.TemplateSyntaxError` when rendering a template with or
without a source.
---------------------------------+------------------------------------

Reporter: Daniel Hahler | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------
Changes (by felixxm):

* version: 2.2 => master
* stage: Unreviewed => Accepted


Comment:

We don't catch `jinja2.TemplateSyntaxError` in
[https://github.com/django/django/blob/673fe2e3ec63614259e86e7a370b9d1e91fcc1e1/django/template/backends/jinja2.py#L61-L71
`Template.render()`], hence `get_exception_info()` is not called when
`jinja2.TemplateSyntaxError` is raised in rendering i.e. when `source` can
be `None`.

I agree that we should catch `jinja2.TemplateSyntaxError` in
`Template.render()` and in such case fix issue with `source = None`:

{{{ #!python
def render(self, context=None, request=None):
try:
...
return self.template.render(context)
except jinja2.TemplateSyntaxError as exc:
new = TemplateSyntaxError(exc.args)
new.template_debug = get_exception_info(exc)
raise new from exc
}}}

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

Django

unread,
Apr 30, 2019, 11:44:54 AM4/30/19
to django-...@googlegroups.com
#30425: Handle `jinja2.TemplateSyntaxError` when rendering a template with or
without a source.
---------------------------------+------------------------------------
Reporter: Daniel Hahler | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+------------------------------------

Comment (by Daniel Hahler):

Indeed I forgot to mention that I've also wrapped `render` in the first
place.

Just for reference, the patch for `render` for django-jinja:

{{{
diff --git i/django_jinja/backend.py w/django_jinja/backend.py
index 7663186..ffc64c7 100644
--- i/django_jinja/backend.py
+++ w/django_jinja/backend.py
@@ -69,6 +69,8 @@ def render(self, context=None, request=None):
context = base.dict_from_context(context)

if request is not None:
+ # import pudb; pu.db
+ __import__('pdb').set_trace()
def _get_val():
token = csrf.get_token(request)
if token is None:
@@ -103,7 +105,12 @@ def dicts(self):
template=self,
context=context)

- return mark_safe(self.template.render(context))
+ try:
+ return mark_safe(self.template.render(context))
+ except jinja2.TemplateSyntaxError as exc:
+ new = TemplateSyntaxError(exc.args)
+ new.template_debug = get_exception_info(exc)
+ six.reraise(TemplateSyntaxError, new, sys.exc_info()[2])
}}}

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

Django

unread,
Nov 17, 2019, 3:49:19 PM11/17/19
to django-...@googlegroups.com
#30425: Handle `jinja2.TemplateSyntaxError` when rendering a template with or
without a source.
-------------------------------------+-------------------------------------
Reporter: Daniel Hahler | Owner: Hasan
| Ramezani
Type: Bug | Status: assigned

Component: Template system | Version: master
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 Hasan Ramezani):

* owner: nobody => Hasan Ramezani
* status: new => assigned
* has_patch: 0 => 1


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

Django

unread,
Nov 27, 2019, 7:21:51 AM11/27/19
to django-...@googlegroups.com
#30425: Handle `jinja2.TemplateSyntaxError` when rendering a template with or
without a source.
-------------------------------------+-------------------------------------
Reporter: Daniel Hahler | Owner: Hasan
| Ramezani
Type: Bug | Status: closed

Component: Template system | Version: master
Severity: Normal | Resolution: fixed
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 <felisiak.mariusz@…>):

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


Comment:

In [changeset:"8d322902799019908ec0ded3f08cc8c09425ee8e" 8d322902]:
{{{
#!CommitTicketReference repository=""
revision="8d322902799019908ec0ded3f08cc8c09425ee8e"
Fixed #30425 -- Handled jinja2.TemplateSyntaxError when rendering a
template.

Jinja raises jinja2.TemplateSyntaxError in render() not in
get_template() when it's in an included template.
}}}

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

Reply all
Reply to author
Forward
0 new messages