aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/test/test_iter.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2019-08-04 14:12:48 +0300
committerGitHub <noreply@github.com>2019-08-04 14:12:48 +0300
commit18b711c5a7f90d88fb74748f18fa8ef49d8486c7 (patch)
treed04ab59ccadbade80f8999c921dd3a19b62a9cc9 /Lib/test/test_iter.py
parent17e52649c0e7e9389f1cc2444a53f059e24e6bca (diff)
downloadcpython-18b711c5a7f90d88fb74748f18fa8ef49d8486c7.tar.gz
cpython-18b711c5a7f90d88fb74748f18fa8ef49d8486c7.zip
bpo-37648: Fixed minor inconsistency in some __contains__. (GH-14904)
The collection's item is now always at the left and the needle is on the right of ==.
Diffstat (limited to 'Lib/test/test_iter.py')
-rw-r--r--Lib/test/test_iter.py23
1 files changed, 22 insertions, 1 deletions
diff --git a/Lib/test/test_iter.py b/Lib/test/test_iter.py
index 542b28419e2..6aceda23e9b 100644
--- a/Lib/test/test_iter.py
+++ b/Lib/test/test_iter.py
@@ -3,7 +3,7 @@
import sys
import unittest
from test.support import run_unittest, TESTFN, unlink, cpython_only
-from test.support import check_free_after_iterating
+from test.support import check_free_after_iterating, ALWAYS_EQ, NEVER_EQ
import pickle
import collections.abc
@@ -41,6 +41,14 @@ class IteratingSequenceClass:
def __iter__(self):
return BasicIterClass(self.n)
+class IteratorProxyClass:
+ def __init__(self, i):
+ self.i = i
+ def __next__(self):
+ return next(self.i)
+ def __iter__(self):
+ return self
+
class SequenceClass:
def __init__(self, n):
self.n = n
@@ -50,6 +58,12 @@ class SequenceClass:
else:
raise IndexError
+class SequenceProxyClass:
+ def __init__(self, s):
+ self.s = s
+ def __getitem__(self, i):
+ return self.s[i]
+
class UnlimitedSequenceClass:
def __getitem__(self, i):
return i
@@ -635,6 +649,13 @@ class TestCase(unittest.TestCase):
for i in "abc", -1, 5, 42.42, (3, 4), [], {1: 1}, 3-12j, sc5:
self.assertNotIn(i, sc5)
+ self.assertIn(ALWAYS_EQ, IteratorProxyClass(iter([1])))
+ self.assertIn(ALWAYS_EQ, SequenceProxyClass([1]))
+ self.assertNotIn(ALWAYS_EQ, IteratorProxyClass(iter([NEVER_EQ])))
+ self.assertNotIn(ALWAYS_EQ, SequenceProxyClass([NEVER_EQ]))
+ self.assertIn(NEVER_EQ, IteratorProxyClass(iter([ALWAYS_EQ])))
+ self.assertIn(NEVER_EQ, SequenceProxyClass([ALWAYS_EQ]))
+
self.assertRaises(TypeError, lambda: 3 in 12)
self.assertRaises(TypeError, lambda: 3 not in map)