@@ -255,6 +255,9 @@ static Py_hash_t wrap_hashfunc(hashfunc f, PyObject* a) {
255
255
return PyLong_FromSsize_t (f (a ));
256
256
}
257
257
258
+ static PyObject * wrap_reverse_binop (binaryfunc f , PyObject * a , PyObject * b ) {
259
+ return f (b , a );
260
+ }
258
261
259
262
int PyType_Ready (PyTypeObject * cls ) {
260
263
#define ADD_IF_MISSING (attr , def ) if (!(attr)) { attr = def; }
@@ -433,11 +436,10 @@ int PyType_Ready(PyTypeObject* cls) {
433
436
PyNumberMethods * numbers = cls -> tp_as_number ;
434
437
if (numbers ) {
435
438
ADD_SLOT ("__add__" , numbers -> nb_add , -2 );
436
- ADD_SLOT ("__radd__" , numbers -> nb_add , -2 );
439
+ ADD_SLOT_CONV ("__radd__" , wrap_reverse_binop , numbers -> nb_add , -2 );
437
440
ADD_SLOT ("__sub__" , numbers -> nb_subtract , -2 );
438
- ADD_SLOT ("__rsub__" , numbers -> nb_subtract , -2 );
441
+ ADD_SLOT_CONV ("__rsub__" , wrap_reverse_binop , numbers -> nb_subtract , -2 );
439
442
ADD_SLOT ("__mul__" , numbers -> nb_multiply , -2 );
440
- ADD_SLOT ("__rmul__" , numbers -> nb_multiply , -2 );
441
443
ADD_SLOT ("__rem__" , numbers -> nb_remainder , -2 );
442
444
ADD_SLOT ("__divmod__" , numbers -> nb_divmod , -2 );
443
445
ADD_SLOT_CONV ("__pow__" , wrap_pow , numbers -> nb_power , -3 );
@@ -449,11 +451,8 @@ int PyType_Ready(PyTypeObject* cls) {
449
451
ADD_SLOT ("__lshift__" , numbers -> nb_lshift , -2 );
450
452
ADD_SLOT ("__rshift__" , numbers -> nb_rshift , -2 );
451
453
ADD_SLOT ("__and__" , numbers -> nb_and , -2 );
452
- ADD_SLOT ("__rand__" , numbers -> nb_and , -2 );
453
454
ADD_SLOT ("__xor__" , numbers -> nb_xor , -2 );
454
- ADD_SLOT ("__rxor__" , numbers -> nb_xor , -2 );
455
455
ADD_SLOT ("__or__" , numbers -> nb_or , -2 );
456
- ADD_SLOT ("__ror__" , numbers -> nb_or , -2 );
457
456
ADD_SLOT ("__int__" , numbers -> nb_int , -1 );
458
457
ADD_SLOT ("__float__" , numbers -> nb_float , -1 );
459
458
ADD_SLOT ("__iadd__" , numbers -> nb_inplace_add , -2 );
0 commit comments