[Patches] [ python-Patches-1390657 ] NotImplemented->TypeError in __add__ and __mul__

SourceForge.net noreply at sourceforge.net
Mon Dec 26 17:09:51 CET 2005

Patches item #1390657, was opened at 2005-12-26 16:09
Message generated for change (Tracker Item Submitted) made by Item Submitter
You can respond by visiting: 

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Core (C code)
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Armin Rigo (arigo)
Assigned to: Nobody/Anonymous (nobody)
Summary: NotImplemented->TypeError in __add__ and __mul__

Initial Comment:
This is a fix for bug #1355842, with a comprehensive
test checking that implementing a binary special method
but returning NotImplemented does indeed cause a
TypeError to be raised.  In addition to the case
reported in #1355842 (x+=y) the test found another case
where NotImplemented was just returned (A()*5), a case
where an OverflowError or ValueError was raised instead
of TypeError (A()*large_integer), and a case where an
unexpected AttributeError was raised (5*A()).

The proposed patch also reverts the changes done in svn
revisions 25556-25557 corresponding to bug #516727,
because it makes that fix unnecessary by adopting a
more radical approach.

All the problems are due to hard-to-control
interactions between the various PyTypeObject slots for
addition and multiplication (nb_add vs sq_concat,
nb_multiply vs sq_repeat).  The approach of the present
patch is to not define sq_concat, sq_repeat,
sq_inplace_concat and sq_inplace_repeat slots *at all*
for user-defined types, even if they have __add__ and
__mul__ methods.  This is the only sane solution I
found, specially with respect to the
OverflowError/ValueError problem (caused by trying to
convert the integer to a C int in order to try to call
sq_repeat).  It is also consistent with the behavior of
instances of old-style classes -- the InstanceType did
not define sq_concat and sq_repeat, either.

I'll propose a redefinition of operator.concat() and
operator.repeat() on python-dev.


You can respond by visiting: 

More information about the Patches mailing list