I have a dict
subclass like this:
class MyDict(dict):
def __getitem__(self, name):
return globals()[name]
This class can be used with eval
and exec
without issues:
>>> eval('bytearray', MyDict())
<class 'bytearray'>
>>> exec('print(bytearray)', MyDict())
<class 'bytearray'>
But if I instantiate a function object with the types.FunctionType
constructor, the function can't access any builtins:
import types
func = lambda: bytearray
func_copy = types.FunctionType(func.__code__,
MyDict(),
func.__name__,
func.__defaults__,
func.__closure__)
print(func_copy())
# Traceback (most recent call last):
# File "untitled.py", line 16, in <module>
# print(func_copy())
# File "untitled.py", line 8, in <lambda>
# func = lambda: bytearray
# NameError: name 'bytearray' is not defined
Replacing MyDict()
with globals()
or dict(globals())
or event {'__builtins__': __builtins__}
makes the code print <class 'bytearray'>
as expected.
I don't understand where this exception is coming from. Can anyone explain this behavior? Why does it work with eval
but not with a function object?