| [case testIsinstanceInt] | |
| def is_int(value: object) -> bool: | |
| return isinstance(value, int) | |
| [out] | |
| def is_int(value): | |
| value :: object | |
| r0 :: bit | |
| L0: | |
| r0 = PyLong_Check(value) | |
| return r0 | |
| [case testIsinstanceNotBool1] | |
| def is_not_bool(value: object) -> bool: | |
| return not isinstance(value, bool) | |
| [out] | |
| def is_not_bool(value): | |
| value :: object | |
| r0, r1 :: bit | |
| L0: | |
| r0 = PyBool_Check(value) | |
| r1 = r0 ^ 1 | |
| return r1 | |
| [case testIsinstanceIntAndNotBool] | |
| # This test is to ensure that 'value' doesn't get coerced to int when we are | |
| # checking if it's a bool, since an int can never be an instance of a bool | |
| def is_not_bool_and_is_int(value: object) -> bool: | |
| return isinstance(value, int) and not isinstance(value, bool) | |
| [out] | |
| def is_not_bool_and_is_int(value): | |
| value :: object | |
| r0 :: bit | |
| r1 :: bool | |
| r2, r3 :: bit | |
| L0: | |
| r0 = PyLong_Check(value) | |
| if r0 goto L2 else goto L1 :: bool | |
| L1: | |
| r1 = r0 | |
| goto L3 | |
| L2: | |
| r2 = PyBool_Check(value) | |
| r3 = r2 ^ 1 | |
| r1 = r3 | |
| L3: | |
| return r1 | |
| [case testBorrowSpecialCaseWithIsinstance_withgil] | |
| class C: | |
| s: str | |
| def g() -> object: | |
| pass | |
| def f() -> None: | |
| x = g() | |
| if isinstance(x, C): | |
| x.s | |
| [out] | |
| def g(): | |
| r0 :: object | |
| L0: | |
| r0 = box(None, 1) | |
| return r0 | |
| def f(): | |
| r0, x, r1 :: object | |
| r2 :: ptr | |
| r3 :: object | |
| r4 :: bit | |
| r5 :: __main__.C | |
| r6 :: str | |
| L0: | |
| r0 = g() | |
| x = r0 | |
| r1 = __main__.C :: type | |
| r2 = get_element_ptr x ob_type :: PyObject | |
| r3 = borrow load_mem r2 :: builtins.object* | |
| keep_alive x | |
| r4 = r3 == r1 | |
| if r4 goto L1 else goto L2 :: bool | |
| L1: | |
| r5 = borrow cast(__main__.C, x) | |
| r6 = r5.s | |
| keep_alive x | |
| L2: | |
| return 1 | |
| [case testBytes] | |
| from typing import Any | |
| def is_bytes(x: Any) -> bool: | |
| return isinstance(x, bytes) | |
| def is_bytearray(x: Any) -> bool: | |
| return isinstance(x, bytearray) | |
| [out] | |
| def is_bytes(x): | |
| x :: object | |
| r0 :: bit | |
| L0: | |
| r0 = PyBytes_Check(x) | |
| return r0 | |
| def is_bytearray(x): | |
| x :: object | |
| r0 :: bit | |
| L0: | |
| r0 = PyByteArray_Check(x) | |
| return r0 | |
| [case testDict] | |
| from typing import Any | |
| def is_dict(x: Any) -> bool: | |
| return isinstance(x, dict) | |
| [out] | |
| def is_dict(x): | |
| x :: object | |
| r0 :: bit | |
| L0: | |
| r0 = PyDict_Check(x) | |
| return r0 | |
| [case testFloat] | |
| from typing import Any | |
| def is_float(x: Any) -> bool: | |
| return isinstance(x, float) | |
| [out] | |
| def is_float(x): | |
| x :: object | |
| r0 :: bit | |
| L0: | |
| r0 = PyFloat_Check(x) | |
| return r0 | |
| [case testSet] | |
| from typing import Any | |
| def is_set(x: Any) -> bool: | |
| return isinstance(x, set) | |
| def is_frozenset(x: Any) -> bool: | |
| return isinstance(x, frozenset) | |
| [out] | |
| def is_set(x): | |
| x :: object | |
| r0 :: bit | |
| L0: | |
| r0 = PySet_Check(x) | |
| return r0 | |
| def is_frozenset(x): | |
| x :: object | |
| r0 :: bit | |
| L0: | |
| r0 = PyFrozenSet_Check(x) | |
| return r0 | |
| [case testStr] | |
| from typing import Any | |
| def is_str(x: Any) -> bool: | |
| return isinstance(x, str) | |
| [out] | |
| def is_str(x): | |
| x :: object | |
| r0 :: bit | |
| L0: | |
| r0 = PyUnicode_Check(x) | |
| return r0 | |
| [case testTuple] | |
| from typing import Any | |
| def is_tuple(x: Any) -> bool: | |
| return isinstance(x, tuple) | |
| [out] | |
| def is_tuple(x): | |
| x :: object | |
| r0 :: bit | |
| L0: | |
| r0 = PyTuple_Check(x) | |
| return r0 | |
| [case testTupleOfPrimitives] | |
| from typing import Any | |
| def is_instance(x: Any) -> bool: | |
| return isinstance(x, (str, int, bytes)) | |
| [out] | |
| def is_instance(x): | |
| x :: object | |
| r0, r1, r2 :: bit | |
| r3 :: bool | |
| L0: | |
| r0 = PyUnicode_Check(x) | |
| if r0 goto L3 else goto L1 :: bool | |
| L1: | |
| r1 = PyLong_Check(x) | |
| if r1 goto L3 else goto L2 :: bool | |
| L2: | |
| r2 = PyBytes_Check(x) | |
| if r2 goto L3 else goto L4 :: bool | |
| L3: | |
| r3 = 1 | |
| goto L5 | |
| L4: | |
| r3 = 0 | |
| L5: | |
| return r3 | |
| [case testIsinstanceOtherBuiltinTypes] | |
| from typing import Any | |
| def is_complex(x: Any) -> bool: | |
| return isinstance(x, complex) | |
| def is_type(x: Any) -> bool: | |
| return isinstance(x, type) | |
| def is_range(x: Any) -> bool: | |
| return isinstance(x, range) | |
| def is_slice(x: Any) -> bool: | |
| return isinstance(x, slice) | |
| def is_memoryview(x: Any) -> bool: | |
| return isinstance(x, memoryview) | |
| def type_is_complex(x: Any) -> bool: | |
| return type(x) is complex | |
| def is_float_or_complex(x: Any) -> bool: | |
| return isinstance(x, (float, complex)) | |
| [out] | |
| def is_complex(x): | |
| x :: object | |
| r0 :: bit | |
| L0: | |
| r0 = PyComplex_Check(x) | |
| return r0 | |
| def is_type(x): | |
| x :: object | |
| r0 :: bit | |
| L0: | |
| r0 = PyType_Check(x) | |
| return r0 | |
| def is_range(x): | |
| x :: object | |
| r0 :: bit | |
| L0: | |
| r0 = PyRange_Check(x) | |
| return r0 | |
| def is_slice(x): | |
| x :: object | |
| r0 :: bit | |
| L0: | |
| r0 = PySlice_Check(x) | |
| return r0 | |
| def is_memoryview(x): | |
| x :: object | |
| r0 :: bit | |
| L0: | |
| r0 = PyMemoryView_Check(x) | |
| return r0 | |
| def type_is_complex(x): | |
| x, r0, r1 :: object | |
| r2 :: bit | |
| L0: | |
| r0 = CPy_TYPE(x) | |
| r1 = load_address PyComplex_Type | |
| r2 = r0 == r1 | |
| return r2 | |
| def is_float_or_complex(x): | |
| x :: object | |
| r0, r1 :: bit | |
| r2 :: bool | |
| L0: | |
| r0 = PyFloat_Check(x) | |
| if r0 goto L2 else goto L1 :: bool | |
| L1: | |
| r1 = PyComplex_Check(x) | |
| if r1 goto L2 else goto L3 :: bool | |
| L2: | |
| r2 = 1 | |
| goto L4 | |
| L3: | |
| r2 = 0 | |
| L4: | |
| return r2 | |
| [case testIsinstanceTupleOfPrimitiveAndOtherClasses] | |
| from typing import Any | |
| class C: pass | |
| def is_instance(x: Any) -> bool: | |
| return isinstance(x, (float, ValueError, C)) | |
| [out] | |
| def is_instance(x): | |
| x, r0 :: object | |
| r1 :: str | |
| r2 :: object | |
| r3 :: bit | |
| r4 :: i32 | |
| r5 :: bit | |
| r6 :: bool | |
| r7 :: object | |
| r8 :: ptr | |
| r9 :: object | |
| r10 :: bit | |
| r11 :: bool | |
| L0: | |
| r0 = builtins :: module | |
| r1 = 'ValueError' | |
| r2 = CPyObject_GetAttr(r0, r1) | |
| r3 = PyFloat_Check(x) | |
| if r3 goto L3 else goto L1 :: bool | |
| L1: | |
| r4 = PyObject_IsInstance(x, r2) | |
| r5 = r4 >= 0 :: signed | |
| r6 = truncate r4: i32 to builtins.bool | |
| if r6 goto L3 else goto L2 :: bool | |
| L2: | |
| r7 = __main__.C :: type | |
| r8 = get_element_ptr x ob_type :: PyObject | |
| r9 = borrow load_mem r8 :: builtins.object* | |
| keep_alive x | |
| r10 = r9 == r7 | |
| if r10 goto L3 else goto L4 :: bool | |
| L3: | |
| r11 = 1 | |
| goto L5 | |
| L4: | |
| r11 = 0 | |
| L5: | |
| return r11 | |
| [case testIsinstanceNestedTuple] | |
| from typing import Any | |
| def is_instance(x: Any) -> bool: | |
| return isinstance(x, (int, (float, str))) | |
| [out] | |
| def is_instance(x): | |
| x :: object | |
| r0, r1, r2 :: bit | |
| r3 :: bool | |
| L0: | |
| r0 = PyLong_Check(x) | |
| if r0 goto L3 else goto L1 :: bool | |
| L1: | |
| r1 = PyFloat_Check(x) | |
| if r1 goto L3 else goto L2 :: bool | |
| L2: | |
| r2 = PyUnicode_Check(x) | |
| if r2 goto L3 else goto L4 :: bool | |
| L3: | |
| r3 = 1 | |
| goto L5 | |
| L4: | |
| r3 = 0 | |
| L5: | |
| return r3 | |
| [case testIsinstanceTupleWithNonNativeClass] | |
| from typing import Any | |
| from mypy_extensions import mypyc_attr | |
| @mypyc_attr(native_class=False) | |
| class M(type): pass | |
| class C(metaclass=M): pass | |
| def is_instance(x: Any) -> bool: | |
| # M could override __instancecheck__ | |
| return isinstance(x, (C, int)) | |
| [out] | |
| def is_instance(x): | |
| x, r0 :: object | |
| r1 :: i32 | |
| r2 :: bit | |
| r3 :: bool | |
| r4 :: bit | |
| r5 :: bool | |
| L0: | |
| r0 = __main__.C :: type | |
| r1 = PyObject_IsInstance(x, r0) | |
| r2 = r1 >= 0 :: signed | |
| r3 = truncate r1: i32 to builtins.bool | |
| if r3 goto L2 else goto L1 :: bool | |
| L1: | |
| r4 = PyLong_Check(x) | |
| if r4 goto L2 else goto L3 :: bool | |
| L2: | |
| r5 = 1 | |
| goto L4 | |
| L3: | |
| r5 = 0 | |
| L4: | |
| return r5 | |
| [case testIsinstanceEmptyTupleEvaluatesObject] | |
| def f() -> object: | |
| return 1 | |
| def is_instance() -> bool: | |
| return isinstance(f(), ()) | |
| [out] | |
| def f(): | |
| r0 :: object | |
| L0: | |
| r0 = object 1 | |
| return r0 | |
| def is_instance(): | |
| r0 :: object | |
| L0: | |
| r0 = f() | |
| return 0 |