Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions mypyc/irbuild/specialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
dict_setdefault_spec_init_op,
dict_values_op,
)
from mypyc.primitives.list_ops import new_list_set_item_op
from mypyc.primitives.list_ops import isinstance_list, new_list_set_item_op
from mypyc.primitives.str_ops import (
str_encode_ascii_strict,
str_encode_latin1_strict,
Expand Down Expand Up @@ -546,6 +546,9 @@ def gen_inner_stmts() -> None:
return retval


isinstance_primitives = {"list": isinstance_list}


@specialize_function("builtins.isinstance")
def translate_isinstance(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None:
"""Special case for builtins.isinstance.
Expand All @@ -554,11 +557,10 @@ def translate_isinstance(builder: IRBuilder, expr: CallExpr, callee: RefExpr) ->
there is no need to coerce something to a new type before checking
what type it is, and the coercion could lead to bugs.
"""
if (
len(expr.args) == 2
and expr.arg_kinds == [ARG_POS, ARG_POS]
and isinstance(expr.args[1], (RefExpr, TupleExpr))
):
if not (len(expr.args) == 2 and expr.arg_kinds == [ARG_POS, ARG_POS]):
return None

if isinstance(expr.args[1], (RefExpr, TupleExpr)):
builder.types[expr.args[0]] = AnyType(TypeOfAny.from_error)

irs = builder.flatten_classes(expr.args[1])
Expand All @@ -569,6 +571,15 @@ def translate_isinstance(builder: IRBuilder, expr: CallExpr, callee: RefExpr) ->
)
obj = builder.accept(expr.args[0], can_borrow=can_borrow)
return builder.builder.isinstance_helper(obj, irs, expr.line)

if isinstance(expr.args[1], RefExpr):
node = expr.args[1].node
if node:
desc = isinstance_primitives.get(node.name)
if desc:
obj = builder.accept(expr.args[0])
return builder.primitive_op(desc, [obj], expr.line)

return None


Expand Down
9 changes: 9 additions & 0 deletions mypyc/primitives/list_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@
extra_int_constants=[(0, int_rprimitive)],
)

# isinstance(obj, list)
isinstance_list = function_op(
name="builtins.isinstance",
arg_types=[object_rprimitive],
return_type=bit_rprimitive,
c_function_name="PyList_Check",
error_kind=ERR_NEVER,
)

new_list_op = custom_op(
arg_types=[c_pyssize_t_rprimitive],
return_type=list_rprimitive,
Expand Down
32 changes: 13 additions & 19 deletions mypyc/test-data/irbuild-lists.test
Original file line number Diff line number Diff line change
Expand Up @@ -498,29 +498,23 @@ def nested_union(a: Union[List[str], List[Optional[str]]]) -> None:
[out]
def narrow(a):
a :: union[list, int]
r0 :: object
r1 :: i32
r2 :: bit
r3 :: bool
r4 :: list
r5 :: native_int
r6 :: short_int
r7 :: int
r0 :: bit
r1 :: list
r2 :: native_int
r3 :: short_int
r4 :: int
L0:
r0 = load_address PyList_Type
r1 = PyObject_IsInstance(a, r0)
r2 = r1 >= 0 :: signed
r3 = truncate r1: i32 to builtins.bool
if r3 goto L1 else goto L2 :: bool
r0 = PyList_Check(a)
if r0 goto L1 else goto L2 :: bool
L1:
r4 = borrow cast(list, a)
r5 = var_object_size r4
r6 = r5 << 1
r1 = borrow cast(list, a)
r2 = var_object_size r1
r3 = r2 << 1
keep_alive a
return r6
return r3
L2:
r7 = unbox(int, a)
return r7
r4 = unbox(int, a)
return r4
def loop(a):
a :: list
r0 :: short_int
Expand Down
11 changes: 11 additions & 0 deletions mypyc/test-data/run-lists.test
Original file line number Diff line number Diff line change
Expand Up @@ -537,3 +537,14 @@ def test_sorted() -> None:
assert sorted((2, 1, 3)) == res
assert sorted({2, 1, 3}) == res
assert sorted({2: "", 1: "", 3: ""}) == res

[case testIsInstance]
def test() -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Similar to my other comment about test_ prefix.

assert isinstance([], list)
assert isinstance([1,2,3], list)
assert isinstance(['a','b'], list)

assert not isinstance({}, list)
assert not isinstance((), list)
assert not isinstance(1, list)
assert not isinstance('a', list)