Skip to content

Commit 38370a3

Browse files
committed
Add a metaclass test
1 parent 60d21fa commit 38370a3

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,6 +2053,31 @@ def testfunc(n):
20532053
self.assertNotIn("_TO_BOOL_BOOL", uops)
20542054
self.assertIn("_GUARD_IS_TRUE_POP", uops)
20552055

2056+
def test_call_isinstance_metaclass(self):
2057+
class EvenNumberMeta(type):
2058+
def __instancecheck__(self, number):
2059+
return number % 2 == 0
2060+
2061+
class EvenNumber(metaclass=EvenNumberMeta):
2062+
pass
2063+
2064+
def testfunc(n):
2065+
x = 0
2066+
for _ in range(n):
2067+
# Only narrowed to bool
2068+
y = isinstance(42, EvenNumber)
2069+
if y:
2070+
x += 1
2071+
return x
2072+
2073+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
2074+
self.assertEqual(res, TIER2_THRESHOLD)
2075+
self.assertIsNotNone(ex)
2076+
uops = get_opnames(ex)
2077+
self.assertIn("_CALL_ISINSTANCE", uops)
2078+
self.assertNotIn("_TO_BOOL_BOOL", uops)
2079+
self.assertIn("_GUARD_IS_TRUE_POP", uops)
2080+
20562081

20572082
def global_identity(x):
20582083
return x

0 commit comments

Comments
 (0)