diff options
author | Sam Gross <colesbury@gmail.com> | 2024-06-03 22:47:34 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-03 22:47:34 (GMT) |
commit | 79fae3b0a15be30d35131420f030c9a31338b357 (patch) | |
tree | f4aea481cce7e56a0886f429780baa18e049e05b /Lib | |
parent | ae705319fcde864b504987dc8e579e3eef68e1e5 (diff) | |
download | cpython-79fae3b0a15be30d35131420f030c9a31338b357.zip cpython-79fae3b0a15be30d35131420f030c9a31338b357.tar.gz cpython-79fae3b0a15be30d35131420f030c9a31338b357.tar.bz2 |
[3.13] gh-117657: Fix itertools.count thread safety (GH-119268) (#120007)
Fix itertools.count in free-threading mode
(cherry picked from commit 87939bd5790accea77c5a81093f16f28d3f0b429)
Co-authored-by: Arnon Yaari <wiggin15@yahoo.com>
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_itertools.py | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py index e243da3..2c92d88 100644 --- a/Lib/test/test_itertools.py +++ b/Lib/test/test_itertools.py @@ -644,7 +644,7 @@ class TestBasicOps(unittest.TestCase): count(1, maxsize+5); sys.exc_info() @pickle_deprecated - def test_count_with_stride(self): + def test_count_with_step(self): self.assertEqual(lzip('abc',count(2,3)), [('a', 2), ('b', 5), ('c', 8)]) self.assertEqual(lzip('abc',count(start=2,step=3)), [('a', 2), ('b', 5), ('c', 8)]) @@ -699,6 +699,28 @@ class TestBasicOps(unittest.TestCase): for proto in range(pickle.HIGHEST_PROTOCOL + 1): self.pickletest(proto, count(i, j)) + @threading_helper.requires_working_threading() + def test_count_threading(self, step=1): + # this test verifies multithreading consistency, which is + # mostly for testing builds without GIL, but nice to test anyway + count_to = 10_000 + num_threads = 10 + c = count(step=step) + def counting_thread(): + for i in range(count_to): + next(c) + threads = [] + for i in range(num_threads): + thread = threading.Thread(target=counting_thread) + thread.start() + threads.append(thread) + for thread in threads: + thread.join() + self.assertEqual(next(c), count_to * num_threads * step) + + def test_count_with_step_threading(self): + self.test_count_threading(step=5) + def test_cycle(self): self.assertEqual(take(10, cycle('abc')), list('abcabcabca')) self.assertEqual(list(cycle('')), []) |