[pypy-svn] r20011 - pypy/branch/somepbc-refactoring/pypy/annotation

arigo at codespeak.net arigo at codespeak.net
Fri Nov 18 14:48:43 CET 2005


Author: arigo
Date: Fri Nov 18 14:48:42 2005
New Revision: 20011

Modified:
   pypy/branch/somepbc-refactoring/pypy/annotation/classdef.py
   pypy/branch/somepbc-refactoring/pypy/annotation/description.py
Log:
(pedronis, mwh, arigo)

cleaning up: removing ClassSource.



Modified: pypy/branch/somepbc-refactoring/pypy/annotation/classdef.py
==============================================================================
--- pypy/branch/somepbc-refactoring/pypy/annotation/classdef.py	(original)
+++ pypy/branch/somepbc-refactoring/pypy/annotation/classdef.py	Fri Nov 18 14:48:42 2005
@@ -73,8 +73,8 @@
         self.readonly = True
         self.read_locations = {}
 
-    def add_constant_source(self, source):
-        s_value = source.s_read_attribute(self.name)
+    def add_constant_source(self, classdef, source):
+        s_value = source.s_read_attribute(classdef, self.name)
         if source.instance_level:
             # a prebuilt instance source forces readonly=False, see above
             self.readonly = False
@@ -156,7 +156,7 @@
                 # the Attribute() exists already for this class (or a parent)
                 attrdef = cdef.attrs[attr]
                 s_prev_value = attrdef.s_value
-                attrdef.add_constant_source(source)
+                attrdef.add_constant_source(self, source)
                 # we should reflow from all the reader's position,
                 # but as an optimization we try to see if the attribute
                 # has really been generalized
@@ -176,7 +176,7 @@
                     if attr in subdef.attrs:
                         attrdef = subdef.attrs[attr]
                         s_prev_value = attrdef.s_value
-                        attrdef.add_constant_source(source)
+                        attrdef.add_constant_source(self, source)
                         if attrdef.s_value != s_prev_value:
                             attrdef.mutated(subdef) # reflow from all read positions
 
@@ -233,7 +233,7 @@
         # first remove the attribute from subclasses -- including us!
         # invariant (I)
         subclass_attrs = []
-        constant_sources = []
+        constant_sources = []    # [(classdef-of-origin, source)]
         for subdef in self.getallsubdefs():
             if attr in subdef.attrs:
                 subclass_attrs.append(subdef.attrs[attr])
@@ -241,7 +241,8 @@
             if attr in subdef.attr_sources:
                 # accumulate attr_sources for this attribute from all subclasses
                 lst = subdef.attr_sources[attr]
-                constant_sources.extend(lst)
+                for source in lst:
+                    constant_sources.append((subdef, source))
                 del lst[:]    # invariant (II)
 
         # accumulate attr_sources for this attribute from all parents, too
@@ -250,7 +251,7 @@
             if attr in superdef.attr_sources:
                 for source in superdef.attr_sources[attr]:
                     if not source.instance_level:
-                        constant_sources.append(source)
+                        constant_sources.append((superdef, source))
 
         # create the Attribute and do the generalization asked for
         newattr = Attribute(attr, self.bookkeeper)
@@ -267,8 +268,8 @@
 
         # add the values of the pending constant attributes
         # completes invariants (II) and (III)
-        for source in constant_sources:
-            newattr.add_constant_source(source)
+        for origin_classdef, source in constant_sources:
+            newattr.add_constant_source(origin_classdef, source)
 
         # reflow from all read positions
         newattr.mutated(self)
@@ -383,7 +384,7 @@
         self.bookkeeper = bookkeeper
         self.obj = obj
  
-    def s_read_attribute(self, name):
+    def s_read_attribute(self, classdef, name):
         s_value = self.bookkeeper.immutablevalue(
             self.obj.__dict__[name])
         return s_value

Modified: pypy/branch/somepbc-refactoring/pypy/annotation/description.py
==============================================================================
--- pypy/branch/somepbc-refactoring/pypy/annotation/description.py	(original)
+++ pypy/branch/somepbc-refactoring/pypy/annotation/description.py	Fri Nov 18 14:48:42 2005
@@ -1,5 +1,5 @@
 import types
-from pypy.objspace.flow.model import FunctionGraph
+from pypy.objspace.flow.model import Constant, FunctionGraph
 from pypy.interpreter.pycode import cpython_code_signature
 from pypy.interpreter.argument import ArgErr
 
@@ -147,39 +147,26 @@
         # XXX static methods
         return self.bookkeeper.getmethoddesc(self, classdef)
 
-class ClassSource:
-    instance_level = False
-
-    def __init__(self, bookkeeper, cls, classdesc):
-        self.bookkeeper = bookkeeper
-        self.cls = cls
-        self.classdesc = classdesc
-
-    def s_read_attribute(self, name):
-        s_value = self.bookkeeper.immutablevalue(
-            self.cls.__dict__[name])
-        s_value = s_value.bindcallables(self.classdesc.getuniqueclassdef())
-        return s_value
 
 class ClassDesc(Desc):
     knowntype = type
+    instance_level = False
 
-    def __init__(self, bookkeeper, pyobj=None, name=None,
-                 basedesc=None, 
-                 classsources=None, specialize=None):
+    def __init__(self, bookkeeper, pyobj=None,
+                 name=None, basedesc=None, classdict=None,
+                 specialize=None):
         super(ClassDesc, self).__init__(bookkeeper, pyobj)
 
         if pyobj is not None:
             cls = pyobj
             base = object
 
-            classsources = {}
+            classdict = {}    # {attr: Constant-or-Desc}
 
             baselist = list(cls.__bases__)
             baselist.reverse()
 
             def add_sources_for_class(cls):
-                source = ClassSource(bookkeeper, cls, self)
                 for name, value in cls.__dict__.items():
                     # ignore some special attributes
                     if name.startswith('_') and not isinstance(value, types.FunctionType):
@@ -188,7 +175,7 @@
                     if isinstance(value, types.FunctionType):
                         if not hasattr(value, 'class_'):
                             value.class_ = self.pyobj # remember that this is really a method
-                    classsources[name] = source
+                    classdict[name] = Constant(value)
 
             for b1 in baselist:
                 if b1 is object:
@@ -212,7 +199,7 @@
             
         self.name = name
         self.basedesc = basedesc
-        self.classsources = classsources
+        self.classdict = classdict
 
         if specialize is None:
             tag = pyobj.__dict__.get('_annspecialcase_', '')
@@ -237,8 +224,13 @@
                     for name, s_value in FORCE_ATTRIBUTES_INTO_CLASSES[cls].items():
                         classdef.generalize_attr(name, s_value)
                         classdef.find_attribute(name).readonly = False
-            
-            classdef.setup(self.classsources)
+
+            # register all class attributes as coming from this ClassDesc
+            # (as opposed to prebuilt instances)
+            classsources = {}
+            for attr in self.classdict:
+                classsources[attr] = self    # comes from this ClassDesc
+            classdef.setup(classsources)
         return self._classdef
 
     def pycall(self, schedule, args):
@@ -259,19 +251,31 @@
                                 " (class %s)" % (self.name,))
         return s_instance
 
+    def s_read_attribute(self, classdef, name):
+        obj = self.classdict[name]
+        if isinstance(obj, Constant):
+            s_value = self.bookkeeper.immutablevalue(obj.value)
+            s_value = s_value.bindcallables(classdef)
+        elif isinstance(obj, Desc):
+            from pypy.annotation.model import SomePBC
+            s_value = SomePBC(obj.bind(classdef))
+        else:
+            raise TypeError("classdict should not contain %r" % (obj,))
+        return s_value
+
     def find_source_for(self, name):
-        if name in self.classsources:
-            return self.classsources[name]
+        if name in self.classdict:
+            return self
         if self.pyobj is not None:
             # check whether in the case the classdesc corresponds to a real class
             # there is a new attribute
             cls = self.pyobj
             if name in cls.__dict__:
-                source = ClassSource(self.bookkeeper, cls, self)
-                self.classsources[name] = source
-                return source
+                self.classdict[name] = Constant(cls.__dict__[name])
+                return self
         return None
 
+
 class MethodDesc(Desc):
     knowntype = types.MethodType
 



More information about the Pypy-commit mailing list