def __init__(self, rtyper, classdef): AbstractClassRepr.__init__(self, rtyper, classdef) # This is the Repr for a reference to the class 'classdef' or # any subclass. In the simple case, the lowleveltype is just # ootype.Class. If we need to store class attributes, we use a # "meta" class where the attributes are defined, and the class # reference is a reference to an instance of this meta class. extra_access_sets = self.rtyper.class_pbc_attributes.get(classdef, {}) has_class_attributes = bool(extra_access_sets) if self.classdef is not None: self.rbase = getclassrepr(self.rtyper, self.classdef.basedef) meta_base_type = self.rbase.lowleveltype baseclass_has_meta = meta_base_type != ootype.Class else: baseclass_has_meta = False if not has_class_attributes and not baseclass_has_meta: self.lowleveltype = ootype.Class # simple case else: if self.classdef is None: raise TyperError("the root 'object' class should not have" " class attributes") if self.classdef.classdesc.pyobj in standardexceptions: raise TyperError("Standard exception class %r should not have" " class attributes" % (self.classdef.name, )) if not baseclass_has_meta: meta_base_type = META self.lowleveltype = ootype.Instance(self.classdef.name + "_meta", meta_base_type)
def __init__(self, rtyper, access_set): self.rtyper = rtyper self.access_set = access_set self.lowleveltype = ootype.Instance('pbc', PBCROOT, _hints={'immutable': True}) self.pbc_cache = {}
def __init__(self, rtyper, classdef, gcflavor='ignored'): AbstractInstanceRepr.__init__(self, rtyper, classdef) self.baserepr = None if self.classdef is None: self.lowleveltype = OBJECT else: b = self.classdef.basedef if b is not None: self.baserepr = getinstancerepr(rtyper, b) b = self.baserepr.lowleveltype else: b = OBJECT if hasattr(classdef.classdesc.pyobj, '_rpython_hints'): hints = classdef.classdesc.pyobj._rpython_hints else: hints = {} hints = self._check_for_immutable_hints(hints) self.lowleveltype = ootype.Instance(classdef.name, b, {}, {}, _hints=hints) self.iprebuiltinstances = identity_dict() self.object_type = self.lowleveltype self.gcflavor = gcflavor
def test_compare_classes(): A = ootype.Instance("A", ootype.ROOT) B = ootype.Instance("B", ootype.ROOT) cls1 = ootype.runtimeClass(A) def fn(n): if n: cls2 = ootype.runtimeClass(A) else: cls2 = ootype.runtimeClass(B) assert (cls1 == cls2) == (not (cls1 != cls2)) return cls1 == cls2 res = interpret(fn, [1], type_system='ootype') assert res
def test_box_unbox_ooinstance_fail(self): A = ootype.Instance('A', ootype.ROOT, {'xx': ootype.Signed}) def fn(flag): b_obj = System.Object() a2 = unbox(b_obj, A) return a2 res = self.interpret(fn, [True]) assert res is None
def test_box_unbox_ooinstance(self): A = ootype.Instance('A', ootype.ROOT, {'xx': ootype.Signed}) def fn(flag): a = ootype.new(A) a.xx = 42 b_obj = box(a) a2 = unbox(b_obj, A) return a2.xx res = self.interpret(fn, [True]) assert res == 42
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 test_cast_native_object(self): A = ootype.Instance("A", ootype.ROOT, {}) 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 res = self.ll_to_tuple(self.interpret(fn, [])) assert res == ('A', True)
import types from rpython.annotator import model as annmodel from rpython.annotator import description from rpython.flowspace import model as flowmodel from rpython.rtyper.rmodel import inputconst, TyperError, warning from rpython.rtyper.rmodel import mangle as pbcmangle from rpython.rtyper.rclass import AbstractClassRepr, AbstractInstanceRepr, \ getinstancerepr, getclassrepr, get_type_repr from rpython.rtyper.ootypesystem import ootype from rpython.rtyper.exceptiondata import standardexceptions from rpython.tool.pairtype import pairtype from rpython.tool.sourcetools import func_with_new_name from rpython.tool.identity_dict import identity_dict OBJECT = ootype.ROOT META = ootype.Instance("Meta", ootype.ROOT, fields={"class_": ootype.Class}) class ClassRepr(AbstractClassRepr): def __init__(self, rtyper, classdef): AbstractClassRepr.__init__(self, rtyper, classdef) # This is the Repr for a reference to the class 'classdef' or # any subclass. In the simple case, the lowleveltype is just # ootype.Class. If we need to store class attributes, we use a # "meta" class where the attributes are defined, and the class # reference is a reference to an instance of this meta class. extra_access_sets = self.rtyper.class_pbc_attributes.get(classdef, {}) has_class_attributes = bool(extra_access_sets) if self.classdef is not None: self.rbase = getclassrepr(self.rtyper, self.classdef.basedef) meta_base_type = self.rbase.lowleveltype
def test_fielddescr_ootype(): A = ootype.Instance("A", ootype.ROOT, {"foo": ootype.Signed}) B = ootype.Instance("B", A) descr1 = CliCPU.fielddescrof(A, "foo") descr2 = CliCPU.fielddescrof(B, "foo") assert descr1 is descr2
def setup_specfunc(self): fields = {} for row in self.uniquerows: fields[row.attrname] = row.fntype return ootype.Instance('specfunc', ootype.ROOT, fields)
shape, index, callfamily = self._get_shape_index_callfamily( opname, s_pbc, args_s, hop) mangled = mangle(self.methodname, self.rtyper.getconfig()) row = self.concretetable[shape, index] derived_mangled = row_method_name(mangled, row.attrname) return derived_mangled class __extend__(pairtype(InstanceRepr, MethodsPBCRepr)): def convert_from_to(_, v, llops): return v # ____________________________________________________________ PBCROOT = ootype.Instance('pbcroot', ootype.ROOT) class MultipleFrozenPBCRepr(AbstractMultipleFrozenPBCRepr): """Representation selected for multiple non-callable pre-built constants.""" def __init__(self, rtyper, access_set): self.rtyper = rtyper self.access_set = access_set self.lowleveltype = ootype.Instance('pbc', PBCROOT, _hints={'immutable': True}) self.pbc_cache = {} def _setup_repr(self): fields_list = self._setup_repr_fields() ootype.addFields(self.lowleveltype, dict(fields_list))