Why can't I access builtins if I use a custom dict as a function's globals?

99封情书 提交于 2019-12-07 01:46:51

问题


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?


回答1:


Not a complete answer, but what seems to be happening is that CPython ignores the custom __getitem__ when it accesses the builtins. It seems to treat MyDict like a normal (not subclassed) dict. If the '__builtins__' key is actually present in the dict, then everything works correctly:

class MyDict(dict):
    def __getitem__(self, name):
        return globals()[name]


import types

globs = MyDict()
globs['__builtins__'] = __builtins__

func = lambda: bytearray
func_copy = types.FunctionType(func.__code__,
                              globs,
                              func.__name__,
                              func.__defaults__,
                              func.__closure__)

print(func_copy())
# output: <class 'bytearray'>

The question remains why this only happens with FunctionType, and not with eval and exec.



来源:https://stackoverflow.com/questions/50604799/why-cant-i-access-builtins-if-i-use-a-custom-dict-as-a-functions-globals

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!