Commits

Philip Jenvey committed 557b939

branch for fixing/adapting the list/dict/etc strategies that were disabled
while focusing on py3k compat

Comments (0)

Files changed (5)

pypy/interpreter/pyopcode.py

         w_ann = None
         if num_annotations:
             names_w = space.fixedview(self.popvalue())
-            w_ann = space.newdict(strdict=True)
+            w_ann = space.newdict(strdict=True) # XXX: strdict??
             for i in range(len(names_w) - 1, -1, -1):
                 space.setitem(w_ann, names_w[i], self.popvalue())
         defaultarguments = self.popvalues(posdefaults)
         w_kw_defs = None
         if kwdefaults:
-            w_kw_defs = space.newdict(strdict=True)
+            w_kw_defs = space.newdict(strdict=True) # XXX:
             for i in range(kwdefaults - 1, -1, -1):
                 w_name = self.popvalue()
                 w_def = self.popvalue()

pypy/objspace/std/dictmultiobject.py

         #elif instance or strdict or module:
         #    assert w_type is None
         #    strategy = space.fromcache(BytesDictStrategy)
-        elif False and kwargs:
+        elif kwargs:
             assert w_type is None
             from pypy.objspace.std.kwargsdict import EmptyKwargsDictStrategy
             strategy = space.fromcache(EmptyKwargsDictStrategy)
         w_dict.setitem(w_key, w_value)
 
     def setitem_str(self, w_dict, key, w_value):
-        self.switch_to_bytes_strategy(w_dict)
+        self.switch_to_unicode_strategy(w_dict)
         w_dict.setitem_str(key, w_value)
 
     def delitem(self, w_dict, w_key):
     def _never_equal_to(self, w_lookup_type):
         return _never_equal_to_string(self.space, w_lookup_type)
 
+    """
     def setitem_str(self, w_dict, key, w_value):
         assert key is not None
         self.unerase(w_dict.dstorage)[key] = w_value
     def getitem_str(self, w_dict, key):
         assert key is not None
         return self.unerase(w_dict.dstorage).get(key, None)
+        """
 
     def listview_bytes(self, w_dict):
         return self.unerase(w_dict.dstorage).keys()
 
     # we should implement the same shortcuts as we do for BytesDictStrategy
 
-    ## def setitem_str(self, w_dict, key, w_value):
-    ##     assert key is not None
-    ##     self.unerase(w_dict.dstorage)[key] = w_value
+    def setitem_str(self, w_dict, key, w_value):
+        assert key is not None
+        self.unerase(w_dict.dstorage)[key.decode('ascii')] = w_value
 
-    ## def getitem(self, w_dict, w_key):
-    ##     space = self.space
-    ##     # -- This is called extremely often.  Hack for performance --
-    ##     if type(w_key) is space.StringObjectCls:
-    ##         return self.getitem_str(w_dict, w_key.unwrap(space))
-    ##     # -- End of performance hack --
-    ##     return AbstractTypedStrategy.getitem(self, w_dict, w_key)
+    def getitem(self, w_dict, w_key):
+        space = self.space
+        # -- This is called extremely often.  Hack for performance --
+        if type(w_key) is space.StringObjectCls:
+            #return self.getitem_str(w_dict, w_key.unwrap(space))
+            # XXX:
+            key = w_key.unwrap(space)
+            return self.unerase(w_dict.dstorage).get(key, None)
+        # -- End of performance hack --
+        return AbstractTypedStrategy.getitem(self, w_dict, w_key)
 
-    ## def getitem_str(self, w_dict, key):
-    ##     assert key is not None
-    ##     return self.unerase(w_dict.dstorage).get(key, None)
+    def getitem_str(self, w_dict, key):
+        assert key is not None
+        return self.unerase(w_dict.dstorage).get(key.decode('utf-8'), None)
 
     def listview_unicode(self, w_dict):
         return self.unerase(w_dict.dstorage).keys()
 
-    ## def w_keys(self, w_dict):
-    ##     return self.space.newlist_bytes(self.listview_bytes(w_dict))
+    #def w_keys(self, w_dict):
+    #    # XXX: I think we can completely kill w_keys...
+    #    return self.space.newlist_str(self.listview_str(w_dict))
 
     def wrapkey(space, key):
         return space.wrap(key)
 
-    ## @jit.look_inside_iff(lambda self, w_dict:
-    ##                      w_dict_unrolling_heuristic(w_dict))
-    ## def view_as_kwargs(self, w_dict):
-    ##     d = self.unerase(w_dict.dstorage)
-    ##     l = len(d)
-    ##     keys, values = [None] * l, [None] * l
-    ##     i = 0
-    ##     for key, val in d.iteritems():
-    ##         keys[i] = key
-    ##         values[i] = val
-    ##         i += 1
-    ##     return keys, values
+    @jit.look_inside_iff(lambda self, w_dict:
+                         w_dict_unrolling_heuristic(w_dict))
+    def view_as_kwargs(self, w_dict):
+        d = self.unerase(w_dict.dstorage)
+        l = len(d)
+        keys, values = [None] * l, [None] * l
+        i = 0
+        for key, val in d.iteritems():
+            keys[i] = key.encode('utf-8')
+            values[i] = val
+            i += 1
+        return keys, values
 
 create_iterator_classes(UnicodeDictStrategy)
 

pypy/objspace/std/kwargsdict.py

 
 from rpython.rlib import rerased, jit
 from pypy.objspace.std.dictmultiobject import (
-    BytesDictStrategy, DictStrategy, EmptyDictStrategy, ObjectDictStrategy,
+    DictStrategy, EmptyDictStrategy, ObjectDictStrategy, UnicodeDictStrategy,
     create_iterator_classes)
 
 
 class EmptyKwargsDictStrategy(EmptyDictStrategy):
-    def switch_to_bytes_strategy(self, w_dict):
+    def switch_to_unicode_strategy(self, w_dict):
         strategy = self.space.fromcache(KwargsDictStrategy)
         storage = strategy.get_empty_storage()
         w_dict.strategy = strategy
 
     def is_correct_type(self, w_obj):
         space = self.space
-        return space.is_w(space.type(w_obj), space.w_str)
+        return space.is_w(space.type(w_obj), space.w_unicode)
+        #return type(w_obj) is space.UnicodeObjectCls
 
     def _never_equal_to(self, w_lookup_type):
         return False
         else:
             # limit the size so that the linear searches don't become too long
             if len(keys) >= 16:
-                self.switch_to_bytes_strategy(w_dict)
+                self.switch_to_unicode_strategy(w_dict)
                 w_dict.setitem_str(key, w_value)
             else:
                 keys.append(key)
 
     def w_keys(self, w_dict):
         l = self.unerase(w_dict.dstorage)[0]
-        return self.space.newlist_bytes(l[:])
+        return self.space.newlist_unicode(l[:])
 
     def values(self, w_dict):
         return self.unerase(w_dict.dstorage)[1][:] # to make non-resizable
         w_dict.dstorage = self.get_empty_storage()
 
     def switch_to_object_strategy(self, w_dict):
-        strategy = self.space.fromcache(ObjectDictStrategy)
+        strategy = self.space.fromcache(UnicodeDictStrategy)
         keys, values_w = self.unerase(w_dict.dstorage)
         d_new = strategy.unerase(strategy.get_empty_storage())
         for i in range(len(keys)):
         w_dict.strategy = strategy
         w_dict.dstorage = strategy.erase(d_new)
 
-    def switch_to_bytes_strategy(self, w_dict):
+    def switch_to_unicode_strategy(self, w_dict):
         strategy = self.space.fromcache(BytesDictStrategy)
         keys, values_w = self.unerase(w_dict.dstorage)
         storage = strategy.get_empty_storage()
         d_new = strategy.unerase(storage)
         for i in range(len(keys)):
-            d_new[keys[i]] = values_w[i]
+            d_new[keys[i].decode('utf-8')] = values_w[i]
         w_dict.strategy = strategy
         w_dict.dstorage = storage
 

pypy/objspace/std/test/test_dictmultiobject.py

 import py
 
 from pypy.objspace.std.dictmultiobject import (W_DictMultiObject,
-    BytesDictStrategy, ObjectDictStrategy)
+    BytesDictStrategy, ObjectDictStrategy, UnicodeDictStrategy)
 
 
 class TestW_DictObject(object):
         return l
     def newlist_bytes(self, l):
         return l
+    def newlist_unicode(self, l):
+        return l
     DictObjectCls = W_DictMultiObject
     def type(self, w_obj):
         if isinstance(w_obj, FakeString):
             return str
         return type(w_obj)
     w_str = str
+    w_unicode = unicode
 
     def str_w(self, string):
+        if isinstance(string, unicode):
+            return string.encode('utf-8')
         assert isinstance(string, str)
         return string
 
+    def unicode_w(self, string):
+        assert isinstance(string, unicode)
+        return string
+
     def int_w(self, integer, allow_conversion=True):
         assert isinstance(integer, int)
         return integer
 
     def wrap(self, obj):
+        if isinstance(obj, str):
+            return obj.decode('ascii')
+        return obj
+
+    def wrapbytes(self, obj):
         return obj
 
     def isinstance_w(self, obj, klass):
 
     def setup_method(self,method):
         self.fakespace = FakeSpace()
-        self.string = self.fakespace.wrap("fish")
-        self.string2 = self.fakespace.wrap("fish2")
+        self.string = self.wrapstroruni("fish")
+        self.string2 = self.wrapstroruni("fish2")
         self.impl = self.get_impl()
 
+    def wrapstrorunicode(self, obj):
+        # XXX: blargh this is all screwed. its referencing FakeString
+        # and using regular strings to setitem.
+        return self.fakespace.wrap(obj)
+
     def get_impl(self):
         strategy = self.StrategyClass(self.fakespace)
         storage = strategy.get_empty_storage()
         assert "s" not in d.w_keys()
         assert F() not in d.w_keys()
 
-class TestBytesDictImplementation(BaseTestRDictImplementation):
-    StrategyClass = BytesDictStrategy
+class TestUnicodeDictImplementation(BaseTestRDictImplementation):
+    StrategyClass = UnicodeDictStrategy
     #ImplementionClass = BytesDictImplementation
 
     def test_str_shortcut(self):
         self.fill_impl()
-        s = FakeString(self.string)
+        #s = FakeString(self.string)
+        s = FakeUnicode(self.string)
         assert self.impl.getitem(s) == 1000
         assert s.unwrapped
 
     def test_view_as_kwargs(self):
-        py.test.py3k_skip("XXX: strategies are currently broken")
+        #py.test.py3k_skip("XXX: strategies are currently broken")
         self.fill_impl()
         assert self.fakespace.view_as_kwargs(self.impl) == (["fish", "fish2"], [1000, 2000])
 
     def check_not_devolved(self):
         pass
 
-class TestDevolvedBytesDictImplementation(BaseTestDevolvedDictImplementation):
-    StrategyClass = BytesDictStrategy
+class TestDevolvedUnicodeDictImplementation(BaseTestDevolvedDictImplementation):
+    StrategyClass = UnicodeDictStrategy
 
 
 def test_module_uses_strdict():

pypy/objspace/std/test/test_kwargsdict.py

     for i in range(100):
         assert d.setitem_str("d%s" % i, 4) is None
     assert d.strategy is not strategy
-    assert "BytesDictStrategy" == d.strategy.__class__.__name__
+    assert "UnicodeDictStrategy" == d.strategy.__class__.__name__
 
 def test_keys_doesnt_wrap():
     space = FakeSpace()
         return r[r.find("(") + 1: r.find(")")]
 
     def test_create(self):
-        py3k_skip("need UnicodeDictStrategy to work in py3k")
         def f(**args):
             return args
         d = f(a=1)
         assert sorted(f(a=2, b=3).values()) == [2, 3]
 
     def test_setdefault(self):
-        py3k_skip("XXX: strategies are currently broken")
         def f(**args):
             return args
         d = f(a=1, b=2)