def BUILD_MAP(self, instr):

        nitems = instr.oparg
        keys = []
        values = []
        for i in range(nitems):
            map_instrs = []
            while 1:
                new_instr = self.ilst.pop(0)

                if new_instr.opname == 'STORE_MAP':
                    break

                map_instrs.append(new_instr)

            items = self.decompile_block(map_instrs).stmnt()
            assert len(items) == 2

            values.append(items[0])
            keys.append(items[1])

        list_ = _ast.Dict(keys=keys,
                          values=values,
                          lineno=instr.lineno,
                          col_offset=0)
        self.ast_stack.append(list_)
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'])),
        ],
    )
예제 #3
0
 def make_const(i, bytecode):
   arg = bytecode[i][3]
   if isinstance(arg, basestring):
     return i, _ast.Str(arg)
   elif isinstance(arg, int) or isinstance(arg, float) or isinstance(arg, long):
     return i, _ast.Num(arg)
   elif isinstance(arg, dict):
     return i, _ast.Dict(arg.keys(), arg.values())
   elif isinstance(arg, set):
     return i, _ast.Dict(arg)
   elif isinstance(arg, tuple):
     return i, _ast.Tuple(arg, _ast.Load())
   elif isinstance(arg, list):
     return i, _ast.List(arg, _ast.Load())
   elif isinstance(arg, bytes):
     return i, _ast.Bytes(arg)
   return i, None
예제 #4
0
  def make_dict(i, bytecode):
    """
      This is a trick. We don't try to resolve exactly what the dict
      contains, but just create a dict type with the proper number of args.
    """
    logger.debug("i=%d, bytecode=\n%s", i, show_bytecode(bytecode))
    num_keys = -1
    if bytecode[i][2] == BUILD_MAP:
      return i, _ast.Dict([], [])
    else:
      keys, values = [], []
      j = i - 1
      build_map_idx = i - 1
      while j >= 0:
        if bytecode[j][2] == BUILD_MAP:
          num_keys = bytecode[j][3]
          build_map_idx = j
        j -= 1
      return build_map_idx, _ast.Dict(range(num_keys), [None] * num_keys)

    return build_map_idx, None
예제 #5
0
def Dict(keys=(), values=()):
    """Creates an _ast.Dict node. This represents a dict literal.

  Args:
    keys: A list of keys as nodes. Must be the same length as values.
    values: A list of values as nodes. Must be the same length as values.

  Raises:
    ValueError: If len(keys) != len(values).

  Returns:
    An _ast.Dict node.
  """
    if len(keys) != len(values):
        raise ValueError('len(keys)={} != len(values)={}'.format(
            len(keys), len(values)))
    return _ast.Dict(list(keys), list(values))
예제 #6
0
def dict_expr(keys: List[_ast.expr], values: List[_ast.expr]) -> _ast.Dict:
    return _ast.Dict(keys=keys, values=values)
예제 #7
0
def test_return_value_is_filled_dict():
    # when method return {'1': '2'}
    assert ReturnedExpression(
        _ast.Return(value=_ast.Dict(keys=['1'], values=['2']),
                    lineno=1), ).value_not_none() is True
예제 #8
0
def test_return_value_is_empty_dict():
    # when method return {}
    assert ReturnedExpression(
        _ast.Return(value=_ast.Dict(keys=[], values=[]),
                    lineno=1), ).value_not_none() is False