[New-bugs-announce] [issue28940] __length_hint__ isn't a hint for list()

Ronald Oussoren report at bugs.python.org
Sun Dec 11 15:53:16 EST 2016


New submission from Ronald Oussoren:

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.

----------
components: Interpreter Core
messages: 282946
nosy: ronaldoussoren
priority: normal
severity: normal
status: open
title: __length_hint__ isn't a hint for list()
versions: Python 3.5, Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue28940>
_______________________________________


More information about the New-bugs-announce mailing list