Skip to content

Commit 49aa010

Browse files
committed
Address review
1 parent d9a5115 commit 49aa010

2 files changed

Lines changed: 40 additions & 45 deletions

File tree

Lib/test/test_capi/test_opt.py

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import contextlib
2-
import enum
32
import itertools
43
import sys
54
import textwrap
@@ -3512,50 +3511,6 @@ def f(n):
35123511
self.assertNotIn("_LOAD_ATTR_METHOD_NO_DICT", uops)
35133512
self.assertIn("_LOAD_CONST_INLINE_BORROW", uops)
35143513

3515-
def test_load_attr_class_with_metaclass_check(self):
3516-
# LOAD_ATTR_CLASS_WITH_METACLASS_CHECK must check
3517-
# for `__class__` writes, see gh-149239
3518-
class ColorMeta(enum.EnumType):
3519-
pass
3520-
3521-
class Color(enum.IntEnum, metaclass=ColorMeta):
3522-
RED = 1
3523-
3524-
red = Color.RED
3525-
3526-
def f1(n):
3527-
for _ in range(n):
3528-
assert Color.RED == 1
3529-
return n
3530-
3531-
res, ex = self._run_with_optimizer(f1, TIER2_THRESHOLD)
3532-
self.assertIsNotNone(ex)
3533-
self.assertEqual(res, TIER2_THRESHOLD)
3534-
uops = get_opnames(ex)
3535-
self.assertIn("_CHECK_ATTR_CLASS", uops)
3536-
self.assertIn("_GUARD_TYPE_VERSION", uops)
3537-
3538-
# Reassign the `__class__` attr to deopt:
3539-
class Descriptor(enum.IntEnum):
3540-
RED = 1
3541-
3542-
def __get__(self, obj, owner):
3543-
return "descr"
3544-
3545-
red.__class__ = Descriptor
3546-
3547-
def f2(n):
3548-
for _ in range(n):
3549-
assert Color.RED == 'descr'
3550-
return n
3551-
3552-
res, ex = self._run_with_optimizer(f2, TIER2_THRESHOLD)
3553-
self.assertIsNotNone(ex)
3554-
self.assertEqual(res, TIER2_THRESHOLD)
3555-
uops = get_opnames(ex)
3556-
self.assertNotIn("_CHECK_ATTR_CLASS", uops)
3557-
self.assertNotIn("_GUARD_TYPE_VERSION", uops)
3558-
35593514
def test_cached_load_special(self):
35603515
class CM:
35613516
def __enter__(self):

Lib/test/test_opcache.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import enum
12
import copy
23
import pickle
34
import dis
@@ -2114,6 +2115,45 @@ def load_enum_member():
21142115
self.assert_specialized(load_enum_member,
21152116
"LOAD_ATTR_CLASS_WITH_METACLASS_CHECK")
21162117

2118+
@cpython_only
2119+
@requires_specialization
2120+
def test_load_attr_class_with_metaclass_check_149239(self):
2121+
# LOAD_ATTR_CLASS_WITH_METACLASS_CHECK must check
2122+
# for `__class__` writes, see gh-149239
2123+
class ColorMeta(enum.EnumType):
2124+
pass
2125+
2126+
class Color(enum.IntEnum, metaclass=ColorMeta):
2127+
RED = 1
2128+
2129+
red = Color.RED
2130+
2131+
def f1():
2132+
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
2133+
assert Color.RED == 1
2134+
2135+
f1()
2136+
self.assert_specialized(f1,
2137+
"LOAD_ATTR_CLASS_WITH_METACLASS_CHECK")
2138+
2139+
# Reassign the `__class__` attr to deopt:
2140+
class Descriptor(enum.IntEnum):
2141+
RED = 1
2142+
2143+
def __get__(self, obj, owner):
2144+
return "descr"
2145+
2146+
red.__class__ = Descriptor
2147+
2148+
def f2():
2149+
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
2150+
assert Color.RED == 'descr'
2151+
2152+
f2()
2153+
self.assert_no_opcode(f2,
2154+
"LOAD_ATTR_CLASS_WITH_METACLASS_CHECK")
2155+
2156+
21172157

21182158
if __name__ == "__main__":
21192159
unittest.main()

0 commit comments

Comments
 (0)