From 32d70b2f55b1f74736fd11bc8efce890ad5fa2f0 Mon Sep 17 00:00:00 2001 From: Carlton Gibson Date: Tue, 20 Dec 2022 11:10:48 +0100 Subject: Refs #34118 -- Adopted asgiref coroutine detection shims. Thanks to Mariusz Felisiak for review. --- django/core/handlers/base.py | 12 ++++++------ django/core/handlers/exception.py | 5 ++--- 2 files changed, 8 insertions(+), 9 deletions(-) (limited to 'django/core') diff --git a/django/core/handlers/base.py b/django/core/handlers/base.py index a934659186..8911543d4e 100644 --- a/django/core/handlers/base.py +++ b/django/core/handlers/base.py @@ -2,7 +2,7 @@ import asyncio import logging import types -from asgiref.sync import async_to_sync, sync_to_async +from asgiref.sync import async_to_sync, iscoroutinefunction, sync_to_async from django.conf import settings from django.core.exceptions import ImproperlyConfigured, MiddlewareNotUsed @@ -119,7 +119,7 @@ class BaseHandler: - Asynchronous methods are left alone """ if method_is_async is None: - method_is_async = asyncio.iscoroutinefunction(method) + method_is_async = iscoroutinefunction(method) if debug and not name: name = name or "method %s()" % method.__qualname__ if is_async: @@ -191,7 +191,7 @@ class BaseHandler: if response is None: wrapped_callback = self.make_view_atomic(callback) # If it is an asynchronous view, run it in a subthread. - if asyncio.iscoroutinefunction(wrapped_callback): + if iscoroutinefunction(wrapped_callback): wrapped_callback = async_to_sync(wrapped_callback) try: response = wrapped_callback(request, *callback_args, **callback_kwargs) @@ -245,7 +245,7 @@ class BaseHandler: if response is None: wrapped_callback = self.make_view_atomic(callback) # If it is a synchronous view, run it in a subthread - if not asyncio.iscoroutinefunction(wrapped_callback): + if not iscoroutinefunction(wrapped_callback): wrapped_callback = sync_to_async( wrapped_callback, thread_sensitive=True ) @@ -278,7 +278,7 @@ class BaseHandler: % (middleware_method.__self__.__class__.__name__,), ) try: - if asyncio.iscoroutinefunction(response.render): + if iscoroutinefunction(response.render): response = await response.render() else: response = await sync_to_async( @@ -346,7 +346,7 @@ class BaseHandler: non_atomic_requests = getattr(view, "_non_atomic_requests", set()) for alias, settings_dict in connections.settings.items(): if settings_dict["ATOMIC_REQUESTS"] and alias not in non_atomic_requests: - if asyncio.iscoroutinefunction(view): + if iscoroutinefunction(view): raise RuntimeError( "You cannot use ATOMIC_REQUESTS with async views." ) diff --git a/django/core/handlers/exception.py b/django/core/handlers/exception.py index 79577c2d0a..a0b1ba678a 100644 --- a/django/core/handlers/exception.py +++ b/django/core/handlers/exception.py @@ -1,9 +1,8 @@ -import asyncio import logging import sys from functools import wraps -from asgiref.sync import sync_to_async +from asgiref.sync import iscoroutinefunction, sync_to_async from django.conf import settings from django.core import signals @@ -34,7 +33,7 @@ def convert_exception_to_response(get_response): no middleware leaks an exception and that the next middleware in the stack can rely on getting a response instead of an exception. """ - if asyncio.iscoroutinefunction(get_response): + if iscoroutinefunction(get_response): @wraps(get_response) async def inner(request): -- cgit v1.3