Author: Lukas Diekmann <[email protected]>
Branch: set-strategies
Changeset: r49180:3904ce218a9c
Date: 2011-05-25 15:12 +0200
http://bitbucket.org/pypy/pypy/changeset/3904ce218a9c/
Log: added _is_sane_hash to delitem; fixed _is_sane_hash in has_key;
added strategy tests
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
@@ -21,6 +21,7 @@
# XXX there are many more types
return (space.is_w(w_lookup_type, space.w_NoneType) or
+ space.is_w(w_lookup_type, space.w_str) or
space.is_w(w_lookup_type, space.w_int) or
space.is_w(w_lookup_type, space.w_bool) or
space.is_w(w_lookup_type, space.w_float)
diff --git a/pypy/objspace/std/setobject.py b/pypy/objspace/std/setobject.py
--- a/pypy/objspace/std/setobject.py
+++ b/pypy/objspace/std/setobject.py
@@ -321,8 +321,11 @@
w_set.add(w_key)
def delitem(self, w_set, w_item):
+ from pypy.objspace.std.dictmultiobject import _is_sane_hash
d = self.cast_from_void_star(w_set.sstorage)
if not self.is_correct_type(w_item):
+ if _is_sane_hash(self.space, self.space.type(w_item)):
+ return False
w_set.switch_to_object_strategy(self.space)
return w_set.delitem(w_item)
@@ -353,7 +356,7 @@
def has_key(self, w_set, w_key):
from pypy.objspace.std.dictmultiobject import _is_sane_hash
if not self.is_correct_type(w_key):
- if not _is_sane_hash(self.space, w_key):
+ if not _is_sane_hash(self.space, self.space.type(w_key)):
w_set.switch_to_object_strategy(self.space)
return w_set.has_key(w_key)
return False
diff --git a/pypy/objspace/std/test/test_setstrategies.py
b/pypy/objspace/std/test/test_setstrategies.py
--- a/pypy/objspace/std/test/test_setstrategies.py
+++ b/pypy/objspace/std/test/test_setstrategies.py
@@ -61,3 +61,43 @@
s5 = set_union__Set(self.space, s1, [s3])
assert s4.strategy is self.space.fromcache(IntegerSetStrategy)
assert s5.strategy is self.space.fromcache(ObjectSetStrategy)
+
+ def test_discard(self):
+ class FakeInt(object):
+ def __init__(self, value):
+ self.value = value
+ def __hash__(self):
+ return hash(self.value)
+ def __eq__(self, other):
+ if other == self.value:
+ return True
+ return False
+
+ from pypy.objspace.std.setobject import set_discard__Set_ANY
+
+ s1 = W_SetObject(self.space, self.wrapped([1,2,3,4,5]))
+ set_discard__Set_ANY(self.space, s1, self.space.wrap("five"))
+ assert s1.strategy is self.space.fromcache(IntegerSetStrategy)
+
+ set_discard__Set_ANY(self.space, s1, self.space.wrap(FakeInt(5)))
+ assert s1.strategy is self.space.fromcache(ObjectSetStrategy)
+
+ def test_has_key(self):
+ class FakeInt(object):
+ def __init__(self, value):
+ self.value = value
+ def __hash__(self):
+ return hash(self.value)
+ def __eq__(self, other):
+ if other == self.value:
+ return True
+ return False
+
+ from pypy.objspace.std.setobject import set_discard__Set_ANY
+
+ s1 = W_SetObject(self.space, self.wrapped([1,2,3,4,5]))
+ assert not s1.has_key(self.space.wrap("five"))
+ assert s1.strategy is self.space.fromcache(IntegerSetStrategy)
+
+ assert s1.has_key(self.space.wrap(FakeInt(2)))
+ assert s1.strategy is self.space.fromcache(ObjectSetStrategy)
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit