Skip to content

module_from_spec #126039

@mwriter

Description

@mwriter

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-importlibtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions