make all builtins available in action code
This commit is contained in:
parent
bede501752
commit
943af428a7
1 changed files with 3 additions and 2 deletions
|
|
@ -1,12 +1,13 @@
|
||||||
# based on https://stackoverflow.com/a/39381428
|
# based on https://stackoverflow.com/a/39381428
|
||||||
# Parses and executes a block of Python code, and returns the eval result of the last statement
|
# Parses and executes a block of Python code, and returns the eval result of the last statement
|
||||||
|
|
||||||
import ast
|
import ast
|
||||||
def exec_then_eval(code, _globals={}, _locals={}):
|
def exec_then_eval(code, _globals={}, _locals={}):
|
||||||
block = ast.parse(code, mode='exec')
|
block = ast.parse(code, mode='exec')
|
||||||
# assumes last node is an expression
|
# assumes last node is an expression
|
||||||
last = ast.Expression(block.body.pop().value)
|
last = ast.Expression(block.body.pop().value)
|
||||||
extended_globals = {
|
extended_globals = {
|
||||||
'__builtins__': {'isinstance': isinstance, 'print': print, 'int': int, 'float': float, 'bool': bool, 'str': str, 'tuple': tuple, 'len': len, 'set': set, 'dict': dict, 'eval': eval },
|
'__builtins__': __builtins__,
|
||||||
**_globals,
|
**_globals,
|
||||||
}
|
}
|
||||||
exec(compile(block, '<string>', mode='exec'), extended_globals, _locals)
|
exec(compile(block, '<string>', mode='exec'), extended_globals, _locals)
|
||||||
|
|
@ -15,7 +16,7 @@ def exec_then_eval(code, _globals={}, _locals={}):
|
||||||
def simply_exec(code, _globals={}, _locals={}):
|
def simply_exec(code, _globals={}, _locals={}):
|
||||||
block = ast.parse(code, mode='exec')
|
block = ast.parse(code, mode='exec')
|
||||||
extended_globals = {
|
extended_globals = {
|
||||||
'__builtins__': {'isinstance': isinstance, 'print': print, 'int': int, 'float': float, 'bool': bool, 'str': str, 'tuple': tuple, 'len': len, 'set': set, 'dict': dict, 'eval': eval },
|
'__builtins__': __builtins__,
|
||||||
**_globals,
|
**_globals,
|
||||||
}
|
}
|
||||||
exec(compile(block, '<string>', mode='exec'), extended_globals, _locals)
|
exec(compile(block, '<string>', mode='exec'), extended_globals, _locals)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue