mirror of
https://github.com/django/django.git
synced 2025-06-05 11:39:13 +00:00
Fix #20745: Don't silence TypeError raised inside templates.
Thanks to robin for the report and claudep for the review.
This commit is contained in:
parent
9d11522599
commit
28a571348b
@ -3,7 +3,7 @@ from __future__ import unicode_literals
|
|||||||
import re
|
import re
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from importlib import import_module
|
from importlib import import_module
|
||||||
from inspect import getargspec
|
from inspect import getargspec, getcallargs
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.template.context import (BaseContext, Context, RequestContext,
|
from django.template.context import (BaseContext, Context, RequestContext,
|
||||||
@ -788,10 +788,13 @@ class Variable(object):
|
|||||||
else:
|
else:
|
||||||
try: # method call (assuming no args required)
|
try: # method call (assuming no args required)
|
||||||
current = current()
|
current = current()
|
||||||
except TypeError: # arguments *were* required
|
except TypeError:
|
||||||
# GOTCHA: This will also catch any TypeError
|
try:
|
||||||
# raised in the function itself.
|
getcallargs(current)
|
||||||
current = settings.TEMPLATE_STRING_IF_INVALID # invalid method call
|
except TypeError: # arguments *were* required
|
||||||
|
current = settings.TEMPLATE_STRING_IF_INVALID # invalid method call
|
||||||
|
else:
|
||||||
|
raise
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if getattr(e, 'silent_variable_failure', False):
|
if getattr(e, 'silent_variable_failure', False):
|
||||||
current = settings.TEMPLATE_STRING_IF_INVALID
|
current = settings.TEMPLATE_STRING_IF_INVALID
|
||||||
|
@ -282,6 +282,9 @@ Templates
|
|||||||
:setting:`TEMPLATE_DEBUG` is ``True``. This allows template origins to be
|
:setting:`TEMPLATE_DEBUG` is ``True``. This allows template origins to be
|
||||||
inspected and logged outside of the ``django.template`` infrastructure.
|
inspected and logged outside of the ``django.template`` infrastructure.
|
||||||
|
|
||||||
|
* ``TypeError`` exceptions are not longer silenced when raised during the
|
||||||
|
rendering of a template.
|
||||||
|
|
||||||
Backwards incompatible changes in 1.7
|
Backwards incompatible changes in 1.7
|
||||||
=====================================
|
=====================================
|
||||||
|
|
||||||
|
@ -98,6 +98,9 @@ class SomeClass:
|
|||||||
def method4(self):
|
def method4(self):
|
||||||
raise SomeOtherException
|
raise SomeOtherException
|
||||||
|
|
||||||
|
def method5(self):
|
||||||
|
raise TypeError
|
||||||
|
|
||||||
def __getitem__(self, key):
|
def __getitem__(self, key):
|
||||||
if key == 'silent_fail_key':
|
if key == 'silent_fail_key':
|
||||||
raise SomeException
|
raise SomeException
|
||||||
@ -680,6 +683,9 @@ class TemplateTests(TransRealMixin, TestCase):
|
|||||||
# Fail silently when accessing a non-simple method
|
# Fail silently when accessing a non-simple method
|
||||||
'basic-syntax20': ("{{ var.method2 }}", {"var": SomeClass()}, ("","INVALID")),
|
'basic-syntax20': ("{{ var.method2 }}", {"var": SomeClass()}, ("","INVALID")),
|
||||||
|
|
||||||
|
# Don't silence a TypeError if it was raised inside a callable
|
||||||
|
'basic-syntax20b': ("{{ var.method5 }}", {"var": SomeClass()}, TypeError),
|
||||||
|
|
||||||
# Don't get confused when parsing something that is almost, but not
|
# Don't get confused when parsing something that is almost, but not
|
||||||
# quite, a template tag.
|
# quite, a template tag.
|
||||||
'basic-syntax21': ("a {{ moo %} b", {}, "a {{ moo %} b"),
|
'basic-syntax21': ("a {{ moo %} b", {}, "a {{ moo %} b"),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user