Skip to content

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

Open

Description

@ronaldoussoren
BPO 28940
Nosy @ronaldoussoren
PRs
  • gh-73126: 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']

    Activity

    1. ronaldoussoren commented on Dec 11, 2016

      @ronaldoussoren
      ContributorAuthor

      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.

    2. transferred this issue fromon Apr 10, 2022
    3. serhiy-storchaka commented on Aug 11, 2026

      @serhiy-storchaka
      Member

      list() and bytes() preallocate the result using __length_hint__(), while PySequence_Tuple() no longer uses it. An incorrect hint can therefore cause MemoryError, or, if it is large but not large enough to fail, exhaust memory and swap.

      There are three variants:

      1. Do nothing. The correctness of __length_hint__() is the responsibility of the class which implements it, and we do not protect against other protocol methods lying either.
      2. Limit the preallocation to some reasonable value.
      3. Do not use the hint for preallocation at all, as PySequence_Tuple() already does.

      The benefit of the hint is mostly saving memory for small lists, where its relative cost in time is higher. Calling __length_hint__() on a Python iterator costs about 0.2 microseconds, which is not repaid until about 32 items, and it saves only 0 to 48 bytes for short lists. For larger lists the difference in time is within the noise, and growing overallocates by at most the growth factor: 800 bytes for 1000 items and 449 KiB for 1000000 items.

      #8635 implemented a fourth variant, retrying without preallocation if the allocation fails, but this only helps in the case of an immediate failure.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      interpreter-core(Objects, Python, Grammar, and Parser dirs)

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions