eval & execevalevalhas valid useseval dangerous?dicts
eval(supplied_syntax, {}, {})
().__class__.__base__.__subclasses__()
syntax = """
[
s for s in ().__class__.__base__.__subclasses__()
if s.__name__ == 'Quitter'
][0]('', 0)()
"""
eval(syntax, {}, {})
evalSee Ned Batchelders excellent talk on the topic.
http://bit.ly/2ZKGWcl https://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html
>>> ast.parse("obj.x == 123", mode="eval")
Expression(
body=Compare(
left=Attribute(
value=Name(id='obj', ctx=Load()),
attr='x',
ctx=Load()
),
ops=[Eq()],
comparators=[Num(n=123)]
)
)
ppast thanks to greentreesnakes.readthedocs.io
"foo", 123, True, Noneprint, raise, del, import...if/else, try/except, for, while, with...Depends on your application!
ast.parse(expression, mode="eval")ast.Call instances egcall.func.id in ("any", "all")not attr.attr.startswith("__")Code example!
evalWhat else can you do?
# The expression
left.name == right.name
# With an extra check
(left.name is not None) and (left.name == right.name)Permissions
has_permission(left, "name") and (left.name == right.name)