Addition and multiplication puzzle

Alex Martelli aleax at aleax.it
Sat Oct 25 17:59:07 EDT 2003


John Roth wrote:
   ...
> "Mark Dickinson" <marketdickinson at yahoo.com> wrote in message
   ...
> I vaguely remember a discussion a while back about
> getting rid of the 'r' operators. I don't remember what
> ever came of it, though, and I agree that the inconsistent
> treatment of __mul__ and __add__ is strange. I suspect
> something may have gotten lost in the boolean implementation.

It's not that -- it's a different bug (good catch guys!).

Objects/typeobject.c takes BOTH __mul__ AND __rmul__ as implying
a "sq_repeat" (sequence repeat) slot, and then function
Objects/abstract.c checks both operand for sq_repeat slots --
thus, it will (erroneously) tread a __mul__ AS IF it was an
__rmul__... BUT only when multiplying by an integer, because
that's what sequence-repeat involves.

Try 3.0 * i rather than 3 * i and you'll see it correctly
raise a TypeError (although the msg is a good hint that the
bug is still at work: "can't multiply sequence to non-int"
rather than "unsupported operand type(s)").


Unfortunately the fix (for Python 2.3.3) isn't as obvious to
me as the diagnosis.  Anyway, I've reported this as a bug on
SF, its "Request ID" is 830261, and I hope I'll get advice
from somebody to whom the fix _is_ obvious:-).


Again, thanks for the bug report!


Alex





More information about the Python-list mailing list