|
2 | 2 |
|
3 | 3 | from sentry_sdk.hub import Hub, _should_send_default_pii |
4 | 4 | from sentry_sdk.utils import capture_internal_exceptions, event_from_exception |
5 | | -from sentry_sdk._compat import PY2, reraise, implements_iterator |
| 5 | +from sentry_sdk._compat import PY2, reraise |
6 | 6 | from sentry_sdk.integrations._wsgi_common import _filter_headers |
7 | 7 |
|
8 | 8 |
|
@@ -57,21 +57,20 @@ def __init__(self, app): |
57 | 57 | self.app = app |
58 | 58 |
|
59 | 59 | def __call__(self, environ, start_response): |
60 | | - hub = Hub.current |
61 | | - hub.push_scope() |
62 | | - with capture_internal_exceptions(): |
63 | | - with hub.configure_scope() as scope: |
64 | | - scope._name = "wsgi" |
65 | | - scope.add_event_processor(_make_wsgi_event_processor(environ)) |
| 60 | + hub = Hub(Hub.current) |
| 61 | + |
| 62 | + with hub: |
| 63 | + with capture_internal_exceptions(): |
| 64 | + with hub.configure_scope() as scope: |
| 65 | + scope._name = "wsgi" |
| 66 | + scope.add_event_processor(_make_wsgi_event_processor(environ)) |
66 | 67 |
|
67 | | - try: |
68 | | - rv = self.app(environ, start_response) |
69 | | - except Exception: |
70 | | - einfo = _capture_exception(hub) |
71 | | - hub.pop_scope_unsafe() |
72 | | - reraise(*einfo) |
| 68 | + try: |
| 69 | + rv = self.app(environ, start_response) |
| 70 | + except Exception: |
| 71 | + reraise(*_capture_exception(hub)) |
73 | 72 |
|
74 | | - return _ScopePoppingResponse(hub, rv) |
| 73 | + return _ScopedResponse(hub, rv) |
75 | 74 |
|
76 | 75 |
|
77 | 76 | def _get_environ(environ): |
@@ -132,45 +131,35 @@ def _capture_exception(hub): |
132 | 131 | return exc_info |
133 | 132 |
|
134 | 133 |
|
135 | | -@implements_iterator |
136 | | -class _ScopePoppingResponse(object): |
137 | | - __slots__ = ("_response", "_iterator", "_hub", "_popped") |
| 134 | +class _ScopedResponse(object): |
| 135 | + __slots__ = ("_response", "_hub") |
138 | 136 |
|
139 | 137 | def __init__(self, hub, response): |
140 | 138 | self._hub = hub |
141 | 139 | self._response = response |
142 | | - self._iterator = None |
143 | | - self._popped = False |
144 | 140 |
|
145 | 141 | def __iter__(self): |
146 | | - try: |
147 | | - self._iterator = iter(self._response) |
148 | | - except Exception: |
149 | | - reraise(*_capture_exception(self._hub)) |
150 | | - return self |
151 | | - |
152 | | - def __next__(self): |
153 | | - if self._iterator is None: |
154 | | - self.__iter__() |
155 | | - |
156 | | - try: |
157 | | - return next(self._iterator) |
158 | | - except StopIteration: |
159 | | - raise |
160 | | - except Exception: |
161 | | - reraise(*_capture_exception(self._hub)) |
| 142 | + iterator = iter(self._response) |
| 143 | + |
| 144 | + while True: |
| 145 | + with self._hub: |
| 146 | + try: |
| 147 | + chunk = next(iterator) |
| 148 | + except StopIteration: |
| 149 | + break |
| 150 | + except Exception: |
| 151 | + reraise(*_capture_exception(self._hub)) |
| 152 | + |
| 153 | + yield chunk |
162 | 154 |
|
163 | 155 | def close(self): |
164 | | - if not self._popped: |
165 | | - self._hub.pop_scope_unsafe() |
166 | | - self._popped = True |
167 | | - |
168 | | - try: |
169 | | - self._response.close() |
170 | | - except AttributeError: |
171 | | - pass |
172 | | - except Exception: |
173 | | - reraise(*_capture_exception(self._hub)) |
| 156 | + with self._hub: |
| 157 | + try: |
| 158 | + self._response.close() |
| 159 | + except AttributeError: |
| 160 | + pass |
| 161 | + except Exception: |
| 162 | + reraise(*_capture_exception(self._hub)) |
174 | 163 |
|
175 | 164 |
|
176 | 165 | def _make_wsgi_event_processor(environ): |
|
0 commit comments