lmfit / asteval

minimalistic evaluator of python expression using ast module
https://lmfit.github.io/asteval
MIT License
183 stars 41 forks source link

hiding errors prints #88

Closed xhajnal closed 8 months ago

xhajnal commented 3 years ago

Could you introduce a switch to turn prints of errors off?

it seems as it is asteval.py lines 324, 328: print(errmsg, file=self.err_writer)

Thank you.

newville commented 3 years ago

@xhajnal One can set the err_writer attribute, or pass it in on creating the interpreter:

   aeval = Interpreter(err_writer=open('/dev/null', 'w'))

for example. I think your app could also replace sys.stderr before creating the Interpreter.

Looking at this again, I think the logic of eval() should be changed to (in part to stay back-compatible) to use show_errors to print error messages (one could still explicitly set the err_writer attribute), and raise_errors to raise the error in the calling code. With that change,

eval(expression, show_errors=False, raise_errors=False) would be silent on errors. Can't say I would recommend using that, but separating raise from show seems reasonable.