Debian Codegraph @

Lib/test/test_type_cache.py

raw
1 """ Tests for the internal type cache in CPython. """
2 import dis
3 import unittest
4 import warnings
5 from test import support
6 from test.support import import_helper, requires_specialization, requires_specialization_ft
7 try:
8 from sys import _clear_type_cache
9 except ImportError:
10 _clear_type_cache = None
11
12 # Skip this test if the _testcapi module isn't available.
13 _testcapi = import_helper.import_module("_testcapi")
14 _testinternalcapi = import_helper.import_module("_testinternalcapi")
15 type_get_version = _testcapi.type_get_version
16 type_assign_specific_version_unsafe = _testinternalcapi.type_assign_specific_version_unsafe
17 type_assign_version = _testcapi.type_assign_version
18 type_modified = _testcapi.type_modified
19
20 def clear_type_cache():
21 with warnings.catch_warnings():
22 warnings.simplefilter("ignore", DeprecationWarning)
23 _clear_type_cache()
24
25 @support.cpython_only
26 @unittest.skipIf(_clear_type_cache is None, "requires sys._clear_type_cache")
27 class TypeCacheTests(unittest.TestCase):
28 def test_tp_version_tag_unique(self):
29 """tp_version_tag should be unique assuming no overflow, even after
30 clearing type cache.
31 """
32 # Check if global version tag has already overflowed.
33 Y = type('Y', (), {})
34 Y.x = 1
35 Y.x # Force a _PyType_Lookup, populating version tag
36 y_ver = type_get_version(Y)
37 # Overflow, or not enough left to conduct the test.
38 if y_ver == 0 or y_ver > 0xFFFFF000:
39 self.skipTest("Out of type version tags")
40 # Note: try to avoid any method lookups within this loop,
41 # It will affect global version tag.
42 all_version_tags = []
43 append_result = all_version_tags.append
44 assertNotEqual = self.assertNotEqual
45 for _ in range(30):
46 clear_type_cache()
47 X = type('Y', (), {})
48 X.x = 1
49 X.x
50 tp_version_tag_after = type_get_version(X)
51 assertNotEqual(tp_version_tag_after, 0, msg="Version overflowed")
52 append_result(tp_version_tag_after)
53 self.assertEqual(len(set(all_version_tags)), 30,
54 msg=f"{all_version_tags} contains non-unique versions")
55
56 def test_type_assign_version(self):
57 class C:
58 x = 5
59
60 self.assertEqual(type_assign_version(C), 1)
61 c_ver = type_get_version(C)
62
63 C.x = 6
64 self.assertEqual(type_get_version(C), 0)
65 self.assertEqual(type_assign_version(C), 1)
66 self.assertNotEqual(type_get_version(C), 0)
67 self.assertNotEqual(type_get_version(C), c_ver)
68
69 def test_type_assign_specific_version(self):
70 """meta-test for type_assign_specific_version_unsafe"""
71 class C:
72 pass
73
74 type_assign_version(C)
75 orig_version = type_get_version(C)
76 if orig_version == 0:
77 self.skipTest("Could not assign a valid type version")
78
79 type_modified(C)
80 type_assign_specific_version_unsafe(C, orig_version + 5)
81 type_assign_version(C) # this should do nothing
82
83 new_version = type_get_version(C)
84 self.assertEqual(new_version, orig_version + 5)
85
86 clear_type_cache()
87
88 def test_per_class_limit(self):
89 class C:
90 x = 0
91
92 type_assign_version(C)
93 orig_version = type_get_version(C)
94 for i in range(1001):
95 C.x = i
96 type_assign_version(C)
97
98 new_version = type_get_version(C)
99 self.assertEqual(new_version, 0)
100
101 def test_119462(self):
102
103 class Holder:
104 value = None
105
106 @classmethod
107 def set_value(cls):
108 cls.value = object()
109
110 class HolderSub(Holder):
111 pass
112
113 for _ in range(1050):
114 Holder.set_value()
115 HolderSub.value
116
117 @support.cpython_only
118 class TypeCacheWithSpecializationTests(unittest.TestCase):
119 def tearDown(self):
120 clear_type_cache()
121
122 def _assign_valid_version_or_skip(self, type_):
123 type_modified(type_)
124 type_assign_version(type_)
125 if type_get_version(type_) == 0:
126 self.skipTest("Could not assign valid type version")
127
128 def _no_more_versions(self, user_type):
129 type_modified(user_type)
130 for _ in range(1001):
131 type_assign_specific_version_unsafe(user_type, 1000_000_000)
132 type_assign_specific_version_unsafe(user_type, 0)
133 self.assertEqual(type_get_version(user_type), 0)
134
135 def _all_opnames(self, func):
136 return set(instr.opname for instr in dis.Bytecode(func, adaptive=True))
137
138 def _check_specialization(self, func, arg, opname, *, should_specialize):
139 for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
140 func(arg)
141
142 if should_specialize:
143 self.assertNotIn(opname, self._all_opnames(func))
144 else:
145 self.assertIn(opname, self._all_opnames(func))
146
147 @requires_specialization
148 def test_class_load_attr_specialization_user_type(self):
149 class A:
150 def foo(self):
151 pass
152
153 self._assign_valid_version_or_skip(A)
154
155 def load_foo_1(type_):
156 type_.foo
157
158 self._check_specialization(load_foo_1, A, "LOAD_ATTR", should_specialize=True)
159 del load_foo_1
160
161 self._no_more_versions(A)
162
163 def load_foo_2(type_):
164 return type_.foo
165
166 self._check_specialization(load_foo_2, A, "LOAD_ATTR", should_specialize=False)
167
168 @requires_specialization
169 def test_class_load_attr_specialization_static_type(self):
170 self.assertNotEqual(type_get_version(str), 0)
171 self.assertNotEqual(type_get_version(bytes), 0)
172
173 def get_capitalize_1(type_):
174 return type_.capitalize
175
176 self._check_specialization(get_capitalize_1, str, "LOAD_ATTR", should_specialize=True)
177 self.assertEqual(get_capitalize_1(str)('hello'), 'Hello')
178 self.assertEqual(get_capitalize_1(bytes)(b'hello'), b'Hello')
179
180 @requires_specialization
181 def test_property_load_attr_specialization_user_type(self):
182 class G:
183 @property
184 def x(self):
185 return 9
186
187 self._assign_valid_version_or_skip(G)
188
189 def load_x_1(instance):
190 instance.x
191
192 self._check_specialization(load_x_1, G(), "LOAD_ATTR", should_specialize=True)
193 del load_x_1
194
195 self._no_more_versions(G)
196
197 def load_x_2(instance):
198 instance.x
199
200 self._check_specialization(load_x_2, G(), "LOAD_ATTR", should_specialize=False)
201
202 @requires_specialization
203 def test_store_attr_specialization_user_type(self):
204 class B:
205 __slots__ = ("bar",)
206
207 self._assign_valid_version_or_skip(B)
208
209 def store_bar_1(type_):
210 type_.bar = 10
211
212 self._check_specialization(store_bar_1, B(), "STORE_ATTR", should_specialize=True)
213 del store_bar_1
214
215 self._no_more_versions(B)
216
217 def store_bar_2(type_):
218 type_.bar = 10
219
220 self._check_specialization(store_bar_2, B(), "STORE_ATTR", should_specialize=False)
221
222 @requires_specialization_ft
223 def test_class_call_specialization_user_type(self):
224 class F:
225 def __init__(self):
226 pass
227
228 self._assign_valid_version_or_skip(F)
229
230 def call_class_1(type_):
231 type_()
232
233 self._check_specialization(call_class_1, F, "CALL", should_specialize=True)
234 del call_class_1
235
236 self._no_more_versions(F)
237
238 def call_class_2(type_):
239 type_()
240
241 self._check_specialization(call_class_2, F, "CALL", should_specialize=False)
242
243 @requires_specialization
244 def test_to_bool_specialization_user_type(self):
245 class H:
246 pass
247
248 self._assign_valid_version_or_skip(H)
249
250 def to_bool_1(instance):
251 not instance
252
253 self._check_specialization(to_bool_1, H(), "TO_BOOL", should_specialize=True)
254 del to_bool_1
255
256 self._no_more_versions(H)
257
258 def to_bool_2(instance):
259 not instance
260
261 self._check_specialization(to_bool_2, H(), "TO_BOOL", should_specialize=False)
262
263
264 if __name__ == "__main__":
265 unittest.main()