mirror of
https://github.com/python/cpython.git
synced 2025-11-01 18:51:43 +00:00
gh-117657: Fix itertools.count thread safety (#119268)
Fix itertools.count in free-threading mode
This commit is contained in:
parent
77ff28bb67
commit
87939bd579
3 changed files with 54 additions and 11 deletions
|
|
@ -546,7 +546,7 @@ class TestBasicOps(unittest.TestCase):
|
|||
#check proper internal error handling for large "step' sizes
|
||||
count(1, maxsize+5); sys.exc_info()
|
||||
|
||||
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)])
|
||||
|
|
@ -590,6 +590,28 @@ class TestBasicOps(unittest.TestCase):
|
|||
self.assertEqual(type(next(c)), int)
|
||||
self.assertEqual(type(next(c)), float)
|
||||
|
||||
@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('')), [])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue