forked from python/mypy
/
irbuild-dunders.test
187 lines (169 loc) · 3.1 KB
/
irbuild-dunders.test
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
# Test cases for (some) dunder methods
[case testDundersLen]
class C:
def __len__(self) -> int:
return 2
def f(c: C) -> int:
return len(c)
[out]
def C.__len__(self):
self :: __main__.C
L0:
return 4
def f(c):
c :: __main__.C
r0 :: int
r1 :: native_int
r2, r3, r4 :: bit
r5 :: bool
L0:
r0 = c.__len__()
r1 = r0 & 1
r2 = r1 != 0
if r2 goto L1 else goto L2 :: bool
L1:
r3 = CPyTagged_IsLt_(r0, 0)
if r3 goto L3 else goto L4 :: bool
L2:
r4 = r0 >= 0 :: signed
if r4 goto L4 else goto L3 :: bool
L3:
r5 = raise ValueError('__len__() should return >= 0')
unreachable
L4:
return r0
[case testDundersSetItem]
class C:
def __setitem__(self, key: int, value: int) -> None:
pass
def f(c: C) -> None:
c[3] = 4
[out]
def C.__setitem__(self, key, value):
self :: __main__.C
key, value :: int
L0:
return 1
def f(c):
c :: __main__.C
r0 :: None
L0:
r0 = c.__setitem__(6, 8)
return 1
[case testDundersContains]
from typing import Any
class C:
def __contains__(self, x: int) -> bool:
return False
def f(c: C) -> bool:
return 7 in c
def g(c: C) -> bool:
return 7 not in c
class D:
def __contains__(self, x: int) -> Any:
return 'x'
def h(d: D) -> bool:
return 7 not in d
[out]
def C.__contains__(self, x):
self :: __main__.C
x :: int
L0:
return 0
def f(c):
c :: __main__.C
r0 :: bool
L0:
r0 = c.__contains__(14)
return r0
def g(c):
c :: __main__.C
r0, r1 :: bool
L0:
r0 = c.__contains__(14)
r1 = r0 ^ 1
return r1
def D.__contains__(self, x):
self :: __main__.D
x :: int
r0 :: str
L0:
r0 = 'x'
return r0
def h(d):
d :: __main__.D
r0 :: object
r1 :: int32
r2 :: bit
r3, r4 :: bool
L0:
r0 = d.__contains__(14)
r1 = PyObject_IsTrue(r0)
r2 = r1 >= 0 :: signed
r3 = truncate r1: int32 to builtins.bool
r4 = r3 ^ 1
return r4
[case testDundersDelItem]
class C:
def __delitem__(self, x: int) -> None:
pass
def f(c: C) -> None:
del c[5]
[out]
def C.__delitem__(self, x):
self :: __main__.C
x :: int
L0:
return 1
def f(c):
c :: __main__.C
r0 :: None
L0:
r0 = c.__delitem__(10)
return 1
[case testDundersUnary]
class C:
def __neg__(self) -> int:
return 1
def __invert__(self) -> int:
return 2
def __int__(self) -> int:
return 3
def __float__(self) -> float:
return 4.0
def f(c: C) -> None:
-c
~c
int(c)
float(c)
[out]
def C.__neg__(self):
self :: __main__.C
L0:
return 2
def C.__invert__(self):
self :: __main__.C
L0:
return 4
def C.__int__(self):
self :: __main__.C
L0:
return 6
def C.__float__(self):
self :: __main__.C
r0 :: float
L0:
r0 = 4.0
return r0
def f(c):
c :: __main__.C
r0, r1 :: int
r2, r3, r4, r5 :: object
L0:
r0 = c.__neg__()
r1 = c.__invert__()
r2 = load_address PyLong_Type
r3 = PyObject_CallFunctionObjArgs(r2, c, 0)
r4 = load_address PyFloat_Type
r5 = PyObject_CallFunctionObjArgs(r4, c, 0)
return 1