Author: Antonio Cuni <[email protected]>
Branch: unicode-strategies
Changeset: r58475:3e98af414d3e
Date: 2012-10-26 21:00 +0200
http://bitbucket.org/pypy/pypy/changeset/3e98af414d3e/

Log:    implement listview_unicode for dicts

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,7 +113,7 @@
                     getitem_str delitem length \
                     clear w_keys values \
                     items iterkeys itervalues iteritems setdefault \
-                    popitem listview_str listview_int".split()
+                    popitem listview_str listview_unicode listview_int".split()
 
     def make_method(method):
         def f(self, *args):
@@ -187,6 +187,9 @@
     def listview_str(self, w_dict):
         return None
 
+    def listview_unicode(self, w_dict):
+        return None
+
     def listview_int(self, w_dict):
         return None
 
@@ -676,8 +679,8 @@
     ##     assert key is not None
     ##     return self.unerase(w_dict.dstorage).get(key, None)
 
-    ## def listview_str(self, w_dict):
-    ##     return self.unerase(w_dict.dstorage).keys()
+    def listview_unicode(self, w_dict):
+        return self.unerase(w_dict.dstorage).keys()
 
     ## def w_keys(self, w_dict):
     ##     return self.space.newlist_str(self.listview_str(w_dict))
diff --git a/pypy/objspace/std/objspace.py b/pypy/objspace/std/objspace.py
--- a/pypy/objspace/std/objspace.py
+++ b/pypy/objspace/std/objspace.py
@@ -469,8 +469,8 @@
         # and isinstance() for others.  See test_listobject.test_uses_custom...
         if type(w_obj) is W_ListObject:
             return w_obj.getitems_unicode()
-        ## if type(w_obj) is W_DictMultiObject:
-        ##     return w_obj.listview_unicode()
+        if type(w_obj) is W_DictMultiObject:
+            return w_obj.listview_unicode()
         ## if type(w_obj) is W_SetObject or type(w_obj) is W_FrozensetObject:
         ##     return w_obj.listview_unicode()
         ## if isinstance(w_obj, W_UnicodeObject):
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
@@ -144,33 +144,48 @@
 
     def test_listview_str_dict(self):
         w = self.space.wrap
-
         w_d = self.space.newdict()
         w_d.initialize_content([(w("a"), w(1)), (w("b"), w(2))])
+        assert self.space.listview_str(w_d) == ["a", "b"]
 
-        assert self.space.listview_str(w_d) == ["a", "b"]
+    def test_listview_unicode_dict(self):
+        w = self.space.wrap
+        w_d = self.space.newdict()
+        w_d.initialize_content([(w(u"a"), w(1)), (w(u"b"), w(2))])
+        assert self.space.listview_unicode(w_d) == [u"a", u"b"]
 
     def test_listview_int_dict(self):
         w = self.space.wrap
         w_d = self.space.newdict()
         w_d.initialize_content([(w(1), w("a")), (w(2), w("b"))])
-
         assert self.space.listview_int(w_d) == [1, 2]
 
-    def test_keys_on_string_int_dict(self):
+    def test_keys_on_string_unicode_int_dict(self, monkeypatch):
         w = self.space.wrap
+        
         w_d = self.space.newdict()
         w_d.initialize_content([(w(1), w("a")), (w(2), w("b"))])
-
         w_l = self.space.call_method(w_d, "keys")
         assert sorted(self.space.listview_int(w_l)) == [1,2]
-
+        
+        # make sure that .keys() calls newlist_str for string dicts
+        def not_allowed(*args):
+            assert False, 'should not be called'
+        monkeypatch.setattr(self.space, 'newlist', not_allowed)
+        #
         w_d = self.space.newdict()
         w_d.initialize_content([(w("a"), w(1)), (w("b"), w(6))])
-
         w_l = self.space.call_method(w_d, "keys")
         assert sorted(self.space.listview_str(w_l)) == ["a", "b"]
 
+        # XXX: it would be nice if the test passed without monkeypatch.undo(),
+        # but we need space.newlist_unicode for it
+        monkeypatch.undo() 
+        w_d = self.space.newdict()
+        w_d.initialize_content([(w(u"a"), w(1)), (w(u"b"), w(6))])
+        w_l = self.space.call_method(w_d, "keys")
+        assert sorted(self.space.listview_unicode(w_l)) == [u"a", u"b"]
+
 class AppTest_DictObject:
     def setup_class(cls):
         cls.w_on_pypy = cls.space.wrap("__pypy__" in sys.builtin_module_names)
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to