Commits

Alex Gaynor committed 8664949

Remove dict.has_key, and switch dict.{keys,values,items} to return views and remove the view* methods.

Comments (0)

Files changed (6)

lib_pypy/_structseq.py

                 field.__name__ = name
         dict['n_fields'] = len(fields_by_index)
 
-        extra_fields = fields_by_index.items()
+        extra_fields = list(fields_by_index.items())
         extra_fields.sort()
         n_sequence_fields = 0
         while extra_fields and extra_fields[0][0] == n_sequence_fields:

pypy/interpreter/baseobjspace.py

     def int_w(self, space):
         raise OperationError(space.w_TypeError,
                              typed_unwrap_error_msg(space, "integer", self))
-    
+
     def uint_w(self, space):
         raise OperationError(space.w_TypeError,
                              typed_unwrap_error_msg(space, "integer", self))
-    
+
     def bigint_w(self, space):
         raise OperationError(space.w_TypeError,
                              typed_unwrap_error_msg(space, "integer", self))
     def export_builtin_exceptions(self):
         """NOT_RPYTHON"""
         w_dic = self.exceptions_module.getdict(self)
-        w_keys = self.call_method(w_dic, "keys")
         exc_types_w = {}
-        for w_name in self.unpackiterable(w_keys):
+        w_iter = self.iter(w_dic)
+        while True:
+            try:
+                w_name = self.next(w_iter)
+            except OperationError, e:
+                if not e.match(self, self.w_StopIteration):
+                    raise
+                break
             name = self.str_w(w_name)
             if not name.startswith('__'):
                 excname = name

pypy/interpreter/mixedmodule.py

         if self.w_initialdict is None:
             Module.init(self, space)
             if not self.lazy and self.w_initialdict is None:
-                self.w_initialdict = space.call_method(self.w_dict, 'items')
+                self.w_initialdict = space.call_method(self.w_dict, 'copy')
 
 
     def get_applevel_name(cls):
                 w_value = self.get(name)
                 space.setitem(self.w_dict, space.new_interned_str(name), w_value)
             self.lazy = False
-            self.w_initialdict = space.call_method(self.w_dict, 'items')
+            self.w_initialdict = space.call_method(self.w_dict, 'copy')
         return self.w_dict
 
     def _freeze_(self):

pypy/objspace/std/dictmultiobject.py

 
 class _UnwrappedIteratorMixin:
     _mixin_ = True
-    
+
     def __init__(self, space, strategy, dictimplementation):
         IteratorImplementation.__init__(self, space, dictimplementation)
         self.iterator = strategy.unerase(dictimplementation.dstorage).iteritems()
 def contains__DictMulti_ANY(space, w_dict, w_key):
     return space.newbool(w_dict.getitem(w_key) is not None)
 
-dict_has_key__DictMulti_ANY = contains__DictMulti_ANY
-
 def iter__DictMulti(space, w_dict):
     return W_DictMultiIterObject(space, w_dict.iter(), KEYSITER)
 
     update1_dict_dict(space, w_new, w_self)
     return w_new
 
+
 def dict_items__DictMulti(space, w_self):
-    return space.newlist(w_self.items())
+    return W_DictViewItemsObject(space, w_self)
 
 def dict_keys__DictMulti(space, w_self):
-    return space.newlist(w_self.keys())
+    return W_DictViewKeysObject(space, w_self)
 
 def dict_values__DictMulti(space, w_self):
-    return space.newlist(w_self.values())
+    return W_DictViewValuesObject(space, w_self)
 
 def dict_iteritems__DictMulti(space, w_self):
     return W_DictMultiIterObject(space, w_self.iter(), ITEMSITER)
 def dict_itervalues__DictMulti(space, w_self):
     return W_DictMultiIterObject(space, w_self.iter(), VALUESITER)
 
-def dict_viewitems__DictMulti(space, w_self):
-    return W_DictViewItemsObject(space, w_self)
-
-def dict_viewkeys__DictMulti(space, w_self):
-    return W_DictViewKeysObject(space, w_self)
-
-def dict_viewvalues__DictMulti(space, w_self):
-    return W_DictViewValuesObject(space, w_self)
-
 def dict_clear__DictMulti(space, w_self):
     w_self.clear()
 

pypy/objspace/std/dicttype.py

 dict_copy       = SMM('copy',          1,
                       doc='D.copy() -> a shallow copy of D')
 dict_items      = SMM('items',         1,
-                      doc="D.items() -> list of D's (key, value) pairs, as"
-                          ' 2-tuples')
+                      doc="D.items() -> a set-like object providing a view on D's item")
 dict_keys       = SMM('keys',          1,
-                      doc="D.keys() -> list of D's keys")
+                      doc="D.keys() -> a set-like object providing a view on D's keys")
 dict_values     = SMM('values',        1,
-                      doc="D.values() -> list of D's values")
-dict_has_key    = SMM('has_key',       2,
-                      doc='D.has_key(k) -> True if D has a key k, else False')
+                      doc="D.values() -> an object providing a view on D's values")
 dict_clear      = SMM('clear',         1,
                       doc='D.clear() -> None.  Remove all items from D.')
 dict_get        = SMM('get',           3, defaults=(None,),
                       doc='D.iterkeys() -> an iterator over the keys of D')
 dict_itervalues = SMM('itervalues',    1,
                       doc='D.itervalues() -> an iterator over the values of D')
-dict_viewkeys   = SMM('viewkeys',      1,
-                      doc="D.viewkeys() -> a set-like object providing a view on D's keys")
-dict_viewitems  = SMM('viewitems',     1,
-                      doc="D.viewitems() -> a set-like object providing a view on D's items")
-dict_viewvalues = SMM('viewvalues',    1,
-                      doc="D.viewvalues() -> an object providing a view on D's values")
 dict_reversed   = SMM('__reversed__',      1)
 
 def dict_reversed__ANY(space, w_dict):
         currently_in_repr[dict_id] = 1
         try:
             items = []
-            # XXX for now, we cannot use iteritems() at app-level because
-            #     we want a reasonable result instead of a RuntimeError
+            # XXX for now, we cannot use items() withut list at app-level
+            #     because we want a reasonable result instead of a RuntimeError
             #     even if the dict is mutated by the repr() in the loop.
-            for k, v in dict.items(d):
+            for k, v in list(dict.items(d)):
                 items.append(repr(k) + ": " + repr(v))
             return "{" +  ', '.join(items) + "}"
         finally:

pypy/objspace/std/test/test_dictmultiobject.py

         assert len(dd) == 1
         raises(KeyError, dd.pop, 33)
 
-    def test_has_key(self):
-        d = {1: 2, 3: 4}
-        assert d.has_key(1)
-        assert not d.has_key(33)
-
     def test_items(self):
         d = {1: 2, 3: 4}
-        its = d.items()
+        its = list(d.items())
         its.sort()
         assert its == [(1, 2), (3, 4)]
 
         values = []
         for k in d.itervalues():
             values.append(k)
-        assert values == d.values()
+        assert values == list(d.values())
 
     def test_keys(self):
         d = {1: 2, 3: 4}
-        kys = d.keys()
+        kys = list(d.keys())
         kys.sort()
         assert kys == [1, 3]
 
 
     def test_values(self):
         d = {1: 2, 3: 4}
-        vals = d.values()
+        vals = list(d.values())
         vals.sort()
-        assert vals == [2,4]
+        assert vals == [2, 4]
 
     def test_eq(self):
         d1 = {1: 2, 3: 4}
     def test_empty_dict(self):
         d = {}
         raises(KeyError, d.popitem)
-        assert d.items() == []
-        assert d.values() == []
-        assert d.keys() == []
+        assert list(d.items()) == []
+        assert list(d.values()) == []
+        assert list(d.keys()) == []
 
 class AppTest_DictMultiObject(AppTest_DictObject):
 
         a = A()
         s = S("abc")
         setattr(a, s, 42)
-        key = a.__dict__.keys()[0]
+        key = next(iter(a.__dict__.keys()))
         assert key == s
         assert key is not s
         assert type(key) is str
 class AppTestDictViews:
     def test_dictview(self):
         d = {1: 2, 3: 4}
-        assert len(d.viewkeys()) == 2
-        assert len(d.viewitems()) == 2
-        assert len(d.viewvalues()) == 2
+        assert len(d.keys()) == 2
+        assert len(d.items()) == 2
+        assert len(d.values()) == 2
 
     def test_constructors_not_callable(self):
-        kt = type({}.viewkeys())
+        kt = type({}.keys())
         raises(TypeError, kt, {})
         raises(TypeError, kt)
-        it = type({}.viewitems())
+        it = type({}.items())
         raises(TypeError, it, {})
         raises(TypeError, it)
-        vt = type({}.viewvalues())
+        vt = type({}.values())
         raises(TypeError, vt, {})
         raises(TypeError, vt)
 
     def test_dict_keys(self):
         d = {1: 10, "a": "ABC"}
-        keys = d.viewkeys()
+        keys = d.keys()
         assert len(keys) == 2
         assert set(keys) == set([1, "a"])
         assert keys == set([1, "a"])
         assert "a" in keys
         assert 10 not in keys
         assert "Z" not in keys
-        assert d.viewkeys() == d.viewkeys()
+        assert d.keys() == d.keys()
         e = {1: 11, "a": "def"}
-        assert d.viewkeys() == e.viewkeys()
+        assert d.keys() == e.keys()
         del e["a"]
-        assert d.viewkeys() != e.viewkeys()
+        assert d.keys() != e.keys()
 
     def test_dict_items(self):
         d = {1: 10, "a": "ABC"}
-        items = d.viewitems()
+        items = d.items()
         assert len(items) == 2
         assert set(items) == set([(1, 10), ("a", "ABC")])
         assert items == set([(1, 10), ("a", "ABC")])
         assert () not in items
         assert (1,) not in items
         assert (1, 2, 3) not in items
-        assert d.viewitems() == d.viewitems()
+        assert d.items() == d.items()
         e = d.copy()
-        assert d.viewitems() == e.viewitems()
+        assert d.items() == e.items()
         e["a"] = "def"
-        assert d.viewitems() != e.viewitems()
+        assert d.items() != e.items()
 
     def test_dict_mixed_keys_items(self):
         d = {(1, 1): 11, (2, 2): 22}
         e = {1: 1, 2: 2}
-        assert d.viewkeys() == e.viewitems()
-        assert d.viewitems() != e.viewkeys()
+        assert d.keys() == e.items()
+        assert d.items() != e.keys()
 
     def test_dict_values(self):
         d = {1: 10, "a": "ABC"}
-        values = d.viewvalues()
+        values = d.values()
         assert set(values) == set([10, "ABC"])
         assert len(values) == 2
 
     def test_dict_repr(self):
         d = {1: 10, "a": "ABC"}
         assert isinstance(repr(d), str)
-        r = repr(d.viewitems())
+        r = repr(d.items())
         assert isinstance(r, str)
         assert (r == "dict_items([('a', 'ABC'), (1, 10)])" or
                 r == "dict_items([(1, 10), ('a', 'ABC')])")
-        r = repr(d.viewkeys())
+        r = repr(d.keys())
         assert isinstance(r, str)
         assert (r == "dict_keys(['a', 1])" or
                 r == "dict_keys([1, 'a'])")
-        r = repr(d.viewvalues())
+        r = repr(d.values())
         assert isinstance(r, str)
         assert (r == "dict_values(['ABC', 10])" or
                 r == "dict_values([10, 'ABC'])")
         d1 = {'a': 1, 'b': 2}
         d2 = {'b': 3, 'c': 2}
         d3 = {'d': 4, 'e': 5}
-        assert d1.viewkeys() & d1.viewkeys() == set('ab')
-        assert d1.viewkeys() & d2.viewkeys() == set('b')
-        assert d1.viewkeys() & d3.viewkeys() == set()
-        assert d1.viewkeys() & set(d1.viewkeys()) == set('ab')
-        assert d1.viewkeys() & set(d2.viewkeys()) == set('b')
-        assert d1.viewkeys() & set(d3.viewkeys()) == set()
+        assert d1.keys() & d1.keys() == set('ab')
+        assert d1.keys() & d2.keys() == set('b')
+        assert d1.keys() & d3.keys() == set()
+        assert d1.keys() & set(d1.keys()) == set('ab')
+        assert d1.keys() & set(d2.keys()) == set('b')
+        assert d1.keys() & set(d3.keys()) == set()
 
-        assert d1.viewkeys() | d1.viewkeys() == set('ab')
-        assert d1.viewkeys() | d2.viewkeys() == set('abc')
-        assert d1.viewkeys() | d3.viewkeys() == set('abde')
-        assert d1.viewkeys() | set(d1.viewkeys()) == set('ab')
-        assert d1.viewkeys() | set(d2.viewkeys()) == set('abc')
-        assert d1.viewkeys() | set(d3.viewkeys()) == set('abde')
+        assert d1.keys() | d1.keys() == set('ab')
+        assert d1.keys() | d2.keys() == set('abc')
+        assert d1.keys() | d3.keys() == set('abde')
+        assert d1.keys() | set(d1.keys()) == set('ab')
+        assert d1.keys() | set(d2.keys()) == set('abc')
+        assert d1.keys() | set(d3.keys()) == set('abde')
 
-        assert d1.viewkeys() ^ d1.viewkeys() == set()
-        assert d1.viewkeys() ^ d2.viewkeys() == set('ac')
-        assert d1.viewkeys() ^ d3.viewkeys() == set('abde')
-        assert d1.viewkeys() ^ set(d1.viewkeys()) == set()
-        assert d1.viewkeys() ^ set(d2.viewkeys()) == set('ac')
-        assert d1.viewkeys() ^ set(d3.viewkeys()) == set('abde')
+        assert d1.keys() ^ d1.keys() == set()
+        assert d1.keys() ^ d2.keys() == set('ac')
+        assert d1.keys() ^ d3.keys() == set('abde')
+        assert d1.keys() ^ set(d1.keys()) == set()
+        assert d1.keys() ^ set(d2.keys()) == set('ac')
+        assert d1.keys() ^ set(d3.keys()) == set('abde')
 
     def test_items_set_operations(self):
         d1 = {'a': 1, 'b': 2}
         d2 = {'a': 2, 'b': 2}
         d3 = {'d': 4, 'e': 5}
-        assert d1.viewitems() & d1.viewitems() == set([('a', 1), ('b', 2)])
-        assert d1.viewitems() & d2.viewitems() == set([('b', 2)])
-        assert d1.viewitems() & d3.viewitems() == set()
-        assert d1.viewitems() & set(d1.viewitems()) == set([('a', 1), ('b', 2)])
-        assert d1.viewitems() & set(d2.viewitems()) == set([('b', 2)])
-        assert d1.viewitems() & set(d3.viewitems()) == set()
+        assert d1.items() & d1.items() == set([('a', 1), ('b', 2)])
+        assert d1.items() & d2.items() == set([('b', 2)])
+        assert d1.items() & d3.items() == set()
+        assert d1.items() & set(d1.items()) == set([('a', 1), ('b', 2)])
+        assert d1.items() & set(d2.items()) == set([('b', 2)])
+        assert d1.items() & set(d3.items()) == set()
 
-        assert d1.viewitems() | d1.viewitems() == set([('a', 1), ('b', 2)])
-        assert (d1.viewitems() | d2.viewitems() ==
+        assert d1.items() | d1.items() == set([('a', 1), ('b', 2)])
+        assert (d1.items() | d2.items() ==
                 set([('a', 1), ('a', 2), ('b', 2)]))
-        assert (d1.viewitems() | d3.viewitems() ==
+        assert (d1.items() | d3.items() ==
                 set([('a', 1), ('b', 2), ('d', 4), ('e', 5)]))
-        assert (d1.viewitems() | set(d1.viewitems()) ==
+        assert (d1.items() | set(d1.items()) ==
                 set([('a', 1), ('b', 2)]))
-        assert (d1.viewitems() | set(d2.viewitems()) ==
+        assert (d1.items() | set(d2.items()) ==
                 set([('a', 1), ('a', 2), ('b', 2)]))
-        assert (d1.viewitems() | set(d3.viewitems()) ==
+        assert (d1.items() | set(d3.items()) ==
                 set([('a', 1), ('b', 2), ('d', 4), ('e', 5)]))
 
-        assert d1.viewitems() ^ d1.viewitems() == set()
-        assert d1.viewitems() ^ d2.viewitems() == set([('a', 1), ('a', 2)])
-        assert (d1.viewitems() ^ d3.viewitems() ==
+        assert d1.items() ^ d1.items() == set()
+        assert d1.items() ^ d2.items() == set([('a', 1), ('a', 2)])
+        assert (d1.items() ^ d3.items() ==
                 set([('a', 1), ('b', 2), ('d', 4), ('e', 5)]))
 
 
     w_float = float
     StringObjectCls = FakeString
     w_dict = W_DictMultiObject
+    w_text = str
     iter = iter
     fixedview = list
     listview  = list