Ejemplo n.º 1
0
def test_return_value_is_filled_dict_by_keyword():
    # when method return dict(a='b')
    try:
        assert ReturnedExpression(
            _ast.Return(
                value=_ast.Call(
                    func=_ast.Name(id='dict', ctx=_ast.Load()),
                    args=[],
                    keywords=[_ast.keyword(arg='a', value=_ast.Str(s='b'))],
                ),
                lineno=1,
            ), ).value_not_none() is True
    except (AttributeError):
        assert ReturnedExpression(
            _ast.Return(
                value=_ast.Call(
                    func=_ast.Name(id='dict', ctx=_ast.Load()),
                    args=[],
                    keywords=[
                        _ast.keyword(arg='a',
                                     value=_ast.JoinedStr(values=['a', 'b']))
                    ],
                ),
                lineno=1,
            ), ).value_not_none() is True
Ejemplo n.º 2
0
 def test_constructor(self):
     ast = self.ast
     body = []
     mod = ast.Module(body)
     assert mod.body is body
     target = ast.Name("hi", ast.Store())
     expr = ast.Name("apples", ast.Load())
     otherwise = []
     fr = ast.For(target, expr, body, otherwise, lineno=0, col_offset=1)
     assert fr.target is target
     assert fr.iter is expr
     assert fr.orelse is otherwise
     assert fr.body is body
     assert fr.lineno == 0
     assert fr.col_offset == 1
     fr = ast.For(body=body,
                  target=target,
                  iter=expr,
                  col_offset=1,
                  lineno=0,
                  orelse=otherwise)
     assert fr.target is target
     assert fr.iter is expr
     assert fr.orelse is otherwise
     assert fr.body is body
     assert fr.lineno == 0
     assert fr.col_offset == 1
     exc = raises(TypeError, ast.Module, 1, 2).value
     msg = str(exc)
     assert msg == "Module constructor takes either 0 or 1 positional argument"
     ast.Module(nothing=23)
Ejemplo n.º 3
0
 def visit_Name(self, node):
     if self.randomize():
         if node.id == 'forall':
             return ast.copy_location(_ast.Name(id='exists'), node)
         elif node.id == 'exists':
             return ast.copy_location(_ast.Name(id='forall'), node)
     return node
    def STORE_NAME(self, instr):

        value = self.ast_stack.pop()
        value = self.process_ifexpr(value)

        if isinstance(value, _ast.Import):

            if value.from_:
                assert isinstance(self.ast_stack[-1], _ast.ImportFrom)
                from_ = self.ast_stack.pop()

                as_name = instr.arg
                name = from_.names[0].name
                if as_name != name:
                    from_.names[0].asname = as_name

                self.ast_stack.append(from_)
            else:
                as_name = instr.arg
                if value.names[0].asname is None:
                    base_name = value.names[0].name.split('.')[0]
                    if base_name != as_name:
                        value.names[0].asname = as_name

            self.ast_stack.append(value)

        elif isinstance(value, (_ast.Attribute)) and isinstance(
                value.value, (_ast.Import)):
            asname = instr.arg
            value = value.value
            value.names[0].asname = asname

            self.ast_stack.append(value)

        elif isinstance(value, (_ast.ClassDef, _ast.FunctionDef)):
            as_name = instr.arg
            value.name = as_name
            self.ast_stack.append(value)
        elif isinstance(value, _ast.AugAssign):
            self.ast_stack.append(value)
        elif isinstance(value, _ast.Assign):
            _ = self.ast_stack.pop()
            assname = _ast.Name(instr.arg,
                                _ast.Store(),
                                lineno=instr.lineno,
                                col_offset=0)
            value.targets.append(assname)
            self.ast_stack.append(value)
        else:

            assname = _ast.Name(instr.arg,
                                _ast.Store(),
                                lineno=instr.lineno,
                                col_offset=0)

            assign = _ast.Assign(targets=[assname],
                                 value=value,
                                 lineno=instr.lineno,
                                 col_offset=0)
            self.ast_stack.append(assign)
Ejemplo n.º 5
0
 def new_class(self, items):
     identifier = items[0]
     if len(items) > 1:
         args = items[1]
     else:
         args = []
     return _ast.Call(func=_ast.Name(id=identifier), args=args, keywords=[])
Ejemplo n.º 6
0
 def runcode(self, the_code, source, filename='<input>'):
     # code taken from InteractiveInterpreter.runsource in code.py
     try:
         tree = ast.parse(source)
         try:
             expr = ast.parse(source, mode='eval')
         except:
             expr = None
         #todo get this to work for multiple expr's, not just 1:
         if expr and len(tree.body) == 1:
             # _ = expr_value
             tree.body[0] = ast_wrap_in_assn('_', tree.body[0])
             # print _
             underscore = _ast.Name(id="_", ctx=_ast.Load())
             print_node = ast_print_node([_ast.Str(s=' ' * 50), underscore])
             tree.body.append(print_node)
             # play_whatever
             #todo doesn't work for generators yet
             play_whatever_node = ast_call_node('music.play_whatever',
                                                '_',
                                                show_notes=SHOW_NOTES)
             tree.body.append(play_whatever_node)
         #print ast.dump(tree)
         code_obj = compile(tree, '<input>', 'exec')
         exec code_obj in self.locals
     except SystemExit:
         raise
     except:
         self.showtraceback()
     else:
         if code.softspace(sys.stdout, 0):
             print
Ejemplo n.º 7
0
        def parse_generator(nodes, ids):
            node = nodes[0]
            tempnode = _ast.For()
            tempnode.target = node.target
            tempnode.iter = _ast.Name(id=ids[0], ctx=_ast.Load())

            if len(nodes) == 1:
                yield_node = _ast.Expr(value=_ast.Yield(value=elt))
                body = [yield_node]
            else:
                body = [parse_generator(nodes[1:], ids[1:])]

            if len(node.ifs) == 1:
                ifnode = _ast.If(test=node.ifs[0], body=body, orelse=[])
                tempnode.body = [ifnode]

            elif len(node.ifs) > 1:
                ifnode = _ast.If(test=_ast.BoolOp(op=_ast.And(),
                                                  values=node.ifs),
                                 body=body,
                                 orelse=[])
                tempnode.body = [ifnode]

            else:
                tempnode.body = body

            tempnode.orelse = None
            return tempnode
Ejemplo n.º 8
0
    def visit_Expr(self, expression_node):
        value = expression_node.value
        if not isinstance(value, _ast.Compare):
            if isinstance(value, _ast.BinOp):
                if hasattr(value,
                           'op') and not isinstance(value.op, _ast.MatMult):
                    return expression_node
            else:
                return expression_node

        left_value = value.left

        # TODO: Support multiple comparators (1 < 2 < 3). This may be tricky because one expression of multiple
        if hasattr(value, 'op'):
            comparison_operation = value.op
            right_value = value.right
        else:
            comparison_operation = value.ops[0]
            right_value = value.comparators[0]
        comparison_operation_type = type(comparison_operation)
        internal_comparison_type = self.comparator_methods[
            comparison_operation_type]

        expression_node.value = _ast.Call(
            func=_ast.Attribute(value=_ast.Name(id='self', ctx=_ast.Load()),
                                attr='_compare',
                                ctx=_ast.Load()),
            args=[
                left_value, right_value,
                _ast.Str(s=internal_comparison_type.name)
            ],
            keywords=[])
        return expression_node
Ejemplo n.º 9
0
  def make_expr(i, bytecode, context=None):
    if context is None:
      context = _ast.Load()

    op = bytecode[i][2]
    if op == LOAD_CONST:
      return Statement.make_const(i, bytecode)
    elif op in (LOAD_GLOBAL, LOAD_NAME, LOAD_FAST,        \
                STORE_GLOBAL, STORE_NAME, STORE_FAST,     \
                DELETE_GLOBAL, DELETE_NAME, DELETE_FAST):
      return Statement.make_name(i, bytecode, context=context)
    elif op in (LOAD_ATTR, STORE_ATTR, DELETE_ATTR):
      return Statement.make_attribute(i, bytecode, context=context)
    elif op in CALL_OPCODES:
      return Statement.make_call(i, bytecode)
    elif op in BINARY_OP_OPCODES:
      return Statement.make_binary_op(i, bytecode)
    elif op in (BUILD_TUPLE, BUILD_LIST):
      return Statement.make_tuple_list(i, bytecode)
    elif op in (STORE_MAP, BUILD_MAP):
      return Statement.make_dict(i, bytecode)
    elif op in (STORE_SUBSCR, BINARY_SUBSCR):
      return Statement.make_subscript(i, bytecode)
    elif op in STORE_SLICE_OPCODES or op in DELETE_SLICE_OPCODES:
      return Statement.make_store_delete_slice(i, bytecode)
    elif op == BUILD_SLICE:
      return Statement.make_slice(i, bytecode)

    logger.debug("Unhandled >> EXPR:\n%s", show_bytecode(bytecode[:i + 1]))

    # if we don't translate it, we generate a new named expr.
    Statement.UNDEFINED_COUNT += 1
    return i, _ast.Name('Undef_%d' % Statement.UNDEFINED_COUNT, _ast.Load())
Ejemplo n.º 10
0
    def visit_AugAssign(self, node):
        """Rewrite the AugAssign visitor function to deal with list
        comprehensions and function calls.

        """

        # do Call/ListComp extraction on the node's value

        # if the value is a list comprehension, the we need to handle
        # it specially
        if isinstance(node.value,
                      (_ast.ListComp, _ast.GeneratorExp, _ast.Lambda)):
            node.value = self.extract(node.value, cthreshold=1)
            iden = self.visit_ListComp(node.value)
            val = _ast.Name(id=iden, ctx=_ast.Load())
            node = ast.Assign(value=val, targets=node.targets)

        elif isinstance(node.value, _ast.Dict) or \
                 isinstance(node.value, _ast.List) or \
                 isinstance(node.value, _ast.Tuple):
            node.value = self.extract(node.value, threshold=0)

        else:
            node.value = self.extract(node.value, cthreshold=0)

        super(PyStochCompiler, self).visit_AugAssign(node)
Ejemplo n.º 11
0
 def _replace_with_block_context(with_node, block_type):
     with_node.items[0].context_expr = _ast.Call(
         func=_ast.Attribute(value=_ast.Name(id='self', ctx=_ast.Load()),
                             attr='_feature_block_context',
                             ctx=_ast.Load()),
         args=[_ast.Str(s=block_type)],
         keywords=[])
Ejemplo n.º 12
0
    def visit_Expr(self, expression_node):
        value = expression_node.value
        if not isinstance(value, _ast.BinOp) or not isinstance(
                value.op, _ast.Mult) or not isinstance(value.right, _ast.Call):
            return expression_node

        number_of_invocations = value.left
        if MockAssertionTransformer._value_is_a_wildcard(
                number_of_invocations):
            number_of_invocations = _ast.Num(n=-1)
        target_mock = value.right.func.value
        target_method = _ast.Str(s=value.right.func.attr)

        list_of_arguments = [
            MockAssertionTransformer._transform_arg_if_wildcard(x)
            for x in value.right.args
        ]
        spread_list_of_arguments = _ast.Starred(value=_ast.List(
            elts=list_of_arguments, ctx=_ast.Load()),
                                                ctx=_ast.Load())
        expression_node.value = _ast.Call(
            func=_ast.Attribute(value=_ast.Name(id='self', ctx=_ast.Load()),
                                attr='_assert_mock',
                                ctx=_ast.Load()),
            args=[
                number_of_invocations, target_mock, target_method,
                spread_list_of_arguments
            ],
            keywords=[])
        return expression_node
    def ROT_TWO(self, instr):

        one = self.ast_stack.pop()
        two = self.ast_stack.pop()

        if self.ilst[0].opname == 'STORE_NAME':

            kw = dict(lineno=instr.lineno, col_offset=0)
            stores = []
            while self.ilst[0].opname == 'STORE_NAME':
                stores.append(self.ilst.pop(0))

            assert len(stores) <= 3, stores
            elts_load = [one, two]
            if len(stores) == 3:
                elts_load.insert(0, self.ast_stack.pop())

            tup_load = _ast.Tuple(elts=elts_load[::-1], ctx=_ast.Load(), **kw)

            elts_store = [
                _ast.Name(id=store.arg, ctx=_ast.Store(), **kw)
                for store in stores
            ]
            tup_store = _ast.Tuple(elts=elts_store, ctx=_ast.Store(), **kw)

            assgn = _ast.Assign(value=tup_load, targets=[tup_store], **kw)
            self.ast_stack.append(assgn)
#            self.ast_stack.append(tup_store)
        else:
            self.ast_stack.append(one)
            self.ast_stack.append(two)
Ejemplo n.º 14
0
def VarReference(*parts, **kwargs):
    """By this we mean either a single name string or one or more Attr nodes.

  This is used whenever we have things like 'a' or 'a.b' or 'a.b.c'.

  Args:
    *parts: The parts that should be dot-separated.
    **kwargs: Only recognized kwarg is 'ctx_type', which controls the
      ctx type of the list. See CtxEnum.

  Raises:
    ValueError: When no parts are specified.

  Returns:
    An _ast.Name node or _ast.Attribute node
  """
    ctx_type = kwargs.pop('ctx_type', CtxEnum.LOAD)

    if not parts:
        raise ValueError('Must have at least one part specified')
    if len(parts) == 1:
        if isinstance(parts[0], str):
            return _ast.Name(id=parts[0], ctx=GetCtx(ctx_type))
        return parts[0]
    return _ast.Attribute(value=VarReference(*parts[:-1], **kwargs),
                          attr=parts[-1],
                          ctx=GetCtx(ctx_type))
Ejemplo n.º 15
0
 def test_bool(self):
     ast = self.ast
     pr = ast.Print(None, [ast.Name("hi", ast.Load())], False)
     assert not pr.nl
     assert isinstance(pr.nl, bool)
     pr.nl = True
     assert pr.nl
Ejemplo n.º 16
0
class SetEncapsulatedAttribsLineNumbersFixture:
    definition_is_none = None
    definition_is_pass = _ast.Pass()
    is_constructor = _ast.FunctionDef(name='__init__')
    without_assign = _ast.FunctionDef(name='test', body=[_ast.Pass()])
    with_assign_without_self = _ast.FunctionDef(
        name='test', body=[
            _ast.Pass(),
            _ast.Assign(targets=[_ast.Attribute(value=(_ast.Name(id='test')), lineno=1)]),
        ],
    )
    with_assign_with_self = _ast.FunctionDef(
        name='test', body=[
            _ast.Pass(),
            _ast.Assign(targets=[_ast.Attribute(value=(_ast.Name(id='self')), lineno=1)]),
        ],
    )
    def DELETE_FAST(self, instr):

        name = _ast.Name(id=instr.arg,
                         ctx=_ast.Del(),
                         lineno=instr.lineno,
                         col_offset=0)

        delete = _ast.Delete(targets=[name], lineno=instr.lineno, col_offset=0)
        self.ast_stack.append(delete)
Ejemplo n.º 18
0
    def DELETE_FAST(self, instr):

        name = _ast.Name(id=instr.arg,
                         ctx=_ast.Del(),
                         lineno=instr.lineno,
                         col_offset=0)

        delete = _ast.Delete(targets=[name], lineno=instr.lineno, col_offset=0)
        self.push_ast_item(delete)
Ejemplo n.º 19
0
 def test_list_syncing(self):
     ast = self.ast
     mod = ast.Module([ast.Lt()])
     raises(TypeError, compile, mod, "<string>", "exec")
     mod = self.get_ast("x = y = 3")
     assign = mod.body[0]
     assert len(assign.targets) == 2
     assign.targets[1] = ast.Name("lemon", ast.Store(),
                                  lineno=0, col_offset=0)
     name = ast.Name("apple", ast.Store(),
                     lineno=0, col_offset=0)
     mod.body.append(ast.Assign([name], ast.Num(4, lineno=0, col_offset=0),
                                lineno=0, col_offset=0))
     co = compile(mod, "<test>", "exec")
     ns = {}
     exec co in ns
     assert "y" not in ns
     assert ns["x"] == ns["lemon"] == 3
     assert ns["apple"] == 4
