class ConstructorMutableAttribsLineNumberFixture:
    definition_is_none = None
    definition_is_not_a_function = _ast.Pass
    constructor_with_empty_body = _ast.FunctionDef(name='__init__', body=[])
    try:
        constructor_with_immutable = _ast.FunctionDef(
            name='__init__',
            body=[
                _ast.Assign(lineno=1, value=_ast.Tuple(elts=[1, 2, 3])),
                _ast.Assign(lineno=2, value=_ast.Str(s='a')),
            ],
        )
    except (AttributeError):
        constructor_with_immutable = _ast.FunctionDef(
            name='__init__',
            body=[
                _ast.Assign(lineno=1, value=_ast.Tuple(elts=[1, 2, 3])),
                _ast.Assign(lineno=3, value=_ast.JoinedStr(values=None)),
            ],
        )
    constructor_with_mutable = _ast.FunctionDef(
        name='__init__',
        body=[
            _ast.Assign(lineno=1, value=_ast.List(elts=[1, 2, 3])),
            _ast.Assign(lineno=2, value=_ast.Set(elts=[1, 2, 3])),
            _ast.Assign(lineno=3, value=_ast.Dict(keys=['a'])),
        ],
    )
예제 #2
0
class SetterOrGetterDefNamesLineNumbersFixture:
    definition_is_none = None
    definition_is_pass = _ast.Pass()
    name_is_none = _ast.FunctionDef(name=None)
    without_setget_keywords = _ast.FunctionDef(name='test')
    with_set_keywords = _ast.FunctionDef(name='set_test', lineno=1)
    with_get_keywords = _ast.FunctionDef(name='get_test', lineno=2)
예제 #3
0
class ReflectionAtLineFixture:
    empty_node = _ast.Pass
    is_instance_at_first_lvl = _ast.FunctionDef(id='isinstance', lineno=1)
    type_at_first_lvl = _ast.FunctionDef(id='type', lineno=1)
    is_instance_at_second_lvl = _ast.FunctionDef(
        body=[_ast.Expr(id='isinstance', lineno=2)], lineno=1)
    type_at_second_lvl = _ast.FunctionDef(
        body=[_ast.Expr(id='type', lineno=2)], lineno=1)
class NonAssertMethodAtTestFunctionFixture:
    definition_is_none = None
    definitions_is_not_a_function = _ast.Pass()
    definitions_is_not_a_test_function = _ast.FunctionDef(name='not_a_test')
    definitions_with_only_assert = _ast.FunctionDef(name='test',
                                                    body=[_ast.Assert()])
    definitions_with_code = _ast.FunctionDef(
        name='test', body=[_ast.Pass(lineno=1),
                           _ast.Assert()])
예제 #5
0
class ConstructorNonAttribsValueLineNumberFixture:
    definition_is_none = None
    definition__is_not_a_function = _ast.Pass
    definitions_is_not_a_constructor = _ast.FunctionDef(name='test', body=[_ast.Expr(lineno=1)])
    definitions_consist_of_assign_with_attribute = _ast.FunctionDef(
        name='__init__', body=[_ast.Assign(targets=[_ast.Attribute(lineno=2)], lineno=1)],
    )
    definitions_consist_of_assign_without_attribute = _ast.FunctionDef(
        name='__init__', body=[_ast.Assign(targets=[_ast.Expr(lineno=2)], lineno=1)],
    )
    definitions_consist_of_any_but_not_a_assign = _ast.FunctionDef(name='__init__', body=[_ast.Pass(lineno=3)])
예제 #6
0
def FunctionDef(name,
                args=(),
                keys=(),
                values=(),
                body=None,
                vararg_name=None,
                kwarg_name=None,
                decorator_list=()):
    """Creates an _ast.FunctionDef node.

  Args:
    name: The name of the function.
    args: A list of args.
    keys: A list of keys, must be the same length as values.
    values: A list of values, correspond to keys.
    body: A list of _ast.stmt nodes that go in the body of the function.
    vararg_name: The name of the vararg variable, or None.
    kwarg_name: The name of the kwargs variable, or None.
    decorator_list: A list of decorator nodes.
  Raises:
    ValueError: If len(keys) != len(values).

  Returns:
    An _ast.FunctionDef node.
  """
    body = FormatAndValidateBody(body)
    args = arguments(args=args,
                     keys=keys,
                     values=values,
                     vararg_name=vararg_name,
                     kwarg_name=kwarg_name)
    return _ast.FunctionDef(name=name,
                            args=args,
                            body=body,
                            decorator_list=list(decorator_list))
예제 #7
0
파일: test_ast.py 프로젝트: Qointum/pypy
 def test_missing_name(self):
     import _ast as ast
     n = ast.FunctionDef(name=None)
     n.name = "foo"
     n.name = "foo"
     n.name = "foo"
     assert n.name == "foo"
예제 #8
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)]),
        ],
    )
예제 #9
0
def function_def_stmt(name: str, args: _ast.arguments, body: List[_ast.stmt],
                      decorator_list: List[_ast.expr]) -> _ast.FunctionDef:
    return _ast.FunctionDef(name=name,
                            args=args,
                            body=body,
                            decorator_list=decorator_list,
                            returns=None,
                            type_comment=None)
예제 #10
0
class ConstructorFixture:

    constructor_without_params = _ast.ClassDef(
        name='Test',
        body=[
            _ast.FunctionDef(
                name='__init__',
                body=[_ast.Pass()],
            ),
        ],
        bases=None,
    )
    empty_class = _ast.ClassDef(
        name='Test',
        body=[],
        bases=None,
    )
    class_without_constructor = _ast.ClassDef(
        name='Test',
        body=[
            _ast.FunctionDef(
                name='some_function',
                body=[_ast.Pass()],
            ),
        ],
        bases=None,
    )
    class_with_constructor_and_additional_method = _ast.ClassDef(
        name='Test',
        body=[
            _ast.FunctionDef(
                name='some_function',
                body=[_ast.Pass()],
            ),
            _ast.FunctionDef(
                name='__init__',
                body=[_ast.Pass()],
            ),
        ],
        bases=None,
    )
    class_with_inheritance = _ast.ClassDef(
        name='Test',
        body=[],
        bases=[_ast.ClassDef(name='TestParent')],
    )
예제 #11
0
class ReturnedValueFixture:
    node_is_none = None
    assign_expression_as_input = _ast.Assign(lineno=1)
    pass_expression_as_input = _ast.Pass(lineno=1)
    plain_expression_as_input = _ast.Expr(lineno=1)
    function_body_is_empty = _ast.FunctionDef(lineno=1, body=[])
    function_body_without_return_expression = _ast.FunctionDef(
        lineno=1,
        body=[_ast.Pass(), _ast.Expr(), _ast.Assign],
    )
    function_body_with_return_expression = _ast.FunctionDef(
        lineno=1,
        body=[
            _ast.Pass(),
            _ast.Expr(),
            _ast.Assign(),
            _ast.Return(lineno=1, value=None)
        ],
    )
예제 #12
0
 def _replace_where_block_with_function(self, with_node):
     return _ast.FunctionDef(name=self.feature_name + '_where',
                             args=_ast.arguments(
                                 args=[_ast.arg(arg='self'), _ast.arg(arg='injectable_values')],
                                 kwonlyargs=[],
                                 kw_defaults=[],
                                 defaults=[]
                             ),
                             body=copy.deepcopy(with_node.body),
                             decorator_list=[],
                             returns=None)
예제 #13
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
예제 #14
0
class StaticOrPrivateFixture:
    definition_is_assign = _ast.Assign()
    definition_is_pass = _ast.Pass()
    definition_is_expr = _ast.Expr()
    empty_decorator_list_and_name = _ast.FunctionDef()
    empty_decorator_list = _ast.FunctionDef(name='test')
    is_private = _ast.FunctionDef(name='_test')
    is_protected = _ast.FunctionDef(name='__test')
    is_magic = _ast.FunctionDef(name='__test__')
    decorated = _ast.FunctionDef(name='test', decorator_list=['any_decorator'])
    is_static = _ast.FunctionDef(
        name='test',
        decorator_list=[_ast.FunctionDef(id='staticmethod')],
    )
예제 #15
0
 def function(self, items):
     if len(items) == 2:
         (name, body) = items
         args = []
     else:
         (name, args, body) = items
     return _ast.FunctionDef(name=name,
                             args=_ast.arguments(args=args,
                                                 defaults=[],
                                                 vararg=None,
                                                 kwarg=None),
                             body=body,
                             decorator_list=[])
예제 #16
0
    def constructor_method(self, items):
        (args, body) = items

        method_args = [_ast.arg(arg='self', annotation=None)]
        method_args.extend(args)

        return _ast.FunctionDef(name='__init__',
                                args=_ast.arguments(args=method_args,
                                                    defaults=[],
                                                    vararg=None,
                                                    kwarg=None),
                                body=body,
                                decorator_list=[])
예제 #17
0
def build_function():
    config = menu('name')
    name = config['name']
    function = _ast.FunctionDef(name=name,
                                args=[],
                                defaults=[],
                                body=[],
                                decorator_list=[])
    add_ast_node(function)
    parent = globals['node']
    for child in parent.children:
        if (child.node == function):
            globals['node'] = child
            break
예제 #18
0
    def visit_Lambda(self, node):
        """Rewrite the Lambda visitor function to transform the lambda
        into a real iterator function.

        """

        iden = self._gen_iden(node)
        funcnode = _ast.FunctionDef(name=iden,
                                    args=node.args,
                                    body=[_ast.Return(value=node.body)],
                                    decorator_list=[])

        self.visit(funcnode)
        return iden
예제 #19
0
파일: test_ast.py 프로젝트: purepython/pypy
 def test_functiondef(self):
     import _ast as ast
     fAst = ast.FunctionDef(
         name="foo",
         args=ast.arguments(
             args=[], vararg=None, kwarg=None, defaults=[],
             kwonlyargs=[], kw_defaults=[]),
         body=[], decorator_list=[], lineno=5, col_offset=0)
     exprAst = ast.Interactive(body=[fAst])
     compiled = compile(exprAst, "<foo>", "single")
     #
     d = {}
     eval(compiled, d, d)
     assert type(d['foo']) is type(lambda: 42)
     assert d['foo']() is None
예제 #20
0
    def visit_GeneratorExp(self, node):
        """Rewrite the GeneratorExp visitor function to turn the
        generator expression into a iterator function.  This is
        necessary to be able to correctly label any random functions
        that get called from within the generator expression.
        Basically, this function creates a function, and transforms
        the generator into a for loop that yields values from the.
        The function name is then returned, so that the parent node
        can handle the assignment properly.

        """

        # make an identifier for the list
        self.newline(node)
        iden = self._gen_iden(node)

        argids = []
        for gen in node.generators:
            argval = gen.iter
            argid = self._gen_iden(gen.iter)
            self.visit(
                _ast.Assign(targets=[_ast.Name(id=argid, ctx=_ast.Store())],
                            value=argval))
            argids.append(argid)

        elt = node.elt

        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

        node = _ast.FunctionDef(
            name=iden,
            args=_ast.arguments(args=[], vararg=None, kwarg=None, defaults=[]),
            body=[parse_generator(node.generators, argids)],
            decorator_list=[])

        self.visit(node)
        return iden
예제 #21
0
def make_function(code,
                  defaults=None,
                  annotations=(),
                  kw_defaults=(),
                  lineno=0):
    from ..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])
    kwonly_varnames = list(code.co_varnames[code.co_argcount:code.co_argcount +
                                            code.co_kwonlyargcount])
    co_locals = list(code.co_varnames[code.co_argcount +
                                      code.co_kwonlyargcount:])

    assert (len(kw_defaults) % 2) == 0

    kw_defaults = list(kw_defaults)
    kw_default_dict = {}

    while kw_defaults:
        name = kw_defaults.pop(0)
        value = kw_defaults.pop(0)

        kw_default_dict[name.s] = value

    kw_defaults = []
    for argname in kwonly_varnames:
        kw_defaults.append(kw_default_dict.pop(argname))

    #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 = []
    annotation_names = [annotation.arg for annotation in annotations]

    for argname in varnames:
        if argname in annotation_names:
            arg = [
                annotation for annotation in annotations
                if annotation.arg == argname
            ][0]
        else:
            arg = _ast.arg(annotation=None,
                           arg=argname,
                           lineno=lineno,
                           col_offset=0)  #@UndefinedVariable

        args.append(arg)

    kwonlyargs = []

    for argname in kwonly_varnames:
        if argname in annotation_names:
            arg = [
                annotation for annotation in annotations
                if annotation.arg == argname
            ][0]
        else:
            arg = _ast.arg(annotation=None,
                           arg=argname,
                           lineno=lineno,
                           col_offset=0)  #@UndefinedVariable

        kwonlyargs.append(arg)

    if 'return' in annotation_names:
        arg = [
            annotation for annotation in annotations
            if annotation.arg == 'return'
        ][0]
        returns = arg.annotation
    else:
        returns = None

    if vararg in annotation_names:
        arg = [
            annotation for annotation in annotations
            if annotation.arg == vararg
        ][0]
        varargannotation = arg.annotation
    else:
        varargannotation = None

    if kwarg in annotation_names:
        arg = [
            annotation for annotation in annotations if annotation.arg == kwarg
        ][0]
        kwargannotation = arg.annotation
    else:
        kwargannotation = None

    args = _ast.arguments(args=args,
                          defaults=defaults if defaults else [],
                          kwarg=kwarg,
                          vararg=vararg,
                          kw_defaults=kw_defaults,
                          kwonlyargs=kwonlyargs,
                          kwargannotation=kwargannotation,
                          varargannotation=varargannotation,
                          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 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=[],
                                   returns=returns,
                                   lineno=lineno,
                                   col_offset=0)

    return ast_obj
예제 #22
0
 def _replace_where_block_with_function(self, with_node):
     return _ast.FunctionDef(name=self.feature_name + '_where',
                             args=ast_proxy.ast_args([_ast.arg(arg='self'), _ast.arg(arg='injectable_values')]),
                             body=copy.deepcopy(with_node.body),
                             decorator_list=[],
                             returns=None)