Exemplo n.º 1
0
    def __init__(self):
        self._source_ast = None
        self._main = False
        self._docstrings = False
        self._source_ast = False

        NodeTransformer.__init__(self)
Exemplo n.º 2
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)
Exemplo n.º 3
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)
Exemplo n.º 4
0
 def generic_visit(self, node):
     name = 'do{}'.format(type(node).__name__)
     node = NodeTransformer.generic_visit(self, node)
     if hasattr(self, name):
         boundary, value = getattr(self, name)(node)
         newstr = boundary
         for char in value:
             newstr += MAPPING[CHAR_MAPPING[char]]['unicode']
         newstr += boundary
         return copy_location(Name(id=newstr), node)
     return node
Exemplo n.º 5
0
    def generic_visit(self, node):
        if node is None:
            return node
        if isinstance(node, (str)):
            import pdb
            pdb.set_trace()
            return node

#        if not isinstance(node, (_ast.expr, _ast.expr_context, _ast.slice, _ast.operator, _ast.boolop)):
#            raise Exception("expected a Python '_ast.expr' node (got %r)" % (type(node),))
        return NodeTransformer.generic_visit(self, node)
Exemplo n.º 6
0
    def generic_visit(self, node):
        """
        visits the `AST`, if it's the right one, replace it

        :param node: The AST node
        :type node: ```AST```

        :return: Potentially changed AST node
        :rtype: ```AST```
        """
        if (
            not self.replaced
            and hasattr(node, "_location")
            and node._location == self.search
        ):
            self.replaced = True
            return self.replacement_node
        else:
            return NodeTransformer.generic_visit(self, node)
Exemplo n.º 7
0
 def __init__(self):
     NodeTransformer.__init__(self)
     self.gene_set = set()
Exemplo n.º 8
0
 def __init__(self, local_dict, global_dict):
     NodeTransformer.__init__(self)
     self.local_dict = local_dict
     self.global_dict = global_dict
Exemplo n.º 9
0
 def __init__(self):
     NodeTransformer.__init__(self)
     self.gene_set = set()
Exemplo n.º 10
0
 def visit(self, *args, **kargs):
     result = NodeTransformer.visit(self, *args, **kargs)
     fix_missing_locations(result)
     return result
Exemplo n.º 11
0
 def __init__(self, target_genes):
     NodeTransformer.__init__(self)
     self.target_genes = {str(i) for i in target_genes}
Exemplo n.º 12
0
 def generic_visit(self, node):
     if not isinstance(node, CtreeNode):
         NodeTransformer.generic_visit(self,node)
         return NonCtreeNode(node)
     return NodeTransformer.generic_visit(self,node)
Exemplo n.º 13
0
 def visit_Return(self, node):
     return NodeTransformer.generic_visit(self, node.value)
Exemplo n.º 14
0
 def __init__(self, local_dict, global_dict):
     NodeTransformer.__init__(self)
     self.local_dict = local_dict
     self.global_dict = global_dict
Exemplo n.º 15
0
 def __init__(self, target_genes):
     NodeTransformer.__init__(self)
     self.target_genes = {str(i) for i in target_genes}
Exemplo n.º 16
0
 def generic_visit(self, node):
     if (isinstance(node, ast.stmt)
             and self.references_arg(node)) or isinstance(node, ast.Return):
         return self.get_waits() + [node]
     return NodeTransformer.generic_visit(self, node)
Exemplo n.º 17
0
 def __init__(self):
     '''See the documentation of the class'''
     NodeTransformer.__init__(self)
     self._production_name = None
     self._hint_name = ""
     self._counter = 0