Issue 28940: length_hint isn't a hint for list() (original) (raw)
The following code raises MemoryError instead of creating an empty list:
START
import sys
class CustomIter: def iter(self): return self def next(self): raise StopIteration def length_hint(self): return sys.maxsize
l = list(CustomIter()) #END
That's because this empty iterator has a length_hint that claims it returns a very large number of methods.
The function listextend in Objects/listobject.c already ignores length_hint() when using it would overflow the size of the list, it would IMHO also be better to ignore the length hint when the attempt to resize fails as length_hint() is documented as a hint that may be incorrect.