Skip to content

Commit 5aabcba

Browse files
fix: Stop unconditionally importing contextvars
1 parent 7f20c23 commit 5aabcba

1 file changed

Lines changed: 22 additions & 23 deletions

File tree

sentry_sdk/utils.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -84,29 +84,6 @@
8484

8585
_installed_modules = None
8686

87-
_is_sentry_internal_task = contextvars.ContextVar(
88-
"is_sentry_internal_task", default=False
89-
)
90-
91-
# These exceptions won't set the span status to error if they occur. Use
92-
# register_control_flow_exception to add to this list
93-
_control_flow_exception_classes: "set[type]" = set()
94-
95-
96-
def is_internal_task() -> bool:
97-
return _is_sentry_internal_task.get()
98-
99-
100-
@contextmanager
101-
def mark_sentry_task_internal() -> "Generator[None, None, None]":
102-
"""Context manager to mark a task as Sentry internal."""
103-
token = _is_sentry_internal_task.set(True)
104-
try:
105-
yield
106-
finally:
107-
_is_sentry_internal_task.reset(token)
108-
109-
11087
BASE64_ALPHABET = re.compile(r"^[a-zA-Z0-9/+=]*$")
11188

11289
FALSY_ENV_VALUES = frozenset(("false", "f", "n", "no", "off", "0"))
@@ -1468,6 +1445,28 @@ def _get_contextvars() -> "Tuple[bool, type]":
14681445
Please refer to https://docs.sentry.io/platforms/python/contextvars/ for more information.
14691446
"""
14701447

1448+
_is_sentry_internal_task = contextvars.ContextVar(
1449+
"is_sentry_internal_task", default=False
1450+
)
1451+
1452+
# These exceptions won't set the span status to error if they occur. Use
1453+
# register_control_flow_exception to add to this list
1454+
_control_flow_exception_classes: "set[type]" = set()
1455+
1456+
1457+
def is_internal_task() -> bool:
1458+
return _is_sentry_internal_task.get()
1459+
1460+
1461+
@contextmanager
1462+
def mark_sentry_task_internal() -> "Generator[None, None, None]":
1463+
"""Context manager to mark a task as Sentry internal."""
1464+
token = _is_sentry_internal_task.set(True)
1465+
try:
1466+
yield
1467+
finally:
1468+
_is_sentry_internal_task.reset(token)
1469+
14711470

14721471
def qualname_from_function(func: "Callable[..., Any]") -> "Optional[str]":
14731472
"""Return the qualified name of func. Works with regular function, lambda, partial and partialmethod."""

0 commit comments

Comments
 (0)