How safe is SymPy's sympify(<string>).evalf()?

梦想的初衷 提交于 2019-11-30 13:58:28

There is nothing common between python eval and sympy evalf (the latter is for calculating the numeric value of sympy expression trees and it has nothing to do with parsing, while eval is all about parsing a string and evaluating it as if it is code).

On the other hand, sympify is just as dangerous as eval, because it actually uses eval.

There are two basic modes in which sympify is used and probably it is a bad idea that they got mixed in the same function:

  • sympify(some_object) would return a representation of the object more suited for use in a CAS, like transforming int(1) into sympy.Integer(1)

  • sympify("some_text") would parse the text almost directly through eval (search for the import from sympy.parsing present in sympify and follow it). It is safer as there are some constraints but it is not safe.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!