[英]How to change __builtins__ module variable?
If you run this code:如果您运行此代码:
src = "import os"
d = dict(__builtins__={})
exec src in d
Python says: Python 说:
ImportError: __import__ not found
That's what I like to do, but when creating (or maybe loading) a new module:这就是我喜欢做的,但是在创建(或加载)新模块时:
import imp
mod = imp.new_module("foo")
src = "import os"
exec src in mod.__dict__
As you can see it runs, but I like it doesn't as in the above program.正如你所看到的,它运行了,但我喜欢它不像上面的程序那样。 I like to disable all the built-in variables and functions.我喜欢禁用所有内置变量和函数。 Is there any way to do this?有没有办法做到这一点?
If you print mod.__dict__
, you can see that it has __builtins__
variable such as any Python module.如果你打印mod.__dict__
,你可以看到它有__builtins__
变量,比如任何 Python 模块。 I think I have to change its value to {}
, but I don't know how.我想我必须将其值更改为{}
,但我不知道如何。
我想我必须将其值更改为 {},但我不知道如何更改。
mod.__dict__["__builtins__"] = {}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.