eval
& exec
eval
eval
has valid useseval
dangerous?dict
s
eval(supplied_syntax, {}, {})
().__class__.__base__.__subclasses__()
syntax = """
[
s for s in ().__class__.__base__.__subclasses__()
if s.__name__ == 'Quitter'
][0]('', 0)()
"""
eval(syntax, {}, {})
eval
See 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, None
print
, 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!
eval
What 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)