Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import ast
- CODE_SAFE = 0
- ERROR_IMPORT = 1
- ERROR_CLASS = 2
- ERROR_GLOBAL = 3
- ERROR_YIELD = 4
- obj_banned = {
- ast.Import: 1,
- ast.ImportFrom: 1,
- ast.ClassDef: 2,
- ast.Global: 3,
- ast.Yield: 4,
- ast.YieldFrom: 5
- }
- def _is_safe(node):
- if type(node) in obj_banned:
- return obj_banned[type(node)]
- return 0
- def check_safe(code):
- source = ast.parse(code)
- next = [source]
- while len(next) > 0:
- new_next = []
- for obj in next:
- result = _is_safe(obj)
- if result > 0:
- return result
- new_next += ast.iter_child_nodes(obj)
- next = new_next
- return 0
- def run_exec(code):
- local_space = {"message": None, "spam": None}
- exec(code, {}, local_space)
- return local_space
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement