File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments