Message10519
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. |
|
Date |
User |
Action |
Args |
2015-12-08 20:50:24 | ceridwen | set | recipients:
+ ceridwen |
2015-12-08 20:50:24 | ceridwen | set | messageid: <1449607824.95.0.127314486673.issue2442@psf.upfronthosting.co.za> |
2015-12-08 20:50:24 | ceridwen | link | issue2442 messages |
2015-12-08 20:50:24 | ceridwen | create | |
|