def exec_compat(expression, globals, locals=None):
    """
    Compatibility exec function for python2 and python3.

    The python2 exec() statement is bound to the following restriction:

        If exec is used in a function and the function contains a nested
        block with free variables, the compiler will raise a SyntaxError
        unless the exec explicitly specifies the local namespace for the
        exec. (In  other words, "exec  obj" would be illegal,  but "exec
        obj in ns" would be legal.)

        http://www.python.org/dev/peps/pep-0227/

    It seems the 3-tuple form of the exec-statement is not recognized as
    explicitly  specifying the  local namespace  therefore creating  the
    need to call it from an intermediate function without nested scope.

    The  3-tuple   form  of   the  exec   statement  is   mandatory  for
    compatibility between python2 and python3.

    http://docs.python.org/2/reference/simple_stmts.html#the-exec-statement
    http://docs.python.org/3.3/library/functions.html?highlight=exec#exec
    """
    exec(expression, globals, locals)
