Reputation: 97
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.
Upvotes: 2
Views: 1402
Reputation: 180441
I think I have to change its value to {}, but I don't know how.
mod.__dict__["__builtins__"] = {}
Upvotes: 2