tags:

views:

117

answers:

2

First off, I'm aware of eval's disadvantages and it will be used in an experiment I want to make only.

I'm creating a script that works just like a Brute-Force algorithm but it won't break passwords but find the solution to a special form of an equation (more details are unnecessary).

There will be lots of strings filled with (often syntactically incorrect) terms like 1+2)+3

  • Is the only way to get the results of these terms via eval?
  • How to make python ignore syntactical errors occurring in eval? (The program shouldn't terminate)
+2  A: 

To suppress SyntaxError:

try:
    eval("1 + 2) + 3")
except SyntaxError:
    pass
Dominic Rodger
Thanks - that looks very pythonic :D
lamas
+1  A: 

Eval usually raises SyntaxError, you can cover your code with

try:
  a = eval('1+2)+3')
except SyntaxError:
  pass

Remember, you can isolate eval from accessing any functions passing { '__builtin__': None } as second parameter.

Vestel