Ejemplo n.º 20
0
 def test_issue793(self):
     import _ast as ast
     body = ast.Module([
         ast.TryExcept([ast.Pass(lineno=2, col_offset=4)],
             [ast.ExceptHandler(ast.Name('Exception', ast.Load(),
                                         lineno=3, col_offset=0),
                                None, [], lineno=4, col_offset=0)],
             [], lineno=1, col_offset=0)
     ])
     exec compile(body, '<string>', 'exec')
Ejemplo n.º 21
0
def test_return_value_is_class_constant():
    # when method return SomeClass.CONSTANT
    assert ReturnedExpression(
        _ast.Return(
            value=_ast.Attribute(value=_ast.Name(id='SomeClass',
                                                 ctx=_ast.Load()),
                                 attr='CONSTANT',
                                 ctx=_ast.Load()),
            lineno=1,
        ), ).value_not_none() is True
Ejemplo n.º 22
0
def build_for():
    config = menu("target", "iterable")
    target = _ast.Name(id=config["target"])
    iterable = ast.parse(config["iterable"]).body[0].value
    add_ast_node(_ast.For(
        target=target,
        iter=iterable,
        body=[],
        orelse=[],
    ))
Ejemplo n.º 23
0
def test_return_value_is_empty_frozenset_by_keyword():
    # when method return frozenset()
    assert ReturnedExpression(
        _ast.Return(
            value=_ast.Call(
                func=_ast.Name(id='frozenset', ctx=_ast.Load()),
                args=[],
                keywords=[],
            ),
            lineno=1,
        ), ).value_not_none() is False
Ejemplo n.º 24
0
def make_function(code, defaults=None, lineno=0):
        from meta.decompiler.disassemble import disassemble

        instructions = Instructions(disassemble(code))

        stmnts = instructions.stmnt()

        if code.co_flags & 2:
            vararg = None
            kwarg = None

        varnames = list(code.co_varnames[:code.co_argcount])
        co_locals = list(code.co_varnames[code.co_argcount:])

        #have var args
        if code.co_flags & 4:
            vararg = co_locals.pop(0)

        #have kw args
        if code.co_flags & 8:
            kwarg = co_locals.pop()

        args = [_ast.Name(id=argname, ctx=_ast.Param(), lineno=lineno, col_offset=0) for argname in varnames]
            
        args = _ast.arguments(args=args,
                              defaults=defaults if defaults else [],
                              kwarg=kwarg,
                              vararg=vararg,
                              lineno=lineno, col_offset=0
                              )
        if code.co_name == '<lambda>':
            if len(stmnts) == 2:
                if isinstance(stmnts[0], _ast.If) and isinstance(stmnts[1], _ast.Return):
                    assert len(stmnts[0].body) == 1
                    assert isinstance(stmnts[0].body[0], _ast.Return)
                    stmnts = [_ast.Return(_ast.IfExp(stmnts[0].test, stmnts[0].body[0].value, stmnts[1].value))]
                    
            assert len(stmnts) == 1, stmnts
            assert isinstance(stmnts[0], _ast.Return)

            stmnt = stmnts[0].value
            ast_obj = _ast.Lambda(args=args, body=stmnt, lineno=lineno, col_offset=0)
        else:

            if instructions.seen_yield:
                return_ = stmnts[-1]

                assert isinstance(return_, _ast.Return)
                assert isinstance(return_.value, _ast.Name)
                assert return_.value.id == 'None'
                return_.value = None
            ast_obj = _ast.FunctionDef(name=code.co_name, args=args, body=stmnts, decorator_list=[], lineno=lineno, col_offset=0)

        return ast_obj
Ejemplo n.º 25
0
    def visit_ClassDef(self, class_node):
        if class_node.name.endswith('Spec'):
            class_node.bases.append(
                _ast.Attribute(value=_ast.Name(id='unittest', ctx=_ast.Load()),
                               attr='TestCase',
                               ctx=_ast.Load()))

            TestMethodTransformer(class_node.name).visit(class_node)
            ComparatorTransformer().visit(class_node)

        return class_node
Ejemplo n.º 26
0
 def test_bug_null_in_objspace_type(self):
     import ast
     code = ast.Expression(
         lineno=1,
         col_offset=1,
         body=ast.ListComp(
             lineno=1,
             col_offset=1,
             elt=ast.Call(lineno=1,
                          col_offset=1,
                          func=ast.Name(lineno=1,
                                        col_offset=1,
                                        id='str',
                                        ctx=ast.Load(lineno=1,
                                                     col_offset=1)),
                          args=[
                              ast.Name(lineno=1,
                                       col_offset=1,
                                       id='x',
                                       ctx=ast.Load(lineno=1, col_offset=1))
                          ],
                          keywords=[]),
             generators=[
                 ast.comprehension(
                     lineno=1,
                     col_offset=1,
                     target=ast.Name(lineno=1,
                                     col_offset=1,
                                     id='x',
                                     ctx=ast.Store(lineno=1, col_offset=1)),
                     iter=ast.List(
                         lineno=1,
                         col_offset=1,
                         elts=[ast.Num(lineno=1, col_offset=1, n=23)],
                         ctx=ast.Load(
                             lineno=1,
                             col_offset=1,
                         )),
                     ifs=[])
             ]))
     compile(code, '<template>', 'eval')
Ejemplo n.º 27
0
def Name(name_id, ctx_type=CtxEnum.LOAD):
    """Creates an _ast.Name node.

  Args:
    name_id: Name of the node.
    ctx_type: See CtxEnum for options.

  Returns:
    An _ast.Name node.
  """
    ctx = GetCtx(ctx_type)
    return _ast.Name(id=name_id, ctx=ctx)
Ejemplo n.º 28
0
 def visit_Call(self, expression_node):
     if isinstance(expression_node, _ast.Call):
         if hasattr(expression_node.func,
                    'id') and expression_node.func.id == 'thrown':
             expected_exception = expression_node.args[0]
             expression_node = _ast.Call(func=_ast.Attribute(
                 value=_ast.Name(id='self', ctx=_ast.Load()),
                 attr='_exception_thrown',
                 ctx=_ast.Load()),
                                         args=[expected_exception],
                                         keywords=[])
     return expression_node
Ejemplo n.º 29
0
    def visit_Assign(self, node):
        """Rewrite the Assign visitor function to deal with list
        comprehensions and function calls.

        """

        # if the value is a list comprehension, the we need to handle
        # it specially
        if isinstance(node.value,
                      (_ast.ListComp, _ast.GeneratorExp, _ast.Lambda)):
            node.value = self.extract(node.value, cthreshold=1)
            iden = self.visit(node.value)

            if isinstance(node.value, _ast.GeneratorExp):
                val = _ast.Call(func=_ast.Name(id=iden, ctx=_ast.Load()),
                                args=[],
                                keywords=[],
                                starargs=None,
                                kwargs=None)
            else:
                val = _ast.Name(id=iden, ctx=_ast.Load())

            node = ast.Assign(value=val, targets=node.targets)

        elif isinstance(node.value, _ast.Dict) or \
                 isinstance(node.value, _ast.List) or \
                 isinstance(node.value, _ast.Tuple):
            node.value = self.extract(node.value, threshold=0)

        else:
            # do Call/ListComp extraction on the node's value
            node.value = self.extract(node.value, cthreshold=0)

        self.newline(node)
        for idx, target in enumerate(node.targets):
            if idx:
                self.write(' = ')
            self.visit(target)
        self.write(' = ')
        self.visit(node.value)
Ejemplo n.º 30
0
def test_return_value_is_filled_frozenset_by_keyword():
    # when method return frozenset('1')
    try:
        assert ReturnedExpression(
            _ast.Return(
                value=_ast.Call(
                    func=_ast.Name(id='frozenset', ctx=_ast.Load()),
                    args=[_ast.Str(s='1')],
                    keywords=[],
                ),
                lineno=1,
            ), ).value_not_none() is True
    except (AttributeError):
        assert ReturnedExpression(
            _ast.Return(
                value=_ast.Call(
                    func=_ast.Name(id='frozenset', ctx=_ast.Load()),
                    args=[_ast.JoinedStr(values=['1'])],
                    keywords=[],
                ),
                lineno=1,
            ), ).value_not_none() is True