Beispiel #1
0
def load_method(state, arg):
    """Like load_attr. Replaces TOS object with method and TOS."""
    o = state.stack.pop()
    name = state.get_name(arg)
    if isinstance(o, Const):
        method = Const(getattr(o.value, name))
    elif isinstance(o, typehints.AnyTypeConstraint):
        method = typehints.Any
    else:
        method = Const(BoundMethod(getattr(o, name), o))

    state.stack.append(method)
Beispiel #2
0
def _getattr(o, name):
    if isinstance(o, Const) and hasattr(o.value, name):
        return Const(getattr(o.value, name))
    elif (inspect.isclass(o)
          and isinstance(getattr(o, name, None),
                         (types.MethodType, types.FunctionType))):
        # TODO(luke-zhu): Support other callable objects
        func = getattr(o, name)  # Python 3 has no unbound methods
        return Const(BoundMethod(func, o))
    elif isinstance(o, row_type.RowTypeConstraint):
        return o.get_type_for(name)
    else:
        return Any
Beispiel #3
0
def load_attr(state, arg):
    """Replaces the top of the stack, TOS, with
  getattr(TOS, co_names[arg])
  """
    o = state.stack.pop()
    name = state.get_name(arg)
    if isinstance(o, Const) and hasattr(o.value, name):
        state.stack.append(Const(getattr(o.value, name)))
    elif (inspect.isclass(o)
          and isinstance(getattr(o, name, None),
                         (types.MethodType, types.FunctionType))):
        # TODO(luke-zhu): Support other callable objects
        if sys.version_info[0] == 2:
            func = getattr(o, name).__func__
        else:
            func = getattr(o, name)  # Python 3 has no unbound methods
        state.stack.append(Const(BoundMethod(func, o)))
    else:
        state.stack.append(Any)
Beispiel #4
0
def load_method(state, arg):
    """Like load_attr. Replaces TOS object with method and TOS."""
    o = state.stack.pop()
    name = state.get_name(arg)
    if isinstance(o, Const):
        method = Const(getattr(o.value, name))
    elif isinstance(o, typehints.AnyTypeConstraint):
        method = typehints.Any
    elif hasattr(o, name):
        attr = getattr(o, name)
        if isinstance(attr, _MethodDescriptorType):
            # Skip builtins since they don't disassemble.
            method = typehints.Any
        else:
            method = Const(BoundMethod(attr, o))
    else:
        method = typehints.Any

    state.stack.append(method)