Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

__length_hint__ isn't a hint for list() #73126

Open
ronaldoussoren opened this issue Dec 11, 2016 · 1 comment
Open

__length_hint__ isn't a hint for list() #73126

ronaldoussoren opened this issue Dec 11, 2016 · 1 comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs)

Comments

@ronaldoussoren
Copy link
Contributor

BPO 28940
Nosy @ronaldoussoren
PRs
  • bpo-28940: Don't fail early when __length_hint__() is too big for preallocation. #8635
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2016-12-11.20:53:16.843>
    labels = ['interpreter-core']
    title = "__length_hint__ isn't a hint for list()"
    updated_at = <Date 2018-08-02.19:00:20.255>
    user = 'https://github.com/ronaldoussoren'

    bugs.python.org fields:

    activity = <Date 2018-08-02.19:00:20.255>
    actor = 'sir-sigurd'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Interpreter Core']
    creation = <Date 2016-12-11.20:53:16.843>
    creator = 'ronaldoussoren'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 28940
    keywords = ['patch']
    message_count = 1.0
    messages = ['282946']
    nosy_count = 1.0
    nosy_names = ['ronaldoussoren']
    pr_nums = ['8635']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue28940'
    versions = ['Python 3.5', 'Python 3.6']

    @ronaldoussoren
    Copy link
    Contributor Author

    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.

    @ronaldoussoren ronaldoussoren added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Dec 11, 2016
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    interpreter-core (Objects, Python, Grammar, and Parser dirs)
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant