New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
undefined behavior and crashes in case of a bad sys.modules #75585
Comments
at least on my Windows, the following code: import sys
sys.modules = []
IIUC, this bug was introduced in #1638 |
I'm looking into this. |
This is pretty messy. :( Ideally we would disallow setting sys.modules to anything except a dict (or perhaps any mapping). However, we don't have that option currently (see https://github.com/ericsnowcurrently/cpython/tree/sys-module). In the meantime we have to fix up all the places that are expecting a mapping. Part of the problem here is that a number of places fail silently, as evidenced by the failures Oren pointed out... |
I haven't finished reviewing PR 1638. I'm not sure that this change is worth. It breaks a code that assumes that sys.module is a dict, and I afraid it slows down importing. Maybe revert this change until resolving all problems? |
Yeah, I'm leaning that way myself. I'll take another look later today or tomorrow. FWIW, the likelihood of this causing problems to actual users is extremely low, so the urgency to fix this isn't high. Still, I don't want this to linger. As I said before, ideally we would prevent non-mappings from getting assigned to sys.modules. One alternative would be to introduce sys.__modules__ and fall back to it if sys.modules is invalid. |
Introducing sys.__modules__ doesn't solve the problem, since you'd be able to recreate the problems Oren reports just by messing with that as well as with sys.modules. And we try reasonable hard to protect users from completely breaking the interpreter just by assigning nonsense to sys module attributes. So my recommendation would be to revert bpo-28411, and make it clearer that the purpose of the interpreter state attribute is to defend against nonsensical rebinding of sys.modules, and that it can't be removed unless/until the sys module is switched to a custom type that enforces type checks on some of its attributes. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: