1

根据PyPy 文档,“内置名称总是引用内置模块,而不是像有时在 CPython 中那样的字典。分配给内置没有任何效果。” 例如在 CPython 中:

>>> eval("__import__('os').system('clear')", {'__builtins__':{}})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name '__import__' is not defined

在 PyPy 中,它运行它没有错误。还有另一种限制可用内置函数的方法吗?

4

1 回答 1

2

不,我们很久以前就决定不提供这个。也许我们现在可以重新考虑它,但请注意它只会给人一种虚假的安全感。即使使用{'__builtins__':{}}. 请参阅Python:使 eval 安全

于 2013-04-15T16:41:24.323 回答