Exemplo n.º 1
0
    def setup_meta_instance(self, meta_instance, rsubcls):
        if self.classdef is None:
            rinstance = getinstancerepr(self.rtyper, rsubcls.classdef)
            setattr(meta_instance, 'class_', rinstance.lowleveltype._class)
        else:
            # setup class attributes: for each attribute name at the level
            # of 'self', look up its value in the subclass rsubcls
            def assign(mangled_name, value):
                if isinstance(value, flowmodel.Constant) and isinstance(value.value, staticmethod):
                    value = flowmodel.Constant(value.value.__get__(42))   # staticmethod => bare function
                llvalue = r.convert_desc_or_const(value)
                setattr(meta_instance, mangled_name, llvalue)

            #mro = list(rsubcls.classdef.getmro())
            #for fldname in self.clsfields:
            #    mangled_name, r = self.clsfields[fldname]
            #    if r.lowleveltype is Void:
            #        continue
            #    value = rsubcls.classdef.classdesc.read_attribute(fldname, None)
            #    if value is not None:
            #        assign(mangled_name, value)
            # extra PBC attributes
            for (access_set, attr), (mangled_name, r) in self.pbcfields.items():
                if rsubcls.classdef.classdesc not in access_set.descs:
                    continue   # only for the classes in the same pbc access set
                if r.lowleveltype is ootype.Void:
                    continue
                attrvalue = rsubcls.classdef.classdesc.read_attribute(attr, None)
                if attrvalue is not None:
                    assign(mangled_name, attrvalue)

            # then initialize the 'super' portion of the vtable
            meta_instance_super = ootype.ooupcast(
                    self.rbase.lowleveltype, meta_instance)
            self.rbase.setup_meta_instance(meta_instance_super, rsubcls)
Exemplo n.º 2
0
 def getruntime(self, expected_type):
     if expected_type == ootype.Class:
         rinstance = getinstancerepr(self.rtyper, self.classdef)
         return ootype.runtimeClass(rinstance.lowleveltype)
     else:
         assert ootype.isSubclass(expected_type, META)
         meta = self.get_meta_instance(cast_to_root_meta=False)
         return ootype.ooupcast(expected_type, meta)
Exemplo n.º 3
0
 def getruntime(self, expected_type):
     if expected_type == ootype.Class:
         rinstance = getinstancerepr(self.rtyper, self.classdef)
         return ootype.runtimeClass(rinstance.lowleveltype)
     else:
         assert ootype.isSubclass(expected_type, META)
         meta = self.get_meta_instance(cast_to_root_meta=False)
         return ootype.ooupcast(expected_type, meta)
Exemplo n.º 4
0
 def get_meta_instance(self, cast_to_root_meta=True):
     if self.meta_instance is None:
         self.meta_instance = ootype.new(self.lowleveltype) 
         self.setup_meta_instance(self.meta_instance, self)
     
     meta_instance = self.meta_instance
     if cast_to_root_meta:
         meta_instance = ootype.ooupcast(CLASSTYPE, meta_instance)
     return meta_instance
Exemplo n.º 5
0
    def get_meta_instance(self, cast_to_root_meta=True):
        if self.meta_instance is None:
            self.meta_instance = ootype.new(self.lowleveltype)
            self.setup_meta_instance(self.meta_instance, self)

        meta_instance = self.meta_instance
        if cast_to_root_meta:
            meta_instance = ootype.ooupcast(CLASSTYPE, meta_instance)
        return meta_instance
Exemplo n.º 6
0
    def get_meta_instance(self, cast_to_root_meta=True):
        if self.lowleveltype == ootype.Class:
            raise TyperError("no meta-instance for class %r" %
                             (self.classdef, ))
        if self.meta_instance is None:
            self.meta_instance = ootype.new(self.lowleveltype)
            self.setup_meta_instance(self.meta_instance, self)

        meta_instance = self.meta_instance
        if cast_to_root_meta:
            meta_instance = ootype.ooupcast(META, meta_instance)
        return meta_instance
Exemplo n.º 7
0
 def get_meta_instance(self, cast_to_root_meta=True):
     if self.lowleveltype == ootype.Class:
         raise TyperError("no meta-instance for class %r" % 
                          (self.classdef,))
     if self.meta_instance is None:
         self.meta_instance = ootype.new(self.lowleveltype) 
         self.setup_meta_instance(self.meta_instance, self)
     
     meta_instance = self.meta_instance
     if cast_to_root_meta:
         meta_instance = ootype.ooupcast(META, meta_instance)
     return meta_instance
Exemplo n.º 8
0
    def setup_meta_instance(self, meta_instance, rsubcls):
        if self.classdef is None:
            rinstance = getinstancerepr(self.rtyper, rsubcls.classdef)
            setattr(meta_instance, 'class_', rinstance.lowleveltype._class)
        else:
            # setup class attributes: for each attribute name at the level
            # of 'self', look up its value in the subclass rsubcls
            def assign(mangled_name, value):
                if isinstance(value, flowmodel.Constant) and isinstance(
                        value.value, staticmethod):
                    value = flowmodel.Constant(value.value.__get__(
                        42))  # staticmethod => bare function
                llvalue = r.convert_desc_or_const(value)
                setattr(meta_instance, mangled_name, llvalue)

            #mro = list(rsubcls.classdef.getmro())
            #for fldname in self.clsfields:
            #    mangled_name, r = self.clsfields[fldname]
            #    if r.lowleveltype is Void:
            #        continue
            #    value = rsubcls.classdef.classdesc.read_attribute(fldname, None)
            #    if value is not None:
            #        assign(mangled_name, value)
            # extra PBC attributes
            for (access_set, attr), (mangled_name,
                                     r) in self.pbcfields.items():
                if rsubcls.classdef.classdesc not in access_set.descs:
                    continue  # only for the classes in the same pbc access set
                if r.lowleveltype is ootype.Void:
                    continue
                attrvalue = rsubcls.classdef.classdesc.read_attribute(
                    attr, None)
                if attrvalue is not None:
                    assign(mangled_name, attrvalue)

            # then initialize the 'super' portion of the vtable
            meta_instance_super = ootype.ooupcast(self.rbase.lowleveltype,
                                                  meta_instance)
            self.rbase.setup_meta_instance(meta_instance_super, rsubcls)
Exemplo n.º 9
0
 def cast_exception(self, TYPE, value):
     return ootype.ooupcast(TYPE, value)
Exemplo n.º 10
0
 def cast_exception(self, TYPE, value):
     return ootype.ooupcast(TYPE, value)
Exemplo n.º 11
0
 def convert_pbc(self, pbc):
     if ootype.typeOf(pbc) != PBCROOT:
         pbc = ootype.ooupcast(PBCROOT, pbc)
     return pbc
Exemplo n.º 12
0
def op_ooupcast(INST, inst):
    return ootype.ooupcast(INST, inst)
Exemplo n.º 13
0
 def upcast(self, result):
     return ootype.ooupcast(self.lowleveltype, result)
Exemplo n.º 14
0
def op_ooupcast(INST, inst):
    return ootype.ooupcast(INST, inst)
Exemplo n.º 15
0
 def upcast(self, result):
     return ootype.ooupcast(self.lowleveltype, result)
Exemplo n.º 16
0
 def convert_pbc(self, pbc):
     if ootype.typeOf(pbc) != PBCROOT:
         pbc = ootype.ooupcast(PBCROOT, pbc)
     return pbc
Exemplo n.º 17
0
    def make_pyexcclass2exc(self, rtyper):
        # ll_pyexcclass2exc(python_exception_class) -> exception_instance
        table = {}
        Exception_def = rtyper.annotator.bookkeeper.getuniqueclassdef(Exception)
        for clsdef in rtyper.class_reprs:
            if (clsdef and clsdef is not Exception_def
                and clsdef.issubclass(Exception_def)):
                if not hasattr(clsdef.classdesc, 'pyobj'):
                    continue
                cls = clsdef.classdesc.pyobj
                if cls in self.standardexceptions and cls not in FORCE_ATTRIBUTES_INTO_CLASSES:
                    is_standard = True
                    assert not clsdef.attrs, (
                        "%r should not have grown attributes" % (cls,))
                else:
                    is_standard = (cls.__module__ == 'exceptions'
                                   and not clsdef.attrs)
                if is_standard:
                    r_inst = rclass.getinstancerepr(rtyper, clsdef)
                    example = r_inst.get_reusable_prebuilt_instance()
                    example = ootype.ooupcast(self.lltype_of_exception_value, example)
                    table[cls] = example
        r_inst = rclass.getinstancerepr(rtyper, None)
        r_inst.setup()
        r_class = rclass.getclassrepr(rtyper, None)
        r_class.setup()
        default_excinst = ootype.new(self.lltype_of_exception_value)
        default_excinst.meta = r_class.get_meta_instance()

        # build the table in order base classes first, subclasses last
        sortedtable = []
        def add_class(cls):
            if cls in table:
                for base in cls.__bases__:
                    add_class(base)
                sortedtable.append((cls, table[cls]))
                del table[cls]
        for cls in table.keys():
            add_class(cls)
        assert table == {}

        initial_value_of_i = len(sortedtable) - 1
        def pyexcclass2exc(python_exception_class):
            python_exception_class = python_exception_class._obj.value
            i = initial_value_of_i
            while i >= 0:
                if issubclass(python_exception_class, sortedtable[i][0]):
                    return sortedtable[i][1]
                i -= 1
            return default_excinst

        # This function will only be used by the llinterpreter which usually
        # expects a low-level callable (_meth, _static_meth), so we just
        # fake it here.
        FakeCallableType = ootype.OOType()
        FakeCallableType.ARGS = ()
        class fake_callable(object):
            def __init__(self, fn):
                self._TYPE = FakeCallableType
                self._callable = fn
        return fake_callable(pyexcclass2exc)