Refs #23919 -- Removed django.utils.decorators.available_attrs() usage.

It's only needed to workaround a bug on Python 2.
This commit is contained in:
Tim Graham 2017-01-21 13:20:17 -05:00 committed by GitHub
parent 8249c5b382
commit 9e6e32bf5d
9 changed files with 19 additions and 26 deletions

View File

@ -5,7 +5,6 @@ from django.conf import settings
from django.contrib.auth import REDIRECT_FIELD_NAME from django.contrib.auth import REDIRECT_FIELD_NAME
from django.core.exceptions import PermissionDenied from django.core.exceptions import PermissionDenied
from django.shortcuts import resolve_url from django.shortcuts import resolve_url
from django.utils.decorators import available_attrs
def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME): def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
@ -16,7 +15,7 @@ def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIE
""" """
def decorator(view_func): def decorator(view_func):
@wraps(view_func, assigned=available_attrs(view_func)) @wraps(view_func)
def _wrapped_view(request, *args, **kwargs): def _wrapped_view(request, *args, **kwargs):
if test_func(request.user): if test_func(request.user):
return view_func(request, *args, **kwargs) return view_func(request, *args, **kwargs)

View File

@ -8,7 +8,6 @@ from django.core.exceptions import PermissionDenied, SuspiciousOperation
from django.http import Http404 from django.http import Http404
from django.http.multipartparser import MultiPartParserError from django.http.multipartparser import MultiPartParserError
from django.urls import get_resolver, get_urlconf from django.urls import get_resolver, get_urlconf
from django.utils.decorators import available_attrs
from django.utils.encoding import force_text from django.utils.encoding import force_text
from django.views import debug from django.views import debug
@ -28,7 +27,7 @@ def convert_exception_to_response(get_response):
no middleware leaks an exception and that the next middleware in the stack no middleware leaks an exception and that the next middleware in the stack
can rely on getting a response instead of an exception. can rely on getting a response instead of an exception.
""" """
@wraps(get_response, assigned=available_attrs(get_response)) @wraps(get_response)
def inner(request): def inner(request):
try: try:
response = get_response(request) response = get_response(request)

View File

@ -22,7 +22,6 @@ from django.db.models.options import Options
from django.template import Template from django.template import Template
from django.test.signals import setting_changed, template_rendered from django.test.signals import setting_changed, template_rendered
from django.urls import get_script_prefix, set_script_prefix from django.urls import get_script_prefix, set_script_prefix
from django.utils.decorators import available_attrs
from django.utils.translation import deactivate from django.utils.translation import deactivate
try: try:
@ -370,7 +369,7 @@ class TestContextDecorator:
raise TypeError('Can only decorate subclasses of unittest.TestCase') raise TypeError('Can only decorate subclasses of unittest.TestCase')
def decorate_callable(self, func): def decorate_callable(self, func):
@wraps(func, assigned=available_attrs(func)) @wraps(func)
def inner(*args, **kwargs): def inner(*args, **kwargs):
with self as context: with self as context:
if self.kwarg_name: if self.kwarg_name:

View File

@ -79,7 +79,7 @@ def method_decorator(decorator, name=''):
# Don't worry about making _dec look similar to a list/tuple as it's rather # Don't worry about making _dec look similar to a list/tuple as it's rather
# meaningless. # meaningless.
if not hasattr(decorator, '__iter__'): if not hasattr(decorator, '__iter__'):
update_wrapper(_dec, decorator, assigned=available_attrs(decorator)) update_wrapper(_dec, decorator)
# Change the name to aid debugging. # Change the name to aid debugging.
if hasattr(decorator, '__name__'): if hasattr(decorator, '__name__'):
_dec.__name__ = 'method_decorator(%s)' % decorator.__name__ _dec.__name__ = 'method_decorator(%s)' % decorator.__name__
@ -113,6 +113,7 @@ def decorator_from_middleware(middleware_class):
return make_middleware_decorator(middleware_class)() return make_middleware_decorator(middleware_class)()
# Unused, for backwards compatibility in Django 2.0.
def available_attrs(fn): def available_attrs(fn):
""" """
Return the list of functools-wrappable attributes on a callable. Return the list of functools-wrappable attributes on a callable.
@ -127,7 +128,7 @@ def make_middleware_decorator(middleware_class):
middleware = middleware_class(*m_args, **m_kwargs) middleware = middleware_class(*m_args, **m_kwargs)
def _decorator(view_func): def _decorator(view_func):
@wraps(view_func, assigned=available_attrs(view_func)) @wraps(view_func)
def _wrapped_view(request, *args, **kwargs): def _wrapped_view(request, *args, **kwargs):
if hasattr(middleware, 'process_request'): if hasattr(middleware, 'process_request'):
result = middleware.process_request(request) result = middleware.process_request(request)

