You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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=Noneclosed_at=Nonecreated_at=<Date2016-12-11.20:53:16.843>labels= ['interpreter-core']
title="__length_hint__ isn't a hint for list()"updated_at=<Date2018-08-02.19:00:20.255>user='https://github.com/ronaldoussoren'
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.
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:
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.
Limit the preallocation to some reasonable value.
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.
Activity
ronaldoussoren commentedon Dec 11, 2016
The following code raises MemoryError instead of creating an empty list:
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.
serhiy-storchaka commentedon Aug 11, 2026
list()andbytes()preallocate the result using__length_hint__(), whilePySequence_Tuple()no longer uses it. An incorrect hint can therefore causeMemoryError, or, if it is large but not large enough to fail, exhaust memory and swap.There are three variants:
__length_hint__()is the responsibility of the class which implements it, and we do not protect against other protocol methods lying either.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.