Beispiel #1
0
def _exec_module_patched(mod: ModuleType, exit_after: bool,
                         transformer: ast.NodeTransformer):
    source = inspect.getsource(mod)
    tree = compile(source,
                   mod.__file__,
                   'exec',
                   ast.PyCF_ONLY_AST,
                   dont_inherit=True,
                   optimize=-1)

    if not did_no_code_run_before_untypy_enable(tree):
        raise AssertionError(
            "Please put 'untypy.enable()' at the start of your module like so:\n"
            "\timport untypy\n"
            "\tuntypy.enable()")

    transformer.visit(tree)
    ast.fix_missing_locations(tree)
    patched_mod = compile(tree,
                          mod.__file__,
                          'exec',
                          dont_inherit=True,
                          optimize=-1)
    stack = list(map(lambda s: s.frame, inspect.stack()))
    try:
        exec(patched_mod, mod.__dict__)
    except Exception as e:
        while e.__traceback__.tb_frame in stack:
            e.__traceback__ = e.__traceback__.tb_next
        sys.excepthook(type(e), e, e.__traceback__)
        if exit_after: sys.exit(-1)
    if exit_after: sys.exit(0)
Beispiel #2
0
 def visit(self, node):
     """Ensure statement only contains allowed nodes."""
     if not isinstance(node, self.ALLOWED):
         raise SyntaxError('Not allowed in environment markers.\n%s\n%s' %
                            (self.statement, 
                            (' ' * node.col_offset) + '^'))
     return NodeTransformer.visit(self, node)
Beispiel #3
0
 def visit(self, *args, **kargs):
     result = NodeTransformer.visit(self, *args, **kargs)
     fix_missing_locations(result)
     return result