Debian Codegraph @

Lib/test/test_free_threading/test_code.py

raw
1 import unittest
2
3 try:
4 import ctypes
5 except ImportError:
6 ctypes = None
7
8 from threading import Thread
9 from unittest import TestCase
10
11 from test.support import threading_helper
12 from test.support.threading_helper import run_concurrently
13
14 if ctypes is not None:
15 capi = ctypes.pythonapi
16
17 freefunc = ctypes.CFUNCTYPE(None, ctypes.c_voidp)
18
19 RequestCodeExtraIndex = capi.PyUnstable_Eval_RequestCodeExtraIndex
20 RequestCodeExtraIndex.argtypes = (freefunc,)
21 RequestCodeExtraIndex.restype = ctypes.c_ssize_t
22
23 SetExtra = capi.PyUnstable_Code_SetExtra
24 SetExtra.argtypes = (ctypes.py_object, ctypes.c_ssize_t, ctypes.c_voidp)
25 SetExtra.restype = ctypes.c_int
26
27 GetExtra = capi.PyUnstable_Code_GetExtra
28 GetExtra.argtypes = (
29 ctypes.py_object,
30 ctypes.c_ssize_t,
31 ctypes.POINTER(ctypes.c_voidp),
32 )
33 GetExtra.restype = ctypes.c_int
34
35 # Note: each call to RequestCodeExtraIndex permanently allocates a slot
36 # (the counter is monotonically increasing), up to MAX_CO_EXTRA_USERS (255).
37 NTHREADS = 20
38
39
40 @threading_helper.requires_working_threading()
41 class TestCode(TestCase):
42 def test_code_attrs(self):
43 """Test concurrent accesses to lazily initialized code attributes"""
44 code_objects = []
45 for _ in range(1000):
46 code_objects.append(compile("a + b", "<string>", "eval"))
47
48 def run_in_thread():
49 for code in code_objects:
50 self.assertIsInstance(code.co_code, bytes)
51 self.assertIsInstance(code.co_freevars, tuple)
52 self.assertIsInstance(code.co_varnames, tuple)
53
54 threads = [Thread(target=run_in_thread) for _ in range(2)]
55 for thread in threads:
56 thread.start()
57 for thread in threads:
58 thread.join()
59
60 @unittest.skipUnless(ctypes, "ctypes is required")
61 def test_request_code_extra_index_concurrent(self):
62 """Test concurrent calls to RequestCodeExtraIndex"""
63 results = []
64
65 def worker():
66 idx = RequestCodeExtraIndex(freefunc(0))
67 self.assertGreaterEqual(idx, 0)
68 results.append(idx)
69
70 run_concurrently(worker_func=worker, nthreads=NTHREADS)
71
72 # Every thread must get a unique index.
73 self.assertEqual(len(results), NTHREADS)
74 self.assertEqual(len(set(results)), NTHREADS)
75
76 @unittest.skipUnless(ctypes, "ctypes is required")
77 def test_code_extra_all_ops_concurrent(self):
78 """Test concurrent RequestCodeExtraIndex + SetExtra + GetExtra"""
79 LOOP = 100
80
81 def f():
82 pass
83
84 code = f.__code__
85
86 def worker():
87 idx = RequestCodeExtraIndex(freefunc(0))
88 self.assertGreaterEqual(idx, 0)
89
90 for i in range(LOOP):
91 ret = SetExtra(code, idx, ctypes.c_voidp(i + 1))
92 self.assertEqual(ret, 0)
93
94 for _ in range(LOOP):
95 extra = ctypes.c_voidp()
96 ret = GetExtra(code, idx, extra)
97 self.assertEqual(ret, 0)
98 # The slot was set by this thread, so the value must
99 # be the last one written.
100 self.assertEqual(extra.value, LOOP)
101
102 run_concurrently(worker_func=worker, nthreads=NTHREADS)
103
104 @unittest.skipUnless(ctypes, "ctypes is required")
105 def test_code_extra_set_get_concurrent(self):
106 """Test concurrent SetExtra + GetExtra on a shared index"""
107 LOOP = 100
108
109 def f():
110 pass
111
112 code = f.__code__
113
114 idx = RequestCodeExtraIndex(freefunc(0))
115 self.assertGreaterEqual(idx, 0)
116
117 def worker():
118 for i in range(LOOP):
119 ret = SetExtra(code, idx, ctypes.c_voidp(i + 1))
120 self.assertEqual(ret, 0)
121
122 for _ in range(LOOP):
123 extra = ctypes.c_voidp()
124 ret = GetExtra(code, idx, extra)
125 self.assertEqual(ret, 0)
126 # Value is set by any writer thread.
127 self.assertTrue(1 <= extra.value <= LOOP)
128
129 run_concurrently(worker_func=worker, nthreads=NTHREADS)
130
131 # Every thread's last write is LOOP, so the final value must be LOOP.
132 extra = ctypes.c_voidp()
133 ret = GetExtra(code, idx, extra)
134 self.assertEqual(ret, 0)
135 self.assertEqual(extra.value, LOOP)
136
137
138 if __name__ == "__main__":
139 unittest.main()