From 615eab6b0255608e2094f0ecf02c5351a1e887cc Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Fri, 12 Mar 2010 13:06:13 +0000 Subject: [PATCH] Fixed #13093 -- Updated some decorators and the decorator_from_middleware function to allow callable classes to be decorated. Thanks to Brian Neal for the report. git-svn-id: http://code.djangoproject.com/svn/django/trunk@12762 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/utils/decorators.py | 12 +++++++++--- django/views/decorators/cache.py | 6 +++--- django/views/decorators/csrf.py | 7 ++++--- django/views/decorators/http.py | 4 ++-- django/views/decorators/vary.py | 5 +++-- tests/regressiontests/utils/decorators.py | 6 ++++++ tests/regressiontests/utils/urls.py | 1 + tests/regressiontests/utils/views.py | 7 +++++++ 8 files changed, 35 insertions(+), 13 deletions(-) diff --git a/django/utils/decorators.py b/django/utils/decorators.py index 810d863849..84ec1bac00 100644 --- a/django/utils/decorators.py +++ b/django/utils/decorators.py @@ -2,9 +2,9 @@ import types try: - from functools import wraps, update_wrapper + from functools import wraps, update_wrapper, WRAPPER_ASSIGNMENTS except ImportError: - from django.utils.functional import wraps, update_wrapper # Python 2.3, 2.4 fallback. + from django.utils.functional import wraps, update_wrapper, WRAPPER_ASSIGNMENTS # Python 2.3, 2.4 fallback. def method_decorator(decorator): @@ -50,6 +50,12 @@ def decorator_from_middleware(middleware_class): """ return make_middleware_decorator(middleware_class)() +def available_attrs(fn): + """ + Return the list of functools-wrappable attributes on a callable. + This is required as a workaround for http://bugs.python.org/issue3445. + """ + return tuple(a for a in WRAPPER_ASSIGNMENTS if hasattr(fn, a)) def make_middleware_decorator(middleware_class): def _make_decorator(*m_args, **m_kwargs): @@ -77,6 +83,6 @@ def make_middleware_decorator(middleware_class): if result is not None: return result return response - return wraps(view_func)(_wrapped_view) + return wraps(view_func, assigned=available_attrs(view_func))(_wrapped_view) return _decorator return _make_decorator diff --git a/django/views/decorators/cache.py b/django/views/decorators/cache.py index 303a41ef7d..eb782e67fd 100644 --- a/django/views/decorators/cache.py +++ b/django/views/decorators/cache.py @@ -16,7 +16,7 @@ try: except ImportError: from django.utils.functional import wraps # Python 2.3, 2.4 fallback. -from django.utils.decorators import decorator_from_middleware_with_args +from django.utils.decorators import decorator_from_middleware_with_args, available_attrs from django.utils.cache import patch_cache_control, add_never_cache_headers from django.middleware.cache import CacheMiddleware @@ -56,7 +56,7 @@ def cache_control(**kwargs): response = viewfunc(request, *args, **kw) patch_cache_control(response, **kwargs) return response - return wraps(viewfunc)(_cache_controlled) + return wraps(viewfunc, assigned=available_attrs(viewfunc))(_cache_controlled) return _cache_controller @@ -69,4 +69,4 @@ def never_cache(view_func): response = view_func(request, *args, **kwargs) add_never_cache_headers(response) return response - return wraps(view_func)(_wrapped_view_func) + return wraps(view_func, assigned=available_attrs(view_func))(_wrapped_view_func) diff --git a/django/views/decorators/csrf.py b/django/views/decorators/csrf.py index b789872efe..5eaa22266e 100644 --- a/django/views/decorators/csrf.py +++ b/django/views/decorators/csrf.py @@ -1,5 +1,6 @@ from django.middleware.csrf import CsrfViewMiddleware -from django.utils.decorators import decorator_from_middleware +from django.utils.decorators import decorator_from_middleware, available_attrs + try: from functools import wraps except ImportError: @@ -22,7 +23,7 @@ def csrf_response_exempt(view_func): resp = view_func(*args, **kwargs) resp.csrf_exempt = True return resp - return wraps(view_func)(wrapped_view) + return wraps(view_func, assigned=available_attrs(view_func))(wrapped_view) def csrf_view_exempt(view_func): """ @@ -34,7 +35,7 @@ def csrf_view_exempt(view_func): def wrapped_view(*args, **kwargs): return view_func(*args, **kwargs) wrapped_view.csrf_exempt = True - return wraps(view_func)(wrapped_view) + return wraps(view_func, assigned=available_attrs(view_func))(wrapped_view) def csrf_exempt(view_func): """ diff --git a/django/views/decorators/http.py b/django/views/decorators/http.py index b51cf9a782..9a5a314f0c 100644 --- a/django/views/decorators/http.py +++ b/django/views/decorators/http.py @@ -11,7 +11,7 @@ from calendar import timegm from datetime import timedelta from email.Utils import formatdate -from django.utils.decorators import decorator_from_middleware +from django.utils.decorators import decorator_from_middleware, available_attrs from django.utils.http import parse_etags, quote_etag from django.middleware.http import ConditionalGetMiddleware from django.http import HttpResponseNotAllowed, HttpResponseNotModified, HttpResponse @@ -35,7 +35,7 @@ def require_http_methods(request_method_list): if request.method not in request_method_list: return HttpResponseNotAllowed(request_method_list) return func(request, *args, **kwargs) - return wraps(func)(inner) + return wraps(func, assigned=available_attrs(func))(inner) return decorator require_GET = require_http_methods(["GET"]) diff --git a/django/views/decorators/vary.py b/django/views/decorators/vary.py index ea1b8d307d..e2b87a8c71 100644 --- a/django/views/decorators/vary.py +++ b/django/views/decorators/vary.py @@ -4,6 +4,7 @@ except ImportError: from django.utils.functional import wraps # Python 2.3, 2.4 fallback. from django.utils.cache import patch_vary_headers +from django.utils.decorators import available_attrs def vary_on_headers(*headers): """ @@ -21,7 +22,7 @@ def vary_on_headers(*headers): response = func(*args, **kwargs) patch_vary_headers(response, headers) return response - return wraps(func)(inner_func) + return wraps(func, assigned=available_attrs(func))(inner_func) return decorator def vary_on_cookie(func): @@ -37,4 +38,4 @@ def vary_on_cookie(func): response = func(*args, **kwargs) patch_vary_headers(response, ('Cookie',)) return response - return wraps(func)(inner_func) + return wraps(func, assigned=available_attrs(func))(inner_func) diff --git a/tests/regressiontests/utils/decorators.py b/tests/regressiontests/utils/decorators.py index c6ae243c98..ca9214f787 100644 --- a/tests/regressiontests/utils/decorators.py +++ b/tests/regressiontests/utils/decorators.py @@ -11,3 +11,9 @@ class DecoratorFromMiddlewareTests(TestCase): Test a middleware that implements process_view. """ self.client.get('/utils/xview/') + + def test_callable_process_view_middleware(self): + """ + Test a middleware that implements process_view, operating on a callable class. + """ + self.client.get('/utils/class_xview/') diff --git a/tests/regressiontests/utils/urls.py b/tests/regressiontests/utils/urls.py index 7b60d0884c..ba09d14b3d 100644 --- a/tests/regressiontests/utils/urls.py +++ b/tests/regressiontests/utils/urls.py @@ -4,4 +4,5 @@ import views urlpatterns = patterns('', (r'^xview/$', views.xview), + (r'^class_xview/$', views.class_xview), ) diff --git a/tests/regressiontests/utils/views.py b/tests/regressiontests/utils/views.py index d22ae26641..ef97c6502d 100644 --- a/tests/regressiontests/utils/views.py +++ b/tests/regressiontests/utils/views.py @@ -8,3 +8,10 @@ xview_dec = decorator_from_middleware(XViewMiddleware) def xview(request): return HttpResponse() xview = xview_dec(xview) + + +class ClassXView(object): + def __call__(self, request): + return HttpResponse() + +class_xview = xview_dec(ClassXView())