| 1 |
import unittest |
| 2 |
from collections import deque |
| 3 |
from copy import copy |
| 4 |
from test.support import threading_helper |
| 5 |
|
| 6 |
threading_helper.requires_working_threading(module=True) |
| 7 |
|
| 8 |
|
| 9 |
class TestDeque(unittest.TestCase): |
| 10 |
def test_copy_race(self): |
| 11 |
# gh-144809: Test that deque copy is thread safe. It previously |
| 12 |
# could raise a "deque mutated during iteration" error. |
| 13 |
d = deque(range(100)) |
| 14 |
|
| 15 |
def mutate(): |
| 16 |
for i in range(1000): |
| 17 |
d.append(i) |
| 18 |
if len(d) > 200: |
| 19 |
d.popleft() |
| 20 |
|
| 21 |
def copy_loop(): |
| 22 |
for _ in range(1000): |
| 23 |
copy(d) |
| 24 |
|
| 25 |
threading_helper.run_concurrently([mutate, copy_loop]) |
| 26 |
|
| 27 |
def test_index_race_in_ac(self): |
| 28 |
# gh-150750: There was a c_default specified as `Py_SIZE(self)`, |
| 29 |
# it was used without a critical section. |
| 30 |
|
| 31 |
d = deque(range(100)) |
| 32 |
|
| 33 |
def index(): |
| 34 |
for _ in range(10000): |
| 35 |
try: |
| 36 |
d.index(50) |
| 37 |
except ValueError: |
| 38 |
pass |
| 39 |
|
| 40 |
def mutate(): |
| 41 |
for _ in range(10000): |
| 42 |
d.append(0) |
| 43 |
d.clear() |
| 44 |
d.extend(range(100)) |
| 45 |
d.appendleft(-1) |
| 46 |
|
| 47 |
threading_helper.run_concurrently( |
| 48 |
[index, *[mutate for _ in range(3)]], |
| 49 |
) |
| 50 |
|
| 51 |
|
| 52 |
if __name__ == "__main__": |
| 53 |
unittest.main() |