Commits

Philip Jenvey  committed f44626e

provide the fromkeys fastpath, differently from default, since it's based
around unicode

  • Participants
  • Parent commits 4cbca19
  • Branches py3k-fix-strategies

Comments (0)

Files changed (2)

File pypy/objspace/std/dictmultiobject.py

         if w_fill is None:
             w_fill = space.w_None
         if space.is_w(w_type, space.w_dict):
-            w_dict = W_DictMultiObject.allocate_and_init_instance(space,
-                                                                  w_type)
-
-            byteslist = space.listview_bytes(w_keys)
-            # XXX: py3k could switch this to listview_unicode, but our
-            # setitem_str accepts utf-8 encoded strs, not unicode!
-            if False and byteslist is not None:
-                for key in byteslist:
-                    w_dict.setitem_str(key, w_fill)
+            ulist = space.listview_unicode(w_keys)
+            if ulist is not None:
+                strategy = space.fromcache(UnicodeDictStrategy)
+                storage = strategy.get_storage_fromkeys(ulist, w_fill)
+                w_dict = space.allocate_instance(W_DictMultiObject, w_type)
+                W_DictMultiObject.__init__(w_dict, space, strategy, storage)
             else:
+                w_dict = W_DictMultiObject.allocate_and_init_instance(space,
+                                                                      w_type)
                 for w_key in space.listview(w_keys):
                     w_dict.setitem(w_key, w_fill)
         else:
             i += 1
         return keys, values
 
+    def get_storage_fromkeys(self, keys_w, w_fill):
+        """Return an initialized storage with keys and fill values"""
+        storage = {}
+        mark_dict_non_null(storage)
+        for key in keys_w:
+            storage[key] = w_fill
+        return self.erase(storage)
+
 create_iterator_classes(UnicodeDictStrategy)
 
 

File pypy/objspace/std/test/test_dictmultiobject.py

     def test_fromkeys_fastpath(self):
         space = self.space
         w = space.wrap
-        wb = space.wrapbytes
 
-        w_l = self.space.newlist([wb("a"),wb("b")])
+        w_l = space.newlist([w("a"),w("b")])
         w_l.getitems = None
         w_d = space.call_method(space.w_dict, "fromkeys", w_l)