summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_itertools.py
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2023-12-04 12:15:41 (GMT)
committerGitHub <noreply@github.com>2023-12-04 12:15:41 (GMT)
commit55896f470b05a1d526607a6423014fffc436eefc (patch)
treecab9c5a38de6edfb3ae584636c2d6b992e09a28a /Lib/test/test_itertools.py
parent8d1b3c0a70752b57c45e458e3fa1e3875f5f4cf5 (diff)
downloadcpython-55896f470b05a1d526607a6423014fffc436eefc.zip
cpython-55896f470b05a1d526607a6423014fffc436eefc.tar.gz
cpython-55896f470b05a1d526607a6423014fffc436eefc.tar.bz2
[3.12] gh-109786: Fix leaks and crash when re-enter itertools.pairwise.__next__() (GH-109788) (GH-112699)
(cherry picked from commit 6ca9d3e0173c38e2eac50367b187d4c1d43f9892) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Lib/test/test_itertools.py')
-rw-r--r--Lib/test/test_itertools.py72
1 files changed, 72 insertions, 0 deletions
diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py
index 512745e..705e880 100644
--- a/Lib/test/test_itertools.py
+++ b/Lib/test/test_itertools.py
@@ -1152,6 +1152,78 @@ class TestBasicOps(unittest.TestCase):
with self.assertRaises(TypeError):
pairwise(None) # non-iterable argument
+ def test_pairwise_reenter(self):
+ def check(reenter_at, expected):
+ class I:
+ count = 0
+ def __iter__(self):
+ return self
+ def __next__(self):
+ self.count +=1
+ if self.count in reenter_at:
+ return next(it)
+ return [self.count] # new object
+
+ it = pairwise(I())
+ for item in expected:
+ self.assertEqual(next(it), item)
+
+ check({1}, [
+ (([2], [3]), [4]),
+ ([4], [5]),
+ ])
+ check({2}, [
+ ([1], ([1], [3])),
+ (([1], [3]), [4]),
+ ([4], [5]),
+ ])
+ check({3}, [
+ ([1], [2]),
+ ([2], ([2], [4])),
+ (([2], [4]), [5]),
+ ([5], [6]),
+ ])
+ check({1, 2}, [
+ ((([3], [4]), [5]), [6]),
+ ([6], [7]),
+ ])
+ check({1, 3}, [
+ (([2], ([2], [4])), [5]),
+ ([5], [6]),
+ ])
+ check({1, 4}, [
+ (([2], [3]), (([2], [3]), [5])),
+ ((([2], [3]), [5]), [6]),
+ ([6], [7]),
+ ])
+ check({2, 3}, [
+ ([1], ([1], ([1], [4]))),
+ (([1], ([1], [4])), [5]),
+ ([5], [6]),
+ ])
+
+ def test_pairwise_reenter2(self):
+ def check(maxcount, expected):
+ class I:
+ count = 0
+ def __iter__(self):
+ return self
+ def __next__(self):
+ if self.count >= maxcount:
+ raise StopIteration
+ self.count +=1
+ if self.count == 1:
+ return next(it, None)
+ return [self.count] # new object
+
+ it = pairwise(I())
+ self.assertEqual(list(it), expected)
+
+ check(1, [])
+ check(2, [])
+ check(3, [])
+ check(4, [(([2], [3]), [4])])
+
def test_product(self):
for args, result in [
([], [()]), # zero iterables