-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathirbuild-isinstance.test
More file actions
219 lines (188 loc) · 3.62 KB
/
irbuild-isinstance.test
File metadata and controls
219 lines (188 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
[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]
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