Skip to content

Commit 13f2b33

Browse files
committed
Add multithreaded test
1 parent 82987bb commit 13f2b33

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

mypyc/test-data/run-generators.test

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,3 +766,35 @@ def test_generator_identities() -> None:
766766
g4 = gen_a(1)
767767
assert id(g4) == id2
768768

769+
[case testGeneratorReuseWithGilDisabled]
770+
import sys
771+
import threading
772+
from typing import Iterator
773+
774+
def gen() -> Iterator[int]:
775+
yield 1
776+
777+
def is_gil_disabled() -> bool:
778+
return hasattr(sys, "_is_gil_enabled") and not sys._is_gil_enabled()
779+
780+
def test_each_thread_gets_separate_instance() -> None:
781+
if not is_gil_disabled():
782+
# This only makes sense if GIL is disabled
783+
return
784+
785+
g = gen()
786+
id1 = id(g)
787+
788+
id2 = 0
789+
790+
def run() -> None:
791+
nonlocal id2
792+
g = gen()
793+
id2 = id(g)
794+
795+
t = threading.Thread(target=run)
796+
t.start()
797+
t.join()
798+
799+
# Each thread should get a separate reused instance
800+
assert id1 != id2

0 commit comments

Comments
 (0)