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

Skip to content
Prev Previous commit
Next Next commit
Bail when encountering an unknown class
  • Loading branch information
tomasr8 committed Jul 5, 2025
commit d2e339f98775753b0abb78ca1f52d68be9d9ce7b
80 changes: 74 additions & 6 deletions Lib/test/test_capi/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2143,13 +2143,13 @@ def testfunc(n):
self.assertIn("_BUILD_TUPLE", uops)
self.assertIn("_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW", uops)

def test_call_isinstance_tuple_of_classes_true_unknown(self):
def test_call_isinstance_tuple_of_classes_true_unknown_1(self):
def testfunc(n):
x = 0
for _ in range(n):
# One of the classes is unknown, but we can still
# narrow to True
y = isinstance(42, (eval('str'), int))
# One of the classes is unknown, but it comes
# after a known class, so we can narrow to True
y = isinstance(42, (int, eval('str')))
if y:
x += 1
return x
Expand All @@ -2160,11 +2160,30 @@ def testfunc(n):
uops = get_opnames(ex)
self.assertNotIn("_CALL_ISINSTANCE", uops)
self.assertNotIn("_TO_BOOL_BOOL", uops)
self.assertNotIn("_GUARD_IS_TRUE_POP", uops)
self.assertNotIn("_GUARD_IS_FALSE_POP", uops)
self.assertIn("_BUILD_TUPLE", uops)
self.assertIn("_POP_CALL_TWO_LOAD_CONST_INLINE_BORROW", uops)

def test_call_isinstance_tuple_of_classes_unknown_not_narrowed(self):
def test_call_isinstance_tuple_of_classes_true_unknown_2(self):
def testfunc(n):
x = 0
for _ in range(n):
# One of the classes is unknown, so we can't narrow
# to True or False, only bool
y = isinstance(42, (eval('str'), int))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused, why can't we narrow to True? We can't remove the call, but the result is known.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that was a brainfart 😄 I somehow conflated narrowing to True/False with replacing the op, but we can obviously just narrow without removing the call as you said!

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_tuple_of_classes_true_unknown_3(self):
def testfunc(n):
x = 0
for _ in range(n):
Expand All @@ -2183,6 +2202,25 @@ def testfunc(n):
self.assertNotIn("_TO_BOOL_BOOL", uops)
self.assertIn("_GUARD_IS_TRUE_POP", uops)

def test_call_isinstance_tuple_of_classes_true_unknown_4(self):
def testfunc(n):
x = 0
for _ in range(n):
# One of the classes is unknown, so we can't narrow
# to True or False, only bool
y = isinstance(42, (eval('int'), str))
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_empty_tuple(self):
def testfunc(n):
x = 0
Expand Down Expand Up @@ -2248,6 +2286,36 @@ def testfunc(n):
self.assertNotIn("_TO_BOOL_BOOL", uops)
self.assertIn("_GUARD_IS_TRUE_POP", uops)

def test_call_isinstance_tuple_metaclass(self):
calls = 0

class Meta(type):
def __instancecheck__(self, _):
nonlocal calls
calls += 1
return False

class Unknown(metaclass=Meta):
pass

def testfunc(n):
x = 0
for _ in range(n):
# Only narrowed to bool
y = isinstance(42, (Unknown, int))
if y:
x += 1
return x, calls

(res, calls), ex = self._run_with_optimizer(testfunc, TIER2_THRESHOLD)
self.assertEqual(res, TIER2_THRESHOLD)
self.assertEqual(calls, 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_set_type_version_sets_type(self):
class C:
A = 1
Expand Down
19 changes: 8 additions & 11 deletions Python/optimizer_bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -938,9 +938,6 @@ dummy_func(void) {
}

op(_CALL_ISINSTANCE, (unused, unused, instance, cls -- res)) {
// The below define is equivalent to PyObject_TypeCheck(inst, cls)
#define sym_IS_SUBTYPE(inst, cls) ((inst) == (cls) || PyType_IsSubtype(inst, cls))

// the result is always a bool, but sometimes we can
// narrow it down to True or False
res = sym_new_type(ctx, &PyBool_Type);
Expand All @@ -951,7 +948,7 @@ dummy_func(void) {
// known types, meaning we can deduce either True or False

PyObject *out = Py_False;
if (sym_IS_SUBTYPE(inst_type, cls_o)) {
if (inst_type == cls_o || PyType_IsSubtype(inst_type, cls_o)) {
out = Py_True;
}
sym_set_const(res, out);
Expand All @@ -972,23 +969,24 @@ dummy_func(void) {
for (int i = 0; i < length; i++) {
JitOptSymbol *item = sym_tuple_getitem(ctx, cls, i);
if (!sym_has_type(item)) {
// There is an unknown item in the tuple,
// we can no longer deduce False.
// There is an unknown item in the tuple.
// It could potentially define its own __instancecheck__
// method so we can only deduce bool.
all_items_known = false;
continue;
break;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be continue? We could still narrow to True later to handle cases like the one I called out above, right?

Suggested change
break;
continue;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, see #134543 (comment). I just needed to add some extra bookkeeping to know when we can replace the call and when not.

}
Comment thread
tomasr8 marked this conversation as resolved.
PyTypeObject *cls_o = (PyTypeObject *)sym_get_const(ctx, item);
if (cls_o &&
sym_matches_type(item, &PyType_Type) &&
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a comment explaining that this is to protect against metaclasses definine __instancecheck__.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you formulate it? I don't think of it as specifically a guard for __instancecheck__ but basically PyObject_TypeCheck adapted to the JIT optimizer.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're not only checking that the object is a subclass of type, we're also checking that it is an exact instance of type itself. We care about this second condition because it guarantees that __instancecheck__ doesn't exist (otherwise we would need to look it up to check if it exists or not).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clarification, I added a comment :)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, wouldn't this only work on something that is isinstance(x, type), like the actual type itself, not any type like int? Sorry, I'm confused!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I don't believe so. If item is e.g. int then sym_matches_type(item, &PyType_Type) should return true

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example something like this seems to work:

JitOptRef ref_type = _Py_uop_sym_new_const(ctx, (PyObject *)&PyLong_Type);
TEST_PREDICATE(_Py_uop_sym_matches_type(ref_type, &PyType_Type), "int is not a type");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That''s kinda strange. The code for _Py_uop_sym_matches_type is:

_Py_uop_sym_get_type(sym) == typ

so it's a pointer comparison, not a subclass check.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The subclass check is done after with PyType_IsSubtype if that's what you mean? The current version of _CALL_ISINSTANCE (which does not handle tuples) works the same way though. @brandtbucher, what do you think?

sym_IS_SUBTYPE(inst_type, cls_o))
(inst_type == cls_o || PyType_IsSubtype(inst_type, cls_o)))
{
out = Py_True;
break;
}
}
if (!out && all_items_known) {
if (out == NULL && all_items_known) {
// We haven't deduced True, but all items in the tuple are known
// so we can deduce False
// so we can deduce False.
out = Py_False;
}
if (out) {
Expand All @@ -997,7 +995,6 @@ dummy_func(void) {
}
}
}
#undef sym_IS_SUBTYPE
}

op(_GUARD_IS_TRUE_POP, (flag -- )) {
Expand Down
11 changes: 4 additions & 7 deletions Python/optimizer_cases.c.h

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

Loading