Skip to content

Commit 467bcb9

Browse files
committed
Add more tests
1 parent 97aca36 commit 467bcb9

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

Lib/test/test_capi/test_opt.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2204,6 +2204,25 @@ def testfunc(n):
22042204
self.assertNotIn("_POP_CALL_ONE_LOAD_CONST_INLINE_BORROW", uops)
22052205
self.assertNotIn("_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW", uops)
22062206

2207+
def test_call_isinstance_tuple_unknown_length(self):
2208+
def testfunc(n):
2209+
x = 0
2210+
for _ in range(n):
2211+
# tuple with an unknown length, we only narrow to bool
2212+
tup = tuple(eval('(int, str)'))
2213+
y = isinstance(42, tup)
2214+
if y:
2215+
x += 1
2216+
return x
2217+
2218+
res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
2219+
self.assertEqual(res, TIER2_THRESHOLD)
2220+
self.assertIsNotNone(ex)
2221+
uops = get_opnames(ex)
2222+
self.assertIn("_CALL_ISINSTANCE", uops)
2223+
self.assertNotIn("_TO_BOOL_BOOL", uops)
2224+
self.assertIn("_GUARD_IS_TRUE_POP", uops)
2225+
22072226
def test_call_isinstance_metaclass(self):
22082227
class EvenNumberMeta(type):
22092228
def __instancecheck__(self, number):

Python/optimizer_bytecodes.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -965,11 +965,10 @@ dummy_func(void) {
965965
// inst is not an instance of any of them.
966966

967967
int length = sym_tuple_length(cls);
968-
bool all_items_known = true;
969-
PyObject *out = NULL;
970-
if (length >= 0) {
971-
// We cannot do anything about tuples with unknown (length == -1)
972-
968+
if (length != -1) {
969+
// We cannot do anything about tuples with unknown length
970+
bool all_items_known = true;
971+
PyObject *out = NULL;
973972
for (int i = 0; i < length; i++) {
974973
JitOptSymbol *item = sym_tuple_getitem(ctx, cls, i);
975974
if (!sym_has_type(item)) {

Python/optimizer_cases.c.h

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)