|
6 | 6 | `django.core.handlers.asgi`. |
7 | 7 | """ |
8 | 8 |
|
| 9 | +import asyncio |
9 | 10 | import functools |
| 11 | +import inspect |
10 | 12 |
|
11 | 13 | from django.core.handlers.wsgi import WSGIRequest |
12 | 14 |
|
13 | 15 | import sentry_sdk |
14 | 16 | from sentry_sdk.consts import OP |
15 | 17 |
|
16 | 18 | from sentry_sdk.integrations.asgi import SentryAsgiMiddleware |
17 | | -from sentry_sdk.integrations._asgi_common import ( |
18 | | - _iscoroutinefunction, |
19 | | - _markcoroutinefunction, |
20 | | -) |
| 19 | +from sentry_sdk.integrations._asgi_common import _iscoroutinefunction |
21 | 20 | from sentry_sdk.scope import should_send_default_pii |
22 | 21 | from sentry_sdk.utils import ( |
23 | 22 | capture_internal_exceptions, |
|
27 | 26 | from typing import TYPE_CHECKING |
28 | 27 |
|
29 | 28 | if TYPE_CHECKING: |
30 | | - from typing import Any, Callable, Union |
| 29 | + from typing import Any, Callable, Union, TypeVar |
31 | 30 |
|
32 | 31 | from django.core.handlers.asgi import ASGIRequest |
33 | 32 | from django.http.response import HttpResponse |
34 | 33 |
|
35 | 34 | from sentry_sdk._types import Event, EventProcessor |
36 | 35 |
|
| 36 | + _F = TypeVar("_F", bound=Callable[..., Any]) |
| 37 | + |
| 38 | +# Python 3.12 deprecates asyncio.iscoroutinefunction() as an alias for |
| 39 | +# inspect.iscoroutinefunction(), whilst also removing the _is_coroutine marker. |
| 40 | +# The latter is replaced with the inspect.markcoroutinefunction decorator. |
| 41 | +# Until 3.12 is the minimum supported Python version, provide a shim. |
| 42 | +# This was copied from https://github.com/django/asgiref/blob/main/asgiref/sync.py |
| 43 | +if hasattr(inspect, "markcoroutinefunction"): |
| 44 | + markcoroutinefunction = inspect.markcoroutinefunction |
| 45 | +else: |
| 46 | + |
| 47 | + def markcoroutinefunction(func: "_F") -> "_F": |
| 48 | + func._is_coroutine = asyncio.coroutines._is_coroutine # type: ignore |
| 49 | + return func |
| 50 | + |
37 | 51 |
|
38 | 52 | def _make_asgi_request_event_processor(request: "ASGIRequest") -> "EventProcessor": |
39 | 53 | def asgi_request_event_processor(event: "Event", hint: "dict[str, Any]") -> "Event": |
@@ -200,7 +214,7 @@ def _async_check(self) -> None: |
200 | 214 | Taken from django.utils.deprecation::MiddlewareMixin._async_check |
201 | 215 | """ |
202 | 216 | if _iscoroutinefunction(self.get_response): |
203 | | - _markcoroutinefunction(self) |
| 217 | + markcoroutinefunction(self) |
204 | 218 |
|
205 | 219 | def async_route_check(self) -> bool: |
206 | 220 | """ |
|
0 commit comments