Debian Codegraph @

Lib/test/test_free_threading/test_io.py

raw
1 import codecs
2 import io
3 import threading
4 from unittest import TestCase
5 from test.support import threading_helper
6 from test.support.threading_helper import run_concurrently
7 from random import randint
8 from io import BytesIO
9 from sys import getsizeof
10
11 threading_helper.requires_working_threading(module=True)
12
13
14 class TestBytesIO(TestCase):
15 # Test pretty much everything that can break under free-threading.
16 # Non-deterministic, but at least one of these things will fail if
17 # BytesIO object is not free-thread safe.
18
19 def check(self, funcs, *args):
20 barrier = threading.Barrier(len(funcs))
21 threads = []
22
23 for func in funcs:
24 thread = threading.Thread(target=func, args=(barrier, *args))
25
26 threads.append(thread)
27
28 with threading_helper.start_threads(threads):
29 pass
30
31 @threading_helper.requires_working_threading()
32 @threading_helper.reap_threads
33 def test_free_threading(self):
34 """Test for segfaults and aborts."""
35
36 def write(barrier, b, *ignore):
37 barrier.wait()
38 try: b.write(b'0' * randint(100, 1000))
39 except ValueError: pass # ignore write fail to closed file
40
41 def writelines(barrier, b, *ignore):
42 barrier.wait()
43 b.write(b'0\n' * randint(100, 1000))
44
45 def truncate(barrier, b, *ignore):
46 barrier.wait()
47 try: b.truncate(0)
48 except BufferError: pass # ignore exported buffer
49
50 def read(barrier, b, *ignore):
51 barrier.wait()
52 b.read()
53
54 def read1(barrier, b, *ignore):
55 barrier.wait()
56 b.read1()
57
58 def readline(barrier, b, *ignore):
59 barrier.wait()
60 b.readline()
61
62 def readlines(barrier, b, *ignore):
63 barrier.wait()
64 b.readlines()
65
66 def readinto(barrier, b, into, *ignore):
67 barrier.wait()
68 b.readinto(into)
69
70 def close(barrier, b, *ignore):
71 barrier.wait()
72 b.close()
73
74 def getvalue(barrier, b, *ignore):
75 barrier.wait()
76 b.getvalue()
77
78 def getbuffer(barrier, b, *ignore):
79 barrier.wait()
80 b.getbuffer()
81
82 def iter(barrier, b, *ignore):
83 barrier.wait()
84 list(b)
85
86 def getstate(barrier, b, *ignore):
87 barrier.wait()
88 b.__getstate__()
89
90 def setstate(barrier, b, st, *ignore):
91 barrier.wait()
92 b.__setstate__(st)
93
94 def sizeof(barrier, b, *ignore):
95 barrier.wait()
96 getsizeof(b)
97
98 self.check([write] * 10, BytesIO())
99 self.check([writelines] * 10, BytesIO())
100 self.check([write] * 10 + [truncate] * 10, BytesIO())
101 self.check([truncate] + [read] * 10, BytesIO(b'0\n'*204800))
102 self.check([truncate] + [read1] * 10, BytesIO(b'0\n'*204800))
103 self.check([truncate] + [readline] * 10, BytesIO(b'0\n'*20480))
104 self.check([truncate] + [readlines] * 10, BytesIO(b'0\n'*20480))
105 self.check([truncate] + [readinto] * 10, BytesIO(b'0\n'*204800), bytearray(b'0\n'*204800))
106 self.check([close] + [write] * 10, BytesIO())
107 self.check([truncate] + [getvalue] * 10, BytesIO(b'0\n'*204800))
108 self.check([truncate] + [getbuffer] * 10, BytesIO(b'0\n'*204800))
109 self.check([truncate] + [iter] * 10, BytesIO(b'0\n'*20480))
110 self.check([truncate] + [getstate] * 10, BytesIO(b'0\n'*204800))
111 self.check([truncate] + [setstate] * 10, BytesIO(b'0\n'*204800), (b'123', 0, None))
112 self.check([truncate] + [sizeof] * 10, BytesIO(b'0\n'*204800))
113
114 # no tests for seek or tell because they don't break anything
115
116
117 class IncrementalNewlineDecoderTest(TestCase):
118 def make_decoder(self):
119 utf8_decoder = codecs.getincrementaldecoder('utf-8')()
120 return io.IncrementalNewlineDecoder(utf8_decoder, translate=True)
121
122 def test_concurrent_reset(self):
123 decoder = self.make_decoder()
124
125 def worker():
126 for _ in range(100):
127 decoder.reset()
128
129 run_concurrently(worker_func=worker, nthreads=2)
130
131 def test_concurrent_decode(self):
132 decoder = self.make_decoder()
133
134 def worker():
135 for _ in range(100):
136 decoder.decode(b"line\r\n", final=False)
137
138 run_concurrently(worker_func=worker, nthreads=2)
139
140 def test_concurrent_getstate_setstate(self):
141 decoder = self.make_decoder()
142 state = decoder.getstate()
143
144 def getstate_worker():
145 for _ in range(100):
146 decoder.getstate()
147
148 def setstate_worker():
149 for _ in range(100):
150 decoder.setstate(state)
151
152 run_concurrently([getstate_worker] * 2 + [setstate_worker] * 2)
153
154 def test_concurrent_decode_and_reset(self):
155 decoder = self.make_decoder()
156
157 def decode_worker():
158 for _ in range(100):
159 decoder.decode(b"line\r\n", final=False)
160
161 def reset_worker():
162 for _ in range(100):
163 decoder.reset()
164
165 run_concurrently([decode_worker] * 2 + [reset_worker] * 2)