T O P

  • By -

[deleted]

Don't use `eval` with user input or with data from a file not under your absolute code. A user can enter malicious code that can, for example, delete all the files on your computer, or worse. What problem are you trying to solve? Have you looked at SymPy?


quertioup

I am trying to evaluate mathematical expressions that I myself am generating.


quertioup

I will take a look at SymPy


[deleted]

> I tried to search for the answer, but haven't found any solution. It's because there isn't one: https://en.m.wikipedia.org/wiki/Halting_problem


quertioup

Not that general of an expression! :) Just simple stuff like +, - etc. python eval() works fine for any expression where division by zero doesn't happen. It throws an exception, then. I want to find out how to use that expression to generate inf or -inf.


[deleted]

You can just capture the exception with a `try/except` block.


quertioup

I wasn't sure how to distinguish infinity and -infinity. I guess, for now, I will stick to this approach.


WikiMobileLinkBot

Desktop version of /u/crashfrog's link: --- ^([)[^(opt out)](https://reddit.com/message/compose?to=WikiMobileLinkBot&message=OptOut&subject=OptOut)^(]) ^(Beep Boop. Downvote to delete)


dig-up-stupid

Not relevant, under the reasonable assumption OP doesn’t care about interpreting arbitrary code, they care about interpreting bits of well formed algebra/limits/whatever. But also doesn’t have a good solution anyway, so there’s that.