Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Prev Previous commit
Next Next commit
Add more tests
  • Loading branch information
tomasr8 committed May 22, 2025
commit 467bcb9eac17de46f7e8cc6252ee9e37cab74984
19 changes: 19 additions & 0 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2204,6 +2204,25 @@ def testfunc(n):
self.assertNotIn("_POP_CALL_ONE_LOAD_CONST_INLINE_BORROW", uops)
self.assertNotIn("_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW", uops)

def test_call_isinstance_tuple_unknown_length(self):
def testfunc(n):
x = 0
for _ in range(n):
# tuple with an unknown length, we only narrow to bool
tup = tuple(eval('(int, str)'))
y = isinstance(42, tup)
if y:
x += 1
return x

res, ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
self.assertEqual(res, TIER2_THRESHOLD)
self.assertIsNotNone(ex)
uops = get_opnames(ex)
self.assertIn("_CALL_ISINSTANCE", uops)
self.assertNotIn("_TO_BOOL_BOOL", uops)
self.assertIn("_GUARD_IS_TRUE_POP", uops)

def test_call_isinstance_metaclass(self):
class EvenNumberMeta(type):
def __instancecheck__(self, number):
Expand Down
9 changes: 4 additions & 5 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -965,11 +965,10 @@ dummy_func(void) {
// inst is not an instance of any of them.

int length = sym_tuple_length(cls);
bool all_items_known = true;
PyObject *out = NULL;
if (length >= 0) {
// We cannot do anything about tuples with unknown (length == -1)

if (length != -1) {
// We cannot do anything about tuples with unknown length
bool all_items_known = true;
PyObject *out = NULL;
for (int i = 0; i < length; i++) {
JitOptSymbol *item = sym_tuple_getitem(ctx, cls, i);
if (!sym_has_type(item)) {
Expand Down
8 changes: 4 additions & 4 deletions Python/optimizer_cases.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading