[pypy-svn] r8893 - in pypy/dist/pypy: interpreter objspace/std

pedronis at codespeak.net pedronis at codespeak.net
Sat Feb 5 02:00:01 CET 2005


Author: pedronis
Date: Sat Feb  5 02:00:01 2005
New Revision: 8893

Modified:
   pypy/dist/pypy/interpreter/typedef.py
   pypy/dist/pypy/objspace/std/objspace.py
   pypy/dist/pypy/objspace/std/stdtypedef.py
   pypy/dist/pypy/objspace/std/typeobject.py
Log:
slots!



Modified: pypy/dist/pypy/interpreter/typedef.py
==============================================================================
--- pypy/dist/pypy/interpreter/typedef.py	(original)
+++ pypy/dist/pypy/interpreter/typedef.py	Sat Feb  5 02:00:01 2005
@@ -18,15 +18,25 @@
 
 
 unique_interplevel_subclass_cache = Cache()
-def get_unique_interplevel_subclass(cls):
-    return unique_interplevel_subclass_cache.getorbuild(cls, _buildusercls, None)
+def get_unique_interplevel_subclass(cls, hasdict, wants_slots):
+    return unique_interplevel_subclass_cache.getorbuild((cls, hasdict, wants_slots), _buildusercls, None)
 
-def _buildusercls(cls, ignored):
+def _buildusercls((cls, hasdict, wants_slots), ignored):
     "NOT_RPYTHON: initialization-time only"
     typedef = cls.typedef
-    name = 'User' + cls.__name__
+    name = ['User']
+    if not hasdict:
+        name.append('NoDict')
+    if wants_slots:
+        name.append('WithSlots')
+    name.append(cls.__name__)
+
+    name = ''.join(name)
+
     body = {}
 
+    no_extra_dict = typedef.hasdict or not hasdict
+
     class User_InsertNameHere(object):
 
         def getclass(self, space):
@@ -39,10 +49,18 @@
         def __del__(self):
             self.space.userdel(self)
 
-        if typedef.hasdict:
-            def user_setup(self, space, w_subtype):
+        if wants_slots:
+            def user_setup_slots(self, nslots):
+                self.slots_w = [None] * nslots 
+        else:
+            def user_setup_slots(self, nslots):
+                assert nslots == 0
+
+        if no_extra_dict:
+            def user_setup(self, space, w_subtype, nslots):
                 self.space = space
                 self.w__class__ = w_subtype
+                self.user_setup_slots(nslots)
 
         else:
             def getdict(self):
@@ -55,10 +73,11 @@
                             space.wrap("setting dictionary to a non-dict"))
                 self.w__dict__ = w_dict
 
-            def user_setup(self, space, w_subtype):
+            def user_setup(self, space, w_subtype, nslots):
                 self.space = space
                 self.w__class__ = w_subtype
                 self.w__dict__ = space.newdict([])
