def get_instance_type(u): types = [] q = collections.deque(u.types()) while q: t = q.pop() if Union(t) == BUILTINS['int']: types.append(INT) elif Union(t) == BUILTINS['str']: types.append(STR) elif Union(t) == BUILTINS['float']: types.append(FLOAT) elif Union(t) == BUILTINS['bool']: types.append(BOOL) elif isinstance(t, InstanceType) and t.cls is TupleClass: for elt in t.unions: q.extend(elt.types()) elif isinstance(t, InstanceType): pass elif isinstance(t, UserClassType): types.append(t.instance()) elif isinstance(t, ClassType): pass elif isinstance(t, (FixedFunction, SpecialFunction, PolymorphicFunction, InstanceMethod, UserFunction)): pass else: print repr(t) print repr(t.display()) raise Exception(t.display()) # raise Exception(t.display(), ast_utils.format_node(e.args[1])) return Union(*set(types))
def _filter_rtn(args, keywords, starargs, context, dryrun, orig_args): assert not keywords assert not starargs assert len(args) == 2 f = args[0] r = context.get_cached(_filter_rtn) if not r: r = InstanceType(ListClass, [Union.EMPTY]) context.set_cached(_filter_rtn, r) rtn = [] for t in args[1].types(): elt_type = get_iter_type(Union(t), context) if f != Union(NONE): f.call([elt_type], {}, None, context, dryrun, orig_args) if t is STR: rtn.append(Union(STR)) else: r.update(0, elt_type) rtn.append(Union(r)) return Union.make_union(*rtn)
def _map_rtn(args, keywords, starargs, context, dryrun, orig_args): assert not keywords assert not starargs f = args[0] elts = [] for a in args[1:]: elts.append(get_iter_type(a, context)) r = context.get_cached(_map_rtn) if not r: r = InstanceType(ListClass, [Union.EMPTY]) context.set_cached(_map_rtn, r) u = f.call(elts, {}, None, context, dryrun, orig_args) r.update(0, u) return Union(r)
def _reduce_rtn(args, keywords, starargs, context, dryrun, orig_args): assert not keywords assert not starargs assert len(args) == 3, args f, l, initial = args elt_type = get_iter_type(l, context) cur = initial MAX_TRIES = 10 for i in xrange(MAX_TRIES): next = f.call([cur, elt_type], {}, None, context, dryrun, orig_args) if next == cur: return next cur = next else: return Union(TOP)
lt = elt_u.getattr("__lt__", context) lt.call([elt_u], {}, None, context, False, None) ListClass.setattr("sort", Union(FixedFunction([InstanceArg(ListClass, [Var(0)])], NONE, implicit_behavior_funcs=[sort_effects]))) ListClass.setattr("reverse", Union(FixedFunction([InstanceArg(ListClass, [Var(0)])], NONE))) ListClass.setattr("index", Union(FixedFunction([InstanceArg(ListClass, [Var(0)]), Subtype(Var(0)), INT, INT], INT, ndefaults=2))) ListClass.setattr("__mul__", Union(FixedFunction([InstanceArg(ListClass, [Var(0)]), INT], InstanceArg(ListClass, [Var(0)])))) def _dict_kw(args, keywords, starargs, context, dryrun, orig_args): try: args, keywords, vararg = translate_call(args, keywords, starargs, ["self"], 0, None, context) except _FuncArgsError, e: context.log_error(e.message) return Union.EMPTY u = Union.make_union(*keywords.values()) for t in args[0].types(): if not isinstance(t, InstanceType) or t.cls != DictClass: print "WTF" continue if t.unions is None: t.unions = [Union.EMPTY, Union.EMPTY] t.update(0, Union(STR)) t.update(1, u) return Union(NONE) DictClass.setattr("__init__", Union(PolymorphicFunction( FixedFunction([InstanceArg(DictClass, [BOT, BOT], update=True)], NONE),
UserClassType, translate_call, _FuncArgsError, get_iter_type, FakeContext, Subtype, InstanceArg, Var, FixedArg, ANY, BOT, ) ObjectClass.setattr( "__init__", Union(FixedFunction([InstanceArg(ObjectClass, [], update=True)], NONE))) ObjectClass.setattr("__nonzero__", Union(FixedFunction([ANY], BOOL))) ObjectClass.setattr("__eq__", Union(FixedFunction([Subtype(Var(8)), ANY], BOOL))) ObjectClass.setattr("__ne__", Union(FixedFunction([Subtype(Var(8)), ANY], BOOL))) TypeClass.setattr( "__init__", Union(FixedFunction([InstanceArg(TypeClass, [], update=True), ANY], NONE))) TypeClass.setattr("__name__", Union(STR)) Iterator.setattr( "next", Union(FixedFunction([InstanceArg(Iterator, [Var(0)])], Var(0)))) Iterator.setattr("__iter__", Union(FixedFunction([Var(0)], Var(0))))