[pypy-svn] r7520 - pypy/trunk/src/pypy/annotation

mwh at codespeak.net mwh at codespeak.net
Sat Nov 20 17:28:31 CET 2004


Author: mwh
Date: Sat Nov 20 17:28:30 2004
New Revision: 7520

Modified:
   pypy/trunk/src/pypy/annotation/factory.py
   pypy/trunk/src/pypy/annotation/unaryop.py
Log:
Slightly de-Rigo the code.
Well, I find this version easier to understand, but maybe that's
just because I wrote it...


Modified: pypy/trunk/src/pypy/annotation/factory.py
==============================================================================
--- pypy/trunk/src/pypy/annotation/factory.py	(original)
+++ pypy/trunk/src/pypy/annotation/factory.py	Sat Nov 20 17:28:30 2004
@@ -251,7 +251,6 @@
         if self.basedef:
             self.basedef.subdefs[cls] = self
         # collect the (supposed constant) class attributes
-        s_self = SomeInstance(self)
         for name, value in cls.__dict__.items():
             # ignore some special attributes
             if name.startswith('_') and not isinstance(value, FunctionType):
@@ -263,7 +262,7 @@
             # generalizes existing values in parent classes
             s_value = immutablevalue(value)
             s_value = s_value.bindcallables(self)
-            self.generalize(name, s_value, bookkeeper)
+            self.generalize_attr(name, s_value, bookkeeper)
 
     def __repr__(self):
         return "<ClassDef '%s.%s'>" % (self.cls.__module__, self.cls.__name__)
@@ -294,15 +293,8 @@
             factories.update(clsdef.instancefactories)
         return factories
 
-    def generalize(self, attr, s_value, bookkeeper=None, readonly=True):
-        # we make sure that an attribute never appears both in a class
-        # and in some subclass, in two steps:
-        # (1) check if the attribute is already in a superclass
-        for clsdef in self.getmro():
-            if attr in clsdef.attrs:
-                self = clsdef   # generalize the parent class instead
-                break
-        # (2) remove the attribute from subclasses
+    def _generalize_attr(self, attr, s_value, bookkeeper, readonly):
+        # first remove the attribute from subclasses -- including us!
         subclass_values = []
         for subdef in self.getallsubdefs():
             if attr in subdef.attrs:
@@ -312,13 +304,26 @@
                 del subdef.readonly[attr]
             # bump the revision number of this class and all subclasses
             subdef.revision += 1
+
+        # do the generalization
         self.attrs[attr] = unionof(s_value, *subclass_values)
         self.readonly[attr] = readonly
+        
         # reflow from all factories
         if bookkeeper:
             for factory in self.getallfactories():
                 bookkeeper.annotator.reflowfromposition(factory.position_key)
 
+
+    def generalize_attr(self, attr, s_value, bookkeeper=None, readonly=True):
+        # if the attribute exists in a superclass, generalize there.
+        for clsdef in self.getmro():
+            if attr in clsdef.attrs:
+                clsdef._generalize_attr(attr, s_value, bookkeeper, readonly)
+                return
+        else:
+            self._generalize_attr(attr, s_value, bookkeeper, readonly)
+
     def about_attribute(self, name):
         for cdef in self.getmro():
             if name in cdef.attrs:

Modified: pypy/trunk/src/pypy/annotation/unaryop.py
==============================================================================
--- pypy/trunk/src/pypy/annotation/unaryop.py	(original)
+++ pypy/trunk/src/pypy/annotation/unaryop.py	Sat Nov 20 17:28:30 2004
@@ -126,7 +126,7 @@
                     return clsdef.attrs[attr]
             # maybe the attribute exists in some subclass? if so, lift it
             clsdef = ins.classdef
-            clsdef.generalize(attr, SomeImpossibleValue(), getbookkeeper())
+            clsdef.generalize_attr(attr, SomeImpossibleValue(), getbookkeeper())
             raise BlockedInference
         return SomeObject()
 
@@ -145,7 +145,7 @@
                 # if the attribute doesn't exist yet, create it here
                 clsdef = ins.classdef
             # create or update the attribute in clsdef
-            clsdef.generalize(attr, s_value, getbookkeeper(), readonly=False)
+            clsdef.generalize_attr(attr, s_value, getbookkeeper(), readonly=False)
             raise BlockedInference
         return SomeObject()
 



More information about the Pypy-commit mailing list