Author: touilleMan <[email protected]>
Branch: 
Changeset: r85833:fc60b4bca8e7
Date: 2016-07-23 18:25 +0200
http://bitbucket.org/pypy/pypy/changeset/fc60b4bca8e7/

Log:    Issue #2352 generilization for tuple/iterator/set/dict.

diff --git a/pypy/objspace/descroperation.py b/pypy/objspace/descroperation.py
--- a/pypy/objspace/descroperation.py
+++ b/pypy/objspace/descroperation.py
@@ -409,7 +409,7 @@
                 if not e.match(space, space.w_StopIteration):
                     raise
                 return space.w_False
-            if space.eq_w(w_next, w_item):
+            if space.eq_w(w_item, w_next):
                 return space.w_True
 
     def hash(space, w_obj):
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
@@ -948,6 +948,41 @@
         helper(lambda x: x.viewkeys())
         helper(lambda x: x.viewitems())
 
+    def test_contains(self):
+        logger = []
+
+        class Foo(object):
+
+            def __init__(self, value, name=None):
+                self.value = value
+                self.name = name or value
+
+            def __repr__(self):
+                return '<Foo %s>' % self.name
+
+            def __eq__(self, other):
+                logger.append((self, other))
+                return self.value == other.value
+
+            def __hash__(self):
+                return 42  # __eq__ will be used given all objects' hashes 
clash
+
+        foo1, foo2, foo3 = Foo(1), Foo(2), Foo(3)
+        foo42 = Foo(42)
+        foo_dict = {foo1: 1, foo2: 1, foo3: 1}
+        del logger[:]
+        foo42 in foo_dict
+        logger_copy = set(logger[:])  # prevent re-evaluation during pytest 
error print
+        assert logger_copy == {(foo3, foo42), (foo2, foo42), (foo1, foo42)}
+
+        del logger[:]
+        foo2_bis = Foo(2, '2 bis')
+        foo2_bis in foo_dict
+        logger_copy = set(logger[:])  # prevent re-evaluation during pytest 
error print
+        assert (foo2, foo2_bis) in logger_copy
+        assert logger_copy.issubset({(foo1, foo2_bis), (foo2, foo2_bis), 
(foo3, foo2_bis)})
+
+
 class AppTestStrategies(object):
     def setup_class(cls):
         if cls.runappdirect:
diff --git a/pypy/objspace/std/test/test_iterobject.py 
b/pypy/objspace/std/test/test_iterobject.py
--- a/pypy/objspace/std/test/test_iterobject.py
+++ b/pypy/objspace/std/test/test_iterobject.py
@@ -97,3 +97,32 @@
     def test_no_len_on_xrange(self):
         iterable = xrange(10)
         raises(TypeError, len, iter(iterable))
+
+    def test_contains(self):
+        logger = []
+
+        class Foo(object):
+
+            def __init__(self, value, name=None):
+                self.value = value
+                self.name = name or value
+
+            def __repr__(self):
+                return '<Foo %s>' % self.name
+
+            def __eq__(self, other):
+                logger.append((self, other))
+                return self.value == other.value
+
+        foo1, foo2, foo3 = Foo(1), Foo(2), Foo(3)
+        foo42 = Foo(42)
+        foo_list = [foo1, foo2, foo3]
+        foo42 in (x for x in foo_list)
+        logger_copy = logger[:]  # prevent re-evaluation during pytest error 
print
+        assert logger_copy == [(foo42, foo1), (foo42, foo2), (foo42, foo3)]
+
+        del logger[:]
+        foo2_bis = Foo(2, '2 bis')
+        foo2_bis in (x for x in foo_list)
+        logger_copy = logger[:]  # prevent re-evaluation during pytest error 
print
+        assert logger_copy == [(foo2_bis, foo1), (foo2_bis, foo2)]
diff --git a/pypy/objspace/std/test/test_setobject.py 
b/pypy/objspace/std/test/test_setobject.py
--- a/pypy/objspace/std/test/test_setobject.py
+++ b/pypy/objspace/std/test/test_setobject.py
@@ -532,6 +532,39 @@
             assert (c in s) == (c in word)
         raises(TypeError, s.__contains__, [])
 
