def call(m, args): args_s, kwds_s = args.unpack() if kwds_s: raise Exception("keyword arguments to call to a low-level bound method") inst = m.ootype._example() _, meth = ootype.typeOf(inst)._lookup(m.name) METH = ootype.typeOf(meth) return lltype_to_annotation(METH.RESULT)
def call(m, args): args_s, kwds_s = args.unpack() if kwds_s: raise Exception( "keyword arguments to call to a low-level bound method") inst = m.ootype._example() _, meth = ootype.typeOf(inst)._lookup(m.name) METH = ootype.typeOf(meth) return lltype_to_annotation(METH.RESULT)
def test_oounicode(): u = ootype.oounicode(u'a', -1) assert isinstance(u, ootype._string) assert ootype.typeOf(u) is ootype.Unicode s = ootype.make_string('a string') u = ootype.oounicode(s, -1) assert isinstance(u, ootype._string) assert ootype.typeOf(u) is ootype.Unicode s = ootype.make_string('non-ascii string: \xe0') py.test.raises(UnicodeDecodeError, ootype.oounicode, s, -1)
def test_simple_empty_base(): def dummyfn(): x = EmptyBase() return x result = interpret(dummyfn, []) assert isinstance(ootype.typeOf(result), ootype.Instance)
def simple_call(m, *args_s): _, meth = m.ootype._lookup(m.name) if isinstance(meth, ootype._overloaded_meth): return meth._resolver.annotate(args_s) else: METH = ootype.typeOf(meth) return lltype_to_annotation(METH.RESULT)
def cast_object_to_record(T, obj): assert isinstance(T, ootype.Record) assert isinstance(obj, ootype._view) assert isinstance(obj._inst, ootype._record) record = obj._inst assert ootype.typeOf(record) == T return record
def constant(value): if isinstance(lltype.typeOf(value), lltype.Ptr): return ConstPtr(value) elif isinstance(ootype.typeOf(value), ootype.OOType): return ConstObj(ootype.cast_to_object(value)) else: return ConstInt(value)
def ll_tuplenext(iter): # for iterating over length 1 tuples only! t = iter.iterable if t: iter.iterable = ootype.null(ootype.typeOf(t)) return t.item0 else: raise StopIteration
def get_primitive_constant(TYPE, value): if is_primitive(TYPE): return TYPE, value if TYPE is ootype.Object: obj = value.obj T2 = ootype.typeOf(obj) if obj is not None and is_primitive(T2): return T2, obj return None, None
def __init__(self, rtyper, methdescs): samplemdesc = methdescs.iterkeys().next() concretetable, uniquerows = get_concrete_calltable(rtyper, samplemdesc.funcdesc.getcallfamily()) self.row_mapping = {} for row in uniquerows: sample_as_static_meth = row.itervalues().next() SM = ootype.typeOf(sample_as_static_meth) M = ootype.Meth(SM.ARGS[1:], SM.RESULT) # cut self self.row_mapping[row.attrname] = row, M
def static_meth_to_signature(self, sm): from pypy.translator.oosupport import metavm graph = getattr(sm, 'graph', None) if graph: return self.graph_to_signature(graph) module, name = metavm.get_primitive_name(sm) func_name = '[pypylib]pypy.builtin.%s::%s' % (module, name) T = ootype.typeOf(sm) return self.format_signatue(func_name, T.ARGS, T.RESULT)
def __init__(self, rtyper, methdescs): samplemdesc = iter(methdescs).next() concretetable, uniquerows = get_concrete_calltable(rtyper, samplemdesc.funcdesc.getcallfamily()) self.row_mapping = {} for row in uniquerows: sample_as_static_meth = row.itervalues().next() SM = ootype.typeOf(sample_as_static_meth) M = ootype.Meth(SM.ARGS[1:], SM.RESULT) # cut self self.row_mapping[row.attrname] = row, M
def genconst(self, llvalue): T = ootype.typeOf(llvalue) if T is ootype.Signed: return IntConst(llvalue) elif T is ootype.Bool: return IntConst(int(llvalue)) elif isinstance(T, ootype.OOType): return ObjectConst(box(llvalue)) else: assert False, "XXX not implemented"
def attach_class_attr_accessor(self, mangled, oovalue): def ll_getclassattr(self): return oovalue M = ootype.Meth([], ootype.typeOf(oovalue)) ll_getclassattr = func_with_new_name(ll_getclassattr, 'll_get_' + mangled) graph = self.rtyper.annotate_helper(ll_getclassattr, [self.lowleveltype]) m = ootype.meth(M, _name=mangled, _callable=ll_getclassattr, graph=graph) ootype.addMethods(self.lowleveltype, {mangled: m})
def clrepr(item, symbol=False): """ This is the main repr function and is the only one that should be used to represent python values in lisp. """ if item is None: return "nil" fun = bltn_dispatch.get(type(item), None) if fun is not None: return fun(item, symbol) if typeOf(item) is Class: return "'" + item._INSTANCE._name return repr_unknown(item)
def _dont_store(self, to_load, to_store): # ugly workaround to make the exceptiontransformer work with # valuetypes: when exceptiontransforming a function whose result is a # .NET valuetype, it tries to store a null into the return variable. # Since it is not possible to store a null into a valuetype, and that # in that case the value is not used anyway, we simply ignore it. from pypy.translator.cli.dotnet import NativeInstance if isinstance(to_load, flowmodel.Constant): value = to_load.value is_null = (not isinstance(value, CDefinedIntSymbolic)) and (not value) T = ootype.typeOf(to_load.value) if isinstance(T, NativeInstance) and T._is_value_type and is_null: return True return OOFunction._dont_store(self, to_load, to_store)
def render(self, generator, op): cts = generator.cts v_obj, c_methname = op.args assert c_methname.concretetype is ootype.Void TYPE = v_obj.concretetype classname = TYPE._name methname = 'o' + c_methname.value # XXX: do proper mangling _, meth = TYPE._lookup(methname) METH = ootype.typeOf(meth) ret_type, arg_types = functype_to_cts(cts, METH) arg_list = ', '.join(arg_types) generator.load(v_obj) desc = '%s class %s::%s(%s)' % (ret_type, classname, methname, arg_list) generator.ilasm.opcode('ldftn instance', desc) generator.ilasm.opcode('newobj', 'instance void class [mscorlib]System.EventHandler::.ctor(object, native int)')
def render(self, generator, op): cts = generator.cts v_obj, c_methname = op.args assert c_methname.concretetype is ootype.Void TYPE = v_obj.concretetype classname = TYPE._name methname = "o" + c_methname.value # XXX: do proper mangling _, meth = TYPE._lookup(methname) METH = ootype.typeOf(meth) ret_type, arg_types = functype_to_cts(cts, METH) arg_list = ", ".join(arg_types) generator.load(v_obj) desc = "%s class %s::%s(%s)" % (ret_type, classname, methname, arg_list) generator.ilasm.opcode("ldftn instance", desc) generator.ilasm.opcode("newobj", "instance void class [mscorlib]System.EventHandler::.ctor(object, native int)")
def __init__(self, SELFTYPE, methname): _, meth = SELFTYPE._lookup(methname) METH = ootype.typeOf(meth) self.SELFTYPE = SELFTYPE self.METH = METH self.methname = methname RESULT = METH.RESULT getargs = make_getargs(METH.ARGS) def callmeth(selfbox, argboxes): selfobj = selfbox.getref(SELFTYPE) meth = getattr(selfobj, methname) methargs = getargs(argboxes) res = llimpl.call_maybe_on_top_of_llinterp(meth, methargs) if RESULT is not ootype.Void: return boxresult(RESULT, res) self.callmeth = callmeth
def __init__(self, SELFTYPE, methname): from pypy.jit.backend.llgraph.runner import boxresult, make_getargs _, meth = SELFTYPE._lookup(methname) METH = ootype.typeOf(meth) getargs = make_getargs(METH.ARGS) def callmeth(selfbox, argboxes): selfobj = selfbox.getref(SELFTYPE) meth = getattr(selfobj, methname) methargs = getargs(argboxes) res = meth(*methargs) if METH.RESULT is not ootype.Void: return boxresult(METH.RESULT, res) self.callmeth = callmeth self.selfclass = ootype.runtimeClass(SELFTYPE) self.methname = methname self.has_result = (METH.RESULT != ootype.Void) self.key = key_manager.getkey((SELFTYPE, methname))
def op_oogetfield(inst, name): checkinst(inst) if not ootype.typeOf(inst)._hints.get('immutable'): raise TypeError("cannot fold oogetfield on mutable instance") return getattr(inst, name)
def test_new(): DT = Dict(Signed, Float) d = new(DT) assert typeOf(d) == DT
elif ishashable(x) and x in BUILTIN_ANALYZERS: _module = getattr(x,"__module__","unknown") result = SomeBuiltin(BUILTIN_ANALYZERS[x], methodname="%s.%s" % (_module, x.__name__)) elif extregistry.is_registered(x, self.policy): entry = extregistry.lookup(x, self.policy) result = entry.compute_annotation_bk(self) elif tp in EXTERNAL_TYPE_ANALYZERS: result = SomeExternalObject(tp) elif isinstance(x, lltype._ptr): result = SomePtr(lltype.typeOf(x)) elif isinstance(x, llmemory.fakeaddress): result = SomeAddress(is_null=not x) elif isinstance(x, llmemory.fakeweakaddress): result = SomeWeakGcAddress() elif isinstance(x, ootype._static_meth): result = SomeOOStaticMeth(ootype.typeOf(x)) elif isinstance(x, ootype._class): result = SomeOOClass(x._INSTANCE) # NB. can be None elif isinstance(x, ootype.instance_impl): # XXX result = SomeOOInstance(ootype.typeOf(x)) elif callable(x): if hasattr(x, '__self__') and x.__self__ is not None: # for cases like 'l.append' where 'l' is a global constant list s_self = self.immutablevalue(x.__self__, need_const) result = s_self.find_method(x.__name__) if result is None: result = SomeObject() elif hasattr(x, 'im_self') and hasattr(x, 'im_func'): # on top of PyPy, for cases like 'l.append' where 'l' is a # global constant list, the find_method() returns non-None s_self = self.immutablevalue(x.im_self, need_const)
def is_inst(inst): T = ootype.typeOf(inst) return T is ootype.Object or T is ootype.Class or\ isinstance(T, (ootype.Instance, ootype.BuiltinType, ootype.StaticMethod,))
def null(I_OR_SM): assert I_OR_SM.is_constant() null = ootype.null(I_OR_SM.const) r = lltype_to_annotation(ootype.typeOf(null)) return r
def immutablevalue(self, x, need_const=True): """The most precise SomeValue instance that contains the immutable value x.""" # convert unbound methods to the underlying function if hasattr(x, 'im_self') and x.im_self is None: x = x.im_func assert not hasattr(x, 'im_self') if x is sys: # special case constant sys to someobject return SomeObject() tp = type(x) if issubclass(tp, Symbolic): # symbolic constants support result = x.annotation() result.const_box = Constant(x) return result if tp is bool: result = SomeBool() elif tp is int: result = SomeInteger(nonneg=x >= 0) elif tp is long: if -sys.maxint - 1 <= x <= sys.maxint: x = int(x) result = SomeInteger(nonneg=x >= 0) else: raise Exception("seeing a prebuilt long (value %s)" % hex(x)) elif issubclass(tp, str): # py.lib uses annotated str subclasses if len(x) == 1: result = SomeChar() else: result = SomeString() elif tp is unicode: if len(x) == 1: result = SomeUnicodeCodePoint() else: result = SomeUnicodeString() elif tp is tuple: result = SomeTuple( items=[self.immutablevalue(e, need_const) for e in x]) elif tp is float: result = SomeFloat() elif tp is list: if need_const: key = Constant(x) try: return self.immutable_cache[key] except KeyError: result = SomeList(ListDef(self, s_ImpossibleValue)) self.immutable_cache[key] = result for e in x: result.listdef.generalize(self.immutablevalue(e)) result.const_box = key return result else: listdef = ListDef(self, s_ImpossibleValue) for e in x: listdef.generalize(self.immutablevalue(e, False)) result = SomeList(listdef) elif tp is dict or tp is r_dict: if need_const: key = Constant(x) try: return self.immutable_cache[key] except KeyError: result = SomeDict( DictDef(self, s_ImpossibleValue, s_ImpossibleValue, is_r_dict=tp is r_dict)) self.immutable_cache[key] = result if tp is r_dict: s_eqfn = self.immutablevalue(x.key_eq) s_hashfn = self.immutablevalue(x.key_hash) result.dictdef.dictkey.update_rdict_annotations( s_eqfn, s_hashfn) seen_elements = 0 while seen_elements != len(x): items = x.items() for ek, ev in items: result.dictdef.generalize_key( self.immutablevalue(ek)) result.dictdef.generalize_value( self.immutablevalue(ev)) result.dictdef.seen_prebuilt_key(ek) seen_elements = len(items) # if the dictionary grew during the iteration, # start over again result.const_box = key return result else: dictdef = DictDef(self, s_ImpossibleValue, s_ImpossibleValue, is_r_dict=tp is r_dict) if tp is r_dict: s_eqfn = self.immutablevalue(x.key_eq) s_hashfn = self.immutablevalue(x.key_hash) dictdef.dictkey.update_rdict_annotations(s_eqfn, s_hashfn) for ek, ev in x.iteritems(): dictdef.generalize_key(self.immutablevalue(ek, False)) dictdef.generalize_value(self.immutablevalue(ev, False)) dictdef.seen_prebuilt_key(ek) result = SomeDict(dictdef) elif tp is weakref.ReferenceType: x1 = x() if x1 is None: result = SomeWeakRef(None) # dead weakref else: s1 = self.immutablevalue(x1) assert isinstance(s1, SomeInstance) result = SomeWeakRef(s1.classdef) elif ishashable(x) and x in BUILTIN_ANALYZERS: _module = getattr(x, "__module__", "unknown") result = SomeBuiltin(BUILTIN_ANALYZERS[x], methodname="%s.%s" % (_module, x.__name__)) elif extregistry.is_registered(x, self.policy): entry = extregistry.lookup(x, self.policy) result = entry.compute_annotation_bk(self) elif isinstance(x, lltype._ptr): result = SomePtr(lltype.typeOf(x)) elif isinstance(x, llmemory.fakeaddress): result = SomeAddress() elif isinstance(x, ootype._static_meth): result = SomeOOStaticMeth(ootype.typeOf(x)) elif isinstance(x, ootype._class): result = SomeOOClass(x._INSTANCE) # NB. can be None elif isinstance(x, ootype.instance_impl): # XXX result = SomeOOInstance(ootype.typeOf(x)) elif isinstance(x, (ootype._record, ootype._string)): result = SomeOOInstance(ootype.typeOf(x)) elif isinstance(x, (ootype._object)): result = SomeOOObject() elif callable(x): if hasattr(x, 'im_self') and hasattr(x, 'im_func'): # on top of PyPy, for cases like 'l.append' where 'l' is a # global constant list, the find_method() returns non-None s_self = self.immutablevalue(x.im_self, need_const) result = s_self.find_method(x.im_func.__name__) elif hasattr(x, '__self__') and x.__self__ is not None: # for cases like 'l.append' where 'l' is a global constant list s_self = self.immutablevalue(x.__self__, need_const) result = s_self.find_method(x.__name__) if result is None: result = SomeObject() else: result = None if result is None: if (self.annotator.policy.allow_someobjects and getattr(x, '__module__', None) == '__builtin__' # XXX note that the print support functions are __builtin__ and tp not in (types.FunctionType, types.MethodType)): result = SomeObject() result.knowntype = tp # at least for types this needs to be correct else: result = SomePBC([self.getdesc(x)]) elif hasattr(x, '_freeze_') and x._freeze_(): # user-defined classes can define a method _freeze_(), which # is called when a prebuilt instance is found. If the method # returns True, the instance is considered immutable and becomes # a SomePBC(). Otherwise it's just SomeInstance(). result = SomePBC([self.getdesc(x)]) elif hasattr(x, '__class__') \ and x.__class__.__module__ != '__builtin__': self.see_mutable(x) result = SomeInstance(self.getuniqueclassdef(x.__class__)) elif x is None: return s_None else: result = SomeObject() if need_const: result.const = x return result
def cast_record_to_object(record): T = ootype.typeOf(record) assert isinstance(T, ootype.Record) return ootype._view(CLR.System.Object._INSTANCE, record)
def convert_pbc(self, pbc): if ootype.typeOf(pbc) != PBCROOT: pbc = ootype.ooupcast(PBCROOT, pbc) return pbc
def render(self, generator, op): generator.load(Constant(self.value, ootype.typeOf(self.value)))
def is_inst(inst): return isinstance(ootype.typeOf(inst), (ootype.Instance, ootype.BuiltinType, ootype.StaticMethod))
def new(I): assert I.is_constant() i = ootype.new(I.const) r = SomeOOInstance(ootype.typeOf(i)) return r
def test_oostr(self): s = oostr("abc") assert ootype.typeOf(s) == ootype.String assert s._str == "abc"
def __init__(self, value=ootype.NULL): assert ootype.typeOf(value) is ootype.Object self.value = value
def is_inst(inst): return isinstance( ootype.typeOf(inst), (ootype.Instance, ootype.BuiltinType, ootype.StaticMethod))
def deref(self, obj): assert isinstance(ootype.typeOf(obj), ootype.OOType) return obj
def immutablevalue(self, x, need_const=True): """The most precise SomeValue instance that contains the immutable value x.""" # convert unbound methods to the underlying function if hasattr(x, 'im_self') and x.im_self is None: x = x.im_func assert not hasattr(x, 'im_self') if x is sys: # special case constant sys to someobject return SomeObject() tp = type(x) if issubclass(tp, Symbolic): # symbolic constants support result = x.annotation() result.const_box = Constant(x) return result if tp is bool: result = SomeBool() elif tp is int: result = SomeInteger(nonneg = x>=0) elif tp is long: if -sys.maxint-1 <= x <= sys.maxint: x = int(x) result = SomeInteger(nonneg = x>=0) else: raise Exception("seeing a prebuilt long (value %s)" % hex(x)) elif issubclass(tp, str): # py.lib uses annotated str subclasses no_nul = not '\x00' in x if len(x) == 1: result = SomeChar(no_nul=no_nul) else: result = SomeString(no_nul=no_nul) elif tp is unicode: if len(x) == 1: result = SomeUnicodeCodePoint() else: result = SomeUnicodeString() elif tp is tuple: result = SomeTuple(items = [self.immutablevalue(e, need_const) for e in x]) elif tp is float: result = SomeFloat() elif tp is list: if need_const: key = Constant(x) try: return self.immutable_cache[key] except KeyError: result = SomeList(ListDef(self, s_ImpossibleValue)) self.immutable_cache[key] = result for e in x: result.listdef.generalize(self.immutablevalue(e)) result.const_box = key return result else: listdef = ListDef(self, s_ImpossibleValue) for e in x: listdef.generalize(self.immutablevalue(e, False)) result = SomeList(listdef) elif tp is dict or tp is r_dict: if need_const: key = Constant(x) try: return self.immutable_cache[key] except KeyError: result = SomeDict(DictDef(self, s_ImpossibleValue, s_ImpossibleValue, is_r_dict = tp is r_dict)) self.immutable_cache[key] = result if tp is r_dict: s_eqfn = self.immutablevalue(x.key_eq) s_hashfn = self.immutablevalue(x.key_hash) result.dictdef.dictkey.update_rdict_annotations(s_eqfn, s_hashfn) seen_elements = 0 while seen_elements != len(x): items = x.items() for ek, ev in items: result.dictdef.generalize_key(self.immutablevalue(ek)) result.dictdef.generalize_value(self.immutablevalue(ev)) result.dictdef.seen_prebuilt_key(ek) seen_elements = len(items) # if the dictionary grew during the iteration, # start over again result.const_box = key return result else: dictdef = DictDef(self, s_ImpossibleValue, s_ImpossibleValue, is_r_dict = tp is r_dict) if tp is r_dict: s_eqfn = self.immutablevalue(x.key_eq) s_hashfn = self.immutablevalue(x.key_hash) dictdef.dictkey.update_rdict_annotations(s_eqfn, s_hashfn) for ek, ev in x.iteritems(): dictdef.generalize_key(self.immutablevalue(ek, False)) dictdef.generalize_value(self.immutablevalue(ev, False)) dictdef.seen_prebuilt_key(ek) result = SomeDict(dictdef) elif tp is weakref.ReferenceType: x1 = x() if x1 is None: result = SomeWeakRef(None) # dead weakref else: s1 = self.immutablevalue(x1) assert isinstance(s1, SomeInstance) result = SomeWeakRef(s1.classdef) elif ishashable(x) and x in BUILTIN_ANALYZERS: _module = getattr(x,"__module__","unknown") result = SomeBuiltin(BUILTIN_ANALYZERS[x], methodname="%s.%s" % (_module, x.__name__)) elif extregistry.is_registered(x, self.policy): entry = extregistry.lookup(x, self.policy) result = entry.compute_annotation_bk(self) elif isinstance(x, lltype._ptr): result = SomePtr(lltype.typeOf(x)) elif isinstance(x, llmemory.fakeaddress): result = SomeAddress() elif isinstance(x, ootype._static_meth): result = SomeOOStaticMeth(ootype.typeOf(x)) elif isinstance(x, ootype._class): result = SomeOOClass(x._INSTANCE) # NB. can be None elif isinstance(x, ootype.instance_impl): # XXX result = SomeOOInstance(ootype.typeOf(x)) elif isinstance(x, (ootype._record, ootype._string)): result = SomeOOInstance(ootype.typeOf(x)) elif isinstance(x, (ootype._object)): result = SomeOOObject() elif callable(x): if hasattr(x, 'im_self') and hasattr(x, 'im_func'): # on top of PyPy, for cases like 'l.append' where 'l' is a # global constant list, the find_method() returns non-None s_self = self.immutablevalue(x.im_self, need_const) result = s_self.find_method(x.im_func.__name__) elif hasattr(x, '__self__') and x.__self__ is not None: # for cases like 'l.append' where 'l' is a global constant list s_self = self.immutablevalue(x.__self__, need_const) result = s_self.find_method(x.__name__) if result is None: result = SomeObject() else: result = None if result is None: if (self.annotator.policy.allow_someobjects and getattr(x, '__module__', None) == '__builtin__' # XXX note that the print support functions are __builtin__ and tp not in (types.FunctionType, types.MethodType)): result = SomeObject() result.knowntype = tp # at least for types this needs to be correct else: result = SomePBC([self.getdesc(x)]) elif hasattr(x, '_freeze_') and x._freeze_(): # user-defined classes can define a method _freeze_(), which # is called when a prebuilt instance is found. If the method # returns True, the instance is considered immutable and becomes # a SomePBC(). Otherwise it's just SomeInstance(). result = SomePBC([self.getdesc(x)]) elif hasattr(x, '__class__') \ and x.__class__.__module__ != '__builtin__': self.see_mutable(x) result = SomeInstance(self.getuniqueclassdef(x.__class__)) elif x is None: return s_None else: result = SomeObject() if need_const: result.const = x return result
def fn(c): s1 = ootype.oostring("xy", -1) s2 = ootype.oostring("x" + chr(c), -1) assert (hash_whatever(ootype.typeOf(s1), s1) == hash_whatever(ootype.typeOf(s2), s2)) assert equal_whatever(ootype.typeOf(s1), s1, s2)