Python: make eval safe [duplicate]

are eval’s security issues fixable or
are there just too many tiny details
to get it working right?

Definitely the latter — a clever hacker will always manage to find a way around your precautions.

If you’re satisfied with plain expressions using elementary-type literals only, use ast.literal_eval — that’s what it’s for! For anything fancier, I recommend a parsing package, such as ply if you’re familiar and comfortable with the classic lexx/yacc approach, or pyparsing for a possibly more Pythonic approach.

Leave a Comment