[New-bugs-announce] [issue11477] Bug in code dispatching based on internal slots

Terry J. Reedy report at bugs.python.org
Sat Mar 12 22:24:51 CET 2011

New submission from Terry J. Reedy <tjreedy at udel.edu>:

Example (which can serve as testcase with buggy output corrected).

class C(object):
    def __iter__(self):
        yield 'yes!'
    def __radd__(self, other):
        return other
    def __rmul__(self, other):
        other *= 2
        return other
    def __index__(self):
          return 3

class L(list):
    def __iadd__(self, other):
        return self
    def __mul__(self, other):
        return L(list.__imul__(self,other))

z1, z2, c = [], L(), C()
z1 += c
z2 += c
print(z1, z2, [1]*c, L([1])*c)
['bug!'] ['yes!'] [1, 1] [1, 1, 1] # PyPy prints ['yes!'], [1, 1, 1]

Cause was diagnosed by Greg Price in
as checking forward and reverse numeric slots before checking sequence slots for C-coded classes. Such a difference does not exist in Python itself.

In "About raising NotPortableWarning for CPython specific code"
pydev thread starting at

Nick Coghlin identified the fix as "When a given operation has multiple C level slots, shouldn't we be checking all the LHS slots before checking any of the RHS slots?"

Guido replied "Yeah, indeed, on everything you said. The code dispatching based on internal slots is horribly ad-hoc and likely wrong in subtle ways."

I personally think fix should be backported to 2.7 and 3.2, but did not select them because that may be controversial.

components: Interpreter Core
messages: 130698
nosy: terry.reedy
priority: normal
severity: normal
stage: needs patch
status: open
title: Bug in code dispatching based on internal slots
type: behavior
versions: Python 3.3

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list