+                self.user_setup_slots(nslots)
 
     body = dict([(key, value)
                  for key, value in User_InsertNameHere.__dict__.items()

Modified: pypy/dist/pypy/objspace/std/objspace.py
==============================================================================
--- pypy/dist/pypy/objspace/std/objspace.py	(original)
+++ pypy/dist/pypy/objspace/std/objspace.py	Sat Feb  5 02:00:01 2005
@@ -354,9 +354,9 @@
             return instantiate(cls)
         else:
             w_type.check_user_subclass(w_subtype)
-            subcls = get_unique_interplevel_subclass(cls)
+            subcls = get_unique_interplevel_subclass(cls, w_subtype.hasdict, w_subtype.nslots != 0)
             instance = instantiate(subcls)
-            instance.user_setup(self, w_subtype)
+            instance.user_setup(self, w_subtype, w_subtype.nslots)
             return instance
 
     def unpacktuple(self, w_tuple, expected_length=None):

Modified: pypy/dist/pypy/objspace/std/stdtypedef.py
==============================================================================
--- pypy/dist/pypy/objspace/std/stdtypedef.py	(original)
+++ pypy/dist/pypy/objspace/std/stdtypedef.py	Sat Feb  5 02:00:01 2005
@@ -1,10 +1,10 @@
 from pypy.interpreter import eval, function, gateway
 from pypy.interpreter.error import OperationError
-from pypy.interpreter.typedef import TypeDef, GetSetProperty
+from pypy.interpreter.typedef import TypeDef, GetSetProperty, Member
 from pypy.objspace.std.multimethod import MultiMethod, FailedToImplement
 
 __all__ = ['StdTypeDef', 'newmethod', 'gateway',
-           'GetSetProperty', 'attrproperty', 'attrproperty_w',
+           'GetSetProperty', 'Member', 'attrproperty', 'attrproperty_w',
            'MultiMethod']
 
 

Modified: pypy/dist/pypy/objspace/std/typeobject.py
==============================================================================
--- pypy/dist/pypy/objspace/std/typeobject.py	(original)
+++ pypy/dist/pypy/objspace/std/typeobject.py	Sat Feb  5 02:00:01 2005
@@ -2,7 +2,7 @@
 from pypy.interpreter.function import Function, StaticMethod
 from pypy.interpreter.argument import Arguments
 from pypy.interpreter import gateway
-from pypy.objspace.std.stdtypedef import std_dict_descr, issubtypedef
+from pypy.objspace.std.stdtypedef import std_dict_descr, issubtypedef, Member
 from pypy.objspace.std.objecttype import object_typedef
 
 class W_TypeObject(W_Object):
@@ -15,6 +15,7 @@
         w_self.bases_w = bases_w
         w_self.dict_w = dict_w
         w_self.ensure_static__new__()
+        w_self.nslots = 0
 
         if overridetypedef is not None:
             w_self.instancetypedef = overridetypedef
@@ -22,7 +23,6 @@
         else:
             # find the most specific typedef
             instancetypedef = object_typedef
-            w_self.hasdict = False
             for w_base in bases_w:
                 if not space.is_true(space.isinstance(w_base, space.w_type)):
                     continue
@@ -32,11 +32,59 @@
                     raise OperationError(space.w_TypeError,
                                 space.wrap("instance layout conflicts in "
                                                     "multiple inheritance"))
-                w_self.hasdict = w_self.hasdict or w_base.hasdict
             w_self.instancetypedef = instancetypedef
-            if not w_self.hasdict:
+            w_self.hasdict = False
+            hasoldstylebase = False
+            w_most_derived_base_with_slots = None
+            for w_base in bases_w:
+                if not space.is_true(space.isinstance(w_base, space.w_type)):
+                    hasoldstylebase = True
+                    continue
+                if w_base.nslots != 0:
+                    if w_base_with_slots is None:
+                        w_base_with_slots = w_base
+                    else:
+                        if space.is_true(space.issubtype(w_base, w_most_derived_base_with_slots)):
+                            w_most_derived_base_with_slots = w_base
+                        elif not space.is_true(space.issubtype(w_most_derived_base_with_slots, w_base)):
+                            raise OperationError(space.w_TypeError,
+                                                 space.wrap("instance layout conflicts in "
+                                                            "multiple inheritance"))
+                w_self.hasdict = w_self.hasdict or w_base.hasdict
+            if w_most_derived_base_with_slots:
+                nslots = w_most_derived_base_with_slots.nslots
+            else:
+                nslots = 0
+  
+            wantdict = True
+            if '__slots__' in dict_w:
+                wantdict = False
+
+                w_slots = dict_w['__slots__']
+                if space.is_true(space.isinstance(w_slots, space.w_str)):
+                    slot_names_w = [w_slots]
+                else:
+                    slot_names_w = space.unpackiterable(w_slots)
+                for w_slot_name in slot_names_w:
+                    slot_name = space.str_w(w_slot_name)
+                    if slot_name == '__dict__':
+                        if wantdict or w_self.hasdict:
+                            raise OperationError(space.w_TypeError,
+                                                 space.wrap("__dict__ slot disallowed: we already got one"))
+                        wantdict = True
+                    else:
+                        # create member
+                        w_self.dict_w[slot_name] = space.wrap(Member(nslots, slot_name))
+                        nslots += 1
+
+            w_self.nslots = nslots
+                        
+            wantdict = wantdict or hasoldstylebase
+
+            if wantdict and not w_self.hasdict:
                 w_self.dict_w['__dict__'] = space.wrap(std_dict_descr)
-                w_self.hasdict = True                
+                w_self.hasdict = True
+               
             w_type = space.type(w_self)
             if not space.is_true(space.is_(w_type, space.w_type)):
                 mro_func = w_type.lookup('mro')



More information about the Pypy-commit mailing list