Skip to content
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

module_from_spec #126039

Closed
mwriter opened this issue Oct 27, 2024 · 7 comments
Closed

module_from_spec #126039

mwriter opened this issue Oct 27, 2024 · 7 comments
Labels
stdlib Python modules in the Lib dir topic-importlib type-bug An unexpected behavior, bug, or error

Comments

@mwriter
Copy link

mwriter commented Oct 27, 2024

Bug report

Bug description:

This may not be a bug, but it's a weirdness.
Why is there an automatic implicit re-registration of the builtins module in the latter case?

import sys
import importlib.util

import email

name = "email"

spec = importlib.util.find_spec(name)

sys.modules[name] = "ha"
print(sys.modules[name]) # ha

importlib.util.module_from_spec(spec)
print(sys.modules[name]) # ha

import builtins

name = "builtins"

spec = importlib.util.find_spec(name)

sys.modules[name] = "ha"
print(sys.modules[name]) # ha

importlib.util.module_from_spec(spec)
print(sys.modules[name]) # <module 'builtins' (built-in)>

CPython versions tested on:

3.12, 3.13

Operating systems tested on:

No response

@mwriter mwriter added the type-bug An unexpected behavior, bug, or error label Oct 27, 2024
@ZeroIntensity ZeroIntensity added stdlib Python modules in the Lib dir topic-importlib labels Oct 27, 2024
@ZeroIntensity
Copy link
Member

builtins is needed by the interpreter for all sorts of things. It probably decides to do this instead of crashing, so I don't think there's anything that can be done here.

@ZeroIntensity ZeroIntensity added the pending The issue will be closed if no feedback is provided label Oct 27, 2024
@mwriter
Copy link
Author

mwriter commented Oct 27, 2024

builtins is needed by the interpreter for all sorts of things. It probably decides to do this instead of crashing, so I don't think there's anything that can be done here.

This isn't true :)
I can do this

import builtins

name = "builtins"

spec = importlib.util.find_spec(name)

sys.modules[name] = "ha"
print(sys.modules[name]) # ha

importlib.util.module_from_spec(spec)
print(sys.modules[name]) # ha

Perhaps I will present this part of my research soon. Although it was bullied on the main forum.

@ZeroIntensity
Copy link
Member

Yes, because you don't overwrite builtins there. The interpreter intentionally keeps its own copy of builtins, it doesn't surprise me that it swaps it out if it needs to.

@ZeroIntensity
Copy link
Member

In general, black magic with sys.modules won't be encouraged or fixed on the Python side. I suggest for both of your issues you use the private C API to manually mess with things, you have more power there.

@ZeroIntensity ZeroIntensity closed this as not planned Won't fix, can't repro, duplicate, stale Oct 27, 2024
@ZeroIntensity ZeroIntensity removed the pending The issue will be closed if no feedback is provided label Oct 27, 2024
@mwriter
Copy link
Author

mwriter commented Oct 27, 2024

The interpreter intentionally keeps its own copy of builtins

According to my research, all copies can be overwritten.

In general, black magic with sys.modules won't be encouraged or fixed on the Python side

There are no problems for me here. At the python level, I can change everything myself.
But there is a clear discrepancy in the logic of the code's behavior.

I suggest for both of your issues you use the private C API to manually mess with things

For example?

@ZeroIntensity
Copy link
Member

According to my research, all copies can be overwritten.

Sure, but that doesn't mean we have to support it :)

For example?

Truly overwriting the sys.modules:

#define Py_BUILD_CORE
#include "pycore_interp.h"

static PyObject *
overwrite_sys_modules(PyObject *self, PyObject *obj)
{
    PyInterpreterState *interp = PyInterpreterState_Get();
    Py_SETREF(interp->imports.modules, Py_NewRef(obj));
    Py_RETURN_NONE;
}

@mwriter
Copy link
Author

mwriter commented Oct 27, 2024

Sure, but that doesn't mean we have to support it

This is understandable. It was only in the context of the behavior inconsistency in the original example here. But for me it's not a problem at all. I just decided to write :)

Truly overwriting the

Thanks for the example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir topic-importlib type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants