classification
Title: __length_hint__ isn't a hint for list()
Type: Stage: patch review
Components: Interpreter Core Versions: Python 3.6, Python 3.5
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: ronaldoussoren
Priority: normal Keywords: patch

Created on 2016-12-11 20:53 by ronaldoussoren, last changed 2018-08-02 19:00 by sir-sigurd.

Pull Requests
URL Status Linked Edit
PR 8635 open sir-sigurd, 2018-08-02 19:00
Messages (1)
msg282946 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2016-12-11 20:53
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.
History
Date User Action Args
2018-08-02 19:00:20sir-sigurdsetkeywords: + patch
stage: patch review
pull_requests: + pull_request8140
2016-12-11 20:53:16ronaldoussorencreate