-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
PyImport_ImportModule #126038
Comments
I'll investigate this a little. At first, I thought this was just user-error, but all |
I'm researching the development of event audit ideas. And these probably won't be the last problems found :) |
A fair warning: issues resulting from manual manipulation of |
My job is to write about strange things. But the verdict is yours :) |
It does, the interpreter relies on Python modules like |
Also import sys
import builtins
name = "json"
print(builtins.__import__(name)) # <module 'json'>
print(ctypes.pythonapi.PyImport_ImportModule(ctypes.c_char_p(name.encode()))) # 3536781451
sys.modules = sys.modules.copy()
name = "email"
print(builtins.__import__(name)) # <module 'email'>
print(ctypes.pythonapi.PyImport_ImportModule(ctypes.c_char_p(name.encode()))) # KeyError: 'email' |
Do you know about any internal caches? |
This is definitely a bug :) import sys
import builtins
sms = sys.modules
sys.modules = sms.copy()
name = "email"
m = builtins.__import__(name)
print(m) # <module 'email'>
sms[name] = m
print(ctypes.pythonapi.PyImport_ImportModule(ctypes.c_char_p(name.encode()))) # 456145783 |
Ah, I found the issue--it's a wontfix. The interpreter uses two references to the modules dictionary: the actual one on the interpreter state, and then The fix would be to allow changes made on |
I'm using a similar idea but for a different task. But the main collection is still only one.
And is this considered normal and beyond fixing? |
It's "fixable", but would be such a large change that a PEP would probably be needed. The main issue is that it's unsafe to store arbitrary objects in the sysdict. It's much easier to just use the private C API and mess with the interpreter state if you want to change it, but accept that you're in uncharted waters when doing that. If you really do want this without the C API or some other black magic, then you'll have to go to DPO, and make a convincing argument for a). why this is needed and b). how it would be implemented (the finalizers part will be exceptionally difficult to figure out). |
But at the same time, this is allowed in the python version ;)
I've already talked enough there and I'll talk more. But I was told to write about bugs here. And I finally want to hear not references back and forth but the opinions of the core developers. |
I don't think this is a bug at this point, it's a feature. The behavior works exactly as I would expect it to, knowing that the interpreter's module dictionary cannot be overwritten. |
I understand your opinion. But I think you understood mine too :) |
See the
You're welcome to play around with this cache. It can be quite useful for debugging or, if you're careful, testing. But if it breaks, you get to keep both pieces. |
That said, if this was a crash, rather than exception or other unexpected behaviour, we would want to fix it. |
It might be worth cross-referencing this from |
Perhaps the |
I think the more useful note would be to mention that |
Bug report
Bug description:
CPython versions tested on:
3.12, 3.13
Operating systems tested on:
No response
The text was updated successfully, but these errors were encountered: