From c2a1af883e18b93a77080650feb9e959536d51ca Mon Sep 17 00:00:00 2001 From: Ryan Rubin Date: Fri, 25 May 2018 10:11:46 -0500 Subject: [PATCH] [2.0.x] Fixed #29400 -- Fixed crash in custom template filters that use decorated functions. Regression in 620e9dd31a2146d70de740f96a8cb9a6db054fc7. Backport of a8d12bc28069d56e0306770ab9c73738293663f7 from master --- AUTHORS | 1 + django/template/base.py | 4 ++-- docs/releases/2.0.6.txt | 3 ++- tests/template_tests/templatetags/custom.py | 8 ++++++++ tests/template_tests/test_custom.py | 5 +++++ 5 files changed, 18 insertions(+), 3 deletions(-) diff --git a/AUTHORS b/AUTHORS index 2ee9adecd2..5da3a13745 100644 --- a/AUTHORS +++ b/AUTHORS @@ -708,6 +708,7 @@ answer newbie questions, and generally made Django that much better: ryankanno Ryan Kelly Ryan Niemeyer + Ryan Rubin Ryno Mathee Sam Newman Sander Dijkhuis diff --git a/django/template/base.py b/django/template/base.py index 6572eae8ab..a90f8bfdd9 100644 --- a/django/template/base.py +++ b/django/template/base.py @@ -51,7 +51,7 @@ times with multiple contexts) import logging import re -from inspect import getcallargs, getfullargspec +from inspect import getcallargs, getfullargspec, unwrap from django.template.context import ( # NOQA: imported for backwards compatibility BaseContext, Context, ContextPopException, RequestContext, @@ -712,7 +712,7 @@ class FilterExpression: # First argument, filter input, is implied. plen = len(provided) + 1 # Check to see if a decorator is providing the real function. - func = getattr(func, '_decorated_function', func) + func = unwrap(func) args, _, _, defaults, _, _, _ = getfullargspec(func) alen = len(args) diff --git a/docs/releases/2.0.6.txt b/docs/releases/2.0.6.txt index 659c3533e7..5d401a7ea9 100644 --- a/docs/releases/2.0.6.txt +++ b/docs/releases/2.0.6.txt @@ -9,4 +9,5 @@ Django 2.0.6 fixes several bugs in 2.0.5. Bugfixes ======== -* ... +* Fixed a regression that broke custom template filters that use decorators + (:ticket:`29400`). diff --git a/tests/template_tests/templatetags/custom.py b/tests/template_tests/templatetags/custom.py index 2e2ccf3782..45acd9655e 100644 --- a/tests/template_tests/templatetags/custom.py +++ b/tests/template_tests/templatetags/custom.py @@ -3,6 +3,7 @@ import operator from django import template from django.template.defaultfilters import stringfilter from django.utils.html import escape, format_html +from django.utils.safestring import mark_safe register = template.Library() @@ -13,6 +14,13 @@ def trim(value, num): return value[:num] +@register.filter +@mark_safe +def make_data_div(value): + """A filter that uses a decorator (@mark_safe).""" + return '
' % value + + @register.filter def noop(value, param=None): """A noop filter that always return its first argument and does nothing with diff --git a/tests/template_tests/test_custom.py b/tests/template_tests/test_custom.py index b37b5f8f1e..dbc5bc267d 100644 --- a/tests/template_tests/test_custom.py +++ b/tests/template_tests/test_custom.py @@ -25,6 +25,11 @@ class CustomFilterTests(SimpleTestCase): "abcde" ) + def test_decorated_filter(self): + engine = Engine(libraries=LIBRARIES) + t = engine.from_string('{% load custom %}{{ name|make_data_div }}') + self.assertEqual(t.render(Context({'name': 'foo'})), '
') + class TagTestCase(SimpleTestCase):