As I said in a comment, eval
or exec
are evil, a better alternative is to use the code module as in the following example. Also, you may prefer to compile the regex, in order to only perform the matching at each iteration over the input instead of building the automaton at each iteration.
import fileinput
import code
console = code.InteractiveConsole(locals())
import re
r = re.compile(r'([ ]{4}|\t)(.*)')
code = ''
for line in fileinput.input():
match = r.match(line)
if match:
console.push(match.group(2))
Though that example will output the results on sys.stdout
so you may want to use instead an overloaded class such as the one in this example.