Message10519

Author ceridwen
Recipients ceridwen
Date 2015-12-08.20:50:24
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1449607824.95.0.127314486673.issue2442@psf.upfronthosting.co.za>
In-reply-to
Content
I have a tuple subclass that overrides those methods.  When trying to iterate over it, it enters an infinite loop between `__iter__` and `__len__`:

```
  File "spf.py", line 187, in __iter__
    yield getitem(node, 0)
  File "spf.py", line 203, in __len__
    return sum(1 for _ in self)
  File "spf.py", line 203, in <genexpr>
    return sum(1 for _ in self)
```

Here `getitem` is a call to `super(<tuple_subclass>, type(self)).__getitem__`, which in this case evaluates to `tuple.__getitem__`.

My working hypothesis for why this happens is that `tuple.__getitem__` calls the __len__ method of my subclass.

```
>>> class C(tuple):
...   def __len__(self):
...      print('Length')
...      return super(C, type(self)).__len__(self)
>>> c = C([1, 2])
>>> c[1]
Length
2
>>> tuple.__getitem__(c, 1)
Length
2
```

In general, my understanding is that calls to the methods of builtin types shouldn't call overridden methods.  This code works on CPython and PyPy.
History
Date User Action Args
2015-12-08 20:50:24ceridwensetrecipients: + ceridwen
2015-12-08 20:50:24ceridwensetmessageid: <1449607824.95.0.127314486673.issue2442@psf.upfronthosting.co.za>
2015-12-08 20:50:24ceridwenlinkissue2442 messages
2015-12-08 20:50:24ceridwencreate