44from threading import Thread , current_thread
55
66import sentry_sdk
7+ from sentry_sdk import Scope
8+ from sentry_sdk .scope import ScopeType
79from sentry_sdk .integrations import Integration
810from sentry_sdk .utils import (
911 event_from_exception ,
1719 from typing import Any
1820 from typing import TypeVar
1921 from typing import Callable
20- from typing import Optional
2122
2223 from sentry_sdk ._types import ExcInfo
2324
@@ -75,8 +76,8 @@ def sentry_start(self, *a, **kw):
7576 isolation_scope = sentry_sdk .get_isolation_scope ().fork ()
7677 current_scope = sentry_sdk .get_current_scope ().fork ()
7778 else :
78- isolation_scope = None
79- current_scope = None
79+ isolation_scope = Scope ( ty = ScopeType . ISOLATION )
80+ current_scope = Scope ( ty = ScopeType . CURRENT )
8081
8182 # Patching instance methods in `start()` creates a reference cycle if
8283 # done in a naive way. See
@@ -98,7 +99,7 @@ def sentry_start(self, *a, **kw):
9899
99100
100101def _wrap_run (isolation_scope_to_use , current_scope_to_use , old_run_func ):
101- # type: (Optional[ sentry_sdk.Scope], Optional[ sentry_sdk.Scope] , F) -> F
102+ # type: (sentry_sdk.Scope, sentry_sdk.Scope, F) -> F
102103 @wraps (old_run_func )
103104 def run (* a , ** kw ):
104105 # type: (*Any, **Any) -> Any
@@ -110,13 +111,8 @@ def _run_old_run_func():
110111 except Exception :
111112 reraise (* _capture_exception ())
112113
113- if isolation_scope_to_use is not None and current_scope_to_use is not None :
114- with sentry_sdk .use_isolation_scope (isolation_scope_to_use ):
115- with sentry_sdk .use_scope (current_scope_to_use ):
116- return _run_old_run_func ()
117- else :
118- with sentry_sdk .isolation_scope () as scope :
119- scope .clear ()
114+ with sentry_sdk .use_isolation_scope (isolation_scope_to_use ):
115+ with sentry_sdk .use_scope (current_scope_to_use ):
120116 return _run_old_run_func ()
121117
122118 return run # type: ignore
0 commit comments