2323 from typing import Any
2424 from typing import Optional
2525 from typing import Tuple
26+ from typing import Dict
2627 from typing import List
2728 from typing import Callable
2829 from typing import Generator
@@ -47,6 +48,24 @@ def overload(x):
4748_local = ContextVar ("sentry_current_hub" )
4849
4950
51+ def _update_scope (base , scope_change , scope_kwargs ):
52+ # type: (Scope, Optional[Any], Dict[str, Any]) -> Scope
53+ if scope_change and scope_kwargs :
54+ raise TypeError ("cannot provide scope and kwargs" )
55+ if scope_change is not None :
56+ final_scope = copy .copy (base )
57+ if callable (scope_change ):
58+ scope_change (final_scope )
59+ else :
60+ final_scope .update_from_scope (scope_change )
61+ elif scope_kwargs :
62+ final_scope = copy .copy (base )
63+ final_scope .update_from_kwargs (scope_kwargs )
64+ else :
65+ final_scope = base
66+ return final_scope
67+
68+
5069def _should_send_default_pii ():
5170 # type: () -> bool
5271 client = Hub .current .client
@@ -285,11 +304,14 @@ def capture_event(
285304 self ,
286305 event , # type: Event
287306 hint = None , # type: Optional[Hint]
307+ scope = None , # type: Optional[Any]
308+ ** scope_args # type: Dict[str, Any]
288309 ):
289310 # type: (...) -> Optional[str]
290311 """Captures an event. Alias of :py:meth:`sentry_sdk.Client.capture_event`.
291312 """
292- client , scope = self ._stack [- 1 ]
313+ client , top_scope = self ._stack [- 1 ]
314+ scope = _update_scope (top_scope , scope , scope_args )
293315 if client is not None :
294316 rv = client .capture_event (event , hint , scope )
295317 if rv is not None :
@@ -301,6 +323,8 @@ def capture_message(
301323 self ,
302324 message , # type: str
303325 level = None , # type: Optional[str]
326+ scope = None , # type: Optional[Any]
327+ ** scope_args # type: Dict[str, Any]
304328 ):
305329 # type: (...) -> Optional[str]
306330 """Captures a message. The message is just a string. If no level
@@ -312,10 +336,15 @@ def capture_message(
312336 return None
313337 if level is None :
314338 level = "info"
315- return self .capture_event ({"message" : message , "level" : level })
339+ return self .capture_event (
340+ {"message" : message , "level" : level }, scope = scope , ** scope_args
341+ )
316342
317343 def capture_exception (
318- self , error = None # type: Optional[Union[BaseException, ExcInfo]]
344+ self ,
345+ error = None , # type: Optional[Union[BaseException, ExcInfo]]
346+ scope = None , # type: Optional[Any]
347+ ** scope_args # type: Dict[str, Any]
319348 ):
320349 # type: (...) -> Optional[str]
321350 """Captures an exception.
@@ -334,7 +363,7 @@ def capture_exception(
334363
335364 event , hint = event_from_exception (exc_info , client_options = client .options )
336365 try :
337- return self .capture_event (event , hint = hint )
366+ return self .capture_event (event , hint = hint , scope = scope , ** scope_args )
338367 except Exception :
339368 self ._capture_internal_exception (sys .exc_info ())
340369
0 commit comments