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 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 static_meth_to_signature(self, sm): from rpython.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 __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 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 _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 rpython.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 __init__(self, SELFTYPE, methname): from rpython.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 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 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 convert_pbc(self, pbc): if ootype.typeOf(pbc) != PBCROOT: pbc = ootype.ooupcast(PBCROOT, pbc) return pbc
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 test_new(): DT = Dict(Signed, Float) d = new(DT) assert typeOf(d) == DT
def compute_annotation(self): return annmodel.SomeOOInstance(ootype=ootype.typeOf(self.instance))