irbuild-isinstance.test 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. [case testIsinstanceInt]
  2. def is_int(value: object) -> bool:
  3. return isinstance(value, int)
  4. [out]
  5. def is_int(value):
  6. value, r0 :: object
  7. r1 :: i32
  8. r2 :: bit
  9. r3 :: bool
  10. L0:
  11. r0 = load_address PyLong_Type
  12. r1 = PyObject_IsInstance(value, r0)
  13. r2 = r1 >= 0 :: signed
  14. r3 = truncate r1: i32 to builtins.bool
  15. return r3
  16. [case testIsinstanceNotBool1]
  17. def is_not_bool(value: object) -> bool:
  18. return not isinstance(value, bool)
  19. [out]
  20. def is_not_bool(value):
  21. value, r0 :: object
  22. r1 :: i32
  23. r2 :: bit
  24. r3, r4 :: bool
  25. L0:
  26. r0 = load_address PyBool_Type
  27. r1 = PyObject_IsInstance(value, r0)
  28. r2 = r1 >= 0 :: signed
  29. r3 = truncate r1: i32 to builtins.bool
  30. r4 = r3 ^ 1
  31. return r4
  32. [case testIsinstanceIntAndNotBool]
  33. # This test is to ensure that 'value' doesn't get coerced to int when we are
  34. # checking if it's a bool, since an int can never be an instance of a bool
  35. def is_not_bool_and_is_int(value: object) -> bool:
  36. return isinstance(value, int) and not isinstance(value, bool)
  37. [out]
  38. def is_not_bool_and_is_int(value):
  39. value, r0 :: object
  40. r1 :: i32
  41. r2 :: bit
  42. r3, r4 :: bool
  43. r5 :: object
  44. r6 :: i32
  45. r7 :: bit
  46. r8, r9 :: bool
  47. L0:
  48. r0 = load_address PyLong_Type
  49. r1 = PyObject_IsInstance(value, r0)
  50. r2 = r1 >= 0 :: signed
  51. r3 = truncate r1: i32 to builtins.bool
  52. if r3 goto L2 else goto L1 :: bool
  53. L1:
  54. r4 = r3
  55. goto L3
  56. L2:
  57. r5 = load_address PyBool_Type
  58. r6 = PyObject_IsInstance(value, r5)
  59. r7 = r6 >= 0 :: signed
  60. r8 = truncate r6: i32 to builtins.bool
  61. r9 = r8 ^ 1
  62. r4 = r9
  63. L3:
  64. return r4
  65. [case testBorrowSpecialCaseWithIsinstance]
  66. class C:
  67. s: str
  68. def g() -> object:
  69. pass
  70. def f() -> None:
  71. x = g()
  72. if isinstance(x, C):
  73. x.s
  74. [out]
  75. def g():
  76. r0 :: object
  77. L0:
  78. r0 = box(None, 1)
  79. return r0
  80. def f():
  81. r0, x, r1 :: object
  82. r2 :: ptr
  83. r3 :: object
  84. r4 :: bit
  85. r5 :: __main__.C
  86. r6 :: str
  87. L0:
  88. r0 = g()
  89. x = r0
  90. r1 = __main__.C :: type
  91. r2 = get_element_ptr x ob_type :: PyObject
  92. r3 = load_mem r2 :: builtins.object*
  93. keep_alive x
  94. r4 = r3 == r1
  95. if r4 goto L1 else goto L2 :: bool
  96. L1:
  97. r5 = borrow cast(__main__.C, x)
  98. r6 = r5.s
  99. keep_alive x
  100. L2:
  101. return 1