def fn_null(): a = ootype.null(A) b = ootype.null(B) obj1 = ootype.cast_to_object(a) obj2 = ootype.cast_to_object(b) assert obj1 == obj2 assert ootype.cast_from_object(A, obj1) == a assert ootype.cast_from_object(B, obj2) == b
def get_exception(self): exc_value = self.get_inputargs().get_exc_value() if exc_value: exc_obj = dotnet.cast_from_native_object(exc_value) exc_inst = ootype.cast_from_object(ootype.ROOT, exc_obj) cls = ootype.classof(exc_value) return ootype.cast_to_object(cls) return ootype.cast_to_object(ootype.nullruntimeclass)
def test_unwrap_object(self): A = ootype.Instance("A", ootype.ROOT, {}) a1 = ootype.new(A) a2 = ootype.new(A) obj1 = ootype.cast_to_object(a1) obj2 = ootype.cast_to_object(a2) def fn(flag): if flag: obj = obj1 else: obj = obj2 a3 = ootype.cast_from_object(A, obj) return a3 is a1 res = self.interpret(fn, [True], backendopt=False) assert res is True
def fn_instance(): a = ootype.new(A) obj = ootype.cast_to_object(a) a2 = ootype.cast_from_object(A, obj) a3 = ootype.cast_from_object(ootype.ROOT, obj) assert a is a2 assert a is a3
def fn_record(): b = ootype.new(B) b.x = 42 obj = ootype.cast_to_object(b) b2 = ootype.cast_from_object(B, obj) assert b2.x == 42 assert b is b2
def fn_is_true(flag): if flag: a = ootype.new(A) else: a = ootype.null(A) obj = ootype.cast_to_object(a) return bool(obj)
def fn_mix_null(flag): a = ootype.new(A) obj = ootype.cast_to_object(a) if flag: return obj else: return ootype.NULL
def boxresult(RESULT, result): if isinstance(RESULT, ootype.OOType): return history.BoxObj(ootype.cast_to_object(result)) elif RESULT is lltype.Float: return history.BoxFloat(result) else: return history.BoxInt(lltype.cast_primitive(ootype.Signed, result))
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 wrap(cpu, value, in_const_box=False): if isinstance(lltype.typeOf(value), lltype.Ptr): if lltype.typeOf(value).TO._gckind == 'gc': value = lltype.cast_opaque_ptr(llmemory.GCREF, value) if in_const_box: return history.ConstPtr(value) else: return history.BoxPtr(value) else: adr = llmemory.cast_ptr_to_adr(value) value = heaptracker.adr2int(adr) # fall through to the end of the function elif isinstance(lltype.typeOf(value), ootype.OOType): value = ootype.cast_to_object(value) if in_const_box: return history.ConstObj(value) else: return history.BoxObj(value) elif isinstance(value, float): value = longlong.getfloatstorage(value) if in_const_box: return history.ConstFloat(value) else: return history.BoxFloat(value) elif isinstance(value, str) or isinstance(value, unicode): assert len(value) == 1 # must be a character value = ord(value) else: value = intmask(value) if in_const_box: return history.ConstInt(value) else: return history.BoxInt(value)
def wrap(cpu, value, in_const_box=False): if isinstance(lltype.typeOf(value), lltype.Ptr): if lltype.typeOf(value).TO._gckind == 'gc': value = lltype.cast_opaque_ptr(llmemory.GCREF, value) if in_const_box: return history.ConstPtr(value) else: return history.BoxPtr(value) else: adr = llmemory.cast_ptr_to_adr(value) value = cpu.cast_adr_to_int(adr) # fall through to the end of the function elif isinstance(lltype.typeOf(value), ootype.OOType): value = ootype.cast_to_object(value) if in_const_box: return history.ConstObj(value) else: return history.BoxObj(value) elif isinstance(value, float): if in_const_box: return history.ConstFloat(value) else: return history.BoxFloat(value) else: value = intmask(value) if in_const_box: return history.ConstInt(value) else: return history.BoxInt(value)
def test_convert_string_to_object(self): s = self.string_to_ll("hello world") obj = ootype.cast_to_object(s) def fn(): s1 = ootype.cast_from_object(ootype.String, obj) return s1 res = self.interpret(fn, [], backendopt=False) assert res == 'hello world'
def fn(): a = ootype.new(A) ahash = ootype.identityhash(a) obj = ootype.cast_to_object(a) native = cast_to_native_object(obj) name = native.GetType().get_Name() obj2 = cast_from_native_object(native) a2 = ootype.cast_from_object(A, obj2) a2hash = ootype.identityhash(a2) return name, ahash == a2hash
def convert_const(self, value): if value._identity is _identity_for_ints: return value._x # FIXME: what should we do here? bk = self.rtyper.annotator.bookkeeper s_obj = value._identity.get_input_annotation(bk) r_obj = self.rtyper.getrepr(s_obj) if r_obj.lowleveltype is lltype.Void: return ootype.NULL v = r_obj.convert_const(value._x) return ootype.cast_to_object(v)
def unspecialize_value(value): """Casts 'value' to a Signed, a GCREF or a FLOATSTORAGE.""" if isinstance(lltype.typeOf(value), lltype.Ptr): if lltype.typeOf(value).TO._gckind == 'gc': return lltype.cast_opaque_ptr(llmemory.GCREF, value) else: adr = llmemory.cast_ptr_to_adr(value) return heaptracker.adr2int(adr) elif isinstance(lltype.typeOf(value), ootype.OOType): return ootype.cast_to_object(value) elif isinstance(value, float): return longlong.getfloatstorage(value) else: return lltype.cast_primitive(lltype.Signed, value)
def __init__(self, cpu, cliloop): self.setoptions() self.cpu = cpu self.name = cliloop.get_fresh_cli_name() self.cliloop = cliloop self.boxes = {} # box --> local var self.branches = [] self.branchlabels = [] self.consts = {} # object --> index self.meth_wrapper = self._get_meth_wrapper() self.il = self.meth_wrapper.get_il_generator() self.av_consts = MethodArgument(0, System.Type.GetType("System.Object[]")) t_InputArgs = dotnet.typeof(InputArgs) self.av_inputargs = MethodArgument(1,t_InputArgs ) self.av_ovf_flag = BoxInt() self.exc_value_field = t_InputArgs.GetField('exc_value') if cpu.rtyper: self.av_OverflowError = ConstObj(ootype.cast_to_object(cpu.ll_ovf_exc)) self.av_ZeroDivisionError = ConstObj(ootype.cast_to_object(cpu.ll_zero_exc)) else: self.av_OverflowError = None self.av_ZeroDivisionError = None self.box2type = {}
def get_const(self, name, typ): if self._consts is None: return name obj = self._consts[name] if self.type_system == 'lltype': if typ == 'ptr': return ConstPtr(obj) else: assert typ == 'class' return ConstAddr(llmemory.cast_ptr_to_adr(obj), self.cpu) else: if typ == 'ptr': return ConstObj(obj) else: assert typ == 'class' return ConstObj(ootype.cast_to_object(obj))
def do_runtimenew(self, classbox): classobj = classbox.getref(ootype.Class) res = ootype.runtimenew(classobj) return BoxObj(ootype.cast_to_object(res))
def constclass(cls_vtable): if self.type_system == 'lltype': return ConstAddr(llmemory.cast_ptr_to_adr(cls_vtable), self.cpu) else: return ConstObj(ootype.cast_to_object(cls_vtable))
def set_overflow_error(self): exc_obj = ootype.cast_to_object(self.ll_ovf_exc) exc_value = dotnet.cast_to_native_object(exc_obj) self.get_inputargs().set_exc_value(exc_value)
def new_ConstRef(self, x): obj = ootype.cast_to_object(x) return history.ConstObj(obj)
def get_zero_division_error(self): ll_err = llimpl._get_error(ZeroDivisionError) return (ootype.cast_to_object(ll_err.args[0]), ootype.cast_to_object(ll_err.args[1]))
def get_exc_value(self): if llimpl._last_exception: earg = llimpl._last_exception.args[1] return ootype.cast_to_object(earg) else: return ootype.NULL
def cls_of_box(self, cpu, box): obj = box.getref(ootype.ROOT) oocls = ootype.classof(obj) return history.ConstObj(ootype.cast_to_object(oocls))
def get_overflow_error(self): ll_err = llimpl._get_error(OverflowError) return (ootype.cast_to_object(ll_err.args[0]), ootype.cast_to_object(ll_err.args[1]))
def get_exception(self): if llimpl._last_exception: e = llimpl._last_exception.args[0] return ootype.cast_to_object(e) else: return ootype.NULL
def typedescr2classbox(self, descr): assert isinstance(descr, TypeDescr) return history.ConstObj( ootype.cast_to_object(ootype.runtimeClass(descr.TYPE)))
def typedescr2classbox(self, descr): assert isinstance(descr, TypeDescr) return ConstObj(ootype.cast_to_object(descr.ooclass))
def _cast_instance_to_native_obj(self, e): from pypy.rpython.annlowlevel import cast_instance_to_base_obj inst = cast_instance_to_base_obj(e) # SomeOOInstance obj = ootype.cast_to_object(inst) # SomeOOObject return dotnet.cast_to_native_object(obj) # System.Object
def get_zero_division_error(self): exc_type = ootype.cast_to_object(ootype.classof(self.ll_zero_exc)) exc_value = ootype.cast_to_object(self.ll_zero_exc) return exc_type, exc_value
def conststr(self, str): oo = oostr(str) return history.ConstObj(ootype.cast_to_object(oo))
def do_runtimenew(self, classbox): "NOT_RPYTHON" classobj = classbox.getref(ootype.Class) res = ootype.runtimenew(classobj) return history.BoxObj(ootype.cast_to_object(res))
def cast_fnptr_to_root(self, fnptr): return ootype.cast_to_object(fnptr)
def op_cast_to_object(inst): return ootype.cast_to_object(inst)
def duplicate_ref(self, x): o = x.obj return ootype.cast_to_object(o)
def get_overflow_error(self): exc_type = ootype.cast_to_object(ootype.classof(self.ll_ovf_exc)) exc_value = ootype.cast_to_object(self.ll_ovf_exc) return exc_type, exc_value
class OOtypeMixin(object): type_system = 'ootype' def get_class_of_box(self, box): root = box.getref(ootype.ROOT) return ootype.classof(root) cpu = runner.OOtypeCPU(None) NODE = ootype.Instance('NODE', ootype.ROOT, {}) NODE._add_fields({ 'value': ootype.Signed, 'floatval': ootype.Float, 'next': NODE }) NODE2 = ootype.Instance('NODE2', NODE, {'other': NODE}) node_vtable = ootype.runtimeClass(NODE) node_vtable_adr = ootype.cast_to_object(node_vtable) node_vtable2 = ootype.runtimeClass(NODE2) node_vtable_adr2 = ootype.cast_to_object(node_vtable2) node = ootype.new(NODE) nodebox = BoxObj(ootype.cast_to_object(node)) myptr = nodebox.value myptr2 = ootype.cast_to_object(ootype.new(NODE)) nodebox2 = BoxObj(ootype.cast_to_object(node)) valuedescr = cpu.fielddescrof(NODE, 'value') floatdescr = cpu.fielddescrof(NODE, 'floatval') nextdescr = cpu.fielddescrof(NODE, 'next') otherdescr = cpu.fielddescrof(NODE2, 'other') nodesize = cpu.typedescrof(NODE) nodesize2 = cpu.typedescrof(NODE2) arraydescr = cpu.arraydescrof(ootype.Array(ootype.Signed)) floatarraydescr = cpu.arraydescrof(ootype.Array(ootype.Float)) # a plain Record S = ootype.Record({'a': ootype.Signed, 'b': NODE}) ssize = cpu.typedescrof(S) adescr = cpu.fielddescrof(S, 'a') bdescr = cpu.fielddescrof(S, 'b') sbox = BoxObj(ootype.cast_to_object(ootype.new(S))) arraydescr2 = cpu.arraydescrof(ootype.Array(S)) T = ootype.Record({'c': ootype.Signed, 'd': ootype.Array(NODE)}) tsize = cpu.typedescrof(T) cdescr = cpu.fielddescrof(T, 'c') ddescr = cpu.fielddescrof(T, 'd') arraydescr3 = cpu.arraydescrof(ootype.Array(NODE)) U = ootype.Instance('U', ootype.ROOT, {'one': ootype.Array(NODE)}) usize = cpu.typedescrof(U) onedescr = cpu.fielddescrof(U, 'one') u_vtable = ootype.runtimeClass(U) u_vtable_adr = ootype.cast_to_object(u_vtable) # force a consistent order valuedescr.sort_key() nextdescr.sort_key() adescr.sort_key() bdescr.sort_key() cpu.class_sizes = { node_vtable_adr: cpu.typedescrof(NODE), node_vtable_adr2: cpu.typedescrof(NODE2), u_vtable_adr: cpu.typedescrof(U) } namespace = locals()
def set_zero_division_error(self): exc_obj = ootype.cast_to_object(self.ll_zero_exc) exc_value = dotnet.cast_to_native_object(exc_obj) self.get_inputargs().set_exc_value(exc_value)
def cast_to_ref(self, value): return ootype.cast_to_object(value)
def cast_vtable_to_hashable(self, cpu, obj): return ootype.cast_to_object(obj)
def ootype_cast_to_object(obj): from pypy.rpython.ootypesystem import ootype return ootype.cast_to_object(obj)
def fresh_ref(self): O = ootype.StaticMethod([], ootype.Signed) o = O._example() return ootype.cast_to_object(o)