| 1 |
""" Tests for the internal type cache in CPython. """ |
| 2 |
import unittest |
| 3 |
import dis |
| 4 |
from test import support |
| 5 |
from test.support import import_helper |
| 6 |
try: |
| 7 |
from sys import _clear_type_cache |
| 8 |
except ImportError: |
| 9 |
_clear_type_cache = None |
| 10 |
|
| 11 |
|
| 12 |
_testcapi = import_helper.import_module("_testcapi") |
| 13 |
type_get_version = _testcapi.type_get_version |
| 14 |
type_assign_specific_version_unsafe = _testcapi.type_assign_specific_version_unsafe |
| 15 |
type_modified = _testcapi.type_modified |
| 16 |
|
| 17 |
|
| 18 |
def type_assign_version(type_): |
| 19 |
try: |
| 20 |
type_.x |
| 21 |
except AttributeError: |
| 22 |
pass |
| 23 |
|
| 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 |
|
| 33 |
Y = type('Y', (), {}) |
| 34 |
Y.x = 1 |
| 35 |
Y.x |
| 36 |
y_ver = type_get_version(Y) |
| 37 |
|
| 38 |
if y_ver == 0 or y_ver > 0xFFFFF000: |
| 39 |
self.skipTest("Out of type version tags") |
| 40 |
|
| 41 |
|
| 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_specific_version(self): |
| 57 |
"""meta-test for type_assign_specific_version_unsafe""" |
| 58 |
class C: |
| 59 |
pass |
| 60 |
|
| 61 |
type_assign_version(C) |
| 62 |
orig_version = type_get_version(C) |
| 63 |
if orig_version == 0: |
| 64 |
self.skipTest("Could not assign a valid type version") |
| 65 |
|
| 66 |
type_modified(C) |
| 67 |
type_assign_specific_version_unsafe(C, orig_version + 5) |
| 68 |
type_assign_version(C) |
| 69 |
|
| 70 |
new_version = type_get_version(C) |
| 71 |
self.assertEqual(new_version, orig_version + 5) |
| 72 |
|
| 73 |
_clear_type_cache() |
| 74 |
|
| 75 |
|
| 76 |
@support.cpython_only |
| 77 |
class TypeCacheWithSpecializationTests(unittest.TestCase): |
| 78 |
def tearDown(self): |
| 79 |
_clear_type_cache() |
| 80 |
|
| 81 |
def _assign_valid_version_or_skip(self, type_): |
| 82 |
type_modified(type_) |
| 83 |
type_assign_version(type_) |
| 84 |
if type_get_version(type_) == 0: |
| 85 |
self.skipTest("Could not assign valid type version") |
| 86 |
|
| 87 |
def _assign_and_check_version_0(self, user_type): |
| 88 |
type_modified(user_type) |
| 89 |
type_assign_specific_version_unsafe(user_type, 0) |
| 90 |
self.assertEqual(type_get_version(user_type), 0) |
| 91 |
|
| 92 |
def _all_opnames(self, func): |
| 93 |
return set(instr.opname for instr in dis.Bytecode(func, adaptive=True)) |
| 94 |
|
| 95 |
def _check_specialization(self, func, arg, opname, *, should_specialize): |
| 96 |
for _ in range(100): |
| 97 |
func(arg) |
| 98 |
|
| 99 |
if should_specialize: |
| 100 |
self.assertNotIn(opname, self._all_opnames(func)) |
| 101 |
else: |
| 102 |
self.assertIn(opname, self._all_opnames(func)) |
| 103 |
|
| 104 |
def test_load_method_specialization_user_type(self): |
| 105 |
class A: |
| 106 |
def foo(self): |
| 107 |
pass |
| 108 |
|
| 109 |
self._assign_valid_version_or_skip(A) |
| 110 |
|
| 111 |
def load_foo_1(instance): |
| 112 |
instance.foo() |
| 113 |
|
| 114 |
self._check_specialization( |
| 115 |
load_foo_1, A(), "LOAD_METHOD_ADAPTIVE", should_specialize=True |
| 116 |
) |
| 117 |
del load_foo_1 |
| 118 |
|
| 119 |
self._assign_and_check_version_0(A) |
| 120 |
|
| 121 |
def load_foo_2(instance): |
| 122 |
instance.foo() |
| 123 |
|
| 124 |
self._check_specialization( |
| 125 |
load_foo_2, A(), "LOAD_METHOD_ADAPTIVE", should_specialize=False |
| 126 |
) |
| 127 |
|
| 128 |
def test_store_attr_specialization_user_type(self): |
| 129 |
class B: |
| 130 |
__slots__ = ("bar",) |
| 131 |
|
| 132 |
self._assign_valid_version_or_skip(B) |
| 133 |
|
| 134 |
def store_bar_1(instance): |
| 135 |
instance.bar = 10 |
| 136 |
|
| 137 |
self._check_specialization( |
| 138 |
store_bar_1, B(), "STORE_ATTR_ADAPTIVE", should_specialize=True |
| 139 |
) |
| 140 |
del store_bar_1 |
| 141 |
|
| 142 |
self._assign_and_check_version_0(B) |
| 143 |
|
| 144 |
def store_bar_2(instance): |
| 145 |
instance.bar = 10 |
| 146 |
|
| 147 |
self._check_specialization( |
| 148 |
store_bar_2, B(), "STORE_ATTR_ADAPTIVE", should_specialize=False |
| 149 |
) |
| 150 |
|
| 151 |
def test_load_attr_specialization_user_type(self): |
| 152 |
class C: |
| 153 |
__slots__ = ("biz",) |
| 154 |
def __init__(self): |
| 155 |
self.biz = 8 |
| 156 |
|
| 157 |
self._assign_valid_version_or_skip(C) |
| 158 |
|
| 159 |
def load_biz_1(type_): |
| 160 |
type_.biz |
| 161 |
|
| 162 |
self._check_specialization( |
| 163 |
load_biz_1, C(), "LOAD_ATTR_ADAPTIVE", should_specialize=True |
| 164 |
) |
| 165 |
del load_biz_1 |
| 166 |
|
| 167 |
self._assign_and_check_version_0(C) |
| 168 |
|
| 169 |
def load_biz_2(type_): |
| 170 |
type_.biz |
| 171 |
|
| 172 |
self._check_specialization( |
| 173 |
load_biz_2, C(), "LOAD_ATTR_ADAPTIVE", should_specialize=False |
| 174 |
) |
| 175 |
|
| 176 |
def test_binary_subscript_specialization_user_type(self): |
| 177 |
class D: |
| 178 |
def __getitem__(self, _): |
| 179 |
return 1 |
| 180 |
|
| 181 |
self._assign_valid_version_or_skip(D) |
| 182 |
|
| 183 |
def subscript_1(instance): |
| 184 |
instance[6] |
| 185 |
|
| 186 |
self._check_specialization( |
| 187 |
subscript_1, D(), "BINARY_SUBSCR_ADAPTIVE", should_specialize=True |
| 188 |
) |
| 189 |
del subscript_1 |
| 190 |
|
| 191 |
self._assign_and_check_version_0(D) |
| 192 |
|
| 193 |
def subscript_2(instance): |
| 194 |
instance[6] |
| 195 |
|
| 196 |
self._check_specialization( |
| 197 |
subscript_2, D(), "BINARY_SUBSCR_ADAPTIVE", should_specialize=False |
| 198 |
) |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
if __name__ == "__main__": |
| 203 |
unittest.main() |