@classmethod<EOL><INDENT>def compile_code(cls, code, filename=None, exec_namespace=None):<DEDENT>
if filename is None:<EOL><INDENT>if isinstance(code, SourceCode):<EOL><INDENT>filename = code.sourcename<EOL><DEDENT>else:<EOL><INDENT>filename = "<STR_LIT>"<EOL><DEDENT><DEDENT>try:<EOL><INDENT>if isinstance(code, SourceCode):<EOL><INDENT>pyc = code.compiled<EOL><DEDENT>else:<EOL><INDENT>pyc = compile(code, filename, '<STR_LIT>')<EOL><DEDENT><DEDENT>except SourceCodeError as e:<EOL><INDENT>reraise(e, RexError)<EOL><DEDENT>except Exception as e:<EOL><INDENT>stack = traceback.format_exc()<EOL>raise RexError("<STR_LIT>" % (filename, stack))<EOL><DEDENT>error_class = Exception if config.catch_rex_errors else None<EOL>if exec_namespace is not None:<EOL><INDENT>try:<EOL><INDENT>if isinstance(code, SourceCode):<EOL><INDENT>code.exec_(globals_=exec_namespace)<EOL><DEDENT>else:<EOL><INDENT>exec(pyc, exec_namespace)<EOL><DEDENT><DEDENT>except RexError:<EOL><INDENT>raise<EOL><DEDENT>except SourceCodeError as e:<EOL><INDENT>reraise(e, RexError)<EOL><DEDENT>except error_class as e:<EOL><INDENT>stack = traceback.format_exc()<EOL>raise RexError("<STR_LIT>" % (filename, stack))<EOL><DEDENT><DEDENT>return pyc<EOL>
Compile and possibly execute rex code.

        Args:
            code (str or SourceCode): The python code to compile.
            filename (str): File to associate with the code, will default to
                '<string>'.
            exec_namespace (dict): Namespace to execute the code in. If None,
                the code is not executed.

        Returns:
            Compiled code object.