Author: Armin Rigo <[email protected]>
Branch:
Changeset: r60811:d80baf6feebf
Date: 2013-02-01 19:09 +0100
http://bitbucket.org/pypy/pypy/changeset/d80baf6feebf/
Log: Test and fix for trying to iterate over __pypy__.identity_dict() and
getting strange results.
diff --git a/pypy/module/__pypy__/interp_identitydict.py
b/pypy/module/__pypy__/interp_identitydict.py
--- a/pypy/module/__pypy__/interp_identitydict.py
+++ b/pypy/module/__pypy__/interp_identitydict.py
@@ -33,6 +33,11 @@
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
@@ -50,8 +55,11 @@
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),
@@ -59,6 +67,7 @@
__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),
diff --git a/pypy/module/__pypy__/test/test_identitydict.py
b/pypy/module/__pypy__/test/test_identitydict.py
--- a/pypy/module/__pypy__/test/test_identitydict.py
+++ b/pypy/module/__pypy__/test/test_identitydict.py
@@ -56,3 +56,10 @@
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)
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit