Addition and multiplication puzzle

Alex Martelli aleax at aleax.it
Sat Oct 25 18:04:16 EDT 2003


Emile van Sebille wrote:
   ...
> In typeobject.c,  I find:
> 
>  SQSLOT("__add__", sq_concat, slot_sq_concat, wrap_binaryfunc,
>         "x.__add__(y) <==> x+y"),
>  SQSLOT("__mul__", sq_repeat, slot_sq_repeat, wrap_intargfunc,
>         "x.__mul__(n) <==> x*n"),
>  SQSLOT("__rmul__", sq_repeat, slot_sq_repeat, wrap_intargfunc,
>         "x.__rmul__(n) <==> n*x"),
> 
> My guess is that this explains why multiplication works and addition
> doesn't, as there is no corresponding SQSLOT("__radd__"...  but adding

Right (though incomplete).

>  SQSLOT("__radd__", sq_concat, slot_sq_concat, wrap_binaryfunc,
>         "x.__radd__(y) <==> y+x"),
> 
> and recompiling didn't fix the problem, so I'm either on the wrong track
> or missed a connection along the way.

Yep: the fact that PyNumber_Add only tries the concatenate-sequences
route if the LEFT operand has a sq_concat slot -- it needs not try
BOTH operands for the purpose, as PyNumber_Multiply, alas, must
(because seq*int and int*seq are both valid).  Both functions are in
Objects/abstract.c , btw.

Unfortunately, even with this complete diagnosis, the fix is NOT
obvious to me.  Anyway, I've opened bug #830261 to see if I can
get advice from somebody to which the fix IS obvious (btw, the fix
involves NOT having 3*i succeed and 3.0*i fail with a weird msg --
it's not acceptable to "fix" by making 3+i succeed and 3.0+i fail
weirdly...:-).


Alex





More information about the Python-list mailing list