|
6 | 6 | `django.core.handlers.asgi`. |
7 | 7 | """ |
8 | 8 |
|
9 | | -import asyncio |
10 | 9 | import functools |
11 | | -import inspect |
12 | 10 |
|
13 | 11 | from django.core.handlers.wsgi import WSGIRequest |
14 | 12 |
|
15 | 13 | import sentry_sdk |
16 | 14 | from sentry_sdk.consts import OP |
17 | 15 |
|
18 | 16 | from sentry_sdk.integrations.asgi import SentryAsgiMiddleware |
| 17 | +from sentry_sdk.integrations._asgi_common import ( |
| 18 | + _iscoroutinefunction, |
| 19 | + _markcoroutinefunction, |
| 20 | +) |
19 | 21 | from sentry_sdk.scope import should_send_default_pii |
20 | 22 | from sentry_sdk.utils import ( |
21 | 23 | capture_internal_exceptions, |
|
25 | 27 | from typing import TYPE_CHECKING |
26 | 28 |
|
27 | 29 | if TYPE_CHECKING: |
28 | | - from typing import Any, Callable, Union, TypeVar |
| 30 | + from typing import Any, Callable, Union |
29 | 31 |
|
30 | 32 | from django.core.handlers.asgi import ASGIRequest |
31 | 33 | from django.http.response import HttpResponse |
32 | 34 |
|
33 | 35 | from sentry_sdk._types import Event, EventProcessor |
34 | 36 |
|
35 | | - _F = TypeVar("_F", bound=Callable[..., Any]) |
36 | | - |
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 | | - iscoroutinefunction = inspect.iscoroutinefunction |
45 | | - markcoroutinefunction = inspect.markcoroutinefunction |
46 | | -else: |
47 | | - iscoroutinefunction = asyncio.iscoroutinefunction # type: ignore[assignment] |
48 | | - |
49 | | - def markcoroutinefunction(func: "_F") -> "_F": |
50 | | - func._is_coroutine = asyncio.coroutines._is_coroutine # type: ignore |
51 | | - return func |
52 | | - |
53 | 37 |
|
54 | 38 | def _make_asgi_request_event_processor(request: "ASGIRequest") -> "EventProcessor": |
55 | 39 | def asgi_request_event_processor(event: "Event", hint: "dict[str, Any]") -> "Event": |
@@ -215,15 +199,15 @@ def _async_check(self) -> None: |
215 | 199 | a thread is not consumed during a whole request. |
216 | 200 | Taken from django.utils.deprecation::MiddlewareMixin._async_check |
217 | 201 | """ |
218 | | - if iscoroutinefunction(self.get_response): |
219 | | - markcoroutinefunction(self) |
| 202 | + if _iscoroutinefunction(self.get_response): |
| 203 | + _markcoroutinefunction(self) |
220 | 204 |
|
221 | 205 | def async_route_check(self) -> bool: |
222 | 206 | """ |
223 | 207 | Function that checks if we are in async mode, |
224 | 208 | and if we are forwards the handling of requests to __acall__ |
225 | 209 | """ |
226 | | - return iscoroutinefunction(self.get_response) |
| 210 | + return _iscoroutinefunction(self.get_response) |
227 | 211 |
|
228 | 212 | async def __acall__(self, *args: "Any", **kwargs: "Any") -> "Any": |
229 | 213 | f = self._acall_method |
|
0 commit comments