Navigation Menu

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

calling MutableSequence.extend on self produces infinite loop #78608

Closed
KittenHero mannequin opened this issue Aug 18, 2018 · 8 comments
Closed

calling MutableSequence.extend on self produces infinite loop #78608

KittenHero mannequin opened this issue Aug 18, 2018 · 8 comments
Assignees
Labels
3.7 (EOL) end of life 3.8 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@KittenHero
Copy link
Mannequin

KittenHero mannequin commented Aug 18, 2018

BPO 34427
Nosy @rhettinger, @serhiy-storchaka, @KittenHero
PRs
  • bpo-34427: islice values when calling MutableSequence.extend on self #8813
  • 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 = 'https://github.com/rhettinger'
    closed_at = <Date 2018-08-30.16:56:54.905>
    created_at = <Date 2018-08-18.09:58:31.474>
    labels = ['3.7', '3.8', 'type-bug', 'library']
    title = 'calling MutableSequence.extend on self produces infinite loop'
    updated_at = <Date 2018-08-30.16:56:54.904>
    user = 'https://github.com/KittenHero'

    bugs.python.org fields:

    activity = <Date 2018-08-30.16:56:54.904>
    actor = 'rhettinger'
    assignee = 'rhettinger'
    closed = True
    closed_date = <Date 2018-08-30.16:56:54.905>
    closer = 'rhettinger'
    components = ['Library (Lib)']
    creation = <Date 2018-08-18.09:58:31.474>
    creator = 'Naris R'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 34427
    keywords = ['patch']
    message_count = 8.0
    messages = ['323696', '323697', '323711', '323712', '324147', '324160', '324393', '324394']
    nosy_count = 4.0
    nosy_names = ['rhettinger', 'stutzbach', 'serhiy.storchaka', 'Naris R']
    pr_nums = ['8813']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue34427'
    versions = ['Python 3.6', 'Python 3.7', 'Python 3.8']

    @KittenHero
    Copy link
    Mannequin Author

    KittenHero mannequin commented Aug 18, 2018

    Example:

    from typing import MutableSequence, TypeVar
    
    CliffordGate = TypeVar('CliffordGate')
    
    class QCircuit(MutableSequence[CliffordGate]):
        def __init__(self, gates):
            self.gates = list(gates)
    
        def __repr__(self):
            return f'{self.__class__.__name__}({self.gates})'
       
        def __getitem__(self, key):
            return self.gates[key]
    
        def __setitem__(self, key, item):
            self.gates[key] = item
    
        def __delitem__(self, key):
            del self.gates[key]
    
        def insert(self, key, item):
            self.gates.insert(key, item)
    
    a = QCircuit(['H0', 'S2'])
    a += a
    

    @KittenHero KittenHero mannequin added 3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Aug 18, 2018
    @KittenHero
    Copy link
    Mannequin Author

    KittenHero mannequin commented Aug 18, 2018

    I forgot to copy over __len__ in the example

    @rhettinger
    Copy link
    Contributor

    It would be reasonable to add special handling for this case to match what is done in the concrete sequences like list, deque, bytes, ...

    @rhettinger rhettinger self-assigned this Aug 18, 2018
    @serhiy-storchaka
    Copy link
    Member

    deque and bytearray make an in-memory copy of self if extended with self. deque creates a temporary list, bytearray creates a temporary bytearray. list just iterates itself as a linear array of known size and avoids infinite loop. It could be possible to avoid creaing a temporary copy in case of deque and bytearray too, but I don't think this special case is worth an additional code.

    But a MutableSequence can represents a sequence that doesn't fit in memory. It can provide an interface to a linear on-disk store. In this case creating an on-memory copy is not possible.

    @terryjreedy terryjreedy added the 3.8 only security fixes label Aug 25, 2018
    @rhettinger
    Copy link
    Contributor

    But a MutableSequence can represents a sequence that doesn't
    fit in memory. It can provide an interface to a linear
    on-disk store. In this case creating an on-memory copy
    is not possible.

    This case is likely not worth worrying about. If a user created such a sequence AND wrote "s += s", it is unclear whether any particular unpleasant outcome (including the current behavior) could be avoided.

    @serhiy-storchaka
    Copy link
    Member

    Agreed. Appending >2**31 items one by one to the collection with external storage is not very efficient, so such collection likely has specialized extend().

    @rhettinger
    Copy link
    Contributor

    New changeset 1b5f9c9 by Raymond Hettinger (Naris R) in branch 'master':
    bpo-34427: Fix infinite loop when calling MutableSequence.extend() on self (GH-8813)
    1b5f9c9

    @rhettinger
    Copy link
    Contributor

    Thank you for the patch.

    @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
    3.7 (EOL) end of life 3.8 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants