Exemple #1
0
 def separate_assing_nodes(self, node: _ast.Assign, variables_names,
                           variables):
     if isinstance(node.targets[0], _ast.Tuple):
         tuple_key = meta.values_for_ast_type[type(node.targets[0])]
         for num, target_node in enumerate(
                 getattr(node.targets[0], tuple_key)):
             if isinstance(node.value, _ast.Tuple):
                 inner_value = getattr(node.value, tuple_key)[num]
             else:
                 node_value = self.get_value(node.value, variables_names,
                                             variables)
                 if isinstance(node_value, dict):
                     inner_value = _ast.Subscript(
                         value=node.value,
                         slice=_ast.Index(value=_ast.Num(n=num)))
                 else:
                     inner_value = node_value
             var = _ast.Assign(targets=[target_node], value=inner_value)
             yield var
     elif isinstance(node.value, _ast.List):
         for target in node.targets:
             for elem in node.value.elts:
                 # for items
                 var = _ast.Assign(targets=[target], value=elem)
                 yield var
     elif isinstance(node.value, _ast.Call):
         value = self.get_value(node.value, variables_names, variables)
         for target in node.targets:
             # for items in call result
             var = _ast.Assign(targets=[target], value=value)
             yield var
Exemple #2
0
  def make_subscript(i, bytecode, context=None):
    op = bytecode[i][2]
    if op == STORE_SUBSCR:
      # TOS1[TOS] = TOS2
      i, index_expr = Statement.make_expr(i - 1, bytecode)
      i, arr_expr = Statement.make_expr(i - 1, bytecode, context=_ast.Store())
      i, rhs_expr = Statement.make_expr(i - 1, bytecode)
      lhs_expr = _ast.Subscript(arr_expr, index_expr, _ast.Store())
      return i, _ast.Assign([lhs_expr], rhs_expr)
    else:
      if context is None:
        context = _ast.Load()

      # BINARY_SUBSCR: TOS1[TOS] and DELETE_SUBSCR TOS1[TOS]
      i, index_expr = Statement.make_expr(i - 1, bytecode)
      i, arr_expr = Statement.make_expr(i - 1, bytecode)
      return i, _ast.Subscript(arr_expr, index_expr, context)
    def SLICE_0(self, instr):
        'obj[:]'
        value = self.ast_stack.pop()

        kw = dict(lineno=instr.lineno, col_offset=0)
        slice = _ast.Slice(lower=None, step=None, upper=None, **kw)
        subscr = _ast.Subscript(value=value, slice=slice, ctx=_ast.Load(), **kw)

        self.ast_stack.append(subscr)
    def DELETE_SLICE_0(self, instr):
        'obj[:] = expr'
        value = self.ast_stack.pop()

        kw = dict(lineno=instr.lineno, col_offset=0)
        slice = _ast.Slice(lower=None, step=None, upper=None, **kw)
        subscr = _ast.Subscript(value=value, slice=slice, ctx=_ast.Del(), **kw)

        delete = _ast.Delete(targets=[subscr], **kw)
        self.ast_stack.append(delete)
    def STORE_SLICE_0(self, instr):
        'obj[:] = expr'
        value = self.ast_stack.pop()
        expr = self.ast_stack.pop()

        kw = dict(lineno=instr.lineno, col_offset=0)
        slice = _ast.Slice(lower=None, step=None, upper=None, **kw)
        subscr = _ast.Subscript(value=value, slice=slice, ctx=_ast.Store(), **kw)

        assign = _ast.Assign(targets=[subscr], value=expr, **kw)
        self.ast_stack.append(assign)
    def DELETE_SLICE_1(self, instr):
        'obj[lower:] = expr'
        lower = self.pop_ast_item()
        value = self.pop_ast_item()

        kw = dict(lineno=instr.lineno, col_offset=0)
        slice = _ast.Slice(lower=lower, step=None, upper=None, **kw)
        subscr = _ast.Subscript(value=value, slice=slice, ctx=_ast.Del(), **kw)

        delete = _ast.Delete(targets=[subscr], **kw)
        self.push_ast_item(delete)
    def BINARY_SUBSCR(self, instr):

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

        kw = dict(lineno=instr.lineno, col_offset=0)

        index = self.format_slice(index, kw)

        subscr = _ast.Subscript(value=value, slice=index, ctx=_ast.Load(), **kw)

        self.ast_stack.append(subscr)
    def DELETE_SUBSCR(self, instr):
        index = self.ast_stack.pop()
        value = self.ast_stack.pop()

        kw = dict(lineno=instr.lineno, col_offset=0)

        index = self.format_slice(index, kw)

        subscr = _ast.Subscript(value=value, slice=index, ctx=_ast.Del(), **kw)

        delete = _ast.Delete(targets=[subscr], **kw)
        self.ast_stack.append(delete)
    def SLICE_2(self, instr):
        'obj[:stop]'
        upper = self.pop_ast_item()
        value = self.pop_ast_item()

        kw = dict(lineno=instr.lineno, col_offset=0)
        slice = _ast.Slice(lower=None, step=None, upper=upper, **kw)
        subscr = _ast.Subscript(value=value,
                                slice=slice,
                                ctx=_ast.Load(),
                                **kw)

        self.push_ast_item(subscr)
