-
Notifications
You must be signed in to change notification settings - Fork 56
/
number.go
304 lines (261 loc) · 8.94 KB
/
number.go
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Copyright 2011 Julian Phillips. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package py
// #include <Python.h>
import "C"
import "unsafe"
// NumberProtocol is a 0-sized type that can be embedded in concrete types after
// the AbstractObject to provide access to the suite of methods that Python
// calls the "Number Protocol".
type NumberProtocol struct{}
func cnp(n *NumberProtocol) *C.PyObject {
return (*C.PyObject)(unsafe.Pointer(n))
}
// Add returns the result of adding n and obj. The equivalent Python is "n +
// obj".
//
// Return value: New Reference.
func (n *NumberProtocol) Add(obj *Base) (*Base, error) {
ret := C.PyNumber_Add(cnp(n), obj.c())
return obj2ObjErr(ret)
}
// Subtract returns the result of subtracting obj from n. The equivalent Python
// is "n - obj".
//
// Return value: New Reference.
func (n *NumberProtocol) Subtract(obj *Base) (*Base, error) {
ret := C.PyNumber_Subtract(cnp(n), obj.c())
return obj2ObjErr(ret)
}
// Multiply returns the result of multiplying n by obj. The equivalent Python
// is "n * obj".
//
// Return value: New Reference.
func (n *NumberProtocol) Multiply(obj *Base) (*Base, error) {
ret := C.PyNumber_Multiply(cnp(n), obj.c())
return obj2ObjErr(ret)
}
// Divide returns the result of dividing n by obj. The equivalent Python is "n
// / obj".
//
// Return value: New Reference.
func (n *NumberProtocol) Divide(obj *Base) (*Base, error) {
ret := C.PyNumber_Divide(cnp(n), obj.c())
return obj2ObjErr(ret)
}
// FloorDivide returns the floor of dividing n obj obj.
//
// Return value: New Reference.
func (n *NumberProtocol) FloorDivide(obj *Base) (*Base, error) {
ret := C.PyNumber_FloorDivide(cnp(n), obj.c())
return obj2ObjErr(ret)
}
// TrueDivide returns the ... TODO
//
// Return value: New Reference.
func (n *NumberProtocol) TrueDivide(obj *Base) (*Base, error) {
ret := C.PyNumber_TrueDivide(cnp(n), obj.c())
return obj2ObjErr(ret)
}
// Remainder returns the remainder of dividing n by obj. The equivalent Python
// is "n % obj".
//
// Return value: New Reference.
func (n *NumberProtocol) Remainder(obj *Base) (*Base, error) {
ret := C.PyNumber_Remainder(cnp(n), obj.c())
return obj2ObjErr(ret)
}
// Divmod returns the result of the Python "divmod(n, obj)".
//
// Return value: New Reference.
func (n *NumberProtocol) Divmod(obj *Base) (*Base, error) {
ret := C.PyNumber_Divmod(cnp(n), obj.c())
return obj2ObjErr(ret)
}
// Power returns the result of the Python "pow(n, obj1, obj2)".
//
// Return value: New Reference.
func (n *NumberProtocol) Power(obj1, obj2 *Base) (*Base, error) {
ret := C.PyNumber_Power(cnp(n), obj1.c(), obj2.c())
return obj2ObjErr(ret)
}
// Negative returns the negation of n. The equivalent Python is "-n".
//
// Return value: New Reference.
func (n *NumberProtocol) Negative() (*Base, error) {
ret := C.PyNumber_Negative(cnp(n))
return obj2ObjErr(ret)
}
// Positive returns the positive of n. The equivalent Python is "+n".
//
// Return value: New Reference.
func (n *NumberProtocol) Positive() (*Base, error) {
ret := C.PyNumber_Positive(cnp(n))
return obj2ObjErr(ret)
}
// Absolute returns the absolute value of n. The equivalent Python is "abs(n)".
//
// Return value: New Reference.
func (n *NumberProtocol) Absolute() (*Base, error) {
ret := C.PyNumber_Absolute(cnp(n))
return obj2ObjErr(ret)
}
// Invert returns the bitwise negation of n. The equivalent Python is "-n".
//
// Return value: New Reference.
func (n *NumberProtocol) Invert() (*Base, error) {
ret := C.PyNumber_Invert(cnp(n))
return obj2ObjErr(ret)
}
// Lshift returns the result of left shifting n by obj. The equivalent Python
// is "n << obj".
//
// Return value: New Reference.
func (n *NumberProtocol) Lshift(obj *Base) (*Base, error) {
ret := C.PyNumber_Lshift(cnp(n), obj.c())
return obj2ObjErr(ret)
}
// Rshift returns the result of right shifting n by obj. The equivalent Python
// is "n << obj".
//
// Return value: New Reference.
func (n *NumberProtocol) Rshift(obj *Base) (*Base, error) {
ret := C.PyNumber_Rshift(cnp(n), obj.c())
return obj2ObjErr(ret)
}
// And returns the bitwise and of n and obj. The equivalent Python is "n &
// obj".
//
// Return value: New Reference.
func (n *NumberProtocol) And(obj *Base) (*Base, error) {
ret := C.PyNumber_And(cnp(n), obj.c())
return obj2ObjErr(ret)
}
// Xor returns the bitwise xor of n and obj. The equivalent Python is "n ^
// obj".
//
// Return value: New Reference.
func (n *NumberProtocol) Xor(obj *Base) (*Base, error) {
ret := C.PyNumber_Xor(cnp(n), obj.c())
return obj2ObjErr(ret)
}
// Or returns the bitwise or of n and obj. The equivalent Python is "n | obj".
//
// Return value: New Reference.
func (n *NumberProtocol) Or(obj *Base) (*Base, error) {
ret := C.PyNumber_Or(cnp(n), obj.c())
return obj2ObjErr(ret)
}
// InPlaceAdd returns the result of adding n and obj. This is done in place if
// supported by n. The equivalent Python is "n += obj".
//
// Return value: New Reference.
func (n *NumberProtocol) InPlaceAdd(obj *Base) (*Base, error) {
ret := C.PyNumber_InPlaceAdd(cnp(n), obj.c())
return obj2ObjErr(ret)
}
// InPlaceSubtract returns the result of subtracting obj from n. This is done
// in place if supported by n. The equivalent Python is "n -= obj".
//
// Return value: New Reference.
func (n *NumberProtocol) InPlaceSubtract(obj *Base) (*Base, error) {
ret := C.PyNumber_InPlaceSubtract(cnp(n), obj.c())
return obj2ObjErr(ret)
}
// InPlaceMultiply returns the result of multiplying n by obj. This is done in
// place if supported by n. The equivalent Python is "n *= obj".
//
// Return value: New Reference.
func (n *NumberProtocol) InPlaceMultiply(obj *Base) (*Base, error) {
ret := C.PyNumber_InPlaceMultiply(cnp(n), obj.c())
return obj2ObjErr(ret)
}
// InPlaceDivide returns the result of dividing n by obj. This is done in place
// if supported by n. The equivalent Python is "n /= obj".
//
// Return value: New Reference.
func (n *NumberProtocol) InPlaceDivide(obj *Base) (*Base, error) {
ret := C.PyNumber_InPlaceDivide(cnp(n), obj.c())
return obj2ObjErr(ret)
}
// TODO returns the ...
//
// Return value: New Reference.
func (n *NumberProtocol) InPlaceFloorDivide(obj *Base) (*Base, error) {
ret := C.PyNumber_InPlaceFloorDivide(cnp(n), obj.c())
return obj2ObjErr(ret)
}
// TODO returns the ...
//
// Return value: New Reference.
func (n *NumberProtocol) InPlaceTrueDivide(obj *Base) (*Base, error) {
ret := C.PyNumber_InPlaceTrueDivide(cnp(n), obj.c())
return obj2ObjErr(ret)
}
// InPlaceRemainder returns the remainder of n divided by obj. This is done in
// place if supported by n. The equivalent Python is "n %= obj".
//
// Return value: New Reference.
func (n *NumberProtocol) InPlaceRemainder(obj *Base) (*Base, error) {
ret := C.PyNumber_InPlaceRemainder(cnp(n), obj.c())
return obj2ObjErr(ret)
}
// InPlacePower returns the result of the Python "pow(n, obj1, obj2)". This is
// done in place if supported by n. If obj2 is None, then the Python "n **=
// obj" is also equivalent, if obj2 is not None, there is no equivalent in
// Python.
//
// Return value: New Reference.
func (n *NumberProtocol) InPlacePower(obj1, obj2 *Base) (*Base, error) {
ret := C.PyNumber_InPlacePower(cnp(n), obj1.c(), obj2.c())
return obj2ObjErr(ret)
}
// InPlaceLshift returns the result of left shifting n by obj. This is done in
// place if supported by n. The equivalent Python is "n <<= obj".
//
// Return value: New Reference.
func (n *NumberProtocol) InPlaceLshift(obj *Base) (*Base, error) {
ret := C.PyNumber_InPlaceLshift(cnp(n), obj.c())
return obj2ObjErr(ret)
}
// InPlaceRshift returns the result of right shifting n by obj. This is done in
// place if supported by n. The equivalent Python is "n >>= obj".
//
// Return value: New Reference.
func (n *NumberProtocol) InPlaceRshift(obj *Base) (*Base, error) {
ret := C.PyNumber_InPlaceRshift(cnp(n), obj.c())
return obj2ObjErr(ret)
}
// InPlaceAnd returns the bitwise and of n and obj. This is done in place if
// supported by n. The equivalent Python is "n &= obj".
//
// Return value: New Reference.
func (n *NumberProtocol) InPlaceAnd(obj *Base) (*Base, error) {
ret := C.PyNumber_InPlaceAnd(cnp(n), obj.c())
return obj2ObjErr(ret)
}
// InPlaceXor returns the bitwise xor of n and obj. This is done in place if
// supported by n. The equivalent Python is "n ^= obj".
//
// Return value: New Reference.
func (n *NumberProtocol) InPlaceXor(obj *Base) (*Base, error) {
ret := C.PyNumber_InPlaceXor(cnp(n), obj.c())
return obj2ObjErr(ret)
}
// InPlaceOr returns the bitwise or of n and obj. This is done in place if
// supported by n. The equivalent Python is "n |= obj".
//
// Return value: New Reference.
func (n *NumberProtocol) InPlaceOr(obj *Base) (*Base, error) {
ret := C.PyNumber_InPlaceOr(cnp(n), obj.c())
return obj2ObjErr(ret)
}
// PyNumber_Coerce: TODO
// PyNumber_CoerceEx: TODO
// PyNumber_Int: TODO
// PyNumber_Long: TODO
// PyNumber_Float: TODO
// PyNumber_Index: TODO
// PyNumber_ToBase: TODO
// PyNumber_AsSsize_t: TODO