Repository: getsentry/sentry-python

Issue:
`sentry_sdk.init` breaks `import exceptiongroup` in virtualenv activated with `activate_this.py`
### How do you use Sentry?

Sentry Saas (sentry.io)

### Version

2.2.1

### Steps to Reproduce

```console
$ docker run --rm -it ubuntu:22.04
root@e264f830878b:/# apt update
root@e264f830878b:/# apt install -y python3-apport virtualenv
root@e264f830878b:/# virtualenv venv
root@e264f830878b:/# venv/bin/pip install exceptiongroup sentry-sdk
…
Successfully installed certifi-2024.2.2 exceptiongroup-1.2.1 sentry-sdk-2.2.1 urllib3-2.2.1
root@e264f830878b:/# cat > test.py <<EOF
exec(open("venv/bin/activate_this.py").read(), {"__file__": "venv/bin/activate_this.py"})
import sentry_sdk
sentry_sdk.init(dsn="https://aaa@bbb.ingest.sentry.io/1234")
import exceptiongroup
EOF
root@e264f830878b:/# python3 test.py
```

### Expected Result

No error.

### Actual Result

```pytb
Traceback (most recent call last):
  File "//test.py", line 4, in <module>
    import exceptiongroup
  File "/venv/lib/python3.10/site-packages/exceptiongroup/__init__.py", line 20, in <module>
    from ._formatting import (
  File "/venv/lib/python3.10/site-packages/exceptiongroup/_formatting.py", line 394, in <module>
    assert sys.excepthook is apport_python_hook.apport_excepthook
AssertionError
Sentry is attempting to send 2 pending events
Waiting up to 2 seconds
Press Ctrl-C to quit
```

The [relevant code within `exceptiongroup`](https://github.com/agronholm/exceptiongroup/blob/1.2.1/src/exceptiongroup/_formatting.py#L374-L400) is

```python
if getattr(sys.excepthook, "__name__", None) in (
    "apport_excepthook",
    # on ubuntu 22.10 the hook was renamed to partial_apport_excepthook
    "partial_apport_excepthook",
):
    …
    import apport_python_hook

    assert sys.excepthook is apport_python_hook.apport_excepthook
```

which fails because Sentry has patched `sys.excepthook` but retained the same `__name__`, due to `functools.wraps` within the `ensure_integration_enabled` decorator, used by `_make_excepthook` as of

- #2906

(cc @sentrivana)

This is arguably poor code within `exceptiongroup` (I opened agronholm/exceptiongroup#123), but Sentry should avoid breaking it since it’s a popular library; for example, it’s a dependency of IPython.


Discussion:

Resolving patch:
diff --git a/sentry_sdk/integrations/excepthook.py b/sentry_sdk/integrations/excepthook.py
--- a/sentry_sdk/integrations/excepthook.py
+++ b/sentry_sdk/integrations/excepthook.py
@@ -3,7 +3,6 @@
 import sentry_sdk
 from sentry_sdk.utils import (
     capture_internal_exceptions,
-    ensure_integration_enabled,
     event_from_exception,
 )
 from sentry_sdk.integrations import Integration
@@ -47,11 +46,16 @@ def setup_once():
 
 def _make_excepthook(old_excepthook):
     # type: (Excepthook) -> Excepthook
-    @ensure_integration_enabled(ExcepthookIntegration, old_excepthook)
     def sentry_sdk_excepthook(type_, value, traceback):
         # type: (Type[BaseException], BaseException, Optional[TracebackType]) -> None
         integration = sentry_sdk.get_client().get_integration(ExcepthookIntegration)
 
+        # Note: If  we replace this with ensure_integration_enabled then
+        # we break the exceptiongroup backport;
+        # See: https://github.com/getsentry/sentry-python/issues/3097
+        if integration is None:
+            return old_excepthook(type_, value, traceback)
+
         if _should_send(integration.always_run):
             with capture_internal_exceptions():
                 event, hint = event_from_exception(


Test patch: