Here is the situation: I am building application that can take a mathematical expression and evaluate it (works as a calculator). I do not have the os
module imported, so I probably would not be able to execute rm -rf /
or anything super malicious like that; but I want to be completely sure. I stumbled across the parser module in Python, and it has a parser function that claims to be much safer than eval. There is somewhere eval() shows up:
expression = expression.split(";")
expression_outputs = []
for i in range(len(expression)):
result = parser.expr(expression[i].strip()).compile()
expression_outputs.append(complex(eval(result)))
print("Answer(s): " + str(expression_outputs))
Is this code safe? If not, what would be some alternatives?