Skip to content

Commit 1b72842

Browse files
committed
Move test to test_generators.
1 parent 6b6b8e6 commit 1b72842

File tree

2 files changed

+42
-41
lines changed

2 files changed

+42
-41
lines changed

Lib/test/test_generators.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import types
1111

1212
from test import support
13+
from test.support import threading_helper
1314

1415
try:
1516
import _testcapi
@@ -268,6 +269,47 @@ def loop():
268269
#This should not raise
269270
loop()
270271

272+
@support.requires_resource("cpu")
273+
def test_list_init_thread_safety(self):
274+
# GH-126369: generators were not thread safe
275+
from threading import Thread, Lock
276+
import contextlib
277+
278+
def my_generator():
279+
for i in range(1000000):
280+
yield i
281+
282+
283+
lock = Lock()
284+
amount = 0
285+
thread_count = 10
286+
287+
def gen_to_list(gen):
288+
# Note: it's intentional to throw out the exception here. Generators
289+
# aren't thread safe, so who knows what will happen. Right now, it just spams
290+
# a lot of ValueError's, but that might change if we decide to make generators
291+
# thread safe in the future. We're just making sure it doesn't crash.
292+
with contextlib.suppress(ValueError):
293+
list(gen)
294+
295+
with lock:
296+
nonlocal amount
297+
amount += 1
298+
299+
generator = my_generator()
300+
with threading_helper.catch_threading_exception() as cm:
301+
ts = [Thread(target=gen_to_list, args=(generator,)) for _ in range(thread_count)]
302+
for t in ts:
303+
t.start()
304+
305+
for t in ts:
306+
t.join()
307+
308+
self.assertIsNone(cm.exc_value)
309+
310+
self.assertEqual(amount, thread_count)
311+
312+
271313

272314
class ModifyUnderlyingIterableTest(unittest.TestCase):
273315
iterables = [

Lib/test/test_list.py

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import sys
22
from test import list_tests
33
from test import support
4-
from test.support import threading_helper
54
from test.support import cpython_only
65
import pickle
76
import unittest
@@ -311,46 +310,6 @@ def test_tier2_invalidates_iterator(self):
311310
a.append(4)
312311
self.assertEqual(list(it), [])
313312

314-
@support.requires_resource("cpu")
315-
def test_list_init_thread_safety(self):
316-
# GH-126369: list.__init__() didn't lock iterables
317-
from threading import Thread, Lock
318-
import contextlib
319-
320-
def my_generator():
321-
for i in range(1000000):
322-
yield i
323-
324-
325-
lock = Lock()
326-
amount = 0
327-
thread_count = 10
328-
329-
def gen_to_list(gen):
330-
# Note: it's intentional to throw out the exception here. Generators
331-
# aren't thread safe, so who knows what will happen. Right now, it just spams
332-
# a lot of ValueError's, but that might change if we decide to make generators
333-
# thread safe in the future. We're just making sure it doesn't crash.
334-
with contextlib.suppress(ValueError):
335-
list(gen)
336-
337-
with lock:
338-
nonlocal amount
339-
amount += 1
340-
341-
generator = my_generator()
342-
with threading_helper.catch_threading_exception() as cm:
343-
ts = [Thread(target=gen_to_list, args=(generator,)) for _ in range(thread_count)]
344-
for t in ts:
345-
t.start()
346-
347-
for t in ts:
348-
t.join()
349-
350-
self.assertIsNone(cm.exc_value)
351-
352-
self.assertEqual(amount, thread_count)
353-
354313

355314
if __name__ == "__main__":
356315
unittest.main()

0 commit comments

Comments
 (0)