[pypy-svn] r68776 - in pypy/branch/shrink-multidict/pypy: interpreter objspace objspace/std objspace/std/test

cfbolz at codespeak.net cfbolz at codespeak.net
Tue Oct 27 10:01:25 CET 2009


Author: cfbolz
Date: Tue Oct 27 10:01:24 2009
New Revision: 68776

Modified:
   pypy/branch/shrink-multidict/pypy/interpreter/baseobjspace.py
   pypy/branch/shrink-multidict/pypy/interpreter/mixedmodule.py
   pypy/branch/shrink-multidict/pypy/interpreter/typedef.py
   pypy/branch/shrink-multidict/pypy/objspace/descroperation.py
   pypy/branch/shrink-multidict/pypy/objspace/std/callmethod.py
   pypy/branch/shrink-multidict/pypy/objspace/std/celldict.py
   pypy/branch/shrink-multidict/pypy/objspace/std/dictmultiobject.py
   pypy/branch/shrink-multidict/pypy/objspace/std/inlinedict.py
   pypy/branch/shrink-multidict/pypy/objspace/std/objspace.py
   pypy/branch/shrink-multidict/pypy/objspace/std/proxyobject.py
   pypy/branch/shrink-multidict/pypy/objspace/std/sharingdict.py
   pypy/branch/shrink-multidict/pypy/objspace/std/test/test_dictmultiobject.py
   pypy/branch/shrink-multidict/pypy/objspace/std/test/test_inlinedict.py
Log:
A new shortcut in the objspace: finditem_str that takes an unwrapped string as
the item to find. Please don't ask me how I managed to triplicate the content of
inlinedict.py and test_inlinedict.py


Modified: pypy/branch/shrink-multidict/pypy/interpreter/baseobjspace.py
==============================================================================
--- pypy/branch/shrink-multidict/pypy/interpreter/baseobjspace.py	(original)
+++ pypy/branch/shrink-multidict/pypy/interpreter/baseobjspace.py	Tue Oct 27 10:01:24 2009
@@ -26,7 +26,10 @@
         return None
 
     def getdictvalue_w(self, space, attr):
-        return self.getdictvalue(space, space.wrap(attr))
+        w_dict = self.getdict()
+        if w_dict is not None:
+            return space.finditem_str(w_dict, attr)
+        return None
 
     def getdictvalue(self, space, w_attr):
         w_dict = self.getdict()
@@ -34,8 +37,8 @@
             return space.finditem(w_dict, w_attr)
         return None
 
-    def getdictvalue_attr_is_in_class(self, space, w_attr):
-        return self.getdictvalue(space, w_attr)
+    def getdictvalue_attr_is_in_class(self, space, attr):
+        return self.getdictvalue_w(space, attr)
 
     def setdictvalue(self, space, w_attr, w_value, shadows_type=True):
         w_dict = self.getdict()
@@ -551,12 +554,6 @@
     # that can be defined in term of more primitive ones.  Subclasses
     # may also override specific functions for performance.
 
-    #def is_(self, w_x, w_y):   -- not really useful.  Must be subclassed
-    #    "'x is y'."
-    #    w_id_x = self.id(w_x)
-    #    w_id_y = self.id(w_y)
-    #    return self.eq(w_id_x, w_id_y)
-
     def not_(self, w_obj):
         return self.wrap(not self.is_true(w_obj))
 
@@ -575,6 +572,9 @@
     def set_str_keyed_item(self, w_obj, w_key, w_value, shadows_type=True):
         return self.setitem(w_obj, w_key, w_value)
 
+    def finditem_str(self, w_obj, key):
+        return self.finditem(w_obj, self.wrap(key))
+
     def finditem(self, w_obj, w_key):
         try:
             return self.getitem(w_obj, w_key)

Modified: pypy/branch/shrink-multidict/pypy/interpreter/mixedmodule.py
==============================================================================
--- pypy/branch/shrink-multidict/pypy/interpreter/mixedmodule.py	(original)
+++ pypy/branch/shrink-multidict/pypy/interpreter/mixedmodule.py	Tue Oct 27 10:01:24 2009
@@ -41,33 +41,43 @@
         w_builtin = self.get(name) 
         return self.space.call_function(w_builtin, *args_w)
 
+    def getdictvalue_w(self, space, name):
+        w_value = space.finditem_str(self.w_dict, name)
+        if self.lazy and w_value is None:
+            return self._load_lazily(space, name)
+        return w_value
+
     def getdictvalue(self, space, w_name):
         w_value = space.finditem(self.w_dict, w_name)
         if self.lazy and w_value is None:
-            name = space.str_w(w_name)
-            w_name = space.new_interned_w_str(w_name)
-            try: 
-                loader = self.loaders[name]
-            except KeyError: 
-                return None 
-            else: 
-                #print "trying to load", name
-                w_value = loader(space) 
-                #print "loaded", w_value 
-                # obscure
-                func = space.interpclass_w(w_value)
-                if type(func) is Function:
-                    try:
-                        bltin = func._builtinversion_
-                    except AttributeError:
-                        bltin = BuiltinFunction(func)
-                        bltin.w_module = self.w_name
-                        func._builtinversion_ = bltin
-                        bltin.name = name
-                    w_value = space.wrap(bltin)
-                space.setitem(self.w_dict, w_name, w_value) 
+            return self._load_lazily(space, space.str_w(w_name))
         return w_value
 
+    def _load_lazily(self, space, name):
+        w_name = space.new_interned_str(name)
+        try: 
+            loader = self.loaders[name]
+        except KeyError: 
+            return None 
+        else: 
+            #print "trying to load", name
+            w_value = loader(space) 
+            #print "loaded", w_value 
+            # obscure
+            func = space.interpclass_w(w_value)
+            if type(func) is Function:
+                try:
+                    bltin = func._builtinversion_
+                except AttributeError:
+                    bltin = BuiltinFunction(func)
+                    bltin.w_module = self.w_name
+                    func._builtinversion_ = bltin
+                    bltin.name = name
+                w_value = space.wrap(bltin)
+            space.setitem(self.w_dict, w_name, w_value) 
+            return w_value
+
+
     def getdict(self): 
         if self.lazy: 
             space = self.space

Modified: pypy/branch/shrink-multidict/pypy/interpreter/typedef.py
==============================================================================
--- pypy/branch/shrink-multidict/pypy/interpreter/typedef.py	(original)
+++ pypy/branch/shrink-multidict/pypy/interpreter/typedef.py	Tue Oct 27 10:01:24 2009
@@ -274,12 +274,12 @@
                 if space.config.objspace.std.withshadowtracking:
                     self.w__dict__.set_shadows_anything()
 
-            def getdictvalue_attr_is_in_class(self, space, w_name):
+            def getdictvalue_attr_is_in_class(self, space, name):
                 w_dict = self.w__dict__
                 if space.config.objspace.std.withshadowtracking:
                     if not w_dict.shadows_anything():
                         return None
-                return space.finditem(w_dict, w_name)
+                return space.finditem_str(w_dict, name)
 
         add(Proto)
 

Modified: pypy/branch/shrink-multidict/pypy/objspace/descroperation.py
==============================================================================
--- pypy/branch/shrink-multidict/pypy/objspace/descroperation.py	(original)
+++ pypy/branch/shrink-multidict/pypy/objspace/descroperation.py	Tue Oct 27 10:01:24 2009
@@ -36,9 +36,9 @@
         if w_descr is not None:
             if space.is_data_descr(w_descr):
                 return space.get(w_descr, w_obj)
-            w_value = w_obj.getdictvalue_attr_is_in_class(space, w_name)
+            w_value = w_obj.getdictvalue_attr_is_in_class(space, name)
         else:
-            w_value = w_obj.getdictvalue(space, w_name)
+            w_value = w_obj.getdictvalue_w(space, name)
         if w_value is not None:
             return w_value
         if w_descr is not None:

Modified: pypy/branch/shrink-multidict/pypy/objspace/std/callmethod.py
==============================================================================
--- pypy/branch/shrink-multidict/pypy/objspace/std/callmethod.py	(original)
+++ pypy/branch/shrink-multidict/pypy/objspace/std/callmethod.py	Tue Oct 27 10:01:24 2009
@@ -54,9 +54,9 @@
         if w_descr is None:
             # this handles directly the common case
             #   module.function(args..)
-            w_value = w_obj.getdictvalue(space, w_name)
+            w_value = w_obj.getdictvalue_w(space, name)
         elif type(w_descr) is function.Function:
-            w_value = w_obj.getdictvalue_attr_is_in_class(space, w_name)
+            w_value = w_obj.getdictvalue_attr_is_in_class(space, name)
             if w_value is None:
                 # fast method path: a function object in the class,
                 # nothing in the instance
@@ -85,15 +85,15 @@
     """An optimized version of space.call_method()
     based on the same principle as above.
     """
-    w_name = space.wrap(methname)
     w_getattribute = space.lookup(w_obj, '__getattribute__')
     if w_getattribute is object_getattribute(space):
         w_descr = space.lookup(w_obj, methname)
         if type(w_descr) is function.Function:
-            w_value = w_obj.getdictvalue_attr_is_in_class(space, w_name)
+            w_value = w_obj.getdictvalue_attr_is_in_class(space, methname)
             if w_value is None:
                 # fast method path: a function object in the class,
                 # nothing in the instance
                 return space.call_function(w_descr, w_obj, *arg_w)
+    w_name = space.wrap(methname)
     w_meth = space.getattr(w_obj, w_name)
     return space.call_function(w_meth, *arg_w)

Modified: pypy/branch/shrink-multidict/pypy/objspace/std/celldict.py
==============================================================================
--- pypy/branch/shrink-multidict/pypy/objspace/std/celldict.py	(original)
+++ pypy/branch/shrink-multidict/pypy/objspace/std/celldict.py	Tue Oct 27 10:01:24 2009
@@ -80,15 +80,19 @@
         space = self.space
         w_lookup_type = space.type(w_lookup)
         if space.is_w(w_lookup_type, space.w_str):
-            res = self.getcell(space.str_w(w_lookup), False)
-            if res is None:
-                return None
-            return res.w_value
+            return self.impl_getitem_str(space.str_w(w_lookup))
+
         elif _is_sane_hash(space, w_lookup_type):
             return None
         else:
             return self._as_rdict().getitem(w_lookup)
 
+    def impl_getitem_str(self, lookup):
+        res = self.getcell(lookup, False)
+        if res is None:
+            return None
+        return res.w_value
+
     def impl_iter(self):
         return ModuleDictIteratorImplementation(self.space, self)
 

Modified: pypy/branch/shrink-multidict/pypy/objspace/std/dictmultiobject.py
==============================================================================
--- pypy/branch/shrink-multidict/pypy/objspace/std/dictmultiobject.py	(original)
+++ pypy/branch/shrink-multidict/pypy/objspace/std/dictmultiobject.py	Tue Oct 27 10:01:24 2009
@@ -109,6 +109,10 @@
         #return w_value or None
         raise NotImplementedError("abstract base class")
 
+    def impl_getitem_str(self, w_key):
+        #return w_value or None
+        raise NotImplementedError("abstract base class")
+
     def impl_setitem_str(self,  w_key, w_value, shadows_type=True):
         raise NotImplementedError("abstract base class")
 
@@ -159,8 +163,8 @@
     # CALL_LIKELY_BUILTIN opcode is set. Otherwise it won't even be seen
     # by the annotator
     def impl_get_builtin_indexed(self, i):
-        w_key = self.space.wrap(OPTIMIZED_BUILTINS[i])
-        return self.impl_getitem(w_key)
+        key = OPTIMIZED_BUILTINS[i]
+        return self.impl_getitem_str(key)
 
     # this method will only be seen whan a certain config option is used
     def impl_shadows_anything(self):
@@ -187,6 +191,9 @@
     def impl_fallback_getitem(self, w_key):
         return self.r_dict_content.get(w_key, None)
 
+    def impl_fallback_getitem_str(self, key):
+        return self.r_dict_content.get(self.space.wrap(key), None)
+
     def impl_fallback_iter(self):
         return RDictIteratorImplementation(self.space, self)
 
@@ -202,8 +209,8 @@
         self.r_dict_content.clear()
 
     def impl_fallback_get_builtin_indexed(self, i):
-        w_key = self.space.wrap(OPTIMIZED_BUILTINS[i])
-        return self.impl_fallback_getitem(w_key)
+        key = OPTIMIZED_BUILTINS[i]
+        return self.impl_fallback_getitem_str(key)
 
     def impl_fallback_shadows_anything(self):
         return True
@@ -214,6 +221,7 @@
 
 implementation_methods = [
     ("getitem", 1),
+    ("getitem_str", 1),
     ("length", 0),
     ("setitem_str", 3),
     ("setitem", 2),
@@ -322,15 +330,18 @@
     def impl_length(self):
         return len(self.content)
 
+    def impl_getitem_str(self, key):
+        return self.content.get(key, None)
+
     def impl_getitem(self, w_key):
         space = self.space
         # -- This is called extremely often.  Hack for performance --
         if type(w_key) is space.StringObjectCls:
-            return self.content.get(w_key.unwrap(space), None)
+            return self.impl_getitem_str(w_key.unwrap(space))
         # -- End of performance hack --
         w_lookup_type = space.type(w_key)
         if space.is_w(w_lookup_type, space.w_str):
-            return self.content.get(space.str_w(w_key), None)
+            return self.impl_getitem_str(space.str_w(w_key))
         elif _is_sane_hash(space, w_lookup_type):
             return None
         else:
@@ -562,6 +573,8 @@
     def impl_length(self):
         self.info.lengths += 1
         return len(self.content)
+    def impl_getitem_str(self, key):
+        return self.impl_getitem(self.space.wrap(key))
     def impl_getitem(self, w_key):
         self.info.gets += 1
         self._read(w_key)

Modified: pypy/branch/shrink-multidict/pypy/objspace/std/inlinedict.py
==============================================================================
--- pypy/branch/shrink-multidict/pypy/objspace/std/inlinedict.py	(original)
+++ pypy/branch/shrink-multidict/pypy/objspace/std/inlinedict.py	Tue Oct 27 10:01:24 2009
@@ -84,285 +84,6 @@
             return getattr(self, attrname) is not None
 
         def getdictvalue_w(self, space, attr):
-            return self.getdictvalue(space, space.wrap(attr))
-
-        def getdictvalue(self, space, w_attr):
-            if self._inlined_dict_valid():
-                return self.impl_getitem(w_attr)
-            w_dict = self.getdict()
-            return w_dict.getitem(w_attr)
-
-        def getdictvalue_attr_is_in_class(self, space, w_attr):
-            return self.getdictvalue(space, w_attr)
-
-        def setdictvalue(self, space, w_attr, w_value, shadows_type=True):
-            if self._inlined_dict_valid():
-                # XXX don't ignore shadows_type
-                self.impl_setitem(w_attr, w_value)
-                return True
-            w_dict = self.getdict()
-            w_dict.setitem(w_attr, w_value)
-            return True
-
-        def deldictvalue(self, space, w_attr):
-            if self._inlined_dict_valid():
-                try:
-                    self.impl_delitem(w_attr)
-                except KeyError:
-                    return False
-                return True
-            w_dict = self.getdict()
-            try:
-                w_dict.delitem(w_attr)
-            except KeyError:
-                return False
-            return True
-
-        def setdict(self, space, w_dict):
-            make_rdict(self) # invalidate attributes on self
-            self.w__dict__ = check_new_dictionary(space, w_dict)
-
-        def _as_rdict(self):
-            make_rdict(self)
-            return self.getdict()
-
-        def initialize_as_rdict(self):
-            return self.getdict().initialize_as_rdict()
-
-    for methname, _ in implementation_methods:
-        implname = "impl_" + methname
-        meth = func_with_new_name(getattr(dictimplclass, implname).im_func,
-                                  implname)
-        if not hasattr(InlineDictMixin, implname):
-            setattr(InlineDictMixin, implname, meth)
-    return InlineDictMixin
-import py
-from pypy.interpreter.typedef import check_new_dictionary
-from pypy.objspace.std.dictmultiobject import W_DictMultiObject
-from pypy.objspace.std.dictmultiobject import StrDictImplementation
-from pypy.objspace.std.dictmultiobject import IteratorImplementation
-from pypy.objspace.std.dictmultiobject import implementation_methods
-from pypy.tool.sourcetools import func_with_new_name
-
-def make_mixin(config):
-    if config.objspace.std.withsharingdict:
-        from pypy.objspace.std.sharingdict import SharedDictImplementation
-        return make_inlinedict_mixin(SharedDictImplementation, "structure")
-    else:
-        return make_inlinedict_mixin(StrDictImplementation, "content")
-
-def make_indirection_method(methname, numargs):
-    # *args don't work, the call normalization gets confused
-    args = ", ".join(["a" + str(i) for i in range(numargs)])
-    code = """def f(self, %s):
-    return self.w_obj.%s(%s)
-""" % (args, methname, args)
-    d = {}
-    exec py.code.Source(code).compile() in d
-    func = d["f"]
-    func.func_name = methname + "_indirect"
-    func.func_defaults = getattr(W_DictMultiObject, methname).func_defaults
-    return func
-
-def make_inlinedict_mixin(dictimplclass, attrname):
-    assert dictimplclass.__base__ is W_DictMultiObject
-    class IndirectionIterImplementation(IteratorImplementation):
-        def __init__(self, space, dictimpl, itemlist):
-            IteratorImplementation.__init__(self, space, dictimpl)
-            self.itemlist = itemlist
-
-        def next_entry(self):
-            return self.itemlist[self.pos]
-            
-    class IndirectionDictImplementation(W_DictMultiObject):
-        def __init__(self, space, w_obj):
-            self.space = space
-            self.w_obj = w_obj
-
-        def impl_iter(self):
-            # XXX sucky
-            items = []
-            for w_item in self.impl_items():
-                w_key, w_value = self.space.viewiterable(w_item)
-                items.append((w_key, w_value))
-            return IndirectionIterImplementation(self.space, self, items)
-
-    IndirectionDictImplementation.__name__ = "IndirectionDictImplementation" + dictimplclass.__name__
-
-    for methname, numargs in implementation_methods:
-        implname = "impl_" + methname
-        if implname != "impl_iter":
-            setattr(IndirectionDictImplementation, implname,
-                    make_indirection_method(implname, numargs))
-
-    init_dictattributes = func_with_new_name(dictimplclass.__init__.im_func,
-                                             "init_dictattributes")
-    make_rdict = func_with_new_name(dictimplclass._as_rdict.im_func,
-                                    "make_rdict")
-
-    class InlineDictMixin(object):
-
-        def user_setup(self, space, w_subtype):
-            self.space = space
-            self.w__class__ = w_subtype
-            self.w__dict__ = None
-            init_dictattributes(self, space)
-            assert getattr(self, attrname) is not None
-            self.user_setup_slots(w_subtype.nslots)
-
-        def getdict(self):
-            w__dict__ = self.w__dict__
-            if w__dict__ is None:
-                w__dict__ = IndirectionDictImplementation(self.space, self)
-                self.w__dict__ = w__dict__
-            assert isinstance(w__dict__, W_DictMultiObject)
-            return w__dict__
-
-        def _inlined_dict_valid(self):
-            return getattr(self, attrname) is not None
-
-        def getdictvalue_w(self, space, attr):
-            if self._inlined_dict_valid():
-                return self.impl_getitem_str(attr)
-            w_dict = self.getdict()
-            return w_dict.getitem_str(attr)
-
-        def getdictvalue(self, space, w_attr):
-            if self._inlined_dict_valid():
-                return self.impl_getitem(w_attr)
-            w_dict = self.getdict()
-            return w_dict.getitem(w_attr)
-
-        def getdictvalue_attr_is_in_class(self, space, attr):
-            return self.getdictvalue_w(space, attr)
-
-        def setdictvalue(self, space, w_attr, w_value, shadows_type=True):
-            if self._inlined_dict_valid():
-                # XXX don't ignore shadows_type
-                self.impl_setitem(w_attr, w_value)
-                return True
-            w_dict = self.getdict()
-            w_dict.setitem(w_attr, w_value)
-            return True
-
-        def deldictvalue(self, space, w_attr):
-            if self._inlined_dict_valid():
-                try:
-                    self.impl_delitem(w_attr)
-                except KeyError:
-                    return False
-                return True
-            w_dict = self.getdict()
-            try:
-                w_dict.delitem(w_attr)
-            except KeyError:
-                return False
-            return True
-
-        def setdict(self, space, w_dict):
-            make_rdict(self) # invalidate attributes on self
-            self.w__dict__ = check_new_dictionary(space, w_dict)
-
-        def _as_rdict(self):
-            make_rdict(self)
-            return self.getdict()
-
-        def initialize_as_rdict(self):
-            return self.getdict().initialize_as_rdict()
-
-    for methname, _ in implementation_methods:
-        implname = "impl_" + methname
-        meth = func_with_new_name(getattr(dictimplclass, implname).im_func,
-                                  implname)
-        if not hasattr(InlineDictMixin, implname):
-            setattr(InlineDictMixin, implname, meth)
-    return InlineDictMixin
-import py
-from pypy.interpreter.typedef import check_new_dictionary
-from pypy.objspace.std.dictmultiobject import W_DictMultiObject
-from pypy.objspace.std.dictmultiobject import StrDictImplementation
-from pypy.objspace.std.dictmultiobject import IteratorImplementation
-from pypy.objspace.std.dictmultiobject import implementation_methods
-from pypy.tool.sourcetools import func_with_new_name
-
-def make_mixin(config):
-    if config.objspace.std.withsharingdict:
-        from pypy.objspace.std.sharingdict import SharedDictImplementation
-        return make_inlinedict_mixin(SharedDictImplementation, "structure")
-    else:
-        return make_inlinedict_mixin(StrDictImplementation, "content")
-
-def make_indirection_method(methname, numargs):
-    # *args don't work, the call normalization gets confused
-    args = ", ".join(["a" + str(i) for i in range(numargs)])
-    code = """def f(self, %s):
-    return self.w_obj.%s(%s)
-""" % (args, methname, args)
-    d = {}
-    exec py.code.Source(code).compile() in d
-    func = d["f"]
-    func.func_name = methname + "_indirect"
-    func.func_defaults = getattr(W_DictMultiObject, methname).func_defaults
-    return func
-
-def make_inlinedict_mixin(dictimplclass, attrname):
-    assert dictimplclass.__base__ is W_DictMultiObject
-    class IndirectionIterImplementation(IteratorImplementation):
-        def __init__(self, space, dictimpl, itemlist):
-            IteratorImplementation.__init__(self, space, dictimpl)
-            self.itemlist = itemlist
-
-        def next_entry(self):
-            return self.itemlist[self.pos]
-            
-    class IndirectionDictImplementation(W_DictMultiObject):
-        def __init__(self, space, w_obj):
-            self.space = space
-            self.w_obj = w_obj
-
-        def impl_iter(self):
-            # XXX sucky
-            items = []
-            for w_item in self.impl_items():
-                w_key, w_value = self.space.viewiterable(w_item)
-                items.append((w_key, w_value))
-            return IndirectionIterImplementation(self.space, self, items)
-
-    IndirectionDictImplementation.__name__ = "IndirectionDictImplementation" + dictimplclass.__name__
-
-    for methname, numargs in implementation_methods:
-        implname = "impl_" + methname
-        if implname != "impl_iter":
-            setattr(IndirectionDictImplementation, implname,
-                    make_indirection_method(implname, numargs))
-
-    init_dictattributes = func_with_new_name(dictimplclass.__init__.im_func,
-                                             "init_dictattributes")
-    make_rdict = func_with_new_name(dictimplclass._as_rdict.im_func,
-                                    "make_rdict")
-
-    class InlineDictMixin(object):
-
-        def user_setup(self, space, w_subtype):
-            self.space = space
-            self.w__class__ = w_subtype
-            self.w__dict__ = None
-            init_dictattributes(self, space)
-            assert getattr(self, attrname) is not None
-            self.user_setup_slots(w_subtype.nslots)
-
-        def getdict(self):
-            w__dict__ = self.w__dict__
-            if w__dict__ is None:
-                w__dict__ = IndirectionDictImplementation(self.space, self)
-                self.w__dict__ = w__dict__
-            assert isinstance(w__dict__, W_DictMultiObject)
-            return w__dict__
-
-        def _inlined_dict_valid(self):
-            return getattr(self, attrname) is not None
-
-        def getdictvalue_w(self, space, attr):
             if self._inlined_dict_valid():
                 return self.impl_getitem_str(attr)
             w_dict = self.getdict()

Modified: pypy/branch/shrink-multidict/pypy/objspace/std/objspace.py
==============================================================================
--- pypy/branch/shrink-multidict/pypy/objspace/std/objspace.py	(original)
+++ pypy/branch/shrink-multidict/pypy/objspace/std/objspace.py	Tue Oct 27 10:01:24 2009
@@ -708,7 +708,7 @@
         e = None
         if w_descr is not None:
             if not self.is_data_descr(w_descr):
-                w_value = w_obj.getdictvalue_attr_is_in_class(self, w_name)
+                w_value = w_obj.getdictvalue_attr_is_in_class(self, name)
                 if w_value is not None:
                     return w_value
             try:
@@ -717,7 +717,7 @@
                 if not e.match(self, self.w_AttributeError):
                     raise
         else:
-            w_value = w_obj.getdictvalue(self, w_name)
+            w_value = w_obj.getdictvalue_w(self, name)
             if w_value is not None:
                 return w_value
 
@@ -729,6 +729,13 @@
         else:
             raiseattrerror(self, w_obj, name)
 
+    def finditem_str(self, w_obj, key):
+        # performance shortcut to avoid creating the OperationError(KeyError)
+        if (isinstance(w_obj, self.DictObjectCls) and
+                not w_obj.user_overridden_class):
+            return w_obj.getitem_str(key)
+        return ObjSpace.finditem_str(self, w_obj, key)
+
     def finditem(self, w_obj, w_key):
         # performance shortcut to avoid creating the OperationError(KeyError)
         if (isinstance(w_obj, self.DictObjectCls) and

Modified: pypy/branch/shrink-multidict/pypy/objspace/std/proxyobject.py
==============================================================================
--- pypy/branch/shrink-multidict/pypy/objspace/std/proxyobject.py	(original)
+++ pypy/branch/shrink-multidict/pypy/objspace/std/proxyobject.py	Tue Oct 27 10:01:24 2009
@@ -34,6 +34,9 @@
             raise OperationError(space.w_TypeError,
                                  space.wrap("You cannot override __class__ for transparent proxies"))
         
+        def getdictvalue_w(self, space, attr):
+            return self.getdictvalue(space, space.wrap(attr))
+
         def getdictvalue(self, space, w_attr):
             try:
                 return space.call_function(self.w_controller, space.wrap('__getattribute__'),

Modified: pypy/branch/shrink-multidict/pypy/objspace/std/sharingdict.py
==============================================================================
--- pypy/branch/shrink-multidict/pypy/objspace/std/sharingdict.py	(original)
+++ pypy/branch/shrink-multidict/pypy/objspace/std/sharingdict.py	Tue Oct 27 10:01:24 2009
@@ -85,15 +85,17 @@
         space = self.space
         w_lookup_type = space.type(w_lookup)
         if space.is_w(w_lookup_type, space.w_str):
-            lookup = space.str_w(w_lookup)
-            i = self.structure.lookup_position(lookup)
-            if i == -1:
-                return None
-            return self.entries[i]
+            return self.impl_getitem_str(space.str_w(w_lookup))
         elif _is_sane_hash(space, w_lookup_type):
             return None
         else:
-            return self._as_rdict().get(w_lookup)
+            return self._as_rdict().getitem(w_lookup)
+
+    def impl_getitem_str(self, lookup):
+        i = self.structure.lookup_position(lookup)
+        if i == -1:
+            return None
+        return self.entries[i]
 
     def impl_setitem(self, w_key, w_value):
         space = self.space
@@ -155,9 +157,9 @@
         return [space.newtuple([space.wrap(key), self.entries[item]])
                     for (key, item) in self.structure.keys.iteritems()]
     def impl_clear(self):
-        SharedDictImplementation.__init__(self, self.space)
-
-
+        space = self.space
+        self.structure = space.fromcache(State).empty_structure
+        self.entries = space.fromcache(State).emptylist
     def _as_rdict(self):
         r_dict_content = self.initialize_as_rdict()
         for k, i in self.structure.keys.items():

Modified: pypy/branch/shrink-multidict/pypy/objspace/std/test/test_dictmultiobject.py
==============================================================================
--- pypy/branch/shrink-multidict/pypy/objspace/std/test/test_dictmultiobject.py	(original)
+++ pypy/branch/shrink-multidict/pypy/objspace/std/test/test_dictmultiobject.py	Tue Oct 27 10:01:24 2009
@@ -550,7 +550,11 @@
         raises(KeyError, "d['def']")
 
 
-class C: pass
+
+class FakeString(str):
+    def unwrap(self, space):
+        self.unwrapped = True
+        return str(self)
 
 # the minimal 'space' needed to use a W_DictMultiObject
 class FakeSpace:
@@ -600,7 +604,7 @@
 
     w_StopIteration = StopIteration
     w_None = None
-    StringObjectCls = None  # xxx untested: shortcut in StrDictImpl.getitem
+    StringObjectCls = FakeString
     w_dict = None
     iter = iter
     viewiterable = list
@@ -658,12 +662,14 @@
         self.impl.setitem(self.string, 1000)
         assert self.impl.length() == 1
         assert self.impl.getitem(self.string) == 1000
+        assert self.impl.getitem_str(self.string) == 1000
         self.check_not_devolved()
 
     def test_setitem_str(self):
         self.impl.setitem_str(self.fakespace.str_w(self.string), 1000)
         assert self.impl.length() == 1
         assert self.impl.getitem(self.string) == 1000
+        assert self.impl.getitem_str(self.string) == 1000
         self.check_not_devolved()
 
     def test_delitem(self):
@@ -719,6 +725,12 @@
 class TestStrDictImplementation(BaseTestRDictImplementation):
     ImplementionClass = StrDictImplementation
 
+    def test_str_shortcut(self):
+        self.fill_impl()
+        s = FakeString(self.string)
+        assert self.impl.getitem(s) == 1000
+        assert s.unwrapped
+
 ## class TestMeasuringDictImplementation(BaseTestRDictImplementation):
 ##     ImplementionClass = MeasuringDictImplementation
 ##     DevolvedClass = MeasuringDictImplementation
@@ -736,7 +748,6 @@
     ImplementionClass = SharedDictImplementation
 
 
-
 class BaseTestDevolvedDictImplementation(BaseTestRDictImplementation):
     def fill_impl(self):
         BaseTestRDictImplementation.fill_impl(self)

Modified: pypy/branch/shrink-multidict/pypy/objspace/std/test/test_inlinedict.py
==============================================================================
--- pypy/branch/shrink-multidict/pypy/objspace/std/test/test_inlinedict.py	(original)
+++ pypy/branch/shrink-multidict/pypy/objspace/std/test/test_inlinedict.py	Tue Oct 27 10:01:24 2009
@@ -125,257 +125,3 @@
         assert self.space.int_w(w_a.content['x']) == 12
         assert self.space.int_w(w_a.content['y']) == 13
 
-from pypy.conftest import gettestobjspace
-from pypy.objspace.std.inlinedict import make_inlinedict_mixin
-from pypy.objspace.std.dictmultiobject import StrDictImplementation
-from pypy.objspace.std.test.test_dictmultiobject import FakeSpace
-from pypy.objspace.std.test.test_dictmultiobject import BaseTestRDictImplementation
-from pypy.objspace.std.sharingdict import SharedDictImplementation
-
-class FakeSubtype:
-    nslots = 0
-
-class TestMixin(object):
-    Mixin = make_inlinedict_mixin(StrDictImplementation, "content")
-    class FakeObject(Mixin):
-        def user_setup_slots(self, nslots):
-            pass
-
-    fakespace = FakeSpace()
-
-    def make_obj(self):
-        obj = self.FakeObject()
-        obj.user_setup(self.fakespace, FakeSubtype)
-        obj.setdictvalue(self.fakespace, "hello", 1)
-        obj.setdictvalue(self.fakespace, "world", 2)
-        assert obj._inlined_dict_valid()
-        assert obj.w__dict__ is None
-        return obj
-
-    def test_setgetdel_dictvalue(self):
-        obj = self.make_obj()
-        assert obj.getdictvalue(self.fakespace, "hello") == 1
-        assert obj.getdictvalue(self.fakespace, "world") == 2
-        assert obj.getdictvalue(self.fakespace, "bla") is None
-        assert not obj.deldictvalue(self.fakespace, "bla")
-        obj.deldictvalue(self.fakespace, "world")
-        assert obj.getdictvalue(self.fakespace, "world") is None
-        obj.deldictvalue(self.fakespace, "hello")
-        assert obj.getdictvalue(self.fakespace, "hello") is None
-
-
-    def test_getdict(self):
-        obj = self.make_obj()
-        w_dict = obj.getdict()
-        assert obj.getdict() or w_dict # always get the same dict
-        assert obj.w__dict__ is w_dict
-
-        assert w_dict.getitem("hello") == 1
-        assert w_dict.getitem("world") == 2
-        w_dict.setitem("hello", 4)
-        w_dict.setitem("world", 5)
-        assert obj.getdictvalue(self.fakespace, "hello") == 4
-        assert obj.getdictvalue(self.fakespace, "world") == 5
-
-    def test_dict_devolves_via_object(self):
-        obj = self.make_obj()
-        obj.setdictvalue(self.fakespace, 4, 1)
-        obj.setdictvalue(self.fakespace, 5, 2)
-        w_dict = obj.w__dict__
-        assert w_dict is not None
-        assert dict(w_dict.r_dict_content) == {4: 1, 5: 2, "hello": 1, "world": 2}
-        assert obj.getdictvalue(self.fakespace, "hello") == 1
-        assert obj.getdictvalue(self.fakespace, "world") == 2
-        assert obj.getdictvalue(self.fakespace, 4) == 1
-        assert obj.getdictvalue(self.fakespace, 5) == 2
-        obj.deldictvalue(self.fakespace, "world")
-        assert obj.getdictvalue(self.fakespace, "world") is None
-        obj.deldictvalue(self.fakespace, "hello")
-        assert obj.getdictvalue(self.fakespace, "hello") is None
-
-    def test_dict_devolves_via_dict(self):
-        obj = self.make_obj()
-        w_dict = obj.getdict()
-        w_dict.setitem(4, 1)
-        w_dict.setitem(5, 2)
-        assert dict(w_dict.r_dict_content) == {4: 1, 5: 2, "hello": 1, "world": 2}
-        assert obj.getdictvalue(self.fakespace, "hello") == 1
-        assert obj.getdictvalue(self.fakespace, "world") == 2
-        assert obj.getdictvalue(self.fakespace, 4) == 1
-        assert obj.getdictvalue(self.fakespace, 5) == 2
-        obj.deldictvalue(self.fakespace, "world")
-        assert obj.getdictvalue(self.fakespace, "world") is None
-        obj.deldictvalue(self.fakespace, "hello")
-        assert obj.getdictvalue(self.fakespace, "hello") is None
-
-class TestMixinShared(TestMixin):
-    Mixin = make_inlinedict_mixin(SharedDictImplementation, "structure")
-    class FakeObject(Mixin):
-        def user_setup_slots(self, nslots):
-            pass
-
-class TestIndirectDict(BaseTestRDictImplementation):
-    Mixin = make_inlinedict_mixin(StrDictImplementation, "content")
-    class FakeObject(Mixin):
-        def user_setup_slots(self, nslots):
-            pass
-
-    def get_impl(self):
-        obj = self.FakeObject()
-        obj.user_setup(self.fakespace, FakeSubtype)
-        return obj.getdict()
-
-
-class TestIndirectDictShared(TestIndirectDict):
-    Mixin = make_inlinedict_mixin(SharedDictImplementation, "structure")
-    class FakeObject(Mixin):
-        def user_setup_slots(self, nslots):
-            pass
-
-
-
-
-class TestInlineDict(object):
-    def setup_class(cls):
-        cls.space = gettestobjspace(**{"objspace.std.withinlineddict": True})
-
-    def test_simple(self):
-        w_a = self.space.appexec([], """():
-            class A(object):
-                pass
-            a = A()
-            a.x = 12
-            a.y = 13
-            return a
-        """)
-        assert w_a.w__dict__ is None
-        assert self.space.int_w(w_a.content['x']) == 12
-        assert self.space.int_w(w_a.content['y']) == 13
-
-from pypy.conftest import gettestobjspace
-from pypy.objspace.std.inlinedict import make_inlinedict_mixin
-from pypy.objspace.std.dictmultiobject import StrDictImplementation
-from pypy.objspace.std.test.test_dictmultiobject import FakeSpace
-from pypy.objspace.std.test.test_dictmultiobject import BaseTestRDictImplementation
-from pypy.objspace.std.sharingdict import SharedDictImplementation
-
-class FakeSubtype:
-    nslots = 0
-
-class TestMixin(object):
-    Mixin = make_inlinedict_mixin(StrDictImplementation, "content")
-    class FakeObject(Mixin):
-        def user_setup_slots(self, nslots):
-            pass
-
-    fakespace = FakeSpace()
-
-    def make_obj(self):
-        obj = self.FakeObject()
-        obj.user_setup(self.fakespace, FakeSubtype)
-        obj.setdictvalue(self.fakespace, "hello", 1)
-        obj.setdictvalue(self.fakespace, "world", 2)
-        assert obj._inlined_dict_valid()
-        assert obj.w__dict__ is None
-        return obj
-
-    def test_setgetdel_dictvalue(self):
-        obj = self.make_obj()
-        assert obj.getdictvalue(self.fakespace, "hello") == 1
-        assert obj.getdictvalue(self.fakespace, "world") == 2
-        assert obj.getdictvalue(self.fakespace, "bla") is None
-        assert not obj.deldictvalue(self.fakespace, "bla")
-        obj.deldictvalue(self.fakespace, "world")
-        assert obj.getdictvalue(self.fakespace, "world") is None
-        obj.deldictvalue(self.fakespace, "hello")
-        assert obj.getdictvalue(self.fakespace, "hello") is None
-
-
-    def test_getdict(self):
-        obj = self.make_obj()
-        w_dict = obj.getdict()
-        assert obj.getdict() or w_dict # always get the same dict
-        assert obj.w__dict__ is w_dict
-
-        assert w_dict.getitem("hello") == 1
-        assert w_dict.getitem("world") == 2
-        w_dict.setitem("hello", 4)
-        w_dict.setitem("world", 5)
-        assert obj.getdictvalue(self.fakespace, "hello") == 4
-        assert obj.getdictvalue(self.fakespace, "world") == 5
-
-    def test_dict_devolves_via_object(self):
-        obj = self.make_obj()
-        obj.setdictvalue(self.fakespace, 4, 1)
-        obj.setdictvalue(self.fakespace, 5, 2)
-        w_dict = obj.w__dict__
-        assert w_dict is not None
-        assert dict(w_dict.r_dict_content) == {4: 1, 5: 2, "hello": 1, "world": 2}
-        assert obj.getdictvalue(self.fakespace, "hello") == 1
-        assert obj.getdictvalue(self.fakespace, "world") == 2
-        assert obj.getdictvalue(self.fakespace, 4) == 1
-        assert obj.getdictvalue(self.fakespace, 5) == 2
-        obj.deldictvalue(self.fakespace, "world")
-        assert obj.getdictvalue(self.fakespace, "world") is None
-        obj.deldictvalue(self.fakespace, "hello")
-        assert obj.getdictvalue(self.fakespace, "hello") is None
-
-    def test_dict_devolves_via_dict(self):
-        obj = self.make_obj()
-        w_dict = obj.getdict()
-        w_dict.setitem(4, 1)
-        w_dict.setitem(5, 2)
-        assert dict(w_dict.r_dict_content) == {4: 1, 5: 2, "hello": 1, "world": 2}
-        assert obj.getdictvalue(self.fakespace, "hello") == 1
-        assert obj.getdictvalue(self.fakespace, "world") == 2
-        assert obj.getdictvalue(self.fakespace, 4) == 1
-        assert obj.getdictvalue(self.fakespace, 5) == 2
-        obj.deldictvalue(self.fakespace, "world")
-        assert obj.getdictvalue(self.fakespace, "world") is None
-        obj.deldictvalue(self.fakespace, "hello")
-        assert obj.getdictvalue(self.fakespace, "hello") is None
-
-class TestMixinShared(TestMixin):
-    Mixin = make_inlinedict_mixin(SharedDictImplementation, "structure")
-    class FakeObject(Mixin):
-        def user_setup_slots(self, nslots):
-            pass
-
-class TestIndirectDict(BaseTestRDictImplementation):
-    Mixin = make_inlinedict_mixin(StrDictImplementation, "content")
-    class FakeObject(Mixin):
-        def user_setup_slots(self, nslots):
-            pass
-
-    def get_impl(self):
-        obj = self.FakeObject()
-        obj.user_setup(self.fakespace, FakeSubtype)
-        return obj.getdict()
-
-
-class TestIndirectDictShared(TestIndirectDict):
-    Mixin = make_inlinedict_mixin(SharedDictImplementation, "structure")
-    class FakeObject(Mixin):
-        def user_setup_slots(self, nslots):
-            pass
-
-
-
-
-class TestInlineDict(object):
-    def setup_class(cls):
-        cls.space = gettestobjspace(**{"objspace.std.withinlineddict": True})
-
-    def test_simple(self):
-        w_a = self.space.appexec([], """():
-            class A(object):
-                pass
-            a = A()
-            a.x = 12
-            a.y = 13
-            return a
-        """)
-        assert w_a.w__dict__ is None
-        assert self.space.int_w(w_a.content['x']) == 12
-        assert self.space.int_w(w_a.content['y']) == 13
-



More information about the Pypy-commit mailing list