View File

@ -2,9 +2,7 @@ from functools import wraps
from django.middleware.cache import CacheMiddleware from django.middleware.cache import CacheMiddleware
from django.utils.cache import add_never_cache_headers, patch_cache_control from django.utils.cache import add_never_cache_headers, patch_cache_control
from django.utils.decorators import ( from django.utils.decorators import decorator_from_middleware_with_args
available_attrs, decorator_from_middleware_with_args,
)
def cache_page(*args, **kwargs): def cache_page(*args, **kwargs):
@ -38,7 +36,7 @@ def cache_page(*args, **kwargs):
def cache_control(**kwargs): def cache_control(**kwargs):
def _cache_controller(viewfunc): def _cache_controller(viewfunc):
@wraps(viewfunc, assigned=available_attrs(viewfunc)) @wraps(viewfunc)
def _cache_controlled(request, *args, **kw): def _cache_controlled(request, *args, **kw):
response = viewfunc(request, *args, **kw) response = viewfunc(request, *args, **kw)
patch_cache_control(response, **kwargs) patch_cache_control(response, **kwargs)
@ -52,7 +50,7 @@ def never_cache(view_func):
Decorator that adds headers to a response so that it will Decorator that adds headers to a response so that it will
never be cached. never be cached.
""" """
@wraps(view_func, assigned=available_attrs(view_func)) @wraps(view_func)
def _wrapped_view_func(request, *args, **kwargs): def _wrapped_view_func(request, *args, **kwargs):
response = view_func(request, *args, **kwargs) response = view_func(request, *args, **kwargs)
add_never_cache_headers(response) add_never_cache_headers(response)

View File

@ -1,7 +1,5 @@
from functools import wraps from functools import wraps
from django.utils.decorators import available_attrs
def xframe_options_deny(view_func): def xframe_options_deny(view_func):
""" """
@ -20,7 +18,7 @@ def xframe_options_deny(view_func):
if resp.get('X-Frame-Options') is None: if resp.get('X-Frame-Options') is None:
resp['X-Frame-Options'] = 'DENY' resp['X-Frame-Options'] = 'DENY'
return resp return resp
return wraps(view_func, assigned=available_attrs(view_func))(wrapped_view) return wraps(view_func)(wrapped_view)
def xframe_options_sameorigin(view_func): def xframe_options_sameorigin(view_func):
@ -40,7 +38,7 @@ def xframe_options_sameorigin(view_func):
if resp.get('X-Frame-Options') is None: if resp.get('X-Frame-Options') is None:
resp['X-Frame-Options'] = 'SAMEORIGIN' resp['X-Frame-Options'] = 'SAMEORIGIN'
return resp return resp
return wraps(view_func, assigned=available_attrs(view_func))(wrapped_view) return wraps(view_func)(wrapped_view)
def xframe_options_exempt(view_func): def xframe_options_exempt(view_func):
@ -58,4 +56,4 @@ def xframe_options_exempt(view_func):
resp = view_func(*args, **kwargs) resp = view_func(*args, **kwargs)
resp.xframe_options_exempt = True resp.xframe_options_exempt = True
return resp return resp
return wraps(view_func, assigned=available_attrs(view_func))(wrapped_view) return wraps(view_func)(wrapped_view)

View File

@ -1,7 +1,7 @@
from functools import wraps from functools import wraps
from django.middleware.csrf import CsrfViewMiddleware, get_token from django.middleware.csrf import CsrfViewMiddleware, get_token
from django.utils.decorators import available_attrs, decorator_from_middleware from django.utils.decorators import decorator_from_middleware
csrf_protect = decorator_from_middleware(CsrfViewMiddleware) csrf_protect = decorator_from_middleware(CsrfViewMiddleware)
csrf_protect.__name__ = "csrf_protect" csrf_protect.__name__ = "csrf_protect"
@ -57,4 +57,4 @@ def csrf_exempt(view_func):
def wrapped_view(*args, **kwargs): def wrapped_view(*args, **kwargs):
return view_func(*args, **kwargs) return view_func(*args, **kwargs)
wrapped_view.csrf_exempt = True wrapped_view.csrf_exempt = True
return wraps(view_func, assigned=available_attrs(view_func))(wrapped_view) return wraps(view_func)(wrapped_view)

View File

@ -9,7 +9,7 @@ from functools import wraps
from django.http import HttpResponseNotAllowed from django.http import HttpResponseNotAllowed
from django.middleware.http import ConditionalGetMiddleware from django.middleware.http import ConditionalGetMiddleware
from django.utils.cache import get_conditional_response from django.utils.cache import get_conditional_response
from django.utils.decorators import available_attrs, decorator_from_middleware from django.utils.decorators import decorator_from_middleware
from django.utils.http import http_date, quote_etag from django.utils.http import http_date, quote_etag
conditional_page = decorator_from_middleware(ConditionalGetMiddleware) conditional_page = decorator_from_middleware(ConditionalGetMiddleware)
@ -29,7 +29,7 @@ def require_http_methods(request_method_list):
Note that request methods should be in uppercase. Note that request methods should be in uppercase.
""" """
def decorator(func): def decorator(func):
@wraps(func, assigned=available_attrs(func)) @wraps(func)
def inner(request, *args, **kwargs): def inner(request, *args, **kwargs):
if request.method not in request_method_list: if request.method not in request_method_list:
logger.warning( logger.warning(
@ -75,7 +75,7 @@ def condition(etag_func=None, last_modified_func=None):
already have them. already have them.
""" """
def decorator(func): def decorator(func):
@wraps(func, assigned=available_attrs(func)) @wraps(func)
def inner(request, *args, **kwargs): def inner(request, *args, **kwargs):
# Compute values (if any) for the requested resource. # Compute values (if any) for the requested resource.
def get_last_modified(): def get_last_modified():

View File

@ -1,7 +1,6 @@
from functools import wraps from functools import wraps
from django.utils.cache import patch_vary_headers from django.utils.cache import patch_vary_headers
from django.utils.decorators import available_attrs
def vary_on_headers(*headers): def vary_on_headers(*headers):
@ -16,7 +15,7 @@ def vary_on_headers(*headers):
Note that the header names are not case-sensitive. Note that the header names are not case-sensitive.
""" """
def decorator(func): def decorator(func):
@wraps(func, assigned=available_attrs(func)) @wraps(func)
def inner_func(*args, **kwargs): def inner_func(*args, **kwargs):
response = func(*args, **kwargs) response = func(*args, **kwargs)
patch_vary_headers(response, headers) patch_vary_headers(response, headers)
@ -34,7 +33,7 @@ def vary_on_cookie(func):
def index(request): def index(request):
... ...
""" """
@wraps(func, assigned=available_attrs(func)) @wraps(func)
def inner_func(*args, **kwargs): def inner_func(*args, **kwargs):
response = func(*args, **kwargs) response = func(*args, **kwargs)
patch_vary_headers(response, ('Cookie',)) patch_vary_headers(response, ('Cookie',))