exceptiongroup
exceptiongroup copied to clipboard
`assert sys.excepthook is apport_python_hook.apport_excepthook` fails with Sentry
Things to check first
-
[X] I have searched the existing issues and didn't find my bug already reported there
-
[X] I have checked that my bug is still present in the latest release
Exceptiongroup version
1.2.1
Python version
3.10.12
What happened?
When using exceptiongroup
with Sentry inside a virtualenv activated with activate_this.py
on Ubuntu (see the reproduction below for the exact conditions), Sentry sets sys.excepthook
to a patched version of apport_python_hook.apport_excepthook
with the same __name__
. This breaks the assert sys.excepthook is apport_python_hook.apport_excepthook
statement in exceptiongroup._formatting
:
https://github.com/agronholm/exceptiongroup/blob/b91b7a34a0c688fe4bb06edcb2a0befffa21ca5f/src/exceptiongroup/_formatting.py#L374-L394
I’ve also reported this to Sentry:
- https://github.com/getsentry/sentry-python/issues/3097
But ideally this fragile assert
would be fixed in exceptiongroup
as well.
How can we reproduce the bug?
$ 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://[email protected]/1234")
import exceptiongroup
EOF
root@e264f830878b:/# python3 test.py
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