Exemple #10
0
  def make_store_delete_slice(i, bytecode, context=None):
    op = bytecode[i][2]
    is_delete = op in DELETE_SLICE_OPCODES

    if context is None:
      context = _ast.Store() if not is_delete else _ast.Del()

    lhs_expr = None

    if op in (STORE_SLICE_0, DELETE_SLICE_0):
      i, lhs_expr = Statement.make_expr(i - 1, bytecode, context=context)
      lhs_expr = _ast.Subscript(lhs_expr,
                                _ast.Slice(None, None, None),
                                _ast.Store())
    elif op in (STORE_SLICE_1, STORE_SLICE_2, DELETE_SLICE_1, DELETE_SLICE_2):
      i, index_expr = Statement.make_expr(i - 1, bytecode)
      i, arr_expr = Statement.make_expr(i - 1, bytecode, context=context)

      args = [None] * 3
      index_index = 0 if op in (STORE_SLICE_1, DELETE_SLICE_1) else 1
      args[index_index] = index_expr
      lhs_expr = _ast.Subscript(arr_expr,
                                _ast.Slice(*args),
                                _ast.Store())
    else:
      i, end_index_expr = Statement.make_expr(i - 1, bytecode)
      i, start_index_expr = Statement.make_expr(i - 1, bytecode)
      i, arr_expr = Statement.make_expr(i - 1, bytecode, context=context)

      lhs_expr = _ast.Subscript(arr_expr,
                                _ast.Slice(start_index_expr, end_index_expr, None),
                                _ast.Store())

    if is_delete:
      return i, _ast.Delete([lhs_expr])
    else:
      i, rhs_expr = Statement.make_expr(i - 1, bytecode)
      return i, _ast.Assign([lhs_expr], rhs_expr)
    def STORE_SLICE_2(self, instr):
        'obj[:upper] = expr'
        upper = self.pop_ast_item()
        value = self.pop_ast_item()
        expr = self.pop_ast_item()

        kw = dict(lineno=instr.lineno, col_offset=0)
        slice = _ast.Slice(lower=None, step=None, upper=upper, **kw)
        subscr = _ast.Subscript(value=value,
                                slice=slice,
                                ctx=_ast.Store(),
                                **kw)

        assign = _ast.Assign(targets=[subscr], value=expr, **kw)
        self.push_ast_item(assign)
Exemple #12
0
 def _assign_list_form_variables(self, where_function_ast_node):
     copy_of_body = copy.deepcopy(where_function_ast_node.body)
     where_function_ast_node.body = []
     for assignment_expression in copy_of_body:
         variable_name = assignment_expression.targets[0].id
         self.spec_metadata.add_feature_variable(self.feature_name,
                                                 variable_name)
         variable_values = assignment_expression.value
         where_function_ast_node.body.append(
             _ast.Assign(targets=[
                 _ast.Subscript(
                     value=_ast.Name(id='injectable_values',
                                     ctx=_ast.Load()),
                     slice=_ast.Index(value=_ast.Str(s=variable_name)),
                     ctx=_ast.Store())
             ],
                         value=variable_values))
    def STORE_SUBSCR(self, instr):
        index = self.ast_stack.pop()
        value = self.ast_stack.pop()
        expr = self.ast_stack.pop()

        expr = self.process_ifexpr(expr)

        if isinstance(expr, _ast.AugAssign):
            self.ast_stack.append(expr)
        else:
            kw = dict(lineno=instr.lineno, col_offset=0)

            index = self.format_slice(index, kw)

            subscr = _ast.Subscript(value=value, slice=index, ctx=_ast.Store(), **kw)

            assign = _ast.Assign(targets=[subscr], value=expr, **kw)
            self.ast_stack.append(assign)
Exemple #14
0
    def _assign_matrix_form_variables(self, where_function_ast_node):
        copy_of_body = copy.deepcopy(where_function_ast_node.body)
        where_function_ast_node.body = []

        variables_and_values = WhereBlockFunctions._get_variables_and_values(copy_of_body)

        # We might be screwing with line numbers here
        for variable_name, variable_values in variables_and_values.items():
            self.spec_metadata.add_feature_variable(self.feature_name, variable_name)
            where_function_ast_node.body.append(
                _ast.Assign(
                    targets=[
                        _ast.Subscript(
                            value=_ast.Name(id='injectable_values', ctx=_ast.Load()),
                            slice=_ast.Index(value=ast_proxy.ast_str(s=variable_name)),
                            ctx=_ast.Store()
                        )
                    ],
                    value=_ast.List(elts=variable_values, ctx=_ast.Load())
                ))
    def STORE_SLICE_3(self, instr):
        'obj[lower:upper] = expr'

        upper = self.ast_stack.pop()
        lower = self.ast_stack.pop()
        value = self.ast_stack.pop()
        expr = self.ast_stack.pop()

        kw = dict(lineno=instr.lineno, col_offset=0)
        slice = _ast.Slice(lower=lower, step=None, upper=upper, **kw)
        subscr = _ast.Subscript(value=value, slice=slice, ctx=_ast.Store(), **kw)

        if isinstance(expr, _ast.AugAssign):
            assign = expr
            result = cmp_ast(expr.target, subscr)

            assert result
        else:
            assign = _ast.Assign(targets=[subscr], value=expr, **kw)

        self.ast_stack.append(assign)
Exemple #16
0
def subscript_expr(value: _ast.expr, slice: _ast.slice,
                   ctx: _ast.expr_context) -> _ast.Subscript:
    return _ast.Subscript(value=value, slice=slice, ctx=ctx)
def Subscript(value, upper=None, lower=None, step=None, ctx=CtxEnum.STORE):
    value = _WrapWithName(value)
    return _ast.Subscript(value=value,
                          slice=Slice(upper, lower, step),
                          ctx=GetCtx(ctx))