mirror of
https://github.com/python/cpython.git
synced 2025-12-04 00:30:19 +00:00
Issue #4296: Fix PyObject_RichCompareBool so that "x in [x]" evaluates to
True, even when x doesn't compare equal to itself. This was a regression from 2.6. Reviewed by R. Hettinger and C. Heimes.
This commit is contained in:
parent
3d4ca74bc8
commit
4a1f593df5
4 changed files with 62 additions and 2 deletions
|
|
@ -1,3 +1,4 @@
|
|||
from collections import deque
|
||||
from test.support import run_unittest
|
||||
import unittest
|
||||
|
||||
|
|
@ -6,7 +7,7 @@ class base_set:
|
|||
def __init__(self, el):
|
||||
self.el = el
|
||||
|
||||
class set(base_set):
|
||||
class myset(base_set):
|
||||
def __contains__(self, el):
|
||||
return self.el == el
|
||||
|
||||
|
|
@ -17,7 +18,7 @@ class seq(base_set):
|
|||
class TestContains(unittest.TestCase):
|
||||
def test_common_tests(self):
|
||||
a = base_set(1)
|
||||
b = set(1)
|
||||
b = myset(1)
|
||||
c = seq(1)
|
||||
self.assert_(1 in b)
|
||||
self.assert_(0 not in b)
|
||||
|
|
@ -80,6 +81,25 @@ class TestContains(unittest.TestCase):
|
|||
except TypeError:
|
||||
pass
|
||||
|
||||
def test_nonreflexive(self):
|
||||
# containment and equality tests involving elements that are
|
||||
# not necessarily equal to themselves
|
||||
|
||||
class MyNonReflexive(object):
|
||||
def __eq__(self, other):
|
||||
return False
|
||||
def __hash__(self):
|
||||
return 28
|
||||
|
||||
values = float('nan'), 1, None, 'abc', MyNonReflexive()
|
||||
constructors = list, tuple, dict.fromkeys, set, frozenset, deque
|
||||
for constructor in constructors:
|
||||
container = constructor(values)
|
||||
for elem in container:
|
||||
self.assert_(elem in container)
|
||||
self.assert_(container == constructor(values))
|
||||
self.assert_(container == container)
|
||||
|
||||
|
||||
def test_main():
|
||||
run_unittest(TestContains)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue