Skip to content

checking for membership in itertools.count enters infinite loop with no way to exit #81221

Closed

Description

@rotu
mannequin
BPO 37040
Nosy @rhettinger, @terryjreedy, @vadmium, @serhiy-storchaka, @rotu, @websurfer5
Superseder
  • bpo-31815: Make itertools iterators interruptible
  • 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 = <Date 2019-05-31.20:30:23.873>
    created_at = <Date 2019-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 = <Date 2019-05-31.20:30:23.869>
    user = 'https://github.com/rotu'

    bugs.python.org fields:

    activity = <Date 2019-05-31.20:30:23.869>
    actor = 'terry.reedy'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-05-31.20:30:23.873>
    closer = 'terry.reedy'
    components = ['Library (Lib)']
    creation = <Date 2019-05-25.04:22:05.240>
    creator = 'Dan Rose'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 37040
    keywords = []
    message_count = 8.0
    messages = ['343452', '343455', '343458', '343486', '343491', '343496', '343497', '344120']
    nosy_count = 6.0
    nosy_names = ['rhettinger', 'terry.reedy', 'martin.panter', 'serhiy.storchaka', 'Dan Rose', 'Jeffrey.Kintscher']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '31815'
    type = 'behavior'
    url = 'https://bugs.python.org/issue37040'
    versions = ['Python 3.8']

    Activity

    1. rotu commented on May 25, 2019

      rotumannequin
      MannequinAuthor

      Checking membership in itertools.count() will either return True in linear time or enter an infinite loop that cannot be terminated with Ctrl-c. This

      import itertools
      1 in itertools.count(0,2)
      

      It is expected that the above code will use an efficient (O(1)) membership algorithm like range objects.

    2. added
      3.7 (EOL)
      stdlib
      on May 25, 2019
    3. SilentGhost commented on May 25, 2019

      SilentGhostmannequin
      Mannequin

      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.

    4. added
      3.8 (EOL)
      and removed
      3.7 (EOL)
      on May 25, 2019
    5. added
      type-bug
      on May 25, 2019
    6. vadmium commented on May 25, 2019

      @vadmium
      Member

      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

    7. rotu commented on May 25, 2019

      rotumannequin
      MannequinAuthor

      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.

    8. reopened this on May 25, 2019
    9. serhiy-storchaka commented on May 25, 2019

      @serhiy-storchaka
      Member

      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.

    10. rotu commented on May 25, 2019

      rotumannequin
      MannequinAuthor

      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.

      On May 25, 2019, at 11:48, Serhiy Storchaka <report@bugs.python.org> wrote:

      Serhiy Storchaka <storchaka+cpython@gmail.com> added the comment:

      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.

      ----------
      nosy: +serhiy.storchaka


      Python tracker <report@bugs.python.org>
      <https://bugs.python.org/issue37040\>


    11. rotu commented on May 25, 2019

      rotumannequin
      MannequinAuthor

      Oops you are right. These enter uninterruptible loops too. They are exceptional situations and should do the expected thing - throw exceptions as well.

    12. terryjreedy commented on May 31, 2019

      @terryjreedy
      Member

      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.

    13. transferred this issue fromon Apr 10, 2022
    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

      3.8 (EOL)stdlibtype-bug

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions