@@ -11,7 +11,7 @@ import (
11
11
12
12
// Add two python objects together returning an Object
13
13
//
14
- // Will raise TypeError if can't be added
14
+ // Will raise TypeError if can't be add can't be run on these objects
15
15
func Add (a , b Object ) Object {
16
16
// Try using a to add
17
17
A , ok := a .(I__add__ )
@@ -51,7 +51,7 @@ func IAdd(a, b Object) Object {
51
51
52
52
// Sub two python objects together returning an Object
53
53
//
54
- // Will raise TypeError if can't be subed
54
+ // Will raise TypeError if can't be sub can't be run on these objects
55
55
func Sub (a , b Object ) Object {
56
56
// Try using a to sub
57
57
A , ok := a .(I__sub__ )
@@ -91,7 +91,7 @@ func ISub(a, b Object) Object {
91
91
92
92
// Mul two python objects together returning an Object
93
93
//
94
- // Will raise TypeError if can't be muled
94
+ // Will raise TypeError if can't be mul can't be run on these objects
95
95
func Mul (a , b Object ) Object {
96
96
// Try using a to mul
97
97
A , ok := a .(I__mul__ )
@@ -131,7 +131,7 @@ func IMul(a, b Object) Object {
131
131
132
132
// TrueDiv two python objects together returning an Object
133
133
//
134
- // Will raise TypeError if can't be truedived
134
+ // Will raise TypeError if can't be truediv can't be run on these objects
135
135
func TrueDiv (a , b Object ) Object {
136
136
// Try using a to truediv
137
137
A , ok := a .(I__truediv__ )
@@ -171,7 +171,7 @@ func ITrueDiv(a, b Object) Object {
171
171
172
172
// FloorDiv two python objects together returning an Object
173
173
//
174
- // Will raise TypeError if can't be floordived
174
+ // Will raise TypeError if can't be floordiv can't be run on these objects
175
175
func FloorDiv (a , b Object ) Object {
176
176
// Try using a to floordiv
177
177
A , ok := a .(I__floordiv__ )
@@ -211,7 +211,7 @@ func IFloorDiv(a, b Object) Object {
211
211
212
212
// Mod two python objects together returning an Object
213
213
//
214
- // Will raise TypeError if can't be moded
214
+ // Will raise TypeError if can't be mod can't be run on these objects
215
215
func Mod (a , b Object ) Object {
216
216
// Try using a to mod
217
217
A , ok := a .(I__mod__ )
@@ -249,9 +249,37 @@ func IMod(a, b Object) Object {
249
249
return Mod (a , b )
250
250
}
251
251
252
+ // DivMod two python objects together returning an Object
253
+ //
254
+ // Will raise TypeError if can't be divmod can't be run on these objects
255
+ func DivMod (a , b Object ) (Object , Object ) {
256
+ // Try using a to divmod
257
+ A , ok := a .(I__divmod__ )
258
+ if ok {
259
+ res , res2 := A .M__divmod__ (b )
260
+ if res != NotImplemented {
261
+ return res , res2
262
+ }
263
+ }
264
+
265
+ // Now using b to rdivmod if different in type to a
266
+ if a .Type () != b .Type () {
267
+ B , ok := b .(I__rdivmod__ )
268
+ if ok {
269
+ res , res2 := B .M__rdivmod__ (a )
270
+ if res != NotImplemented {
271
+ return res , res2
272
+ }
273
+ }
274
+ }
275
+
276
+ // FIXME should be TypeError
277
+ panic (fmt .Sprintf ("TypeError: unsupported operand type(s) for divmod: '%s' and '%s'" , a .Type ().Name , b .Type ().Name ))
278
+ }
279
+
252
280
// Lshift two python objects together returning an Object
253
281
//
254
- // Will raise TypeError if can't be lshifted
282
+ // Will raise TypeError if can't be lshift can't be run on these objects
255
283
func Lshift (a , b Object ) Object {
256
284
// Try using a to lshift
257
285
A , ok := a .(I__lshift__ )
@@ -291,7 +319,7 @@ func ILshift(a, b Object) Object {
291
319
292
320
// Rshift two python objects together returning an Object
293
321
//
294
- // Will raise TypeError if can't be rshifted
322
+ // Will raise TypeError if can't be rshift can't be run on these objects
295
323
func Rshift (a , b Object ) Object {
296
324
// Try using a to rshift
297
325
A , ok := a .(I__rshift__ )
@@ -331,7 +359,7 @@ func IRshift(a, b Object) Object {
331
359
332
360
// And two python objects together returning an Object
333
361
//
334
- // Will raise TypeError if can't be anded
362
+ // Will raise TypeError if can't be and can't be run on these objects
335
363
func And (a , b Object ) Object {
336
364
// Try using a to and
337
365
A , ok := a .(I__and__ )
@@ -371,7 +399,7 @@ func IAnd(a, b Object) Object {
371
399
372
400
// Xor two python objects together returning an Object
373
401
//
374
- // Will raise TypeError if can't be xored
402
+ // Will raise TypeError if can't be xor can't be run on these objects
375
403
func Xor (a , b Object ) Object {
376
404
// Try using a to xor
377
405
A , ok := a .(I__xor__ )
@@ -411,7 +439,7 @@ func IXor(a, b Object) Object {
411
439
412
440
// Or two python objects together returning an Object
413
441
//
414
- // Will raise TypeError if can't be ored
442
+ // Will raise TypeError if can't be or can't be run on these objects
415
443
func Or (a , b Object ) Object {
416
444
// Try using a to or
417
445
A , ok := a .(I__or__ )
@@ -448,3 +476,45 @@ func IOr(a, b Object) Object {
448
476
}
449
477
return Or (a , b )
450
478
}
479
+
480
+ // Pow three python objects together returning an Object
481
+ //
482
+ // If c != None then it won't attempt to call __rpow__
483
+ //
484
+ // Will raise TypeError if can't be pow can't be run on these objects
485
+ func Pow (a , b , c Object ) Object {
486
+ // Try using a to pow
487
+ A , ok := a .(I__pow__ )
488
+ if ok {
489
+ res := A .M__pow__ (b , c )
490
+ if res != NotImplemented {
491
+ return res
492
+ }
493
+ }
494
+
495
+ // Now using b to rpow if different in type to a
496
+ if c == None && a .Type () != b .Type () {
497
+ B , ok := b .(I__rpow__ )
498
+ if ok {
499
+ res := B .M__rpow__ (a )
500
+ if res != NotImplemented {
501
+ return res
502
+ }
503
+ }
504
+ }
505
+
506
+ // FIXME should be TypeError
507
+ panic (fmt .Sprintf ("TypeError: unsupported operand type(s) for ** or pow(): '%s' and '%s'" , a .Type ().Name , b .Type ().Name ))
508
+ }
509
+
510
+ // Inplace pow
511
+ func IPow (a , b , c Object ) Object {
512
+ A , ok := a .(I__ipow__ )
513
+ if ok {
514
+ res := A .M__ipow__ (b , c )
515
+ if res != NotImplemented {
516
+ return res
517
+ }
518
+ }
519
+ return Pow (a , b , c )
520
+ }
0 commit comments