Skip to content
This repository was archived by the owner on Dec 13, 2021. It is now read-only.

Commit d3d4b6d

Browse files
committed
MISP-665: made sdk compatible with Python 3.4
1 parent c06a0c6 commit d3d4b6d

File tree

5 files changed

+52
-27
lines changed

5 files changed

+52
-27
lines changed

sentry_sdk/integrations/aiohttp.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,16 @@ def setup_once():
5555

5656
old_handle = Application._handle
5757

58-
async def sentry_app_handle(self, request, *args, **kwargs):
58+
@asyncio.coroutine
59+
def sentry_app_handle(self, request, *args, **kwargs):
5960
# type: (Any, Request, *Any, **Any) -> Any
60-
async def inner():
61+
@asyncio.coroutine
62+
def inner():
6163
# type: () -> Any
6264
hub = Hub.current
6365
if hub.get_integration(AioHttpIntegration) is None:
64-
return await old_handle(self, request, *args, **kwargs)
66+
old_handle_response = yield from old_handle(self, request, *args, **kwargs)
67+
return old_handle_response
6568

6669
weak_request = weakref.ref(request)
6770

@@ -78,7 +81,7 @@ async def inner():
7881

7982
with hub.start_span(span):
8083
try:
81-
response = await old_handle(self, request)
84+
response = yield from old_handle(self, request)
8285
except HTTPException as e:
8386
span.set_http_status(e.status_code)
8487
raise
@@ -96,15 +99,17 @@ async def inner():
9699
# Explicitly wrap in task such that current contextvar context is
97100
# copied. Just doing `return await inner()` will leak scope data
98101
# between requests.
99-
return await asyncio.get_event_loop().create_task(inner())
102+
event_loop_response = yield from asyncio.get_event_loop().create_task(inner())
103+
return event_loop_response
100104

101105
Application._handle = sentry_app_handle
102106

103107
old_urldispatcher_resolve = UrlDispatcher.resolve
104108

105-
async def sentry_urldispatcher_resolve(self, request):
109+
@asyncio.coroutine
110+
def sentry_urldispatcher_resolve(self, request):
106111
# type: (UrlDispatcher, Request) -> AbstractMatchInfo
107-
rv = await old_urldispatcher_resolve(self, request)
112+
rv = yield from old_urldispatcher_resolve(self, request)
108113

109114
name = None
110115

sentry_sdk/integrations/asgi.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,26 @@ def __init__(self, app):
7070

7171
def _run_asgi2(self, scope):
7272
# type: (Any) -> Any
73-
async def inner(receive, send):
73+
@asyncio.coroutine
74+
def inner(receive, send):
7475
# type: (Any, Any) -> Any
75-
return await self._run_app(scope, lambda: self.app(scope)(receive, send))
76+
response = yield from self._run_app(scope, lambda: self.app(scope)(receive, send))
77+
return response
7678

7779
return inner
7880

79-
async def _run_asgi3(self, scope, receive, send):
81+
@asyncio.coroutine
82+
def _run_asgi3(self, scope, receive, send):
8083
# type: (Any, Any, Any) -> Any
81-
return await self._run_app(scope, lambda: self.app(scope, receive, send))
84+
response = yield from self._run_app(scope, lambda: self.app(scope, receive, send))
85+
return response
8286

83-
async def _run_app(self, scope, callback):
87+
@asyncio.coroutine
88+
def _run_app(self, scope, callback):
8489
# type: (Any, Any) -> Any
8590
if _asgi_middleware_applied.get(False):
86-
return await callback()
91+
response = yield from callback()
92+
return response
8793

8894
_asgi_middleware_applied.set(True)
8995
try:
@@ -112,7 +118,8 @@ async def _run_app(self, scope, callback):
112118
# would have to wrap send(). That is a bit hard to do with
113119
# the current abstraction over ASGI 2/3.
114120
try:
115-
return await callback()
121+
response = yield from callback()
122+
return response
116123
except Exception as exc:
117124
_capture_exception(hub, exc)
118125
raise exc from None

sentry_sdk/integrations/django/asgi.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
from sentry_sdk.integrations.django import DjangoIntegration
1313
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
1414

15+
import asyncio
16+
1517
if MYPY:
1618
from typing import Any
1719

@@ -20,13 +22,14 @@ def patch_django_asgi_handler_impl(cls):
2022
# type: (Any) -> None
2123
old_app = cls.__call__
2224

23-
async def sentry_patched_asgi_handler(self, scope, receive, send):
25+
@asyncio.coroutine
26+
def sentry_patched_asgi_handler(self, scope, receive, send):
2427
# type: (Any, Any, Any, Any) -> Any
2528
if Hub.current.get_integration(DjangoIntegration) is None:
26-
return await old_app(self, scope, receive, send)
29+
yield from old_app(self, scope, receive, send)
2730

2831
middleware = SentryAsgiMiddleware(old_app.__get__(self, cls))._run_asgi3
29-
return await middleware(scope, receive, send)
32+
yield from middleware(scope, receive, send)
3033

3134
cls.__call__ = sentry_patched_asgi_handler
3235

@@ -35,13 +38,14 @@ def patch_channels_asgi_handler_impl(cls):
3538
# type: (Any) -> None
3639
old_app = cls.__call__
3740

38-
async def sentry_patched_asgi_handler(self, receive, send):
41+
@asyncio.coroutine
42+
def sentry_patched_asgi_handler(self, receive, send):
3943
# type: (Any, Any, Any) -> Any
4044
if Hub.current.get_integration(DjangoIntegration) is None:
41-
return await old_app(self, receive, send)
45+
yield from old_app(self, receive, send)
4246

4347
middleware = SentryAsgiMiddleware(lambda _scope: old_app.__get__(self, cls))
4448

45-
return await middleware(self.scope)(receive, send)
49+
yield from middleware(self.scope)(receive, send)
4650

4751
cls.__call__ = sentry_patched_asgi_handler

sentry_sdk/integrations/sanic.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
from sentry_sdk._types import MYPY
2222

23+
import asyncio
24+
2325
if MYPY:
2426
from typing import Any
2527
from typing import Callable
@@ -61,7 +63,8 @@ def setup_once():
6163

6264
old_handle_request = Sanic.handle_request
6365

64-
async def sentry_handle_request(self, request, *args, **kwargs):
66+
@asyncio.coroutine
67+
def sentry_handle_request(self, request, *args, **kwargs):
6568
# type: (Any, Request, *Any, **Any) -> Any
6669
hub = Hub.current
6770
if hub.get_integration(SanicIntegration) is None:
@@ -76,7 +79,7 @@ async def sentry_handle_request(self, request, *args, **kwargs):
7679

7780
response = old_handle_request(self, request, *args, **kwargs)
7881
if isawaitable(response):
79-
response = await response
82+
response = yield from response
8083

8184
return response
8285

@@ -109,12 +112,13 @@ def sentry_error_handler_lookup(self, exception):
109112
if Hub.current.get_integration(SanicIntegration) is None:
110113
return old_error_handler
111114

112-
async def sentry_wrapped_error_handler(request, exception):
115+
@asyncio.coroutine
116+
def sentry_wrapped_error_handler(request, exception):
113117
# type: (Request, Exception) -> Any
114118
try:
115119
response = old_error_handler(request, exception)
116120
if isawaitable(response):
117-
response = await response
121+
response = yield from response
118122
return response
119123
except Exception:
120124
# Report errors that occur in Sanic error handler. These

sentry_sdk/integrations/tornado.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
from sentry_sdk._types import MYPY
2424

25+
import asyncio
26+
2527
if MYPY:
2628
from typing import Any
2729
from typing import Optional
@@ -59,20 +61,23 @@ def setup_once():
5961
if awaitable:
6062
# Starting Tornado 6 RequestHandler._execute method is a standard Python coroutine (async/await)
6163
# In that case our method should be a coroutine function too
62-
async def sentry_execute_request_handler(self, *args, **kwargs):
64+
@asyncio.coroutine
65+
def sentry_execute_request_handler(self, *args, **kwargs):
6366
# type: (Any, *Any, **Any) -> Any
6467
hub = Hub.current
6568
integration = hub.get_integration(TornadoIntegration)
6669
if integration is None:
67-
return await old_execute(self, *args, **kwargs)
70+
result = yield from old_execute(self, *args, **kwargs)
71+
return result
6872

6973
weak_handler = weakref.ref(self)
7074

7175
with Hub(hub) as hub:
7276
with hub.configure_scope() as scope:
7377
scope.clear_breadcrumbs()
7478
scope.add_event_processor(_make_event_processor(weak_handler))
75-
return await old_execute(self, *args, **kwargs)
79+
result = yield from old_execute(self, *args, **kwargs)
80+
return result
7681

7782
else:
7883

0 commit comments

Comments
 (0)