If you run this code:
src = "import os"
d = dict(__builtins__={})
exec src in d
Python says:
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. I think I have to change its value to {}
, but I don't know how.
我想我必须将其值更改为 {},但我不知道如何更改。
mod.__dict__["__builtins__"] = {}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.