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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
cimport cython
import sys
IS_PYTHON2 = sys.version_info[0] == 2
__doc__ = ""
@cython.c_api_binop_methods(False)
@cython.cclass
class Base(object):
"""
>>> Base() + 2
'Base.__add__(Base(), 2)'
>>> 2 + Base()
'Base.__radd__(Base(), 2)'
>>> Base(implemented=False) + 2 #doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError: unsupported operand type...
>>> 2 + Base(implemented=False) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError: unsupported operand type...
>>> Base() ** 2
'Base.__pow__(Base(), 2, None)'
>>> 2 ** Base()
'Base.__rpow__(Base(), 2, None)'
>>> pow(Base(), 2, 100)
'Base.__pow__(Base(), 2, 100)'
"""
implemented: cython.bint
def __init__(self, *, implemented=True):
self.implemented = implemented
def __add__(self, other):
assert cython.typeof(self) == "Base"
if self.implemented:
return "Base.__add__(%s, %s)" % (self, other)
else:
return NotImplemented
def __radd__(self, other):
assert cython.typeof(self) == "Base"
if self.implemented:
return "Base.__radd__(%s, %s)" % (self, other)
else:
return NotImplemented
def __pow__(self, other, mod):
assert cython.typeof(self) == "Base"
if self.implemented:
return "Base.__pow__(%s, %s, %s)" % (self, other, mod)
else:
return NotImplemented
def __rpow__(self, other, mod):
assert cython.typeof(self) == "Base"
if self.implemented:
return "Base.__rpow__(%s, %s, %s)" % (self, other, mod)
else:
return NotImplemented
def __repr__(self):
return "%s()" % (self.__class__.__name__)
@cython.c_api_binop_methods(False)
@cython.cclass
class OverloadLeft(Base):
"""
>>> OverloadLeft() + 2
'OverloadLeft.__add__(OverloadLeft(), 2)'
>>> 2 + OverloadLeft()
'Base.__radd__(OverloadLeft(), 2)'
>>> OverloadLeft() + Base()
'OverloadLeft.__add__(OverloadLeft(), Base())'
>>> Base() + OverloadLeft()
'Base.__add__(Base(), OverloadLeft())'
>>> OverloadLeft(implemented=False) + Base(implemented=False) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError: unsupported operand type...
>>> Base(implemented=False) + OverloadLeft(implemented=False) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError: unsupported operand type...
"""
derived_implemented: cython.bint
def __init__(self, *, implemented=True):
super().__init__(implemented=implemented)
self.derived_implemented = implemented
def __add__(self, other):
assert cython.typeof(self) == "OverloadLeft"
if self.derived_implemented:
return "OverloadLeft.__add__(%s, %s)" % (self, other)
else:
return NotImplemented
@cython.c_api_binop_methods(False)
@cython.cclass
class OverloadRight(Base):
"""
>>> OverloadRight() + 2
'Base.__add__(OverloadRight(), 2)'
>>> 2 + OverloadRight()
'OverloadRight.__radd__(OverloadRight(), 2)'
>>> OverloadRight() + Base()
'Base.__add__(OverloadRight(), Base())'
>>> Base() + OverloadRight()
'OverloadRight.__radd__(OverloadRight(), Base())'
>>> OverloadRight(implemented=False) + Base(implemented=False) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError: unsupported operand type...
>>> Base(implemented=False) + OverloadRight(implemented=False) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError: unsupported operand type...
"""
derived_implemented: cython.bint
def __init__(self, *, implemented=True):
super().__init__(implemented=implemented)
self.derived_implemented = implemented
def __radd__(self, other):
assert cython.typeof(self) == "OverloadRight"
if self.derived_implemented:
return "OverloadRight.__radd__(%s, %s)" % (self, other)
else:
return NotImplemented
@cython.c_api_binop_methods(True)
@cython.cclass
class OverloadCApi(Base):
"""
>>> OverloadCApi() + 2
'OverloadCApi.__add__(OverloadCApi(), 2)'
>>> 2 + OverloadCApi()
'OverloadCApi.__add__(2, OverloadCApi())'
>>> OverloadCApi() + Base()
'OverloadCApi.__add__(OverloadCApi(), Base())'
>>> Base() + OverloadCApi()
'OverloadCApi.__add__(Base(), OverloadCApi())'
>>> OverloadCApi(derived_implemented=False) + 2 #doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError: unsupported operand type...
>>> 2 + OverloadCApi(derived_implemented=False) #doctest: +ELLIPSIS
Traceback (most recent call last):
...
TypeError: unsupported operand type...
"""
derived_implemented: cython.bint
def __init__(self, *, derived_implemented=True):
super().__init__(implemented=True)
self.derived_implemented = derived_implemented
def __add__(self, other):
assert cython.typeof(self) != "OverloadCApi" # should be untyped
if isinstance(self, OverloadCApi):
derived_implemented = (<OverloadCApi>self).derived_implemented
else:
derived_implemented = (<OverloadCApi>other).derived_implemented
if derived_implemented:
return "OverloadCApi.__add__(%s, %s)" % (self, other)
else:
return NotImplemented
if sys.version_info >= (3, 5):
__doc__ += """
>>> d = PyVersionDependent()
>>> d @ 2
9
>>> 2 @ d
99
>>> i = d
>>> i @= 2
>>> i
999
"""
@cython.c_api_binop_methods(False)
@cython.cclass
class PyVersionDependent:
"""
>>> d = PyVersionDependent()
>>> d / 2
5
>>> 2 / d
2
>>> d // 2
55
>>> 2 // d
22
>>> i = d
>>> i /= 2
>>> i
4
>>> i = d
>>> i //= 2
>>> i
44
"""
def __div__(self, other):
assert IS_PYTHON2
return 5
def __rdiv__(self, other):
assert IS_PYTHON2
return 2
def __idiv__(self, other):
assert IS_PYTHON2
return 4
def __truediv__(self, other):
assert not IS_PYTHON2
return 5
def __rtruediv__(self, other):
assert not IS_PYTHON2
return 2
def __itruediv__(self, other):
assert not IS_PYTHON2
return 4
def __floordiv__(self, other):
return 55
def __rfloordiv__(self, other):
return 22
def __ifloordiv__(self, other):
return 44
def __matmul__(self, other):
return 9
def __rmatmul__(self, other):
return 99
def __imatmul__(self, other):
return 999
# TODO: Test a class that only defines the `__r...__()` methods.
|