Skip to content

Comprehension iteration variable overwrite a variable of the same name in the outer scope inside a function with try..except #108654

Closed

Description

@Fatal1ty

Bug report

Checklist

  • I am confident this is a bug in CPython, not a bug in a third-party project
  • I have searched the CPython issue tracker,
    and am confident this bug has not been reported before

CPython versions tested on:

3.12

Operating systems tested on:

macOS

Output from running 'python -VV' on the command line:

Python 3.12.0rc1 (main, Aug 29 2023, 19:34:25) [Clang 12.0.5 (clang-1205.0.22.9)]

A clear and concise description of the bug:

In the release notes for 3.12.0rc1 there is the following paragraph about comprehension inlining:

Comprehension iteration variables remain isolated; they don’t overwrite a variable of the same name in the outer scope, nor are they visible after the comprehension. This isolation is now maintained via stack/locals manipulation, not via separate function scope.

I found a case when a comprehension iteration variable overwrite a variable of the same name in the outer scope:

def foo(value):
    try:
        {int(key): value for key, value in value.items()}
    except:
        print(repr(value))  # will print 'baz' instead of {'bar': 'baz'}
foo({'bar':'baz'})

However, if you run this code outside of the function, isolation works:

value = {'bar': 'baz'}
try:
    {int(key): value for key, value in value.items()}
except:
    print(repr(value))  # ok, it will print {'bar': 'baz'}

Linked PRs

Activity

  1. added
    type-bug
    on Aug 29, 2023
  2. Eclips4 commented on Aug 29, 2023

    @Eclips4
    Member
  3. added
    3.12
    3.13
    on Aug 29, 2023
  4. self-assigned this
    on Aug 29, 2023
  5. added 4 commits that reference this issue on Aug 30, 2023
  6. added a commit that references this issue on Aug 30, 2023
  7. moved this from Todo to In Progress in Release and Deferred blockers 🚫on Aug 30, 2023
  8. added a commit that references this issue on Aug 31, 2023
  9. AlexWaygood commented on Aug 31, 2023

    @AlexWaygood
    Member

    This looks completed, but please reopen if not @carljm!

  10. Fatal1ty commented on Sep 6, 2023

    @Fatal1ty
    Author

    I can confirm that it's fixed it 3.12.0rc2 🎉

  11. added 2 commits that reference this issue on Sep 9, 2023
  12. added a commit that references this issue on Sep 11, 2023
  13. added a commit that references this issue on Sep 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3.123.13release-blockertype-bug

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions