Skip to content

Python 3.6 change in dict iteration when inserting keys #73606

Closed

Description

@MatthewBrett
BPO 29420
Nosy @bitdancer
Superseder
  • bpo-19332: Guard against changing dict during iteration
  • 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 2017-02-02.14:38:17.926>
    created_at = <Date 2017-02-02.14:10:45.449>
    labels = ['type-bug']
    title = 'Python 3.6 change in dict iteration when inserting keys'
    updated_at = <Date 2017-02-02.15:23:38.378>
    user = 'https://bugs.python.org/matthewbrett'

    bugs.python.org fields:

    activity = <Date 2017-02-02.15:23:38.378>
    actor = 'matthew.brett'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-02-02.14:38:17.926>
    closer = 'r.david.murray'
    components = []
    creation = <Date 2017-02-02.14:10:45.449>
    creator = 'matthew.brett'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 29420
    keywords = []
    message_count = 3.0
    messages = ['286794', '286797', '286804']
    nosy_count = 2.0
    nosy_names = ['r.david.murray', 'matthew.brett']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '19332'
    type = 'behavior'
    url = 'https://bugs.python.org/issue29420'
    versions = ['Python 3.6']

    Activity

    1. MatthewBrett commented on Feb 2, 2017

      MatthewBrettmannequin
      MannequinAuthor

      The behavior of dict iteration has changed in Python 3.6, in that inserting keys during iteration has a different and unpredictable affect. For this code:

      d = {'foo': 1}
      for key in d:
         print(key)
         d.pop(key)
         d[key] = 1

      Python 3.5 prints a single 'foo' (one pass through the loop). Python 3.6 generates five lines of 'foo' (five passes through the loop). Of course this code is pathological, but I found this behavior from a bug in code where the pathology was a lot less obvious - see nipy/nipy#420

    2. bitdancer commented on Feb 2, 2017

      @bitdancer
      Member

      This "bug" has always existed, its just that its manifestation has changed. See bpo-19332 for background.

    3. MatthewBrett commented on Feb 2, 2017

      MatthewBrettmannequin
      MannequinAuthor

      To clarify from comments on bpo-19332:

      """

      • The normal rule (not just for Python) is that a data structures have undefined behavior for mutating while iterating, unless there is a specific guarantee
        """

      The change in Python 3.6 is just a different undefined behavior.

    4. 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

      type-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions