Debian Codegraph @

Lib/test/test_free_threading/test_frame.py

raw
1 import functools
2 import sys
3 import threading
4 import unittest
5
6 from test.support import threading_helper
7
8 threading_helper.requires_working_threading(module=True)
9
10
11 def run_with_frame(funcs, runner=None, iters=10):
12 """Run funcs with a frame from another thread that is currently executing.
13
14 Args:
15 funcs: A function or list of functions that take a frame argument
16 runner: Optional function to run in the executor thread. If provided,
17 it will be called and should return eventually. The frame
18 passed to funcs will be the runner's frame.
19 iters: Number of iterations each func should run
20 """
21 if not isinstance(funcs, list):
22 funcs = [funcs]
23
24 frame_var = None
25 e = threading.Event()
26 b = threading.Barrier(len(funcs) + 1)
27
28 if runner is None:
29 def runner():
30 j = 0
31 for i in range(100):
32 j += i
33
34 def executor():
35 nonlocal frame_var
36 frame_var = sys._getframe()
37 e.set()
38 b.wait()
39 runner()
40
41 def func_wrapper(func):
42 e.wait()
43 frame = frame_var
44 b.wait()
45 for _ in range(iters):
46 func(frame)
47
48 test_funcs = [functools.partial(func_wrapper, f) for f in funcs]
49 threading_helper.run_concurrently([executor] + test_funcs)
50
51
52 class TestFrameRaces(unittest.TestCase):
53 def test_concurrent_f_lasti(self):
54 run_with_frame(lambda frame: frame.f_lasti)
55
56 def test_concurrent_f_lineno(self):
57 run_with_frame(lambda frame: frame.f_lineno)
58
59 def test_concurrent_f_code(self):
60 run_with_frame(lambda frame: frame.f_code)
61
62 def test_concurrent_f_back(self):
63 run_with_frame(lambda frame: frame.f_back)
64
65 def test_concurrent_f_globals(self):
66 run_with_frame(lambda frame: frame.f_globals)
67
68 def test_concurrent_f_builtins(self):
69 run_with_frame(lambda frame: frame.f_builtins)
70
71 def test_concurrent_f_locals(self):
72 run_with_frame(lambda frame: frame.f_locals)
73
74 def test_concurrent_f_trace_read(self):
75 run_with_frame(lambda frame: frame.f_trace)
76
77 def test_concurrent_f_trace_opcodes_read(self):
78 run_with_frame(lambda frame: frame.f_trace_opcodes)
79
80 def test_concurrent_repr(self):
81 run_with_frame(lambda frame: repr(frame))
82
83 def test_concurrent_f_trace_write(self):
84 def trace_func(frame, event, arg):
85 return trace_func
86
87 def writer(frame):
88 frame.f_trace = trace_func
89 frame.f_trace = None
90
91 run_with_frame(writer)
92
93 def test_concurrent_f_trace_read_write(self):
94 # Test concurrent reads and writes of f_trace on a live frame.
95 def trace_func(frame, event, arg):
96 return trace_func
97
98 def reader(frame):
99 _ = frame.f_trace
100
101 def writer(frame):
102 frame.f_trace = trace_func
103 frame.f_trace = None
104
105 run_with_frame([reader, writer, reader, writer])
106
107 def test_concurrent_f_trace_opcodes_write(self):
108 def writer(frame):
109 frame.f_trace_opcodes = True
110 frame.f_trace_opcodes = False
111
112 run_with_frame(writer)
113
114 def test_concurrent_f_trace_opcodes_read_write(self):
115 # Test concurrent reads and writes of f_trace_opcodes on a live frame.
116 def reader(frame):
117 _ = frame.f_trace_opcodes
118
119 def writer(frame):
120 frame.f_trace_opcodes = True
121 frame.f_trace_opcodes = False
122
123 run_with_frame([reader, writer, reader, writer])
124
125 def test_concurrent_frame_clear(self):
126 # Test race between frame.clear() and attribute reads.
127 def create_frame():
128 x = 1
129 y = 2
130 return sys._getframe()
131
132 frame = create_frame()
133
134 def reader():
135 for _ in range(10):
136 try:
137 _ = frame.f_locals
138 _ = frame.f_code
139 _ = frame.f_lineno
140 except ValueError:
141 # Frame may be cleared
142 pass
143
144 def clearer():
145 frame.clear()
146
147 threading_helper.run_concurrently([reader, reader, clearer])
148
149
150 if __name__ == "__main__":
151 unittest.main()