Author: Philip Jenvey <pjen...@underboss.org>
Branch: py3k-fix-strategies
Changeset: r70979:f44626e10d82
Date: 2014-04-25 11:45 -0700
http://bitbucket.org/pypy/pypy/changeset/f44626e10d82/

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

diff --git a/pypy/objspace/std/dictmultiobject.py 
b/pypy/objspace/std/dictmultiobject.py
--- a/pypy/objspace/std/dictmultiobject.py
+++ b/pypy/objspace/std/dictmultiobject.py
@@ -113,16 +113,15 @@
         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:
@@ -943,6 +942,14 @@
             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)
 
 
diff --git a/pypy/objspace/std/test/test_dictmultiobject.py 
b/pypy/objspace/std/test/test_dictmultiobject.py
--- a/pypy/objspace/std/test/test_dictmultiobject.py
+++ b/pypy/objspace/std/test/test_dictmultiobject.py
@@ -127,9 +127,8 @@
     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)
 
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to