Skip to content

lazy import hooks receive globals from the accessing module #162415

Open

Description

@tonghuaroot

Bug description:

A lazy import hook receives the globals of the module that first accesses the
binding, rather than the globals that own the binding.

import builtins
import types

owner = types.ModuleType("owner")
seen = []

def hook(name, globals=None, locals=None, fromlist=(), level=0):
    seen.append(globals is owner.__dict__)
    return types.ModuleType(name)

owner.__dict__["__builtins__"] = dict(vars(builtins), __import__=hook)
exec("lazy import target", owner.__dict__)
consumer = {"owner": owner, "__builtins__": vars(builtins)}
exec("owner.target", consumer)
print(seen)

This prints [False]. It should print [True].

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions