2020-02-13 06:15:00 +08:00
|
|
|
import asyncio
|
2013-03-08 22:15:23 +08:00
|
|
|
import inspect
|
|
|
|
import warnings
|
|
|
|
|
2020-02-13 06:15:00 +08:00
|
|
|
from asgiref.sync import sync_to_async
|
|
|
|
|
2013-03-08 22:15:23 +08:00
|
|
|
|
2020-05-04 18:07:45 +08:00
|
|
|
class RemovedInDjango40Warning(DeprecationWarning):
|
2014-02-27 05:48:20 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-05-04 18:07:45 +08:00
|
|
|
class RemovedInDjango41Warning(PendingDeprecationWarning):
|
2015-09-27 01:38:04 +08:00
|
|
|
pass
|
2014-03-08 18:13:45 +08:00
|
|
|
|
|
|
|
|
2020-05-04 18:07:45 +08:00
|
|
|
RemovedInNextVersionWarning = RemovedInDjango40Warning
|
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class warn_about_renamed_method:
|
2013-03-08 22:15:23 +08:00
|
|
|
def __init__(self, class_name, old_method_name, new_method_name, deprecation_warning):
|
|
|
|
self.class_name = class_name
|
|
|
|
self.old_method_name = old_method_name
|
|
|
|
self.new_method_name = new_method_name
|
|
|
|
self.deprecation_warning = deprecation_warning
|
|
|
|
|
|
|
|
def __call__(self, f):
|
|
|
|
def wrapped(*args, **kwargs):
|
|
|
|
warnings.warn(
|
|
|
|
"`%s.%s` is deprecated, use `%s` instead." %
|
|
|
|
(self.class_name, self.old_method_name, self.new_method_name),
|
|
|
|
self.deprecation_warning, 2)
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
return wrapped
|
|
|
|
|
|
|
|
|
|
|
|
class RenameMethodsBase(type):
|
|
|
|
"""
|
|
|
|
Handles the deprecation paths when renaming a method.
|
|
|
|
|
|
|
|
It does the following:
|
|
|
|
1) Define the new method if missing and complain about it.
|
|
|
|
2) Define the old method if missing.
|
|
|
|
3) Complain whenever an old method is called.
|
|
|
|
|
|
|
|
See #15363 for more details.
|
|
|
|
"""
|
|
|
|
|
|
|
|
renamed_methods = ()
|
|
|
|
|
|
|
|
def __new__(cls, name, bases, attrs):
|
2017-01-21 21:13:44 +08:00
|
|
|
new_class = super().__new__(cls, name, bases, attrs)
|
2013-03-08 22:15:23 +08:00
|
|
|
|
|
|
|
for base in inspect.getmro(new_class):
|
|
|
|
class_name = base.__name__
|
|
|
|
for renamed_method in cls.renamed_methods:
|
|
|
|
old_method_name = renamed_method[0]
|
|
|
|
old_method = base.__dict__.get(old_method_name)
|
|
|
|
new_method_name = renamed_method[1]
|
|
|
|
new_method = base.__dict__.get(new_method_name)
|
|
|
|
deprecation_warning = renamed_method[2]
|
|
|
|
wrapper = warn_about_renamed_method(class_name, *renamed_method)
|
|
|
|
|
|
|
|
# Define the new method if missing and complain about it
|
|
|
|
if not new_method and old_method:
|
|
|
|
warnings.warn(
|
|
|
|
"`%s.%s` method should be renamed `%s`." %
|
|
|
|
(class_name, old_method_name, new_method_name),
|
|
|
|
deprecation_warning, 2)
|
|
|
|
setattr(base, new_method_name, old_method)
|
|
|
|
setattr(base, old_method_name, wrapper(old_method))
|
|
|
|
|
|
|
|
# Define the old method as a wrapped call to the new method.
|
|
|
|
if not old_method and new_method:
|
|
|
|
setattr(base, old_method_name, wrapper(new_method))
|
|
|
|
|
|
|
|
return new_class
|
2015-09-27 01:38:04 +08:00
|
|
|
|
|
|
|
|
|
|
|
class DeprecationInstanceCheck(type):
|
|
|
|
def __instancecheck__(self, instance):
|
|
|
|
warnings.warn(
|
|
|
|
"`%s` is deprecated, use `%s` instead." % (self.__name__, self.alternative),
|
|
|
|
self.deprecation_warning, 2
|
|
|
|
)
|
2017-01-21 21:13:44 +08:00
|
|
|
return super().__instancecheck__(instance)
|
2016-04-02 19:18:26 +08:00
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class MiddlewareMixin:
|
2020-02-13 06:15:00 +08:00
|
|
|
sync_capable = True
|
|
|
|
async_capable = True
|
|
|
|
|
2019-09-27 01:06:35 +08:00
|
|
|
# RemovedInDjango40Warning: when the deprecation ends, replace with:
|
|
|
|
# def __init__(self, get_response):
|
2015-11-07 23:12:37 +08:00
|
|
|
def __init__(self, get_response=None):
|
2019-09-27 01:06:35 +08:00
|
|
|
self._get_response_none_deprecation(get_response)
|
2015-11-07 23:12:37 +08:00
|
|
|
self.get_response = get_response
|
2020-02-13 06:15:00 +08:00
|
|
|
self._async_check()
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__()
|
2015-11-07 23:12:37 +08:00
|
|
|
|
2020-02-13 06:15:00 +08:00
|
|
|
def _async_check(self):
|
|
|
|
"""
|
|
|
|
If get_response is a coroutine function, turns us into async mode so
|
|
|
|
a thread is not consumed during a whole request.
|
|
|
|
"""
|
|
|
|
if asyncio.iscoroutinefunction(self.get_response):
|
|
|
|
# Mark the class as async-capable, but do the actual switch
|
|
|
|
# inside __call__ to avoid swapping out dunder methods
|
|
|
|
self._is_coroutine = asyncio.coroutines._is_coroutine
|
|
|
|
|
2015-11-07 23:12:37 +08:00
|
|
|
def __call__(self, request):
|
2020-02-13 06:15:00 +08:00
|
|
|
# Exit out to async mode, if needed
|
|
|
|
if asyncio.iscoroutinefunction(self.get_response):
|
|
|
|
return self.__acall__(request)
|
2015-11-07 23:12:37 +08:00
|
|
|
response = None
|
|
|
|
if hasattr(self, 'process_request'):
|
|
|
|
response = self.process_request(request)
|
2018-01-04 07:52:12 +08:00
|
|
|
response = response or self.get_response(request)
|
2015-11-07 23:12:37 +08:00
|
|
|
if hasattr(self, 'process_response'):
|
|
|
|
response = self.process_response(request, response)
|
|
|
|
return response
|
2019-09-27 01:06:35 +08:00
|
|
|
|
2020-02-13 06:15:00 +08:00
|
|
|
async def __acall__(self, request):
|
|
|
|
"""
|
|
|
|
Async version of __call__ that is swapped in when an async request
|
|
|
|
is running.
|
|
|
|
"""
|
|
|
|
response = None
|
|
|
|
if hasattr(self, 'process_request'):
|
2020-08-21 00:41:22 +08:00
|
|
|
response = await sync_to_async(
|
|
|
|
self.process_request,
|
|
|
|
thread_sensitive=True,
|
|
|
|
)(request)
|
2020-02-13 06:15:00 +08:00
|
|
|
response = response or await self.get_response(request)
|
|
|
|
if hasattr(self, 'process_response'):
|
2020-08-21 00:41:22 +08:00
|
|
|
response = await sync_to_async(
|
|
|
|
self.process_response,
|
|
|
|
thread_sensitive=True,
|
|
|
|
)(request, response)
|
2020-02-13 06:15:00 +08:00
|
|
|
return response
|
|
|
|
|
2019-09-27 01:06:35 +08:00
|
|
|
def _get_response_none_deprecation(self, get_response):
|
|
|
|
if get_response is None:
|
|
|
|
warnings.warn(
|
|
|
|
'Passing None for the middleware get_response argument is '
|
|
|
|
'deprecated.',
|
|
|
|
RemovedInDjango40Warning, stacklevel=3,
|
|
|
|
)
|