Commits

Armin Rigo  committed d80baf6

Test and fix for trying to iterate over __pypy__.identity_dict() and
getting strange results.

  • Participants
  • Parent commits ee3ed02

Comments (0)

Files changed (2)

File pypy/module/__pypy__/interp_identitydict.py

         except KeyError:
             raise OperationError(space.w_KeyError, w_key)
 
+    def descr_iter(self, space):
+        raise OperationError(space.w_TypeError,
+            space.wrap("'identity_dict' object does not support iteration; "
+                       "iterate over x.keys()"))
+
     def get(self, space, w_key, w_default=None):
         if w_default is None:
             w_default = space.w_None
 W_IdentityDict.typedef = TypeDef("identity_dict",
     __doc__="""\
 A dictionary that considers keys by object identity.
-Distinct objects that compare equal will have separate entries.
-All objects can be used as keys, even non-hashable ones.
+Distinct objects will have separate entries even if they
+compare equal.  All objects can be used as keys, even
+non-hashable ones --- but avoid using immutable objects
+like integers: two int objects 42 may or may not be
+internally the same object.
 """,
     __new__ = interp2app(W_IdentityDict.descr_new.im_func),
     __len__ = interp2app(W_IdentityDict.descr_len),
     __setitem__ = interp2app(W_IdentityDict.descr_setitem),
     __getitem__ = interp2app(W_IdentityDict.descr_getitem),
     __delitem__ = interp2app(W_IdentityDict.descr_delitem),
+    __iter__ = interp2app(W_IdentityDict.descr_iter),
     get = interp2app(W_IdentityDict.get),
     keys = interp2app(W_IdentityDict.keys),
     values = interp2app(W_IdentityDict.values),

File pypy/module/__pypy__/test/test_identitydict.py

 
         assert None in d
         assert [] not in d
+
+    def test_iterate(self):
+        from __pypy__ import identity_dict
+        d = identity_dict()
+        d[None] = 1
+        raises(TypeError, iter, d)
+        raises(TypeError, list, d)