+        logger = []
+
+        class Foo(object):
+
+            def __init__(self, value, name=None):
+                self.value = value
+                self.name = name or value
+
+            def __repr__(self):
+                return '<Foo %s>' % self.name
+
+            def __eq__(self, other):
+                logger.append((self, other))
+                return self.value == other.value
+
+            def __hash__(self):
+                return 42  # __eq__ will be used given all objects' hashes 
clash
+
+        foo1, foo2, foo3 = Foo(1), Foo(2), Foo(3)
+        foo42 = Foo(42)
+        foo_set = {foo1, foo2, foo3}
+        del logger[:]
+        foo42 in foo_set
+        logger_copy = set(logger[:])  # prevent re-evaluation during pytest 
error print
+        assert logger_copy == {(foo3, foo42), (foo2, foo42), (foo1, foo42)}
+
+        del logger[:]
+        foo2_bis = Foo(2, '2 bis')
+        foo2_bis in foo_set
+        logger_copy = set(logger[:])  # prevent re-evaluation during pytest 
error print
+        assert (foo2, foo2_bis) in logger_copy
+        assert logger_copy.issubset({(foo1, foo2_bis), (foo2, foo2_bis), 
(foo3, foo2_bis)})
+
     def test_remove(self):
         s = set('abc')
         s.remove('a')
diff --git a/pypy/objspace/std/test/test_tupleobject.py 
b/pypy/objspace/std/test/test_tupleobject.py
--- a/pypy/objspace/std/test/test_tupleobject.py
+++ b/pypy/objspace/std/test/test_tupleobject.py
@@ -269,6 +269,34 @@
         assert not 11 in t
         assert not t in t
 
+        logger = []
+
+        class Foo(object):
+
+            def __init__(self, value, name=None):
+                self.value = value
+                self.name = name or value
+
+            def __repr__(self):
+                return '<Foo %s>' % self.name
+
+            def __eq__(self, other):
+                logger.append((self, other))
+                return self.value == other.value
+
+        foo1, foo2, foo3 = Foo(1), Foo(2), Foo(3)
+        foo42 = Foo(42)
+        foo_tuple = (foo1, foo2, foo3)
+        foo42 in foo_tuple
+        logger_copy = logger[:]  # prevent re-evaluation during pytest error 
print
+        assert logger_copy == [(foo42, foo1), (foo42, foo2), (foo42, foo3)]
+
+        del logger[:]
+        foo2_bis = Foo(2, '2 bis')
+        foo2_bis in foo_tuple
+        logger_copy = logger[:]  # prevent re-evaluation during pytest error 
print
+        assert logger_copy == [(foo2_bis, foo1), (foo2_bis, foo2)]
+
     def test_add(self):
         t0 = ()
         t1 = (5, 3, 99)
diff --git a/pypy/objspace/std/tupleobject.py b/pypy/objspace/std/tupleobject.py
--- a/pypy/objspace/std/tupleobject.py
+++ b/pypy/objspace/std/tupleobject.py
@@ -153,7 +153,7 @@
     @jit.unroll_safe
     def _descr_contains_unroll_safe(self, space, w_obj):
         for w_item in self.tolist():
-            if space.eq_w(w_item, w_obj):
+            if space.eq_w(w_obj, w_item):
                 return space.w_True
         return space.w_False
 
@@ -161,7 +161,7 @@
         tp = space.type(w_obj)
         for w_item in self.tolist():
             contains_jmp.jit_merge_point(tp=tp)
-            if space.eq_w(w_item, w_obj):
+            if space.eq_w(w_obj, w_item):
                 return space.w_True
         return space.w_False
 
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to