Skip to content

Commit c715d28

Browse files
committed
Improved eval2 handling in helpers.expressions
1 parent 0232407 commit c715d28

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/tinyscript/VERSION.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.30.12
1+
1.30.13

src/tinyscript/helpers/expressions.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414

1515
BL_BUILTINS = ("breakpoint", "classmethod", "compile", "delattr", "eval", "exec", "exit", "getattr", "globals", "help",
1616
"hasattr", "input", "locals", "memoryview", "open", "print", "property", "quit", "staticmethod", "super")
17+
COMP_NODES = ("DictComp", "Lambda", "ListComp", "SetComp", "GeneratorExp")
1718
WL_NODES = ("add", "and", "binop", "bitand", "bitor", "bitxor", "boolop", "call", "cmpop", "compare", "comprehension",
18-
"constant", "dict", "div", "eq", "expr", "expr_context", "expression", "floordiv", "for", "generatorexp",
19-
"gt", "gte", "in", "index", "invert", "is", "isnot", "list", "listcomp", "load", "lshift", "lt", "lte",
20-
"matmult", "mod", "mult", "name", "nameconstant", "not", "noteq", "notin", "num", "operator", "or", "pow",
21-
"rshift", "set", "slice", "store", "str", "sub", "subscript", "tuple", "uadd", "unaryop", "usub")
19+
"constant", "dict", "dictcomp", "div", "eq", "expr", "expr_context", "expression", "floordiv", "for",
20+
"generatorexp", "gt", "gte", "in", "index", "invert", "is", "isnot", "list", "listcomp", "load", "lshift",
21+
"lt", "lte", "matmult", "mod", "mult", "name", "nameconstant", "not", "noteq", "notin", "num", "operator",
22+
"or", "pow", "rshift", "set", "setcomp", "slice", "store", "str", "sub", "subscript", "tuple", "uadd",
23+
"unaryop", "usub")
2224

2325

2426
set_exception("ForbiddenNameError", "NameError")
@@ -40,7 +42,7 @@ def __eval(expr, globals=None, locals=None, bl_builtins=BL_BUILTINS, wl_nodes=WL
4042
# walk the AST and only allow the whitelisted nodes
4143
extra_names = []
4244
for node in __walk(ast.parse(expr, mode="eval")):
43-
if any(n in list(map(lambda x: x.name, node.parents)) for n in ("Lambda", "ListComp", "GeneratorExp")) and \
45+
if any(n in list(map(lambda x: x.name, node.parents)) for n in COMP_NODES) and \
4446
hasattr(node, "id") and node.id not in extra_names:
4547
extra_names.append(node.id)
4648
# blacklist dunders and input list
@@ -100,7 +102,7 @@ def eval_free_variables(expression, **variables):
100102
"""
101103
free_vars = []
102104
for node in __walk(ast.parse(expression, mode="eval")):
103-
if any(n in list(map(lambda x: x.name, node.parents)) for n in ("Lambda", "ListComp", "GeneratorExp")) and \
105+
if any(n in list(map(lambda x: x.name, node.parents)) for n in COMP_NODES) and \
104106
hasattr(node, "id") and node.id not in variables and node.id not in free_vars:
105107
free_vars.append(node.id)
106108
return free_vars

0 commit comments

Comments
 (0)