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
{{ message }}
Repository navigation
checking for membership in itertools.count enters infinite loop with no way to exit #81221
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=<Date2019-05-31.20:30:23.873>created_at=<Date2019-05-25.04:22:05.240>labels= ['3.8', 'type-bug', 'library']
title='checking for membership in itertools.count enters infinite loop with no way to exit'updated_at=<Date2019-05-31.20:30:23.869>user='https://github.com/rotu'
This seem like a misdirected expectation. count returns a regular iterator and the purpose behind it is not to enable membership checks, this only works because it's a regular iterators and normal rules for checking membership are applied.
In any case, it is possible to write an infinite loop in Python either by accident or intentionally, this doesn't seem like a reasonable rationale for making this sort changes.
Problems with long-running iterators are already discussed in:
bpo-31815: rejected proposal to check for interrupts bpo-33939: proposal to flag iterators as being infinite
rotu commented on May 25, 2019
rotumannequin
MannequinAuthor
More actions
Reopening. While my behavioral expectation may be wrong, the current behavior is inappropriate. It would make more sense for count to have a __contains__ method which raises a TypeError.
Adding the __contains__() method to the count iterator would not solve the general problem with infinite iterators. For example with the following expressions:
-1 in filter(None, itertools.count())
-1 in map(float, itertools.count())
It is not worth to add a method just to handle a single case of misusing. You should not use "in" with infinite iterators.
rotu commented on May 25, 2019
rotumannequin
MannequinAuthor
More actions
The general problem with infinite iterators is indeed a bigger issue and its resolution would probably resolve this issue too. With the examples you gave at least the user can ctrl-c to interrupt. Entering an infinite, *uninterruptible* loop is a consequence so bad that it deserves a guard rail.
Adding the __contains__() method to the count iterator would not solve the general problem with infinite iterators. For example with the following expressions:
-1 in filter(None, itertools.count())
-1 in map(float, itertools.count())
It is not worth to add a method just to handle a single case of misusing. You should not use "in" with infinite iterators.
Oops you are right. These enter uninterruptible loops too. They are exceptional situations and should do the expected thing - throw exceptions as well.
Dan, please leave this closed. It duplicates bpo-31815 and should be closed on that grounds alone, regardless of the status of the latter. bpo-31815 was closed in favor of bpo-33939, which is open, with ongoing discussion. Resolution either way is blocked because different core developers disagree.
Activity
rotu commentedon May 25, 2019
Checking membership in
itertools.count()will either return True in linear time or enter an infinite loop that cannot be terminated with Ctrl-c. ThisIt is expected that the above code will use an efficient (O(1)) membership algorithm like range objects.
SilentGhost commentedon May 25, 2019
This seem like a misdirected expectation. count returns a regular iterator and the purpose behind it is not to enable membership checks, this only works because it's a regular iterators and normal rules for checking membership are applied.
In any case, it is possible to write an infinite loop in Python either by accident or intentionally, this doesn't seem like a reasonable rationale for making this sort changes.
vadmium commentedon May 25, 2019
Problems with long-running iterators are already discussed in:
bpo-31815: rejected proposal to check for interrupts
bpo-33939: proposal to flag iterators as being infinite
rotu commentedon May 25, 2019
Reopening. While my behavioral expectation may be wrong, the current behavior is inappropriate. It would make more sense for count to have a
__contains__method which raises a TypeError.serhiy-storchaka commentedon May 25, 2019
Adding the __contains__() method to the count iterator would not solve the general problem with infinite iterators. For example with the following expressions:
It is not worth to add a method just to handle a single case of misusing. You should not use "in" with infinite iterators.
rotu commentedon May 25, 2019
The general problem with infinite iterators is indeed a bigger issue and its resolution would probably resolve this issue too. With the examples you gave at least the user can ctrl-c to interrupt. Entering an infinite, *uninterruptible* loop is a consequence so bad that it deserves a guard rail.
rotu commentedon May 25, 2019
Oops you are right. These enter uninterruptible loops too. They are exceptional situations and should do the expected thing - throw exceptions as well.
terryjreedy commentedon May 31, 2019
Dan, please leave this closed. It duplicates bpo-31815 and should be closed on that grounds alone, regardless of the status of the latter. bpo-31815 was closed in favor of bpo-33939, which is open, with ongoing discussion. Resolution either way is blocked because different core developers disagree.