[pypy-commit] pypy no-class-specialize: Add ClassDesc method to retrieve interp-level class attributes used as translatioin hints (like '_virtualizable_', '_alloc_flavor_', etc.)

rlamy noreply at buildbot.pypy.org
Thu Oct 15 21:06:25 EDT 2015


Author: Ronan Lamy <ronan.lamy at gmail.com>
Branch: no-class-specialize
Changeset: r80251:b49e148d9af1
Date: 2015-10-16 02:06 +0100
http://bitbucket.org/pypy/pypy/changeset/b49e148d9af1/

Log:	Add ClassDesc method to retrieve interp-level class attributes used
	as translatioin hints (like '_virtualizable_', '_alloc_flavor_',
	etc.)

diff --git a/rpython/annotator/classdesc.py b/rpython/annotator/classdesc.py
--- a/rpython/annotator/classdesc.py
+++ b/rpython/annotator/classdesc.py
@@ -274,8 +274,6 @@
         # create the Attribute and do the generalization asked for
         newattr = Attribute(attr)
         if s_value:
-            #if newattr.name == 'intval' and getattr(s_value, 'unsigned', False):
-            #    import pdb; pdb.set_trace()
             newattr.s_value = s_value
 
         # keep all subattributes' values
@@ -690,6 +688,18 @@
         else:
             return cdesc
 
+    def get_param(self, name, default=None, inherit=True):
+        if inherit:
+            try:
+                return self.read_attribute(name).value
+            except AttributeError:
+                return default
+        else:
+            try:
+                return self.classdict[name].value
+            except KeyError:
+                return default
+
     def read_attribute(self, name, default=NODEFAULT):
         cdesc = self.lookup(name)
         if cdesc is None:
diff --git a/rpython/rlib/jit.py b/rpython/rlib/jit.py
--- a/rpython/rlib/jit.py
+++ b/rpython/rlib/jit.py
@@ -299,8 +299,7 @@
             if isinstance(s_x, annmodel.SomeInstance):
                 from rpython.flowspace.model import Constant
                 classdesc = s_x.classdef.classdesc
-                virtualizable = classdesc.read_attribute('_virtualizable_',
-                                                         Constant(None)).value
+                virtualizable = classdesc.get_param('_virtualizable_')
                 if virtualizable is not None:
                     flags = s_x.flags.copy()
                     flags['access_directly'] = True
@@ -340,7 +339,7 @@
     Used by _vmprof
     """
     from rpython.rtyper.lltypesystem import lltype, llmemory
-    
+
     return lltype.nullptr(llmemory.GCREF.TO)
 
 class GetVirtualizableTokenEntry(ExtRegistryEntry):
diff --git a/rpython/rtyper/rclass.py b/rpython/rtyper/rclass.py
--- a/rpython/rtyper/rclass.py
+++ b/rpython/rtyper/rclass.py
@@ -97,8 +97,7 @@
         unboxed = [subdef for subdef in classdef.getallsubdefs() if
             subdef.classdesc.pyobj is not None and
             issubclass(subdef.classdesc.pyobj, UnboxedValue)]
-        virtualizable = classdef.classdesc.read_attribute(
-            '_virtualizable_', Constant(False)).value
+        virtualizable = classdef.classdesc.get_param('_virtualizable_', False)
     config = rtyper.annotator.translator.config
     usetagging = len(unboxed) != 0 and config.translation.taggedpointers
 
@@ -529,26 +528,26 @@
         self.allinstancefields = allinstancefields
 
     def _check_for_immutable_hints(self, hints):
-        loc = self.classdef.classdesc.lookup('_immutable_')
-        if loc is not None:
-            if loc is not self.classdef.classdesc:
+        hints = hints.copy()
+        classdesc = self.classdef.classdesc
+        immut = classdesc.get_param('_immutable_', inherit=False)
+        if immut is None:
+            if classdesc.get_param('_immutable_', inherit=True):
                 raise ImmutableConflictError(
                     "class %r inherits from its parent _immutable_=True, "
                     "so it should also declare _immutable_=True" % (
                         self.classdef,))
-            if loc.classdict.get('_immutable_').value is not True:
-                raise TyperError(
-                    "class %r: _immutable_ = something else than True" % (
-                        self.classdef,))
-            hints = hints.copy()
+        elif immut is not True:
+            raise TyperError(
+                "class %r: _immutable_ = something else than True" % (
+                    self.classdef,))
+        else:
             hints['immutable'] = True
         self.immutable_field_set = set()  # unless overwritten below
-        if self.classdef.classdesc.lookup('_immutable_fields_') is not None:
-            hints = hints.copy()
-            immutable_fields = self.classdef.classdesc.classdict.get(
-                '_immutable_fields_')
-            if immutable_fields is not None:
-                self.immutable_field_set = set(immutable_fields.value)
+        if classdesc.get_param('_immutable_fields_'):
+            own_fields = classdesc.get_param('_immutable_fields_', inherit=False)
+            if own_fields is not None:
+                self.immutable_field_set = set(own_fields)
             accessor = FieldListAccessor()
             hints['immutable_fields'] = accessor
         return hints
diff --git a/rpython/rtyper/rmodel.py b/rpython/rtyper/rmodel.py
--- a/rpython/rtyper/rmodel.py
+++ b/rpython/rtyper/rmodel.py
@@ -390,8 +390,7 @@
 
 def getgcflavor(classdef):
     classdesc = classdef.classdesc
-    alloc_flavor = classdesc.read_attribute('_alloc_flavor_',
-                                            Constant('gc')).value
+    alloc_flavor = classdesc.get_param('_alloc_flavor_', default='gc')
     return alloc_flavor
 
 def externalvsinternal(rtyper, item_repr): # -> external_item_repr, (internal_)item_repr
diff --git a/rpython/rtyper/rvirtualizable.py b/rpython/rtyper/rvirtualizable.py
--- a/rpython/rtyper/rvirtualizable.py
+++ b/rpython/rtyper/rvirtualizable.py
@@ -11,13 +11,13 @@
     def __init__(self, rtyper, classdef):
         self._super().__init__(rtyper, classdef)
         classdesc = classdef.classdesc
-        if '_virtualizable2_' in classdesc.classdict:
+        if classdesc.get_param('_virtualizable2_'):
             raise Exception("_virtualizable2_ is now called _virtualizable_, "
                             "please rename")
-        if '_virtualizable_' in classdesc.classdict:
+        if classdesc.get_param('_virtualizable_', inherit=False):
             basedesc = classdesc.basedesc
-            assert basedesc is None or basedesc.lookup(
-                '_virtualizable_') is None
+            assert (basedesc is None or
+                    basedesc.get_param('_virtualizable_') is None)
             self.top_of_virtualizable_hierarchy = True
             self.accessor = FieldListAccessor()
         else:
@@ -37,9 +37,9 @@
                 self._super()._setup_repr(llfields, hints=hints)
             else:
                 self._super()._setup_repr(hints = hints)
-            c_vfields = self.classdef.classdesc.classdict['_virtualizable_']
+            vfields = self.classdef.classdesc.get_param('_virtualizable_')
             self.my_redirected_fields = self._parse_field_list(
-                c_vfields.value, self.accessor, hints)
+                vfields, self.accessor, hints)
         else:
             self._super()._setup_repr()
             # ootype needs my_redirected_fields even for subclass. lltype does
diff --git a/rpython/rtyper/test/test_rtyper.py b/rpython/rtyper/test/test_rtyper.py
--- a/rpython/rtyper/test/test_rtyper.py
+++ b/rpython/rtyper/test/test_rtyper.py
@@ -91,8 +91,6 @@
     class R:
         _alloc_flavor_ = "raw"
 
-    NDF = object()
-
     class DummyClsDescDef:
         def __init__(self, cls):
             self._cls = cls
@@ -102,14 +100,8 @@
         def getmro(self):
             return [self]
 
-        def read_attribute(self, attr, default=NDF):
-            try:
-                return Constant(getattr(self._cls, attr))
-            except AttributeError:
-                if default is NDF:
-                    raise
-                else:
-                    return default
+        def get_param(self, name, default=None, inherit=True):
+            return getattr(self._cls, name, default)
 
     assert rmodel.getgcflavor(DummyClsDescDef(A)) == 'gc'
     assert rmodel.getgcflavor(DummyClsDescDef(B)) == 'gc'


More information about the pypy-commit mailing list