from testutils import assert_raises

assert "__builtins__" in globals()
# assert type(__builtins__).__name__ == 'module'
with assert_raises(AttributeError):
    __builtins__.__builtins__

assert __builtins__.__name__ == "builtins"
import builtins

assert builtins.__name__ == "builtins"

__builtins__.x = "new"
assert x == "new"  # noqa: F821

exec('assert "__builtins__" in globals()', dict())
exec("assert __builtins__ == 7", {"__builtins__": 7})
exec("assert not isinstance(__builtins__, dict)")
exec("assert isinstance(__builtins__, dict)", {})

namespace = {}
exec("", namespace)
assert namespace["__builtins__"] == __builtins__.__dict__


# function.__builtins__ should be a dict, not a module
# See: https://docs.python.org/3/reference/datamodel.html
def test_func():
    pass


assert isinstance(test_func.__builtins__, dict), (
    f"function.__builtins__ should be dict, got {type(test_func.__builtins__)}"
)

# with assert_raises(NameError):
#     exec('print(__builtins__)', {'__builtins__': {}})

# __builtins__ is deletable but names are alive
del __builtins__
with assert_raises(NameError):
    __builtins__  # noqa: F821

assert print
